package com.gyee.wisdom.alarm.sharding.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyee.wisdom.alarm.sharding.entity.*;
import com.gyee.wisdom.alarm.sharding.mapper.AlarmCountMapper;
import com.gyee.wisdom.alarm.sharding.mapper.EarlyWarnScoreMapper;
import com.gyee.wisdom.alarm.sharding.util.SnowflakeGenerator;
import com.gyee.wisdom.common.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Service;

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


@Service
@Slf4j
@EnableScheduling
public class AlarmReportService extends ServiceImpl<AlarmCountMapper, AlarmCount> {

    @Autowired
    private AlarmCountMapper alarmCountMapper;

    @Autowired
    private EarlyWarnScoreMapper earlyWarnScoreMapper;

    @Autowired
    private WindturbineService windturbineService;


    public boolean startCount() {


        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 = windturbineService.windturbineList();

            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);
                } catch (Exception e) {
                    log.error(e.getMessage());
                    log.error(e.getStackTrace().toString());
                    log.error("生成诊断报告异常,风机id为"+wt.getId());
                }
            }
        } catch (Exception ex) {
            log.error(ex.getMessage());
            log.error(ex.getStackTrace().toString());
            return false;
        }

        return true;
    }

    private EarlyWarnScoreOne getEarlyWarnScoreOne(AlarmCountInfo info) {
        EarlyWarnScoreOne scoreOne = new EarlyWarnScoreOne();
        double score = getScore(info);
        scoreOne.setTotalscore(score);
        if (info.getProject() == null) {
            scoreOne.setOtherscore(score);
        } else {
            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);
            }
        }
        if (info.getRelatedParts() == null) {
            scoreOne.setJcscore(score);
        } else {
            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) {
        if (info.getRank() == null) {
            return 1;
        }
        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;
        }
    }

}
