package com.ruoyi.mxjb.handlers;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mxjb.domain.SubmitYearsStats;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

@MappedJdbcTypes(value = {JdbcType.VARCHAR, JdbcType.NULL})
@MappedTypes(SubmitYearsStats.class)
public class SubmitYearsStatsHandler implements TypeHandler<SubmitYearsStats> {
    private static ObjectMapper om = new ObjectMapper();
    static {
        om.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, SubmitYearsStats stats, JdbcType jdbcType) throws SQLException {
        try {
            preparedStatement.setString(i, null == stats ? null : om.writeValueAsString(stats));
        } catch (JsonProcessingException e) {
            preparedStatement.setString(i, null);
        }
    }

    @Override
    public SubmitYearsStats getResult(ResultSet resultSet, String s) throws SQLException {
        return toObject(resultSet.getString(s));
    }

    @Override
    public SubmitYearsStats getResult(ResultSet resultSet, int i) throws SQLException {
        return toObject(resultSet.getString(i));
    }

    @Override
    public SubmitYearsStats getResult(CallableStatement callableStatement, int i) throws SQLException {
        return toObject(callableStatement.getString(i));
    }

    private SubmitYearsStats toObject(String s) {
        if(StringUtils.isNotBlank(s) ) {
            try {
                SubmitYearsStats yearStats = om.readValue(s, SubmitYearsStats.class);
                SubmitYearsStats.YearStat[] stats = yearStats.getStats();
                if (null != stats[0].getYear()) {
                    Integer minYear = yearStats.getYear();
                    Map<Integer, SubmitYearsStats.YearStat> statMap = Maps.newHashMap();
                    for (SubmitYearsStats.YearStat ys : stats) {
                        statMap.put(ys.getYear(), ys);
                        minYear = Math.min(minYear, ys.getYear());
                    }
                    Integer cnt = Math.max(yearStats.getYear() - minYear, 4);
                    SubmitYearsStats.YearStat[] newStats = new SubmitYearsStats.YearStat[cnt];
                    for (int i = 0; i < cnt; i++) {
                        newStats[i] = statMap.get(yearStats.getYear() - i);
                    }
                    yearStats.setStats(newStats);
                }
                return yearStats;
            } catch (JsonProcessingException e) {

            }
        }
        return null;
    }
}
