package com.gyee.power.fitting.dispersionanalysis; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DateField; import cn.hutool.core.date.DateRange; import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.gyee.power.fitting.common.feign.RemoteServiceBuilder; import com.gyee.power.fitting.model.ProBasicEquipment; import com.gyee.power.fitting.model.ProBasicEquipmentPoint; import com.gyee.power.fitting.model.StateCause; import com.gyee.power.fitting.model.custom.TsDoubleData; import com.gyee.power.fitting.service.IStateCauseService; import com.gyee.power.fitting.service.ProBasicEquipmentPointService; import com.gyee.power.fitting.service.ProBasicEquipmentService; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; @Slf4j @Component public class CalculateTask { @Resource private IStateCauseService stateCauseService; @Resource private ProBasicEquipmentService proBasicEquipmentService; @Resource private ProBasicEquipmentPointService proBasicEquipmentPointService; @Resource private RemoteServiceBuilder remoteService; //秒 分 时 日 月 周 @Scheduled(cron = "0 0 2 * * ?") public void doTask() { List equipmentList = proBasicEquipmentService.getCacheList(null); Map sbztMap = proBasicEquipmentPointService.getEquipmentMapByUniformcode("SBZT"); Map mxztMap = proBasicEquipmentPointService.getEquipmentMapByUniformcode("MXZT"); //获取最新更新时间 QueryWrapper scWrapper = new QueryWrapper<>(); scWrapper.select("equipment_id,last_row(end_time) end_time"); StateCause sc = stateCauseService.getOne(scWrapper); DateRange dateRange = getDateRange(sc); if (dateRange == null) return; List stateCauseList = new ArrayList<>(); long starttime, endtime; for (DateTime time : dateRange) { starttime = time.getTime(); endtime = DateUtil.endOfDay(time).getTime(); String equipmentId, tbName; for (ProBasicEquipment equipment : equipmentList) { equipmentId = equipment.getId(); tbName = equipmentId + "_SC_CI08"; List datas8 = getDatas(sbztMap.get(equipmentId).getNemCode(), starttime, endtime); if (CollUtil.isNotEmpty(datas8)) { long ts0 = endtime; long ts1; int doubleValue0 = (int) datas8.get(0).getDoubleValue(); int doubleValue1 = 0; for (TsDoubleData data : datas8) { ts1 = data.getTs(); doubleValue1 = (int) data.getDoubleValue(); if (doubleValue1 == doubleValue0) continue; //0待机, 1运行, 2故障, 3检修, 4限电, 5受累, 6离线 if (doubleValue0 == 1) { stateCauseList.add(new StateCause(tbName, 1, ts1, ts0, ts0 - ts1)); } else if (doubleValue0 == 2) { stateCauseList.add(new StateCause(tbName, 2, ts1, ts0, ts0 - ts1)); } else if (doubleValue0 == 3) { stateCauseList.add(new StateCause(tbName, 3, ts1, ts0, ts0 - ts1)); } else if (doubleValue0 == 4) { stateCauseList.add(new StateCause(tbName, 4, ts1, ts0, ts0 - ts1)); } else if (doubleValue0 == 5) { stateCauseList.add(new StateCause(tbName, 5, ts1, ts0, ts0 - ts1)); } else if (doubleValue0 == 6) { stateCauseList.add(new StateCause(tbName, 6, ts1, ts0, ts0 - ts1)); } else { stateCauseList.add(new StateCause(tbName, 0, ts1, ts0, ts0 - ts1)); } ts0 = ts1; doubleValue0 = doubleValue1; } if (ts0 > starttime) { stateCauseList.add(new StateCause(tbName, doubleValue1, starttime, ts0, ts0 - starttime)); } }else { System.out.println(equipmentId+":8种状态:"+sbztMap.get(equipmentId).getNemCode()); } tbName = equipmentId + "_SC_CI14"; List datas14 = getDatas(mxztMap.get(equipmentId).getNemCode(), starttime, endtime); if (CollUtil.isNotEmpty(datas14)) { long ts0 = endtime; long ts1; int doubleValue0 = (int) datas14.get(0).getDoubleValue(); int doubleValue1 = 0; for (TsDoubleData data : datas14) { ts1 = data.getTs(); doubleValue1 = (int) data.getDoubleValue(); if (doubleValue1 == doubleValue0) continue; //0待机, 1手动停机, 2正常发电, 3发电降出力, 4故障, 5故障受累, 6检修, 7检修受累, 8限电降出力, 9限电停机, 10电网受累, 11环境受累, 12通讯中断, 13设备离线 if (doubleValue0 == 1) { stateCauseList.add(new StateCause(tbName, 1, ts1, ts0, ts0 - ts1)); } else if (doubleValue0 == 2) { stateCauseList.add(new StateCause(tbName, 2, ts1, ts0, ts0 - ts1)); } else if (doubleValue0 == 3) { stateCauseList.add(new StateCause(tbName, 3, ts1, ts0, ts0 - ts1)); } else if (doubleValue0 == 4) { stateCauseList.add(new StateCause(tbName, 4, ts1, ts0, ts0 - ts1)); } else if (doubleValue0 == 5) { stateCauseList.add(new StateCause(tbName, 5, ts1, ts0, ts0 - ts1)); } else if (doubleValue0 == 6) { stateCauseList.add(new StateCause(tbName, 6, ts1, ts0, ts0 - ts1)); } else if (doubleValue0 == 7) { stateCauseList.add(new StateCause(tbName, 7, ts1, ts0, ts0 - ts1)); } else if (doubleValue0 == 8) { stateCauseList.add(new StateCause(tbName, 8, ts1, ts0, ts0 - ts1)); } else if (doubleValue0 == 9) { stateCauseList.add(new StateCause(tbName, 9, ts1, ts0, ts0 - ts1)); } else if (doubleValue0 == 10) { stateCauseList.add(new StateCause(tbName, 10, ts1, ts0, ts0 - ts1)); } else if (doubleValue0 == 11) { stateCauseList.add(new StateCause(tbName, 11, ts1, ts0, ts0 - ts1)); } else if (doubleValue0 == 12) { stateCauseList.add(new StateCause(tbName, 12, ts1, ts0, ts0 - ts1)); } else if (doubleValue0 == 13) { stateCauseList.add(new StateCause(tbName, 13, ts1, ts0, ts0 - ts1)); } else { stateCauseList.add(new StateCause(tbName, 0, ts1, ts0, ts0 - ts1)); } ts0 = ts1; doubleValue0 = doubleValue1; } if (ts0 > starttime) { stateCauseList.add(new StateCause(tbName, doubleValue1, starttime, ts0, ts0 - starttime)); } }else { System.out.println(equipmentId+":14种状态:"+sbztMap.get(equipmentId).getNemCode()); } } } int i = stateCauseService.saveBatch(stateCauseList); System.out.println("计算完成" + i); } private DateRange getDateRange(StateCause sc) { DateTime startTime; DateTime endTime = DateUtil.beginOfDay(DateUtil.yesterday()); if (sc == null) { startTime = endTime; } else { startTime = DateUtil.offsetDay(DateUtil.beginOfDay(DateUtil.date(sc.getEndTime())), 1); if (startTime.isAfter(endTime)) return null; } return DateUtil.range(startTime, endTime, DateField.DAY_OF_YEAR); } private List getDatas(String nemCode, long starttime, long endtime) { List rawValuesByKey; if (nemCode.startsWith("GF")) { rawValuesByKey = remoteService.adaptergf().getRawValuesByKey(nemCode, starttime, endtime); } else { rawValuesByKey = remoteService.adapterfd().getRawValuesByKey(nemCode, starttime, endtime); } rawValuesByKey.sort(Comparator.comparing(TsDoubleData::getTs).reversed()); return rawValuesByKey; } //秒 分 时 日 月 周 // @Scheduled(cron = "0 11 10 * * ?") public void creatStables() { List equipmentList = proBasicEquipmentService.getCacheList(null); String equipmentId, tbName; List stateCauseList = new ArrayList<>(); for (ProBasicEquipment equipment : equipmentList) { equipmentId = equipment.getId(); tbName = equipmentId + "_SC_CI08"; StateCause sc1 = new StateCause(); sc1.setEquipmentId(equipmentId); sc1.setTbname(tbName); sc1.setStationId(equipment.getWindpowerstationId()); sc1.setStateType("8种状态"); stateCauseList.add(sc1); tbName = equipmentId + "_SC_CI14"; StateCause sc2 = new StateCause(); sc2.setEquipmentId(equipmentId); sc2.setTbname(tbName); sc2.setStationId(equipment.getWindpowerstationId()); sc2.setStateType("14种状态"); stateCauseList.add(sc2); } stateCauseService.creatTablesBatch(stateCauseList); } }