package com.ruoyi; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.ruoyi.quartz.handler.IJobHandler; import com.ruoyi.quartz.task.ChangedSave; import com.ruoyi.ucp.entity.*; import com.ruoyi.ucp.feign.AdapterApi; import com.ruoyi.ucp.service.*; import com.ruoyi.ucp.util.CalcCache; import org.springframework.data.redis.core.RedisTemplate; import javax.annotation.Resource; import java.net.URI; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; public class JavaFunctionJobHandler extends IJobHandler { @Resource private AdapterApi adapter; @Resource private IPointInfoService pointService; @Resource private IStationInfoHourService stationInfoHourService; @Resource private IStationInfoDayService stationInfoDayService; @Resource private ILineInfoDayService lineInfoDayService; @Resource private IStateAiService stateAiService; @Resource private RedisTemplate redisTemplate; @Resource private IEquipmentModelService equipmentModelService; @Resource private ITurbineInfoDayService turbineInfoDayService; @Override public void execute() throws Exception { } @Override public IJobHandler getFunctionHandler() { return null; } @Override public void setFunctionHandler(IJobHandler jobHandler) { } public void calcAQTS(Date date) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.select("min(record_date) record_date"); StationInfoDay one = stationInfoDayService.getOne(wrapper); wrapper = new QueryWrapper<>(); wrapper.eq("record_date", one.getRecordDate()); //最早时间的列表 List list = stationInfoDayService.list(wrapper); List today = getStationinfoByDate(date); if (today.isEmpty()) { for (StationInfoDay infoDay : list) { StationInfoDay day = new StationInfoDay(); day.setStationId(infoDay.getStationId()); day.setRecordDate(date); day.setAqts(infoDay.getAqts() + (int) DateUtil.betweenDay(infoDay.getRecordDate(), date, true)); today.add(day); } } else { Map collect = list.stream().collect(Collectors.toMap(StationInfoDay::getStationId, Function.identity())); for (StationInfoDay day : today) { StationInfoDay day1 = collect.get(day.getStationId()); day.setAqts(day1.getAqts() + (int) DateUtil.betweenDay(day1.getRecordDate(), date, true)); } } stationInfoDayService.saveOrUpdateBatch(today); } public List getStationinfoByDate(Date date) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("record_date", date); return stationInfoDayService.list(wrapper); } public URI taosGoldenUriTest() { return URI.create("http://127.0.0.1:8011/ts"); } public URI taosUri() { return URI.create("http://172.16.12.101:8012/ts"); } public void calcStationHourRFDL(Date hour) { PointInfo pointInfo = new PointInfo(); pointInfo.setInstitutionType("station"); pointInfo.setUniformCode("RFDLSYZ"); List entity = pointService.getByEntity(pointInfo); Map section = adapter.getHistorySection(taosGoldenUriTest() , pointInfos2Keys(entity), hour.getTime()); List byHours = getStationinfoByHour(hour); if (byHours.isEmpty()) { for (PointInfo point : entity) { StationInfoHour info = new StationInfoHour(); info.setStationId(point.getStationId()); info.setRecordDate(DateUtil.beginOfHour(hour)); info.setRfdl(section.get(point.getPointKey()).getDoubleValue()); byHours.add(info); } } else { Map collect = entity.stream().collect(Collectors.toMap(PointInfo::getStationId, PointInfo::getPointKey)); for (StationInfoHour byHour : byHours) { byHour.setRfdl(section.get(collect.get(byHour.getStationId())).getDoubleValue()); } } stationInfoHourService.saveOrUpdateBatch(byHours); } public List getStationinfoByHour(Date hour) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("record_date", DateUtil.beginOfHour(hour)); return stationInfoHourService.list(wrapper); } public String pointInfos2Keys(List entity) { return entity.stream().map(PointInfo::getPointKey).collect(Collectors.joining(",")); } public void calcStationHourPJGL(Date hour) { PointInfo pointInfo = new PointInfo(); pointInfo.setInstitutionType("station"); pointInfo.setUniformCode("SSZGL"); List entity = pointService.getByEntity(pointInfo); List byHours = getStationinfoByHour(hour); Map collect = new HashMap<>(); if (byHours.size() > 0) { collect = byHours.stream().collect( Collectors.toMap(StationInfoHour::getStationId, Function.identity())); } List stat; List byHours2 = new ArrayList<>(); for (PointInfo point : entity) { stat = adapter.getHistoryStat(taosGoldenUriTest(), point.getPointKey(), DateUtil.offsetHour(hour, -1).getTime(), hour.getTime()); if (byHours.isEmpty()) { StationInfoHour info = new StationInfoHour(); info.setStationId(point.getStationId()); info.setRecordDate(DateUtil.beginOfHour(hour)); if (stat.isEmpty()) info.setPjgl(0.0); info.setPjgl(stat.get(0).getAvg().getDoubleValue()); byHours2.add(info); } else { collect.get(point.getStationId()).setPjgl(stat.get(0).getAvg().getDoubleValue()); } } stationInfoHourService.saveOrUpdateBatch(byHours.isEmpty() ? byHours2 : byHours); } public void calcLineDjlRfdl(Date date) { //date当天零点 DateTime time = DateUtil.beginOfDay(date); //date昨天零点 DateTime time0 = DateUtil.offsetDay(time, -1); //date当天零点加三分钟 DateTime time2 = DateUtil.offsetMinute(time, 3); //date昨天零点加三分钟 DateTime time1 = DateUtil.offsetMinute(time0, 3); PointInfo pi = new PointInfo(); pi.setUniformCode("Z-ZXYG-JX"); pi.setLineId("all"); List entity = pointService.getByEntity(pi); String keys = pointInfos2Keys(entity); Map latest2 = adapter.getHistorySection(taosGoldenUriTest(), keys, time2.getTime()); Map latest1 = adapter.getHistorySection(taosGoldenUriTest(), keys, time1.getTime()); List byDate = getLineinfoByDate(time0.toJdkDate()); Map collect = new HashMap<>(); if (byDate.size() > 0) { collect = byDate.stream().collect(Collectors.toMap(LineInfoDay::getLineId, Function.identity())); } List list = new ArrayList<>(); for (PointInfo info : entity) { LineInfoDay day; if (byDate.isEmpty()) { day = new LineInfoDay(); day.setStationId(info.getStationId()); day.setProjectId(info.getProjectId()); day.setLineId(info.getLineId()); day.setRecordDate(time0.toJdkDate()); } else { day = collect.get(info.getLineId()); } day.setRfdl((int) ((latest2.get(info.getPointKey()).getDoubleValue() - latest1.get(info.getPointKey()).getDoubleValue()) * info.getCoef())); list.add(day); } lineInfoDayService.saveOrUpdateBatch(list); } public List getLineinfoByDate(Date date) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("record_date", date); return lineInfoDayService.list(wrapper); } public void calcStationSwGwCyDl(Date date) { //date当天零点 DateTime time = DateUtil.beginOfDay(date); //date昨天零点 DateTime time0 = DateUtil.offsetDay(time, -1); //date当天零点加三分钟 DateTime time2 = DateUtil.offsetMinute(time, 3); //date昨天零点加三分钟 DateTime time1 = DateUtil.offsetMinute(time0, 3); PointInfo swdlPi = new PointInfo(); swdlPi.setUniformCode("Z-ZXYG-CX"); PointInfo gwdlPi = new PointInfo(); gwdlPi.setUniformCode("Z-FXYG-CX"); PointInfo cydlPi = new PointInfo(); cydlPi.setUniformCode("Z-ZXYG-ZYB"); List swdlEt = pointService.getByEntity(swdlPi); List gwdlEt = pointService.getByEntity(gwdlPi); Map gwdlMap = gwdlEt.stream().collect(Collectors.toMap(PointInfo::getStationId, Function.identity())); List cydlEt = pointService.getByEntity(cydlPi); Map cydlMap = cydlEt.stream().collect(Collectors.toMap(PointInfo::getStationId, Function.identity())); String swdlK = pointInfos2Keys(swdlEt); String gwdlK = pointInfos2Keys(gwdlEt); String cydlK = pointInfos2Keys(cydlEt); Map swdlL2 = adapter.getHistorySection(taosGoldenUriTest(), swdlK, time2.getTime()); Map swdlL1 = adapter.getHistorySection(taosGoldenUriTest(), swdlK, time1.getTime()); Map gwdlL2 = adapter.getHistorySection(taosGoldenUriTest(), gwdlK, time2.getTime()); Map gwdlL1 = adapter.getHistorySection(taosGoldenUriTest(), gwdlK, time1.getTime()); Map cydlL2 = adapter.getHistorySection(taosGoldenUriTest(), cydlK, time2.getTime()); Map cydlL1 = adapter.getHistorySection(taosGoldenUriTest(), cydlK, time1.getTime()); List byDate = getStationinfoByDate(time0.toJdkDate()); Map collect = new HashMap<>(); if (byDate.size() > 0) { collect = byDate.stream().collect(Collectors.toMap(StationInfoDay::getStationId, Function.identity())); } StationInfoDay infoDay; String stationId; PointInfo gwdlKey, cydlKey; List infoDays = new ArrayList<>(); for (PointInfo info : swdlEt) { stationId = info.getStationId(); if (byDate.isEmpty()) { infoDay = new StationInfoDay(); infoDay.setStationId(stationId); infoDay.setRecordDate(time0.toJdkDate()); } else { infoDay = collect.get(stationId); } infoDay.setSwdl((int) ((swdlL2.get(info.getPointKey()).getDoubleValue() - swdlL1.get(info.getPointKey()).getDoubleValue()) * info.getCoef())); gwdlKey = gwdlMap.get(stationId); infoDay.setGwdl((int) ((gwdlL2.get(gwdlKey.getPointKey()).getDoubleValue() - gwdlL1.get(gwdlKey.getPointKey()).getDoubleValue()) * gwdlKey.getCoef())); cydlKey = cydlMap.get(stationId); infoDay.setCydl((int) ((cydlL2.get(cydlKey.getPointKey()).getDoubleValue() - cydlL1.get(cydlKey.getPointKey()).getDoubleValue()) * cydlKey.getCoef())); infoDays.add(infoDay); } stationInfoDayService.saveOrUpdateBatch(infoDays); } public void calcStationZhcydl(Date date) { //date当天零点 DateTime time = DateUtil.beginOfDay(date); //date昨天零点 DateTime time0 = DateUtil.offsetDay(time, -1); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.select("sum(rfdl) rfdl,station_id").eq("record_date", time0.toJdkDate()) .groupBy("station_id"); List list = lineInfoDayService.list(wrapper); List byDate = getStationinfoByDate(time0.toJdkDate()); Map collect = new HashMap<>(); if (byDate.size() > 0) { collect = byDate.stream().collect(Collectors.toMap(StationInfoDay::getStationId, Function.identity())); } StationInfoDay infoDay; List infoDays = new ArrayList<>(); for (LineInfoDay day : list) { if (byDate.isEmpty()) { infoDay = new StationInfoDay(); infoDay.setStationId(day.getStationId()); infoDay.setRecordDate(time0.toJdkDate()); } else { infoDay = collect.get(day.getStationId()); } infoDay.setRfdl(day.getRfdl()); infoDay.setZhcydl(infoDay.getRfdl() + infoDay.getGwdl() - infoDay.getSwdl()); infoDays.add(infoDay); } stationInfoDayService.saveOrUpdateBatch(infoDays); } public void calcTurbine5s() { } //此处加公式 public void calcTurbineSsztAi(Date date) { PointInfo pi = new PointInfo(); pi.setUniformCode("AI422"); pi.setInstitutionType("state"); List entity = pointService.getByEntity(pi); Map collect = entity.stream().collect(Collectors.toMap(PointInfo::getTurbineId, Function.identity())); String keys = pointInfos2Keys(entity); PointInfo pimx = new PointInfo(); pimx.setUniformCode("MXZT"); pimx.setInstitutionType("turbine"); List entityMx = pointService.getByEntity(pimx); Map collectMX = entityMx.stream().collect(Collectors.toMap(PointInfo::getTurbineId, Function.identity())); PointInfo pisb = new PointInfo(); pisb.setUniformCode("SBZT"); pisb.setInstitutionType("turbine"); List entitySB = pointService.getByEntity(pimx); Map collectSB = entitySB.stream().collect(Collectors.toMap(PointInfo::getTurbineId, Function.identity())); Map latest = adapter.getLatest(taosGoldenUriTest(), keys); List list = stateAiService.list(); Map> collectAi = list.stream().collect(Collectors.groupingBy(StateAi::getModel, Collectors.toMap(StateAi::getOriginalState, StateAi::getMappingState))); List pointDatas = new ArrayList<>(); int i1; for (PointInfo info : entity) { PointData data = latest.get(info.getPointKey()); data.setTagName(collectMX.get(info.getTurbineId()).getPointKey()); i1 = (int) data.getDoubleValue(); //缓存值 Integer i2 = ChangedSave.map.get(info.getPointKey()); if (i2 != null && i1 == i2) continue; ChangedSave.map.put(info.getPointKey(), i1); Integer i = collectAi.get(info.getSpare()).get(i1); data.setDoubleValue(i); pointDatas.add(data); } adapter.writeHistoryBatch(taosGoldenUriTest(), pointDatas); //redisTemplate.opsForHash().put("state_point",key,value); } public void calcStationXd(boolean priorRation) { //限电状态 PointInfo piAgc = new PointInfo(); //AGC piAgc.setUniformCode("AGC002"); piAgc.setInstitutionType("booster"); List entityAgc = pointService.getByEntity(piAgc); Map collectAgc = entityAgc.stream().collect(Collectors.toMap(PointInfo::getTurbineId, Function.identity())); String keysAgc = pointInfos2Keys(entityAgc); //出线 PointInfo piCx = new PointInfo(); piCx.setUniformCode("AGC001"); piCx.setInstitutionType("booster"); List entityCx = pointService.getByEntity(piCx); Map collectCx = entityCx.stream().collect(Collectors.toMap(PointInfo::getTurbineId, Function.identity())); String keysCx = pointInfos2Keys(entityCx); Map statAgc = adapter.getLatest(goldenUri(), keysAgc); Map statCx = adapter.getLatest(goldenUri(), keysCx); DateTime time5 = DateUtil.offsetMinute(DateUtil.date(), -5); List statAgc5 = adapter.getHistoryStat(goldenUri(), keysAgc, time5.getTime(), DateUtil.date().getTime()); List statCx5 = adapter.getHistoryStat(goldenUri(), keysCx, time5.getTime(), DateUtil.date().getTime()); //AI066 for (PointInfo info : entityAgc) { //如果场站限电状态为0 /*if (priorRation) { if (zsgl5 >= sub.getCapacity() * 0.4) { if (zsgl / agc > 1.15) { b = true; } if (agc5 <= cxgl5) { b = true; } if (zsgl > agc && agc5 - cxgl5 < 4) { b = true; } } } else { if (zsgl5 >= sub.getCapacity() * 0.4) { if (zsgl / agc >= 1.2 && agc - cxgl <= 2) { b = true; } if (agc5 <= cxgl5) { b = true; } } else { if (k) { if (agc5 <= cxgl5) { b = true; } if (zsgl / agc >= 1.1 && agc5 - cxgl5 <= 2) { b = true; } } } }*/ } } public void calcNhglZs(boolean priorRation) { //拟合功率(自算) //实时风速 PointInfo piFs = new PointInfo(); piFs.setUniformCode("AI066"); piFs.setInstitutionType("turbine"); List entityFs = pointService.getByEntity(piFs); Map collectFs = entityFs.stream().collect(Collectors.toMap(PointInfo::getTurbineId, Function.identity())); Map> collect = entityFs.stream().collect(Collectors.groupingBy(PointInfo::getStationId)); String keysFs = pointInfos2Keys(entityFs); //设备拟合功率(自算) PointInfo piFjzsgl = new PointInfo(); piFjzsgl.setUniformCode("ZSGL"); piFjzsgl.setInstitutionType("turbine"); List entityFjzsgl = pointService.getByEntity(piFjzsgl); Map collectFjzsgl = entityFjzsgl.stream().collect(Collectors.toMap(PointInfo::getTurbineId, PointInfo::getPointKey)); String keysFjzsgl = pointInfos2Keys(entityFjzsgl); //场站自算功率 PointInfo piCzzsgl = new PointInfo(); piCzzsgl.setUniformCode("AGC010"); piCzzsgl.setInstitutionType("booster"); List entityCzzsgl = pointService.getByEntity(piCzzsgl); Map collectCzzsgl = entityCzzsgl.stream().collect(Collectors.toMap(PointInfo::getStationId, PointInfo::getPointKey)); String keysCzzsgl = pointInfos2Keys(entityCzzsgl); DateTime time = DateUtil.beginOfMinute(DateUtil.date()); DateTime time5 = DateUtil.offsetMinute(time, -5); List statFs5; PointData avg; List avgs = new ArrayList<>(); double value, v; for (Map.Entry> entry : collect.entrySet()) { double stV = 0; for (PointInfo info : entry.getValue()) { //todo statFs5 = adapter.getHistoryStat(goldenUri(), info.getPointKey(), time5.getTime(), time.getTime()); if (CollUtil.isNotEmpty(statFs5)) { //todo value = statFs5.get(0).getAvg().getValue(); avg = new PointData(); v = CalcCache.wtMcfMap.get(info.getTurbineId()).get(value); stV += v; avg.setTs(time.getTime()); avg.setDoubleValue(v); avg.setTagName(collectFjzsgl.get(info.getTurbineId())); avgs.add(avg); } } adapter.writeHistoryBatch(taosUri(), avgs); PointData data = new PointData(); data.setTs(time.getTime()); data.setDoubleValue(stV); data.setTagName(collectCzzsgl.get(entry.getKey())); adapter.writeHistory(taosUri(), data); } } public URI goldenUri() { return URI.create("http://172.16.12.103:8011/ts"); } public void calcTurbineRFDL(Date date) { //date当天零点 DateTime time = DateUtil.beginOfDay(date); //date昨天零点 DateTime time0 = DateUtil.offsetDay(time, -1); //date当天零点加三分钟 DateTime time2 = DateUtil.offsetMinute(time, 3); //date昨天零点加三分钟 DateTime time1 = DateUtil.offsetMinute(time0, 3); PointInfo pi = new PointInfo(); pi.setUniformCode("AI121"); pi.setInstitutionType("turbine"); List entity = pointService.getByEntity(pi); Map turMap = entity.stream().collect(Collectors.toMap(PointInfo::getTurbineId, PointInfo::getPointKey)); String keys = pointInfos2Keys(entity); Map swdlL2 = adapter.getHistorySection(goldenUri(), keys, time2.getTime()); Map swdlL1 = adapter.getHistorySection(goldenUri(), keys, time1.getTime()); List byDate = getTurbineinfoByDate(time0.toJdkDate()); Map collect = new HashMap<>(); if (byDate.size() > 0) { collect = byDate.stream().collect(Collectors.toMap(TurbineInfoDay::getLineId, Function.identity())); } TurbineInfoDay infoDay; String turbineId, key; List infoDays = new ArrayList<>(); for (PointInfo info : entity) { turbineId = info.getTurbineId(); if (byDate.isEmpty()) { infoDay = new TurbineInfoDay(); infoDay.setStationId(info.getStationId()); infoDay.setProjectId(info.getProjectId()); infoDay.setLineId(info.getLineId()); infoDay.setTurbineId(turbineId); infoDay.setRecordDate(time0.toJdkDate()); } else { infoDay = collect.get(turbineId); } key = turMap.get(turbineId); double v = (swdlL2.get(key).getValue() - swdlL2.get(key).getValue()) * info.getCoef(); if (v < 0 || v > 1000000) v = 0; infoDay.setRfdl(v); infoDays.add(infoDay); } turbineInfoDayService.saveOrUpdateBatch(infoDays); } public List getTurbineinfoByDate(Date date) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("record_date", date); return turbineInfoDayService.list(wrapper); } }