package com.gyee.generation.service;//package com.gyee.generation.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gyee.common.contant.ContantXk;
import com.gyee.common.model.PointData;
import com.gyee.common.model.StringUtils;
import com.gyee.generation.init.CacheContext;
import com.gyee.generation.model.auto.ProBasicEquipment;
import com.gyee.generation.model.auto.ProBasicEquipmentPoint;
import com.gyee.generation.model.auto.ProEconEquipmentInfoDay5;
import com.gyee.generation.model.auto.ProEconShutdownEvent2;
import com.gyee.generation.service.auto.IProEconEquipmentInfoDay5Service;
import com.gyee.generation.service.auto.IProEconShutdownEvent2Service;
import com.gyee.generation.util.DateUtils;
import com.gyee.generation.util.realtimesource.IEdosUtil;
import com.gyee.generation.util.statisticcs.Initial;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class EquipmentInfo5Service {

    //    private static final Logger logger = LoggerFactory.getLogger(EquipmentInfo5Service.class);
    @Resource
    private IEdosUtil edosUtil;

    @Resource
    private IProEconShutdownEvent2Service proEconShutdownEvent2Service;
    //状态转换率
    @Resource
    private IProEconEquipmentInfoDay5Service proEconEquipmentInfoDay5Service;


    public void calEquipmentInfoDay(Date currentDate) throws Exception {
        resetRate(DateUtils.truncate(currentDate));


    }

    /**
     * 复位及时率
     */
    public void resetRate(Date currentDate) throws Exception {

        Calendar c = Calendar.getInstance();
        c.setTime(currentDate);
        if (c.get(Calendar.HOUR_OF_DAY) == 0) {
            //      if(c.get(Calendar.HOUR_OF_DAY)==0 && c.get(Calendar.MINUTE)==0)
            c.add(Calendar.DAY_OF_MONTH, -1);
        }

        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        Date endDate = c.getTime();

        Date begin = DateUtils.truncate(c.getTime());


        QueryWrapper<ProEconShutdownEvent2> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("stop_Time", begin).le("stop_Time", endDate)
                .isNotNull("stop_Type_Id").ne("stop_Type_Id", "wh").isNotNull("stop_Hours");
        List<ProEconShutdownEvent2> shutdownevents = proEconShutdownEvent2Service.list(queryWrapper);


        Map<String, ProEconEquipmentInfoDay5> rates = new HashMap<>();
        List<String> wpls = new ArrayList<>();
        for (ProBasicEquipment wt : CacheContext.wtls) {

            if (wt.getWindpowerstationId().contains("GDC")) {
                continue;
            }
            ProEconEquipmentInfoDay5 rate = new ProEconEquipmentInfoDay5();
            rate.setRecordDate(currentDate);
            rate.setWindpowerstationId(wt.getWindpowerstationId());
            rate.setProjectId(wt.getProjectId());
            rate.setLineId(wt.getLineId());
            rate.setWindturbineId(wt.getId());
            rate.setCompanyId(wt.getCompanyId());
            rate.setRegionId(wt.getRegionId());
            Initial.initial(rate);
            rates.put(wt.getId(), rate);

            if (!wpls.contains(wt.getWindpowerstationId())) {
                wpls.add(wt.getWindpowerstationId());
            }
        }

        for (ProEconShutdownEvent2 item : shutdownevents) {
            if (rates.containsKey(item.getWindturbineId())) {
                if (item.getStopHours() <= 1) {
                    double temp = rates.get(item.getWindturbineId()).getTimerate20();
                    temp = temp + 1;
                    rates.get(item.getWindturbineId()).setTimerate20(temp);


                    if (item.getStopHours() <= 0.08) {
                        temp = rates.get(item.getWindturbineId()).getTimerate16();
                        temp = temp + 1;
                        rates.get(item.getWindturbineId()).setTimerate16(temp);

                        temp = rates.get(item.getWindturbineId()).getTimerate17();
                        temp = temp + 1;
                        rates.get(item.getWindturbineId()).setTimerate17(temp);

                        temp = rates.get(item.getWindturbineId()).getTimerate18();
                        temp = temp + 1;
                        rates.get(item.getWindturbineId()).setTimerate18(temp);

                        temp = rates.get(item.getWindturbineId()).getTimerate19();
                        temp = temp + 1;
                        rates.get(item.getWindturbineId()).setTimerate19(temp);
                        item.setResetRate("5");

                    } else if (item.getStopHours() <= 0.17) {
                        temp = rates.get(item.getWindturbineId()).getTimerate17();
                        temp = temp + 1;
                        rates.get(item.getWindturbineId()).setTimerate17(temp);

                        temp = rates.get(item.getWindturbineId()).getTimerate18();
                        temp = temp + 1;
                        rates.get(item.getWindturbineId()).setTimerate18(temp);

                        temp = rates.get(item.getWindturbineId()).getTimerate19();
                        temp = temp + 1;
                        rates.get(item.getWindturbineId()).setTimerate19(temp);
                        item.setResetRate("10");
                    } else if (item.getStopHours() <= 0.25) {
                        temp = rates.get(item.getWindturbineId()).getTimerate18();
                        temp = temp + 1;
                        rates.get(item.getWindturbineId()).setTimerate18(temp);

                        temp = rates.get(item.getWindturbineId()).getTimerate19();
                        temp = temp + 1;
                        rates.get(item.getWindturbineId()).setTimerate19(temp);
                        item.setResetRate("15");
                    } else if (item.getStopHours() <= 0.33) {
                        temp = rates.get(item.getWindturbineId()).getTimerate19();
                        temp = temp + 1;
                        rates.get(item.getWindturbineId()).setTimerate19(temp);
                        item.setResetRate("20");
                    } else if (item.getResetRate() == null) {
                        item.setResetRate("99");

                    } else {
                        item.setResetRate(null);
                    }
                }

                if (item.getStopHours() < 24) {

                    double temp = rates.get(item.getWindturbineId()).getTimerate21();
                    temp = temp + 1;
                    rates.get(item.getWindturbineId()).setTimerate21(temp);


                }
                double temp = rates.get(item.getWindturbineId()).getTimerate22();
                temp = temp + 1;
                rates.get(item.getWindturbineId()).setTimerate22(temp);


            }

        }

        stateRate(currentDate, rates);
        List<ProEconEquipmentInfoDay5> dayls = new ArrayList<>();
        //        QueryWrapper<ProEconEquipmentInfoDay5> queryWrapper2 = new QueryWrapper<>();
        //        queryWrapper2.ge("record_date",currentDate);
        //
        //            List<String> dayInfoids = proEconEquipmentInfoDay5Service.list(queryWrapper2)
        //                    .stream()
        ////                    .filter(i -> i.getRecordDate().compareTo(currentDate) == 0
        ////            )
        //        .map(ProEconEquipmentInfoDay5::getId).collect(Collectors.toList());
        //
        //
        ////            if (!dayInfoids.isEmpty()) {
        ////
        ////                proEconEquipmentInfoDay5Service.removeByIds(dayInfoids);
        ////            }
        //
        //        List<String> tempids=new ArrayList<>();
        //
        //        for(int i=0;i<dayInfoids.size();i++)
        //        {
        //            tempids.add(dayInfoids.get(i));
        //            if(tempids.size()==1000)
        //            {
        //                proEconEquipmentInfoDay5Service.removeByIds(tempids);
        //                tempids=new ArrayList<>();
        //            }
        //        }
        //
        //        if(!tempids.isEmpty())
        //        {
        //            proEconEquipmentInfoDay5Service.removeByIds(tempids);
        //        }

        ProEconEquipmentInfoDay5 dayInfo;
        for (ProEconEquipmentInfoDay5 day : rates.values()) {

            dayInfo = new ProEconEquipmentInfoDay5();

            buildresetRate(dayInfo, day);

            dayls.add(dayInfo);
            //proEconEquipmentInfoDay5Service.save(dayInfo);

        }


        QueryWrapper<ProEconEquipmentInfoDay5> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.ge("record_date", currentDate);
        queryWrapper2.in("windpowerstation_id", wpls);

        List<String> dayInfoids = proEconEquipmentInfoDay5Service.list(queryWrapper2)
                .stream()
                .map(ProEconEquipmentInfoDay5::getId).collect(Collectors.toList());


        List<String> tempids = new ArrayList<>();

        for (int i = 0; i < dayInfoids.size(); i++) {
            tempids.add(dayInfoids.get(i));
            if (tempids.size() == 1000) {
                proEconEquipmentInfoDay5Service.removeByIds(tempids);
                tempids = new ArrayList<>();
            }
        }

        if (!tempids.isEmpty()) {
            proEconEquipmentInfoDay5Service.removeByIds(tempids);
        }

        List<ProEconEquipmentInfoDay5> templs = new ArrayList<>();
        for (ProEconEquipmentInfoDay5 vo : dayls) {
            if (StringUtils.notEmp(vo.getWindturbineId())) {
                templs.add(vo);
                if (templs.size() == 1000) {
                    proEconEquipmentInfoDay5Service.saveBatch(templs);
                    templs = new ArrayList<>();
                }
            }

        }

        if (!templs.isEmpty()) {
            proEconEquipmentInfoDay5Service.saveBatch(templs);
        }

        //   proEconEquipmentInfoDay5Service.saveBatch(dayls);
    }

    public void buildresetRate(ProEconEquipmentInfoDay5 dayInfo, ProEconEquipmentInfoDay5 day) {

        dayInfo.setRecordDate(day.getRecordDate());
        dayInfo.setWindpowerstationId(day.getWindpowerstationId());
        dayInfo.setProjectId(day.getProjectId());
        dayInfo.setLineId(day.getLineId());
        dayInfo.setWindturbineId(day.getWindturbineId());
        dayInfo.setRegionId(day.getRegionId());
        dayInfo.setCompanyId(day.getCompanyId());
        dayInfo.setTimerate1(day.getTimerate1());
        dayInfo.setTimerate2(day.getTimerate2());
        dayInfo.setTimerate3(day.getTimerate3());
        dayInfo.setTimerate4(day.getTimerate4());
        dayInfo.setTimerate5(day.getTimerate5());
        dayInfo.setTimerate6(day.getTimerate6());
        dayInfo.setTimerate7(day.getTimerate7());
        dayInfo.setTimerate8(day.getTimerate8());
        dayInfo.setTimerate9(day.getTimerate9());
        dayInfo.setTimerate10(day.getTimerate10());
        dayInfo.setTimerate11(day.getTimerate11());
        dayInfo.setTimerate12(day.getTimerate12());
        dayInfo.setTimerate13(day.getTimerate13());
        dayInfo.setTimerate14(day.getTimerate14());
        dayInfo.setTimerate15(day.getTimerate15());

        dayInfo.setTimerate16(day.getTimerate16());
        dayInfo.setTimerate17(day.getTimerate17());
        dayInfo.setTimerate18(day.getTimerate18());
        dayInfo.setTimerate19(day.getTimerate19());
        dayInfo.setTimerate20(day.getTimerate20());
        dayInfo.setTimerate21(day.getTimerate21());
        dayInfo.setTimerate22(day.getTimerate22());
    }

    /**
     * 状态转换及时率
     */
    Map<Double, Double> stateDescription1 = new HashMap<>();
    Map<Double, Double> stateDescription2 = new HashMap<>();


    public void stateRate(Date currentDate, Map<String, ProEconEquipmentInfoDay5> rates) throws Exception {


        if (stateDescription1.size() == 0) {
            stateDescription1.put(3.0, 6.0);
            stateDescription1.put(3.5, 0.0);
            stateDescription1.put(4.0, 0.0);
        }
        if (stateDescription2.size() == 0) {
            stateDescription2.put(3.0, 6.0);
            stateDescription2.put(3.5, 2.0);
            stateDescription2.put(4.0, 2.0);
        }

        //调度时间是第二天，currentDate 是第一天

        Date beginDate = DateUtils.truncate(currentDate);
        Date endDate = DateUtils.addDays(beginDate, 1);

        //
        //        final CountDownLatch latch = new CountDownLatch(+CacheContext.wtls.size());
        for (ProBasicEquipment wt : CacheContext.wtls) {

            if (wt.getWindpowerstationId().contains("GDC")) {
                continue;
            }
            ProEconEquipmentInfoDay5 rate = rates.get(wt.getId());

            List<PointData> speedDataList = null;
            List<PointData> statusDataList = null;
            if (CacheContext.wtpAimap.containsKey(wt.getId())) {
                Map<String, ProBasicEquipmentPoint> wtpmap = CacheContext.wtpAimap.get(wt.getId());
                if (wtpmap.containsKey(ContantXk.CJ_SSFS)) {
                    ProBasicEquipmentPoint point = wtpmap.get(ContantXk.CJ_SSFS);
                    speedDataList = edosUtil.getHistoryDatasSnap(point.getNemCode(), beginDate.getTime() / 1000, endDate.getTime() / 1000);
                }
                if (wtpmap.containsKey(ContantXk.MXZT)) {
                    ProBasicEquipmentPoint point = wtpmap.get(ContantXk.MXZT);
                    statusDataList = edosUtil.getHistoryDatasSnap(point.getNemCode(), beginDate.getTime() / 1000, endDate.getTime() / 1000);
                }

            }


//            if (speedDataList != null && statusDataList != null && statusDataList.size() == speedDataList.size() && !speedDataList.isEmpty()) {
            if (speedDataList != null && statusDataList != null && (statusDataList.size() - speedDataList.size() < 3) && !speedDataList.isEmpty()) {

                double laststatus = -1;
                Date begin = new Date();
                int w = 0;
                int q = speedDataList.size() - statusDataList.size();
                if (q >= 0) {
                    w = statusDataList.size();
                } else {
                    w = speedDataList.size();
                }
                for (int i = 0; i < w; i++) {

                    double status = statusDataList.get(i).getPointValueInDouble();
//                    double speed = speedDataList.get(i).getPointValueInDouble();
                    double speed = speedDataList.get(w-i).getPointValueInDouble();
                    if (i == 0) {
//                        begin = new Date(speedDataList.get(i).getPointTime());
                        begin = new Date(speedDataList.get(w-i).getPointTime());
                        laststatus = status;
                    }

                    if (speed <= 3.0) {
                        if (laststatus != status && (status == stateDescription1.get(3.0) || status == stateDescription2.get(3.0))) {

//                            Date end = new Date(speedDataList.get(i).getPointTime());
                            Date end = new Date(speedDataList.get(w-i).getPointTime());
                            double zhcs = DateUtils.hoursDiff2(begin, end);
                            if (zhcs < 0.25)//5分钟
                            {

                                double temp = rate.getTimerate1();
                                temp = temp + 1;
                                rate.setTimerate1(temp);

                                temp = rate.getTimerate2();
                                temp = temp + 1;
                                rate.setTimerate2(temp);

                                temp = rate.getTimerate3();
                                temp = temp + 1;
                                rate.setTimerate3(temp);

                                temp = rate.getTimerate4();
                                temp = temp + 1;
                                rate.setTimerate4(temp);

                            } else if (zhcs < 0.33)//10分钟
                            {
                                double temp = rate.getTimerate2();
                                temp = temp + 1;
                                rate.setTimerate2(temp);

                                temp = rate.getTimerate3();
                                temp = temp + 1;
                                rate.setTimerate3(temp);

                                temp = rate.getTimerate4();
                                temp = temp + 1;
                                rate.setTimerate4(temp);
                            } else if (zhcs < 0.42)//15分钟
                            {
                                double temp = rate.getTimerate3();
                                temp = temp + 1;
                                rate.setTimerate3(temp);

                                temp = rate.getTimerate4();
                                temp = temp + 1;
                                rate.setTimerate4(temp);
                            } else if (zhcs < 0.5)//20分钟
                            {
                                double temp = rate.getTimerate4();
                                temp = temp + 1;
                                rate.setTimerate4(temp);
                            }


                            double temp = rate.getTimerate13();
                            temp = temp + 1;
                            rate.setTimerate13(temp);
                        }

                        laststatus = status;
                        begin = new Date(speedDataList.get(i).getPointTime());
                    } else if (speed <= 4.0) {
                        if (laststatus != status &&
                                (status == stateDescription1.get(3.5) || status == stateDescription2.get(3.5))) {

//                            Date end = new Date(speedDataList.get(i).getPointTime());
                            Date end = new Date(speedDataList.get(w-i).getPointTime());
                            double zhcs = DateUtils.hoursDiff2(begin, end);
                            if (zhcs < 0.25)//5分钟
                            {

                                double temp = rate.getTimerate5();
                                temp = temp + 1;
                                rate.setTimerate5(temp);

                                temp = rate.getTimerate6();
                                temp = temp + 1;
                                rate.setTimerate6(temp);

                                temp = rate.getTimerate7();
                                temp = temp + 1;
                                rate.setTimerate7(temp);

                                temp = rate.getTimerate8();
                                temp = temp + 1;
                                rate.setTimerate8(temp);
                            } else if (zhcs < 0.33)//10分钟
                            {

                                double temp = rate.getTimerate6();
                                temp = temp + 1;
                                rate.setTimerate6(temp);

                                temp = rate.getTimerate7();
                                temp = temp + 1;
                                rate.setTimerate7(temp);

                                temp = rate.getTimerate8();
                                temp = temp + 1;
                                rate.setTimerate8(temp);
                            } else if (zhcs < 0.42)//15分钟
                            {
                                double temp = rate.getTimerate7();
                                temp = temp + 1;
                                rate.setTimerate7(temp);

                                temp = rate.getTimerate8();
                                temp = temp + 1;
                                rate.setTimerate8(temp);

                            } else if (zhcs < 0.5)//20分钟
                            {
                                double temp = rate.getTimerate8();
                                temp = temp + 1;
                                rate.setTimerate8(temp);
                            }

                            //}
                            double temp = rate.getTimerate14();
                            temp = temp + 1;
                            rate.setTimerate14(temp);

                        }

                        laststatus = status;
                        begin = new Date(speedDataList.get(i).getPointTime());
                    } else {
                        if (laststatus != status &&
                                (status == stateDescription1.get(4.0) || status == stateDescription2.get(4.0))) {

//                            Date end = new Date(speedDataList.get(i).getPointTime());
                            Date end = new Date(speedDataList.get(w-i).getPointTime());
                            double zhcs = DateUtils.hoursDiff2(begin, end);
                            if (zhcs < 0.25)//5分钟
                            {

                                double temp = rate.getTimerate9();
                                temp = temp + 1;
                                rate.setTimerate9(temp);

                                temp = rate.getTimerate10();
                                temp = temp + 1;
                                rate.setTimerate10(temp);

                                temp = rate.getTimerate11();
                                temp = temp + 1;
                                rate.setTimerate11(temp);

                                temp = rate.getTimerate12();
                                temp = temp + 1;
                                rate.setTimerate12(temp);
                            } else if (zhcs < 0.33)//10分钟
                            {

                                double temp = rate.getTimerate10();
                                temp = temp + 1;
                                rate.setTimerate10(temp);

                                temp = rate.getTimerate11();
                                temp = temp + 1;
                                rate.setTimerate11(temp);

                                temp = rate.getTimerate12();
                                temp = temp + 1;
                                rate.setTimerate12(temp);
                            } else if (zhcs < 0.42)//15分钟
                            {

                                double temp = rate.getTimerate11();
                                temp = temp + 1;
                                rate.setTimerate11(temp);

                                temp = rate.getTimerate12();
                                temp = temp + 1;
                                rate.setTimerate12(temp);
                            } else if (zhcs < 0.5)//20分钟
                            {
                                double temp = rate.getTimerate12();
                                temp = temp + 1;
                                rate.setTimerate12(temp);
                            }

                            double temp = rate.getTimerate15();
                            temp = temp + 1;
                            rate.setTimerate15(temp);


                            laststatus = status;
                            begin = new Date(speedDataList.get(i).getPointTime());
                        }
                    }
                }

            }
        }
        // latch.await();


    }

}








