package com.gyee.wisdom.alarm.schedule.job;

import com.gyee.wisdom.alarm.schedule.entity.AlarmCountInfo;
import com.gyee.wisdom.alarm.schedule.entity.EarlyWarnScore;
import com.gyee.wisdom.alarm.schedule.entity.EarlyWarnScoreOne;
import com.gyee.wisdom.alarm.schedule.entity.Windturbine;
import com.gyee.wisdom.alarm.schedule.mapper.AlarmCountMapper;
import com.gyee.wisdom.alarm.schedule.mapper.EarlyWarnScoreMapper;
import com.gyee.wisdom.alarm.schedule.mapper.WindturbineMapper;
import com.gyee.wisdom.alarm.schedule.util.SnowflakeGenerator;
import com.gyee.wisdom.common.utils.DateUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @description: 评分计算 sharding-mysql2中AlarmReportService中startCount方法
 * @auther: Wanghs
 * @date: 2022-03-15
 */
@Component
public class EarlyWarnScoreHandler {


    @Autowired
    private AlarmCountMapper alarmCountMapper;

    @Autowired
    private EarlyWarnScoreMapper earlyWarnScoreMapper;

    @Autowired
    private WindturbineMapper windturbineMapper;


    @XxlJob("calculationEarlyWarnScore")
    public ReturnT<String> calculationEarlyWarnScore() {
        ReturnT<String> result = new ReturnT<>();

        StringBuffer msg = new StringBuffer();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String strDate = sdf.format(DateUtil.addDays(new Date(), -1));
            Date currentDate = sdf.parse(strDate);

            List<Windturbine> wtList = windturbineMapper.selectWindturbines();
            for (Windturbine wt : wtList) {
                try {
                    EarlyWarnScore ews = new EarlyWarnScore();
                    ews.setId(SnowflakeGenerator.generateId() + "");
                    ews.setWindturbineid(wt.getId());
                    ews.setStatdate(currentDate);
                    List<AlarmCountInfo> listInfo = alarmCountMapper.getAlarmCountInfo(wt.getId(), currentDate);
                    if (listInfo != null && listInfo.size() > 0) {
                        for (AlarmCountInfo aci : listInfo) {
                            EarlyWarnScoreOne scoreOne = getEarlyWarnScoreOne(aci);
                            ews.setTotalscore(ews.getTotalscore() + scoreOne.getTotalscore());
                            ews.setBjxtscore(ews.getBjxtscore() + scoreOne.getBjxtscore());
                            ews.setCdlscore(ews.getCdlscore() + scoreOne.getCdlscore());
                            ews.setCfxtscore(ews.getCfxtscore() + scoreOne.getCfxtscore());
                            ews.setClxscore(ews.getClxscore() + scoreOne.getClxscore());
                            ews.setFdjscore(ews.getFdjscore() + scoreOne.getFdjscore());
                            ews.setZkgscore(ews.getZkgscore() + scoreOne.getZkgscore());
                            ews.setJcscore(ews.getJcscore() + scoreOne.getJcscore());
                            ews.setSensorscore(ews.getSensorscore() + scoreOne.getSensorscore());
                            ews.setPartsscore(ews.getPartsscore() + scoreOne.getPartsscore());
                            ews.setControlscore(ews.getControlscore() + scoreOne.getControlscore());
                            ews.setPerformancescore(ews.getPerformancescore() + scoreOne.getPerformancescore());
                            ews.setOtherscore(ews.getOtherscore() + scoreOne.getOtherscore());
                        }
                    }

                    earlyWarnScoreMapper.insert(ews);
                    msg.append(wt.getId()+"--计算成功\n");
                } catch (Exception e) {
                    XxlJobHelper.log(e.getMessage()+"\n"+e.getStackTrace());
                    msg.append(wt.getId()+"--计算失败"+e.getStackTrace()+"\n");
                }
            }
           result.setCode(ReturnT.SUCCESS_CODE);
            result.setContent(msg.toString());
        } catch (Exception ex) {
            msg.append("计算失败"+ex.getStackTrace()+"\n");
            msg.append(ex.getStackTrace().toString());
            result.setCode(ReturnT.FAIL_CODE);
            result.setMsg(msg.toString());
        }finally {
            return result;

        }
    }

    private EarlyWarnScoreOne getEarlyWarnScoreOne(AlarmCountInfo info) {
        EarlyWarnScoreOne scoreOne = new EarlyWarnScoreOne();
        double score = getScore(info);
        scoreOne.setTotalscore(score);
        switch (info.getProject()) {
            case "sensors":
                scoreOne.setSensorscore(score);
                break;
            case "parts":
                scoreOne.setPartsscore(score);
                break;
            case "control":
                scoreOne.setControlscore(score);
                break;
            case "performance":
                scoreOne.setPerformancescore(score);
                break;
            default:
                scoreOne.setOtherscore(score);
        }

        switch (info.getRelatedParts()) {
            case "CFXT":
                scoreOne.setCfxtscore(score);
                break;
            case "CLX":
                scoreOne.setClxscore(score);
                break;
            case "FDJ":
                scoreOne.setFdjscore(score);
                break;
            case "CDL":
                scoreOne.setCdlscore(score);
                break;
            case "BJXT":
                scoreOne.setBjxtscore(score);
                break;
            case "ZKG":
                scoreOne.setZkgscore(score);
                break;
            default:
                scoreOne.setJcscore(score);
        }


        return scoreOne;
    }

    private double getScore(AlarmCountInfo info) {
        switch (info.getRank()) {
            case "5":
                return getFreqScore(info, 10);
            case "4":
                return getFreqScore(info, 7);
            case "3":
                return getFreqScore(info, 4);
            default:
                return 1;
        }
    }

    private double getFreqScore(AlarmCountInfo info, double baseScore) {
        if (info.getTime1() > 60)
            return baseScore;
        else if (info.getTime1() > 10)
            return baseScore - 1;
        else {
            if (info.getCount() > 10)
                return baseScore - 1;
            else
                return baseScore - 2;
        }
    }

}
