package com.ruoyi.mxjb.service.impl;

import com.google.common.collect.Maps;
import com.ruoyi.mxjb.domain.MxjbWishScoreRate;
import com.ruoyi.mxjb.mapper.MxjbWishScoreRateMapper;
import com.ruoyi.mxjb.service.IMxjbWishScoreRateService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * mxjbWishScoreRateService业务层处理
 * 
 * @author voluntary
 * @date 2023-02-05
 */
@Service
public class MxjbWishScoreRateServiceImpl implements IMxjbWishScoreRateService 
{
    private final MxjbWishScoreRateMapper mxjbWishScoreRateMapper;
    // [Level=冲的最大Diff, 冲的最低Diff, 稳的最低Diff, 保的最低Diff, Rate>=99的最高Diff, Rate<=1的最小Diff]
	private Map<Integer, Integer[]> wishScoreRangeMap = Maps.newHashMap();
    // key is year, key2 is diff, key3 is Enroll/Ratio/Risk
    private Map<Integer, Map<Integer, ScoreRatioResult>> yearDiffRateMap = Maps.newHashMap();


    public MxjbWishScoreRateServiceImpl(MxjbWishScoreRateMapper mxjbWishScoreRateMapper) {
        this.mxjbWishScoreRateMapper = mxjbWishScoreRateMapper;
        refresh();
    }

    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    public static class ScoreRatioResult {
        private Integer diff;
        private Integer enrollLevel;
        private Integer overflow = 0; // 1 0%, 0 正常 -1 100%
        private Integer ratio;
        private String riskLevel;
    }

    /**
     *
     * @param year
     * @param selfScore
     * @return [Level=冲的最大值, 冲的最低值, 稳的最低值, 保的最低值, Rate>=99的最高值, Rate<=1的最小值]
     */
    @Override
    public Integer[] getWishScoreRatioRanges(Integer year, Integer selfScore) {
        Integer[] ranges = wishScoreRangeMap.get(year); // 最高分加一分，需要闭区间
        return null != ranges ? new Integer[] {ranges[0] + selfScore + 1, ranges[1] + selfScore, ranges[2] + selfScore, ranges[3] + selfScore, ranges[4] + selfScore, ranges[5] + selfScore } : null;
    }

    @Override
    public ScoreRatioResult getWishScoreRatioResult(Integer year, Integer selfScore, Integer targetScore) {
        if (targetScore <= 0) {
            return null;
        }
        Integer diff = targetScore - selfScore;
        Integer[] ranges = wishScoreRangeMap.get(year); // [Level=冲的最大Diff, 冲的最低Diff, 稳的最低Diff, 保的最低Diff, Rate>=99的最高Diff, Rate<=1的最小Diff]
        Map<Integer, ScoreRatioResult> diffRateMap = yearDiffRateMap.get(year);
        if (null == ranges || null == diffRateMap) {
            return null;
        }
        if (diff < ranges[3]) { // 超时最大最小值算 超  0
            return diffRateMap.get(ranges[3] - 100);
        } else if (diff > ranges[0]) { // 100
            return diffRateMap.get(ranges[0] + 100);
        }
        return diffRateMap.get(diff);
    }

    private void refresh() {
        Map<Integer, Integer[]> tmpWishScoreRangeMap = Maps.newHashMap();
        Map<Integer, Map<Integer, ScoreRatioResult>> tmpYearDiffRateMap = Maps.newHashMap();
        mxjbWishScoreRateMapper.selectMxjbWishScoreRateList(new MxjbWishScoreRate()).forEach(t -> {
            Integer[] scoreRanges = tmpWishScoreRangeMap.get(t.getYear());
            if (null == scoreRanges) {
                scoreRanges = new Integer[6]; // [Level=冲的最大Diff, 冲的最低Diff, 稳的最低Diff, 保的最低Diff, Rate>=99的最高Diff, Rate<=1的最小Diff]
                tmpWishScoreRangeMap.put(t.getYear(), scoreRanges);
            }
            if (t.getRatio() <= 2) {
                if (null == scoreRanges[5] || t.getDiff() < scoreRanges[5]) {
                    scoreRanges[5] = t.getDiff();
                }
            } else if (t.getRatio() >= 98 && (null == scoreRanges[4] || t.getDiff() > scoreRanges[4])) {
                scoreRanges[4] = t.getDiff();
            }
            if (t.getEnrollLevel().equals(0) && (null == scoreRanges[0] || t.getDiff() > scoreRanges[0])) {
                scoreRanges[0] = t.getDiff();
            }
            Integer index = t.getEnrollLevel() + 1;
            if (null == scoreRanges[index] || t.getDiff() < scoreRanges[index]) {
                scoreRanges[index] = t.getDiff();
            }
            Map<Integer, ScoreRatioResult> diffRateMap = tmpYearDiffRateMap.get(t.getYear());
            if (null == diffRateMap) {
                diffRateMap = Maps.newHashMap();
                tmpYearDiffRateMap.put(t.getYear(), diffRateMap);
            }
            diffRateMap.put(t.getDiff(), new ScoreRatioResult(t.getDiff(), t.getEnrollLevel(), 0, t.getRatio(), t.getRiskLevel()));
        });
        for (Integer year : tmpYearDiffRateMap.keySet()) {
            Integer[] scoreRanges = tmpWishScoreRangeMap.get(year);
            Map<Integer, ScoreRatioResult> diffRateMap = tmpYearDiffRateMap.get(year);
            // 高于冲的最大Diff的设置特殊值
            ScoreRatioResult result = diffRateMap.get(scoreRanges[0]);
            Integer diff = scoreRanges[0] + 100;
            diffRateMap.put(diff, new ScoreRatioResult(scoreRanges[0], result.enrollLevel, 1, 1, result.getRiskLevel()));
            // 小于保的最小Diff的设置特殊值
            result = diffRateMap.get(scoreRanges[3]);
            diff = scoreRanges[3] - 100;
            diffRateMap.put(diff, new ScoreRatioResult(scoreRanges[3], result.enrollLevel, -1, 99, result.getRiskLevel()));
        }
        yearDiffRateMap.putAll(tmpYearDiffRateMap);
        wishScoreRangeMap.putAll(tmpWishScoreRangeMap);
    }

    /**
     * 查询mxjbWishScoreRate
     * 
     * @param id mxjbWishScoreRate主键
     * @return mxjbWishScoreRate
     */
    @Override
    public MxjbWishScoreRate selectMxjbWishScoreRateById(Long id)
    {
        return mxjbWishScoreRateMapper.selectMxjbWishScoreRateById(id);
    }

    /**
     * 查询mxjbWishScoreRate列表
     * 
     * @param mxjbWishScoreRate mxjbWishScoreRate
     * @return mxjbWishScoreRate
     */
    @Override
    public List<MxjbWishScoreRate> selectMxjbWishScoreRateList(MxjbWishScoreRate mxjbWishScoreRate)
    {
        return mxjbWishScoreRateMapper.selectMxjbWishScoreRateList(mxjbWishScoreRate);
    }

    /**
     * 新增mxjbWishScoreRate
     * 
     * @param mxjbWishScoreRate mxjbWishScoreRate
     * @return 结果
     */
    @Override
    public int insertMxjbWishScoreRate(MxjbWishScoreRate mxjbWishScoreRate)
    {
        int iRet = mxjbWishScoreRateMapper.insertMxjbWishScoreRate(mxjbWishScoreRate);
        refresh();
        return iRet;
    }

    /**
     * 修改mxjbWishScoreRate
     * 
     * @param mxjbWishScoreRate mxjbWishScoreRate
     * @return 结果
     */
    @Override
    public int updateMxjbWishScoreRate(MxjbWishScoreRate mxjbWishScoreRate)
    {
        int iRet = mxjbWishScoreRateMapper.updateMxjbWishScoreRate(mxjbWishScoreRate);
        refresh();
        return iRet;
    }

    /**
     * 批量删除mxjbWishScoreRate
     * 
     * @param ids 需要删除的mxjbWishScoreRate主键
     * @return 结果
     */
    @Override
    public int deleteMxjbWishScoreRateByIds(Long[] ids)
    {
        int iRet = mxjbWishScoreRateMapper.deleteMxjbWishScoreRateByIds(ids);
        refresh();
        return iRet;
    }

    /**
     * 删除mxjbWishScoreRate信息
     * 
     * @param id mxjbWishScoreRate主键
     * @return 结果
     */
    @Override
    public int deleteMxjbWishScoreRateById(Long id)
    {
        int iRet = mxjbWishScoreRateMapper.deleteMxjbWishScoreRateById(id);
        refresh();
        return iRet;
    }
}
