package com.gyee.generation.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gyee.common.contant.ContantXk;
import com.gyee.common.model.PointData;
import com.gyee.generation.feign.AdapterApi;
import com.gyee.generation.init.CacheContext;
import com.gyee.generation.mapper.auto.AlarmTsMapper;
import com.gyee.generation.model.PointData2;
import com.gyee.generation.model.auto.*;
import com.gyee.generation.model.vo.AlarmSuperTalbeType;
import com.gyee.generation.model.vo.AlarmVo;
import com.gyee.generation.model.vo.StatusDetailValue;
import com.gyee.generation.service.auto.IProEconInterruptionService;
import com.gyee.generation.service.auto.IProEconShutdownEvent2Service;
import com.gyee.generation.service.auto.IProEconShutdownEventService;
import com.gyee.generation.service.auto.IProEconWindturbineStatusService;
import com.gyee.generation.util.DateUtils;
import com.gyee.generation.util.StringUtils;
import com.gyee.generation.util.realtimesource.IEdosUtil;
import com.gyee.generation.util.realtimesource.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.net.URI;
import java.util.*;
import java.util.concurrent.Executor;
import java.util.stream.Collectors;

@Service
public class ShutdownnewService {


    @Resource
    private IEdosUtil edosUtil;

    @Resource
    private IProEconWindturbineStatusService proEconWindturbineStatusService;

    @Resource
    private IProEconShutdownEventService proEconShutdownEventService;

    @Resource
    private IProEconShutdownEvent2Service proEconShutdownEvent2Service;
    @Resource
    private IProEconInterruptionService proEconInterruptionService;

    @Resource
    private AlarmTsMapper alarmTsMapper;


    public static Logger logger = LoggerFactory.getLogger(ShutdownnewService.class);


    @Resource
    private AdapterApi adapter;


    public URI goldenUri() {
        return URI.create("http://10.220.1.8:8011/ts");
    }

//    public URI goldenUri() {
//        return URI.create("http://10.220.1.241:8011/ts");
//    }


    public void execShutdown(Date recordDate) throws Exception {

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

        Map<String, PointData2> wtStatusMap = new HashMap<>();


        Map<String, ProEconWindturbineStatus> oldStatusMap = new HashMap<>();
        Map<String, ProEconShutdownEvent> oldShutdownEvent = new HashMap<>();
        Map<String, ProEconInterruption> oldInterrup = new HashMap<>();
        Map<String, ProEconShutdownEvent2> oldShutdownEvent2 = new HashMap<>();

        List<ProBasicEquipment> wtls = new ArrayList<>();

        wtls.addAll(CacheContext.wtls);
        for (ProBasicEquipment wt : wtls) {
            Map<String, ProBasicEquipmentPoint> windturbinetestingpointnewMap = CacheContext.wtpAimap.get(wt.getId());

            //取实时状态数据 赋值给status

            if (windturbinetestingpointnewMap.containsKey(ContantXk.MXZT)) {
                ProBasicEquipmentPoint mxztPoint = windturbinetestingpointnewMap.get(ContantXk.MXZT);
                wtStatusIdLs.add(mxztPoint.getNemCode());
            }

            //初始化将所有风机编号作为key，value全部赋值为null
            oldStatusMap.put(wt.getId(), null);
            oldShutdownEvent.put(wt.getId(), null);
            oldInterrup.put(wt.getId(), null);
            oldShutdownEvent2.put(wt.getId(), null);
        }

//        List<PointData> wtStatusLs = edosUtil.getHistMatrix(wtStatusIdLs, recordDate.getTime() / 1000);
        String tagNameString = StringUtil.join(wtStatusIdLs, ",");
        Map<String, PointData2> wtStatusLs = adapter.getHistorySection(goldenUri(), tagNameString, recordDate.getTime());

        if (wtStatusLs.values().size() == wtls.size()) {

            for (int i = 0; i < wtls.size(); i++) {

                ProBasicEquipment wt = wtls.get(i);
                wtStatusMap.put(wt.getId(), wtStatusLs.get(wtStatusIdLs.get(i)));

            }
        }
        //******************************************************************************************************/

        //读取数据库存在的恢复时间为NULL的数据，并通过风机编号把数据遍历到对应的map
        QueryWrapper<ProEconWindturbineStatus> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNull("start_Time");
        queryWrapper.orderByDesc("stop_Time");
        List<ProEconWindturbineStatus> oldStatusQuery = proEconWindturbineStatusService.list(queryWrapper).stream().collect(Collectors.toList());

        for (ProEconWindturbineStatus oldStatus : oldStatusQuery) {
            if (oldStatusMap.containsKey(oldStatus.getWindturbineId())) {
                oldStatusMap.put(oldStatus.getWindturbineId(), oldStatus);
            }
        }
        QueryWrapper<ProEconShutdownEvent> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.isNull("start_Time");
        queryWrapper2.orderByDesc("stop_Time");
        List<ProEconShutdownEvent> oldShutdwonQuery = proEconShutdownEventService.list(queryWrapper2).stream().collect(Collectors.toList());

        for (ProEconShutdownEvent shutdown : oldShutdwonQuery) {
            if (oldShutdownEvent.containsKey(shutdown.getWindturbineId())) {
                oldShutdownEvent.put(shutdown.getWindturbineId(), shutdown);
            }
        }

        QueryWrapper<ProEconInterruption> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.isNull("start_Time");
        queryWrapper3.orderByDesc("stop_Time");
        List<ProEconInterruption> oldInterrupQuery = proEconInterruptionService.list(queryWrapper3).stream().collect(Collectors.toList());

        for (ProEconInterruption interrup : oldInterrupQuery) {
            if (oldInterrup.containsKey(interrup.getWindturbineId())) {
                oldInterrup.put(interrup.getWindturbineId(), interrup);
            }
        }
        QueryWrapper<ProEconShutdownEvent2> queryWrapper4 = new QueryWrapper<>();
        queryWrapper4.isNull("start_Time");
        queryWrapper4.orderByDesc("stop_Time");
        List<ProEconShutdownEvent2> oldShutdwonQuery2 = proEconShutdownEvent2Service.list(queryWrapper4).stream().collect(Collectors.toList());

        for (ProEconShutdownEvent2 shutdown : oldShutdwonQuery2) {
            if (oldShutdownEvent2.containsKey(shutdown.getWindturbineId())) {
                oldShutdownEvent2.put(shutdown.getWindturbineId(), shutdown);
            }
        }
        //******************************************************************************************************/

        List<ProEconShutdownEvent> updateProEconShutdownEventls = new ArrayList<>();
        List<ProEconShutdownEvent2> updateProEconShutdownEvent2ls = new ArrayList<>();
        List<ProEconInterruption> updateProEconInterruptionls = new ArrayList<>();
        List<ProEconWindturbineStatus> updateProEconWindturbineStatusls = new ArrayList<>();

        List<ProEconShutdownEvent> insertProEconShutdownEventls = new ArrayList<>();
        List<ProEconShutdownEvent2> insertProEconShutdownEvent2ls = new ArrayList<>();
        List<ProEconInterruption> insertProEconInterruptionls = new ArrayList<>();
        List<ProEconWindturbineStatus> insertProEconWindturbineStatusls = new ArrayList<>();
        //遍历PG数据库中存在的记录，恢复时间为空的集合
        for (String wtId : oldStatusMap.keySet()) {

            ProEconShutdownEvent shutdwonEvent = null;
            ProEconInterruption interrup = null;
            ProEconShutdownEvent2 shutdwonEvent2 = null;
            ProEconWindturbineStatus item = null;

            if (oldStatusMap.containsKey(wtId)) {
                item = oldStatusMap.get(wtId);
            }
            if (oldShutdownEvent.containsKey(wtId)) {

                //                if(wtId.equals("SXJ_KGDL_HSM_F_WT_0021_EQ"))
                //                {
                //                    System.out.println("");
                //                }
                shutdwonEvent = oldShutdownEvent.get(wtId);
            }
            if (oldShutdownEvent2.containsKey(wtId)) {
                shutdwonEvent2 = oldShutdownEvent2.get(wtId);
            }
            if (oldInterrup.containsKey(wtId)) {
                interrup = oldInterrup.get(wtId);
            }

            if (wtStatusMap.containsKey(wtId)) {
                //                if(wtId.equals("SXJ_KGDL_YF_F_WT_0043_EQ"))
                //                {
                //                    System.out.println("");
                //                }
                PointData2 wtPoint = wtStatusMap.get(wtId);
                //获取当前时间
                Date currentTime = recordDate;
                if (StringUtils.empty(item)) {
                    item = new ProEconWindturbineStatus();
                    initalWindturbineStatus(insertProEconWindturbineStatusls, wtId, wtStatusMap, currentTime, item);
                } else if (StringUtils.notEmp(item) && wtPoint.getValue() != item.getSatusCode()) {
                    finishWindturbineStatus(updateProEconWindturbineStatusls, wtId, currentTime, item);

                }

                if (wtPoint.getValue() == StatusDetailValue.TXZD.getCode()) {
                    if (StringUtils.empty(interrup)) {
                        interrup = new ProEconInterruption();
                        initalInterruption(insertProEconInterruptionls, wtId, currentTime, interrup);
                    }
                } else if (StringUtils.notEmp(interrup)) {

                    finishInterruption(updateProEconInterruptionls, wtId, currentTime, interrup);

                }

                if (wtPoint.getValue() == StatusDetailValue.GZ.getCode()) {
                    if (StringUtils.empty(shutdwonEvent)) {
                        shutdwonEvent = new ProEconShutdownEvent();
                        initalShutdownEvent(insertProEconShutdownEventls, wtId, wtStatusMap, currentTime, shutdwonEvent);
                    }
                    if (StringUtils.empty(shutdwonEvent2)) {
                        shutdwonEvent2 = new ProEconShutdownEvent2();
                        initalShutdownEvent2(insertProEconShutdownEvent2ls, wtId, wtStatusMap, currentTime, shutdwonEvent2);
                    }
                } else {
                    if (StringUtils.notEmp(shutdwonEvent) && shutdwonEvent.getStatusCode() != wtPoint.getValue()) {
                        finishShutdownEvent(updateProEconShutdownEventls, wtId, wtStatusMap, currentTime, shutdwonEvent);
                    }
                    if (StringUtils.notEmp(shutdwonEvent2) && shutdwonEvent2.getStatusCode() != wtPoint.getValue()) {
                        finishShutdownEvent2(updateProEconShutdownEvent2ls, wtId, wtStatusMap, currentTime, shutdwonEvent2);
                    }

                }

                if (wtPoint.getValue() == StatusDetailValue.JX.getCode()) {
                    if (StringUtils.empty(shutdwonEvent)) {
                        shutdwonEvent = new ProEconShutdownEvent();
                        initalShutdownEvent(insertProEconShutdownEventls, wtId, wtStatusMap, currentTime, shutdwonEvent);
                    }
                    if (StringUtils.empty(shutdwonEvent2)) {
                        shutdwonEvent2 = new ProEconShutdownEvent2();
                        initalShutdownEvent2(insertProEconShutdownEvent2ls, wtId, wtStatusMap, currentTime, shutdwonEvent2);
                    }
                } else {
                    if (StringUtils.notEmp(shutdwonEvent) && shutdwonEvent.getStatusCode() != wtPoint.getValue()) {
                        finishShutdownEvent(updateProEconShutdownEventls, wtId, wtStatusMap, currentTime, shutdwonEvent);
                    }
                    if (StringUtils.notEmp(shutdwonEvent2) && shutdwonEvent2.getStatusCode() != wtPoint.getValue()) {
                        finishShutdownEvent2(updateProEconShutdownEvent2ls, wtId, wtStatusMap, currentTime, shutdwonEvent2);
                    }

                }
            }

        }


        for (ProEconShutdownEvent vo : updateProEconShutdownEventls) {
            UpdateWrapper<ProEconShutdownEvent> updateWrapper = new UpdateWrapper<>();
            //            updateWrapper.set("start_time", vo.getStartTime());
            //            updateWrapper.set("loss_power", vo.getLossPower());
            //            updateWrapper.set("stop_hours", vo.getStopHours());
            //            proEconShutdownEventService.update(vo, updateWrapper);
            proEconShutdownEventService.updateShutdownevent(vo.getStartTime(), vo.getLossPower(), vo.getStopHours(), vo.getId());
        }

        for (ProEconShutdownEvent2 vo : updateProEconShutdownEvent2ls) {
            UpdateWrapper<ProEconShutdownEvent2> updateWrapper = new UpdateWrapper<>();
            //            updateWrapper.set("start_time", vo.getStartTime());
            //            updateWrapper.set("loss_power", vo.getLossPower());
            //            updateWrapper.set("stop_hours", vo.getStopHours());
            //            proEconShutdownEvent2Service.update(vo, updateWrapper);
            proEconShutdownEvent2Service.updateShutdownevent2(vo.getStartTime(), vo.getLossPower(), vo.getStopHours(), vo.getId());
        }
        for (ProEconWindturbineStatus vo : updateProEconWindturbineStatusls) {
            UpdateWrapper<ProEconWindturbineStatus> updateWrapper = new UpdateWrapper<>();
            //            updateWrapper.set("start_time", vo.getStartTime());
            //            updateWrapper.set("loss_power", vo.getLossPower());
            //            updateWrapper.set("stop_hours", vo.getStopHours());
            //            proEconWindturbineStatusService.update(vo, updateWrapper);
            proEconWindturbineStatusService.updateWindturbineStatus(vo.getStartTime(), vo.getLossPower(), vo.getStopHours(), vo.getId());

        }

        for (ProEconInterruption vo : updateProEconInterruptionls) {
            UpdateWrapper<ProEconInterruption> updateWrapper = new UpdateWrapper<>();
            //            updateWrapper.set("start_time", vo.getStartTime());
            //            updateWrapper.set("stop_hours", vo.getStopHours());
            //            proEconInterruptionService.update(vo, updateWrapper);
            proEconInterruptionService.updateInterruption(vo.getStartTime(), vo.getStopHours(), vo.getId());
        }

        List<ProEconWindturbineStatus> temp1ls = new ArrayList<>();
        for (ProEconWindturbineStatus vo : insertProEconWindturbineStatusls) {
            temp1ls.add(vo);
            if (temp1ls.size() == 100) {
                proEconWindturbineStatusService.saveBatch(temp1ls);
                temp1ls = new ArrayList<>();
            }
        }

        if (!temp1ls.isEmpty()) {
            proEconWindturbineStatusService.saveBatch(temp1ls);
        }
        List<ProEconInterruption> temp2ls = new ArrayList<>();
        for (ProEconInterruption vo : insertProEconInterruptionls) {
            temp2ls.add(vo);
            if (temp2ls.size() == 100) {
                proEconInterruptionService.saveBatch(temp2ls);
                temp2ls = new ArrayList<>();
            }
        }

        if (!temp2ls.isEmpty()) {
            proEconInterruptionService.saveBatch(temp2ls);
        }

        List<ProEconShutdownEvent> temp3ls = new ArrayList<>();
        for (ProEconShutdownEvent vo : insertProEconShutdownEventls) {
            temp3ls.add(vo);
            if (temp3ls.size() == 100) {
                proEconShutdownEventService.saveBatch(temp3ls);
                temp3ls = new ArrayList<>();
            }
        }

        if (!temp3ls.isEmpty()) {
            proEconShutdownEventService.saveBatch(temp3ls);
        }
        List<ProEconShutdownEvent2> temp4ls = new ArrayList<>();
        for (ProEconShutdownEvent2 vo : insertProEconShutdownEvent2ls) {
            temp4ls.add(vo);
            if (temp4ls.size() == 100) {
                proEconShutdownEvent2Service.saveBatch(temp4ls);
                temp4ls = new ArrayList<>();
            }
        }

        if (!temp4ls.isEmpty()) {
            proEconShutdownEvent2Service.saveBatch(temp4ls);
        }


        //******************************************************************************************************/


    }

    private void finishShutdownEvent(List<ProEconShutdownEvent> updateProEconShutdownEventls,
                                     String wtId, Map<String, PointData2> wtStatusMap, Date currentTime, ProEconShutdownEvent item) throws Exception {

        if (item.getStatusCode() != wtStatusMap.get(wtId).getValue()) {
            item.setStartTime(currentTime);

            double timehour = DateUtils.hoursDiff2(item.getStartTime(), item.getStopTime());
            item.setStopHours(timehour);
            double losspower = calLossPower(wtId, item.getStartTime(), item.getStopTime());
            item.setLossPower(losspower);


            updateProEconShutdownEventls.add(item);
        }

    }


    private void finishShutdownEvent2(List<ProEconShutdownEvent2> insertProEconShutdownEvent2ls,
                                      String wtId, Map<String, PointData2> wtStatusMap, Date currentTime, ProEconShutdownEvent2 item) throws Exception {

        if (item.getStatusCode() != wtStatusMap.get(wtId).getValue()) {
            item.setStartTime(currentTime);

            double timehour = DateUtils.hoursDiff2(item.getStartTime(), item.getStopTime());
            item.setStopHours(timehour);
            double losspower = calLossPower(wtId, item.getStartTime(), item.getStopTime());
            item.setLossPower(losspower);

            if (timehour <= 24) {

                insertProEconShutdownEvent2ls.add(item);

            } else {
                int days = DateUtils.daysDiff1(item.getStartTime(), item.getStopTime());
                Calendar c = Calendar.getInstance();
                Date startTime = item.getStartTime();
                c.setTime(item.getStopTime());
                c.set(Calendar.HOUR_OF_DAY, 23);
                c.set(Calendar.MINUTE, 59);
                c.set(Calendar.SECOND, 59);
                for (int i = 0; i < days; i++) {
                    if (i == 0) {
                        item.setStartTime(c.getTime());
                        losspower = calLossPower(wtId, item.getStartTime(), item.getStopTime());
                        item.setLossPower(losspower);
                        insertProEconShutdownEvent2ls.add(item);

                    } else {
                        ProEconShutdownEvent2 newto = initalShutdownEventnew2(wtId, wtStatusMap, DateUtils.truncate(c.getTime()));

                        if (i == days - 1) {
                            newto.setStartTime(startTime);

                        } else {
                            newto.setStartTime(c.getTime());

                        }
                        losspower = calLossPower(wtId, newto.getStartTime(), newto.getStopTime());
                        newto.setLossPower(losspower);
                        insertProEconShutdownEvent2ls.add(newto);

                    }

                    c.add(Calendar.DAY_OF_MONTH, 1);
                }
            }
        }


    }


    private void initalShutdownEvent2(List<ProEconShutdownEvent2> insertProEconShutdownEvent2ls,
                                      String wtId, Map<String, PointData2> wtStatusMap, Date currentTime, ProEconShutdownEvent2 item) {
        item.setId(StringUtils.getUUID());
        item.setProjectId(CacheContext.wtmap.get(wtId).getProjectId());
        item.setStatusCode((int) wtStatusMap.get(wtId).getValue());
        item.setStopTime(currentTime);
        item.setWinpowerstationId(CacheContext.wtmap.get(wtId).getWindpowerstationId());
        item.setWindturbineId(wtId);
        if (wtStatusMap.get(wtId).getValue() == StatusDetailValue.GZ.getCode()) {

            item.setStopTypeId("gz");

            //读取报警记录表前后五分钟数据，选取第一个报警报警类型

            List<AlarmVo> vos = alarmTsMapper.selectLastRowByDeviceid(AlarmSuperTalbeType.WT.getCode(), wtId);
            if (!vos.isEmpty()) {
                AlarmVo vo = vos.get(0);

                if (StringUtils.notEmp(vo.getAlarmid())) {
                    item.setWarningId(vo.getAlarmid());
                }
                if (StringUtils.notEmp(vo.getDescription())) {
                    item.setFaultView(vo.getDescription());
                }
                if (StringUtils.notEmp(vo.getFaultCause())) {
                    item.setFaultView(vo.getFaultCause());
                }
                if (StringUtils.notEmp(vo.getResolvent())) {
                    item.setHandleWay(vo.getResolvent());
                }
            }
        } else if (wtStatusMap.get(wtId).getValue() == StatusDetailValue.JX.getCode()) {
            item.setStopTypeId("wh");

        }

        insertProEconShutdownEvent2ls.add(item);
    }

    private ProEconShutdownEvent2 initalShutdownEventnew2(String wtId, Map<String, PointData2> wtStatusMap, Date currentTime) {
        ProEconShutdownEvent2 item = new ProEconShutdownEvent2();
        item.setId(StringUtils.getUUID());
        item.setProjectId(CacheContext.wtmap.get(wtId).getProjectId());
        item.setStatusCode((int) wtStatusMap.get(wtId).getValue());
        item.setStopTime(currentTime);
        item.setWinpowerstationId(CacheContext.wtmap.get(wtId).getWindpowerstationId());
        item.setWindturbineId(wtId);
        if (wtStatusMap.get(wtId).getValue() == StatusDetailValue.GZ.getCode()) {

            item.setStopTypeId("gz");

            //读取报警记录表前后五分钟数据，选取第一个报警报警类型

            List<AlarmVo> vos = alarmTsMapper.selectLastRowByDeviceid(AlarmSuperTalbeType.WT.getCode(), wtId);
            if (!vos.isEmpty()) {
                AlarmVo vo = vos.get(0);

                if (StringUtils.notEmp(vo.getAlarmid())) {
                    item.setWarningId(vo.getAlarmid());
                }
                if (StringUtils.notEmp(vo.getDescription())) {
                    item.setFaultView(vo.getDescription());
                }
                if (StringUtils.notEmp(vo.getFaultCause())) {
                    item.setFaultView(vo.getFaultCause());
                }
                if (StringUtils.notEmp(vo.getResolvent())) {
                    item.setHandleWay(vo.getResolvent());
                }
            }
        } else if (wtStatusMap.get(wtId).getValue() == StatusDetailValue.JX.getCode()) {
            item.setStopTypeId("wh");

        }
        return item;
    }

    private void initalShutdownEvent(List<ProEconShutdownEvent> insertProEconShutdownEventls,
                                     String wtId, Map<String, PointData2> wtStatusMap, Date currentTime, ProEconShutdownEvent item) {
        item.setId(StringUtils.getUUID());
        item.setProjectId(CacheContext.wtmap.get(wtId).getProjectId());
        item.setStatusCode((int) wtStatusMap.get(wtId).getValue());
        item.setStopTime(currentTime);
        item.setWindpowerstationId(CacheContext.wtmap.get(wtId).getWindpowerstationId());
        item.setWindturbineId(wtId);
        if (wtStatusMap.get(wtId).getValue() == StatusDetailValue.GZ.getCode()) {

            item.setStoptypeId("gz");

            //读取报警记录表前后五分钟数据，选取第一个报警报警类型

            List<AlarmVo> vos = alarmTsMapper.selectLastRowByDeviceid(AlarmSuperTalbeType.WT.getCode(), wtId);
            if (!vos.isEmpty()) {
                AlarmVo vo = vos.get(0);

                if (StringUtils.notEmp(vo.getAlarmid())) {
                    item.setWarningId(vo.getAlarmid());
                }
                if (StringUtils.notEmp(vo.getDescription())) {
                    item.setFaultView(vo.getDescription());
                }
                if (StringUtils.notEmp(vo.getFaultCause())) {
                    item.setFaultView(vo.getFaultCause());
                }
                if (StringUtils.notEmp(vo.getResolvent())) {
                    item.setHandleWay(vo.getResolvent());
                }
            }

        } else if (wtStatusMap.get(wtId).getValue() == StatusDetailValue.JX.getCode()) {
            item.setStoptypeId("wh");

        }

        insertProEconShutdownEventls.add(item);
    }

    private void initalWindturbineStatus(List<ProEconWindturbineStatus> insertProEconWindturbineStatusls,
                                         String wtId, Map<String, PointData2> wtStatusMap, Date currentTime, ProEconWindturbineStatus item) {

        item.setProjectId(CacheContext.wtmap.get(wtId).getProjectId());
        item.setSatusCode((int) wtStatusMap.get(wtId).getValue());
        item.setStatusDesc(StatusDetailValue.getNmaeByCode(wtStatusMap.get(wtId).getValue()));
        item.setStopTime(currentTime);
        item.setWindpowerstationId(CacheContext.wtmap.get(wtId).getWindpowerstationId());
        item.setWindturbineId(wtId);
        insertProEconWindturbineStatusls.add(item);

    }

    private void finishWindturbineStatus(List<ProEconWindturbineStatus> updateProEconWindturbineStatusls,
                                         String wtId, Date currentTime, ProEconWindturbineStatus item) throws Exception {
        item.setStartTime(currentTime);
        double timehour = DateUtils.hoursDiff2(item.getStartTime(), item.getStopTime());
        item.setStopHours(timehour);
        double losspower = calLossPower(wtId, item.getStartTime(), item.getStopTime());
        item.setLossPower(losspower);
        updateProEconWindturbineStatusls.add(item);
    }

    private void initalInterruption(List<ProEconInterruption> insertProEconWindturbineStatusls,
                                    String wtId, Date currentTime, ProEconInterruption item) {
        item.setProjectId(CacheContext.wtmap.get(wtId).getProjectId());
        item.setStopTime(currentTime);
        item.setWindpowerstationId(CacheContext.wtmap.get(wtId).getWindpowerstationId());
        item.setWindturbineId(wtId);
        insertProEconWindturbineStatusls.add(item);
    }

    private void finishInterruption(List<ProEconInterruption> updateProEconWindturbineStatusls,
                                    String wtId, Date currentTime, ProEconInterruption item) {

        item.setStartTime(currentTime);
        double timehour = DateUtils.hoursDiff2(item.getStartTime(), item.getStopTime());
        item.setStopHours(timehour);

        updateProEconWindturbineStatusls.add(item);
    }


    private Double calLossPower(String windturbineId, Date startDate, Date endDate) throws Exception {
        double result = 0.0;

        Map<String, Map<String, ProBasicEquipmentPoint>> wtpAimap = CacheContext.wtpAimap;
        Map<String, ProBasicEquipmentPoint> wtpointmap = wtpAimap.get(windturbineId);

        Calendar c1 = Calendar.getInstance();
        c1.setTime(startDate);
        Calendar c2 = Calendar.getInstance();
        c2.setTime(endDate);
        //判定限电是否跨年
        if (endDate.after(startDate) && c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR)) {
            //年故障损失电量
            if (wtpointmap.containsKey(ContantXk.NGZSSDL)) {
                double beginvalue;
                double endvalue;

                List<String> pointid = new ArrayList<>();
                pointid.add(wtpointmap.get(ContantXk.NGZSSDL).getNemCode());
                List<PointData> xdbegin = edosUtil.getHistMatrix(pointid, startDate.getTime() / 1000);
                List<PointData> xdend = edosUtil.getHistMatrix(pointid, endDate.getTime() / 1000);


                if (xdbegin.size() == 1 && xdend.size() == 1) {
                    beginvalue = xdbegin.get(0).getPointValueInDouble();
                    endvalue = xdend.get(0).getPointValueInDouble();

                    if (endvalue > beginvalue) {
                        result = result + (endvalue - beginvalue);
                    }
                }
            }


        } else {
            Calendar c = Calendar.getInstance();
            c.setTime(DateUtils.truncate(startDate));
            c.add(Calendar.DAY_OF_MONTH, 1);
            //获得1月1日零点值
            Date endtime = c.getTime();
            //年故障损失电量
            if (wtpointmap.containsKey(ContantXk.NGZSSDL)) {
                double beginvalue;
                double endvalue;


                List<String> pointid = new ArrayList<>();
                pointid.add(wtpointmap.get(ContantXk.NGZSSDL).getNemCode());
                List<PointData> xdbegin = edosUtil.getHistMatrix(pointid, startDate.getTime() / 1000);
                List<PointData> xdend = edosUtil.getHistMatrix(pointid, endtime.getTime() / 1000);


                if (xdbegin.size() == 1 && xdend.size() == 1) {
                    beginvalue = xdbegin.get(0).getPointValueInDouble();
                    endvalue = xdend.get(0).getPointValueInDouble();

                    if (endvalue > beginvalue) {
                        result = result + (endvalue - beginvalue);
                    }
                }
            }

            //日故障损失电量
            if (wtpointmap.containsKey(ContantXk.NGZSSDL)) {

                double endvalue;

                List<String> pointid = new ArrayList<>();
                pointid.add(wtpointmap.get(ContantXk.NGZSSDL).getNemCode());
                List<PointData> xdend = edosUtil.getHistMatrix(pointid, startDate.getTime() / 1000);

                if (xdend.size() == 1) {

                    endvalue = xdend.get(0).getPointValueInDouble();

                    if (endvalue > 0) {
                        result = result + endvalue;
                    }
                }
            }

        }

        return result;

    }

}







