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.ProEconAlarmRule;
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 org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

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


@Service
public class AlarmReportService {

    @Resource
    private AlarmTsMapper alarmTsMapper;

    @Resource
    private IProBasicEarlyWarnScoreService proBasicEarlyWarnScoreService;

    @Resource
    private TokenService tokenService;

    public boolean startCount() throws Exception{


            Date currentDate = DateUtils.truncate(DateUtils.addDays(new Date(), -1));
            Date endData=DateUtils.truncate(new Date());
        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        List<ProBasicEquipment> wtList = tokenService.getWtls(request);

            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.CT.getCode(),wt.getId(), currentDate.getTime(),endData.getTime());

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


                            if(CacheContext.ctAlarmMap.containsKey(aci.getAlarmid()))
                            {
                                ProEconAlarmRule alarm=CacheContext.ctAlarmMap.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( ProEconAlarmRule alarm,AlarmTsVo info) {
        EarlyWarnScoreVo scoreOne = new EarlyWarnScoreVo();
        double score = getScore(info);
        scoreOne.setTotalscore(score);
        if (alarm.getRange() == null) {
            scoreOne.setOtherscore(score);
        } else {
            switch (alarm.getRange()) {
                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.getRelatedParts() == null) {
            scoreOne.setJcscore(score);
        } else {
            switch (alarm.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 "ZZ":
                    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;
        }
    }

}
