package com.example.system.mapper;

import com.example.system.common.Chart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class ChartMapper {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 保存图表信息
     * @param chart 图表对象
     * @return 受影响的行数
     */
    public int saveChart(Chart chart) {
        String sql = "INSERT INTO t_chart (id, name, goal, chart_data, chart_type, gen_chart, gen_result, admin_id, create_time, update_time) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        return jdbcTemplate.update(sql,
                chart.getId(),
                chart.getName(),
                chart.getGoal(),
                chart.getChartData(),
                chart.getChartType(),
                chart.getGenChart(),
                chart.getGenResult(),
                chart.getAdminId(),
                chart.getCreateTime(),
                chart.getUpdateTime()
        );
    }

    /**
     * 更新图表信息
     * @param chart 图表对象
     * @return 受影响的行数
     */
    public int updateChart(Chart chart) {
        String sql = "UPDATE t_chart SET " +
                "name = ?, " +
                "goal = ?, " +
                "chart_type = ?, " +
                "gen_chart = ?, " +
                "gen_result = ?, " +
                "update_time = ? " +
                "WHERE id = ?";

        return jdbcTemplate.update(sql,
                chart.getName(),
                chart.getGoal(),
                chart.getChartType(),
                chart.getGenChart(),
                chart.getGenResult(),
                chart.getUpdateTime(),
                chart.getId()
        );
    }

    /**
     * 根据ID查询图表
     * @param id 图表ID
     * @return 图表对象
     */
    public Chart findById(String id) {
        String sql = "SELECT * FROM t_chart WHERE id = ?";

        try {
            return jdbcTemplate.queryForObject(sql, (rs, rowNum) -> {
                Chart chart = new Chart();
                chart.setId(rs.getString("id"));
                chart.setName(rs.getString("name"));
                chart.setGoal(rs.getString("goal"));
                chart.setChartData(rs.getString("chart_data"));
                chart.setChartType(rs.getString("chart_type"));
                chart.setGenChart(rs.getString("gen_chart"));
                chart.setGenResult(rs.getString("gen_result"));
                chart.setAdminId(rs.getLong("admin_id"));
                chart.setCreateTime(rs.getLong("create_time"));
                chart.setUpdateTime(rs.getLong("update_time"));
                return chart;
            }, id);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 删除图表
     * @param id 图表ID
     * @return 受影响的行数
     */
    public int deleteById(String id) {
        String sql = "DELETE FROM t_chart WHERE id = ?";
        return jdbcTemplate.update(sql, id);
    }
}