package com.gyee.alarm.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyee.alarm.init.CacheContext;
import com.gyee.alarm.mapper.auto.AlarmTsMapper;
import com.gyee.alarm.model.auto.ProBasicEarlyWarnScore;
import com.gyee.alarm.model.auto.ProBasicEquipment;
import com.gyee.alarm.model.auto.ProEconAlarmConfiguration;
import com.gyee.alarm.model.vo.AlarmSuperTalbeType;
import com.gyee.alarm.model.vo.AlarmTsVo;
import com.gyee.alarm.model.vo.EarlyWarnScoreVo;
import com.gyee.alarm.service.auto.IProBasicEarlyWarnScoreService;
import com.gyee.alarm.util.DateUtils;
import com.gyee.alarm.util.SnowflakeGenerator;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.Date;
import java.util.List;


@Service
public class AlarmReportService {

    @Resource
    private AlarmTsMapper alarmTsMapper;

    @Resource
    private IProBasicEarlyWarnScoreService proBasicEarlyWarnScoreService;



    public boolean startCount() throws Exception{


            Date currentDate = DateUtils.truncate(DateUtils.addDays(new Date(), -1));
            Date endData=DateUtils.truncate(new Date());
            List<ProBasicEquipment> wtList = CacheContext.wtls;

            for (ProBasicEquipment wt : wtList) {

                    ProBasicEarlyWarnScore ews = new ProBasicEarlyWarnScore();
                    ews.setId(SnowflakeGenerator.generateId() + "");
                    ews.setWindturbineId(wt.getId());
                    ews.setStatdate(currentDate);
                    
                  
                    List<AlarmTsVo> listInfo = alarmTsMapper.selectByWtGroup(AlarmSuperTalbeType.WT.getCode(),wt.getId(), currentDate.getTime(),endData.getTime());

                    if (listInfo != null && listInfo.size() > 0) {
                        for (AlarmTsVo aci : listInfo) {


                            if(CacheContext.alarmMap.containsKey(aci.getAlarmid()))
                            {
                                ProEconAlarmConfiguration alarm=CacheContext.alarmMap.get(aci.getAlarmid());

                                EarlyWarnScoreVo scoreOne = getEarlyWarnScoreOne(alarm,aci);
                                ews.setTotalScore(ews.getTotalScore() + scoreOne.getTotalscore());
                                ews.setBjxtScore(ews.getBjxtScore() + scoreOne.getBjxtscore());
                                ews.setZcScore(ews.getZcScore() + 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());

                            }


                        }
                    }

                    proBasicEarlyWarnScoreService.save(ews);


            }

        return true;
    }

    private EarlyWarnScoreVo getEarlyWarnScoreOne( ProEconAlarmConfiguration alarm,AlarmTsVo info) {
        EarlyWarnScoreVo scoreOne = new EarlyWarnScoreVo();
        double score = getScore(info);
        scoreOne.setTotalscore(score);
        if (alarm.getCharacteristic() == null) {
            scoreOne.setOtherscore(score);
        } else {
            switch (alarm.getCharacteristic()) {
                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 (alarm.getComponents() == null) {
            scoreOne.setJcscore(score);
        } else {
            switch (alarm.getComponents()) {
                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(AlarmTsVo 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(AlarmTsVo info, double baseScore) {


        if (info.getTimeLong() > 60)
            return baseScore;
        else if (info.getTimeLong() > 10)
            return baseScore - 1;
        else {
            if (info.getVal() > 10)
                return baseScore - 1;
            else
                return baseScore - 2;
        }
    }

}
