package cn.gyee.tamplate.service.benchmark;

import cn.gyee.tamplate.contant.OpeContant;
import cn.gyee.tamplate.controller.DutyscheduleController;
import cn.gyee.tamplate.init.CacheContext;
import cn.gyee.tamplate.model.auto.Benchmarkingbetween;
import cn.gyee.tamplate.model.auto.Dutyschedule;
import cn.gyee.tamplate.model.auto.Windpowerstationtestingpoint2;
import cn.gyee.tamplate.model.custom.PointData;
import cn.gyee.tamplate.service.IBenchmarkingbetweenService;
import cn.gyee.tamplate.service.IDutyscheduleService;
import cn.gyee.tamplate.util.DateUtils;
import cn.gyee.tamplate.util.common.StringUtils;
import cn.gyee.tamplate.util.golden.EdosUtil;
import cn.gyee.tamplate.util.golden.IEdosUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName : BenchmarkService
 * @Author : xieshengjie
 * @Date: 2021/2/25 15:36
 * @Description : 对标service
 */
@Service
public class BenchmarkService {
    private static IEdosUtil edosUtil = new EdosUtil();

    @Resource
    private IDutyscheduleService dutyscheduleService;
    @Resource
    private IBenchmarkingbetweenService benchmarkingbetweenService;

    public void createBeanchmarkList() throws Exception {

        String yeseterdayStr = DateUtils.getYesterdayStr("yyyy-MM-dd");
        Map<String, Object> params = new HashMap<>();
        params.put("theday", DateUtils.parseDate(yeseterdayStr));
        benchmarkingbetweenService.removeByMap(params);
        Map<String, Object> params1 = new HashMap<>();
        params1.put("createdate", DateUtils.parseDate(yeseterdayStr));
//        List<Dutyschedule> dutyList = dutyscheduleService.list();
        List<Dutyschedule> dutyList = dutyscheduleService.listByMap(params1);
        String[] points = OpeContant.benchPoints.split(",");
        Map<String, Windpowerstationtestingpoint2> wpmap = CacheContext.wppointmap.get("0");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String,Map<String,Double>> remap = new HashMap<>();

        for (Dutyschedule dutyschedule : dutyList) {
            String[] begins=dutyschedule.getBegin().split(",");
            String[] ends=dutyschedule.getEnd().split(",");
            Map<String,Double> map = new HashMap<>();
            for (int i=0 ; i<begins.length;i++) {
                Date begin = df.parse(yeseterdayStr + " "+begins[i]+":00");
                Date end = df.parse(yeseterdayStr + " "+ends[i]+":00");
                for (String point : points) {
                    Windpowerstationtestingpoint2 windpowerstationtestingpoint2 = wpmap.get(point);
                    List<PointData> maxs = edosUtil.getHistStat(windpowerstationtestingpoint2.getCode(),begin.getTime()/1000,end.getTime()/1000, (long) 1,null,0);
                    List<PointData> mins = edosUtil.getHistStat(windpowerstationtestingpoint2.getCode(),begin.getTime()/1000,end.getTime()/1000, (long) 1,null,1);
                    double ssdl = 0;
                    if (StringUtils.isNotEmpty(maxs) && StringUtils.isNotEmpty(mins)){
                        ssdl = (maxs.get(0).getPointValueInDouble()-mins.get(0).getPointValueInDouble())/10000;
                    }
                    if (!map.containsKey(point)) {
                        map.put(point, ssdl);
                    } else{
                        map.put(point,map.get(point)+ssdl);
                    }
                }
                Windpowerstationtestingpoint2 windpowerstationtestingpoint2 = wpmap.get("RFDL");
                List<PointData> maxs = edosUtil.getHistStat(windpowerstationtestingpoint2.getCode(),begin.getTime()/1000,end.getTime()/1000, (long) 1,null,0);
                List<PointData> mins = edosUtil.getHistStat(windpowerstationtestingpoint2.getCode(),begin.getTime()/1000,end.getTime()/1000, (long) 1,null,1);
                double fdl = 0;
                if (StringUtils.isNotEmpty(maxs) && StringUtils.isNotEmpty(mins)){
                    fdl = maxs.get(0).getPointValueInDouble()-mins.get(0).getPointValueInDouble();
                }
                if (!map.containsKey("RFDL")) {
                    map.put("RFDL", fdl);
                } else{
                    map.put("RFDL",map.get("RFDL")+fdl);
                }
                remap.put(dutyschedule.getName(),map);
            }




        }
        Set<String> dutyset = remap.keySet();
        for (String duty : dutyset) {
            Benchmarkingbetween bench = new Benchmarkingbetween();
            bench.setDutyname(duty);
            bench.setTheday(DateUtils.parseDate(yeseterdayStr));
            Map<String,Double> pointmap = remap.get(duty);
            bench.setPerformanceloss(pointmap.get("RSDJZSDL")+pointmap.get("RSSTZSDL")+pointmap.get("RXNZSDL")+pointmap.get("RSQXZSDL"));
            bench.setFaultloss(pointmap.get("RGZZSDL")+pointmap.get("RSZZSDL"));
            bench.setMainloss(pointmap.get("RJXZSDL")+pointmap.get("RLZZSDL"));
            bench.setRationingloss(pointmap.get("RQFZSDL")+pointmap.get("RXDZSDL"));
            bench.setInvolvesloss(pointmap.get("RWZZSDL")+pointmap.get("RTZZSDL"));
            bench.setGeneratity(pointmap.get("RFDL"));
            bench.setTheoreticalgeneratity(bench.getPerformanceloss()+bench.getFaultloss()+bench.getMainloss()+bench.getRationingloss()+bench.getInvolvesloss()+bench.getGeneratity());
            benchmarkingbetweenService.save(bench);
        }

    }

    public List<Benchmarkingbetween> findbenchlist(String beginDate, String endDate) {
        Date begin = DateUtils.parseDate(beginDate);
        Date end = DateUtils.parseDate(endDate);

        List<Benchmarkingbetween> resuList =  benchmarkingbetweenService.findByBeginAndEnd(begin,end);
        return resuList;
    }
}
