CalculateTask.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package com.gyee.power.fitting.dispersionanalysis;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.date.DateField;
  4. import cn.hutool.core.date.DateRange;
  5. import cn.hutool.core.date.DateTime;
  6. import cn.hutool.core.date.DateUtil;
  7. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  8. import com.gyee.power.fitting.common.feign.RemoteServiceBuilder;
  9. import com.gyee.power.fitting.model.ProBasicEquipment;
  10. import com.gyee.power.fitting.model.ProBasicEquipmentPoint;
  11. import com.gyee.power.fitting.model.StateCause;
  12. import com.gyee.power.fitting.model.custom.TsDoubleData;
  13. import com.gyee.power.fitting.service.IStateCauseService;
  14. import com.gyee.power.fitting.service.ProBasicEquipmentPointService;
  15. import com.gyee.power.fitting.service.ProBasicEquipmentService;
  16. import lombok.extern.slf4j.Slf4j;
  17. import org.springframework.scheduling.annotation.Scheduled;
  18. import org.springframework.stereotype.Component;
  19. import javax.annotation.Resource;
  20. import java.util.*;
  21. import java.util.function.Function;
  22. import java.util.stream.Collectors;
  23. @Slf4j
  24. @Component
  25. public class CalculateTask {
  26. @Resource
  27. private IStateCauseService stateCauseService;
  28. @Resource
  29. private ProBasicEquipmentService proBasicEquipmentService;
  30. @Resource
  31. private ProBasicEquipmentPointService proBasicEquipmentPointService;
  32. @Resource
  33. private RemoteServiceBuilder remoteService;
  34. //秒 分 时 日 月 周
  35. @Scheduled(cron = "0 0 2 * * ?")
  36. public void doTask() {
  37. List<ProBasicEquipment> equipmentList = proBasicEquipmentService.getCacheList(null);
  38. Map<String, ProBasicEquipmentPoint> sbztMap = proBasicEquipmentPointService.getEquipmentMapByUniformcode("SBZT");
  39. Map<String, ProBasicEquipmentPoint> mxztMap = proBasicEquipmentPointService.getEquipmentMapByUniformcode("MXZT");
  40. //获取最新更新时间
  41. QueryWrapper<StateCause> scWrapper = new QueryWrapper<>();
  42. scWrapper.select("equipment_id,last_row(end_time) end_time");
  43. StateCause sc = stateCauseService.getOne(scWrapper);
  44. DateRange dateRange = getDateRange(sc);
  45. if (dateRange == null) return;
  46. List<StateCause> stateCauseList = new ArrayList<>();
  47. long starttime, endtime;
  48. for (DateTime time : dateRange) {
  49. starttime = time.getTime();
  50. endtime = DateUtil.endOfDay(time).getTime();
  51. String equipmentId, tbName;
  52. for (ProBasicEquipment equipment : equipmentList) {
  53. equipmentId = equipment.getId();
  54. tbName = equipmentId + "_SC_CI08";
  55. List<TsDoubleData> datas8 = getDatas(sbztMap.get(equipmentId).getNemCode(), starttime, endtime);
  56. if (CollUtil.isNotEmpty(datas8)) {
  57. long ts0 = endtime;
  58. long ts1;
  59. int doubleValue0 = (int) datas8.get(0).getDoubleValue();
  60. int doubleValue1 = 0;
  61. for (TsDoubleData data : datas8) {
  62. ts1 = data.getTs();
  63. doubleValue1 = (int) data.getDoubleValue();
  64. if (doubleValue1 == doubleValue0) continue;
  65. //0待机, 1运行, 2故障, 3检修, 4限电, 5受累, 6离线
  66. if (doubleValue0 == 1) {
  67. stateCauseList.add(new StateCause(tbName, 1, ts1, ts0, ts0 - ts1));
  68. } else if (doubleValue0 == 2) {
  69. stateCauseList.add(new StateCause(tbName, 2, ts1, ts0, ts0 - ts1));
  70. } else if (doubleValue0 == 3) {
  71. stateCauseList.add(new StateCause(tbName, 3, ts1, ts0, ts0 - ts1));
  72. } else if (doubleValue0 == 4) {
  73. stateCauseList.add(new StateCause(tbName, 4, ts1, ts0, ts0 - ts1));
  74. } else if (doubleValue0 == 5) {
  75. stateCauseList.add(new StateCause(tbName, 5, ts1, ts0, ts0 - ts1));
  76. } else if (doubleValue0 == 6) {
  77. stateCauseList.add(new StateCause(tbName, 6, ts1, ts0, ts0 - ts1));
  78. } else {
  79. stateCauseList.add(new StateCause(tbName, 0, ts1, ts0, ts0 - ts1));
  80. }
  81. ts0 = ts1;
  82. doubleValue0 = doubleValue1;
  83. }
  84. if (ts0 > starttime) {
  85. stateCauseList.add(new StateCause(tbName, doubleValue1, starttime, ts0, ts0 - starttime));
  86. }
  87. }else {
  88. System.out.println(equipmentId+":8种状态:"+sbztMap.get(equipmentId).getNemCode());
  89. }
  90. tbName = equipmentId + "_SC_CI14";
  91. List<TsDoubleData> datas14 = getDatas(mxztMap.get(equipmentId).getNemCode(), starttime, endtime);
  92. if (CollUtil.isNotEmpty(datas14)) {
  93. long ts0 = endtime;
  94. long ts1;
  95. int doubleValue0 = (int) datas14.get(0).getDoubleValue();
  96. int doubleValue1 = 0;
  97. for (TsDoubleData data : datas14) {
  98. ts1 = data.getTs();
  99. doubleValue1 = (int) data.getDoubleValue();
  100. if (doubleValue1 == doubleValue0) continue;
  101. //0待机, 1手动停机, 2正常发电, 3发电降出力, 4故障, 5故障受累, 6检修, 7检修受累, 8限电降出力, 9限电停机, 10电网受累, 11环境受累, 12通讯中断, 13设备离线
  102. if (doubleValue0 == 1) {
  103. stateCauseList.add(new StateCause(tbName, 1, ts1, ts0, ts0 - ts1));
  104. } else if (doubleValue0 == 2) {
  105. stateCauseList.add(new StateCause(tbName, 2, ts1, ts0, ts0 - ts1));
  106. } else if (doubleValue0 == 3) {
  107. stateCauseList.add(new StateCause(tbName, 3, ts1, ts0, ts0 - ts1));
  108. } else if (doubleValue0 == 4) {
  109. stateCauseList.add(new StateCause(tbName, 4, ts1, ts0, ts0 - ts1));
  110. } else if (doubleValue0 == 5) {
  111. stateCauseList.add(new StateCause(tbName, 5, ts1, ts0, ts0 - ts1));
  112. } else if (doubleValue0 == 6) {
  113. stateCauseList.add(new StateCause(tbName, 6, ts1, ts0, ts0 - ts1));
  114. } else if (doubleValue0 == 7) {
  115. stateCauseList.add(new StateCause(tbName, 7, ts1, ts0, ts0 - ts1));
  116. } else if (doubleValue0 == 8) {
  117. stateCauseList.add(new StateCause(tbName, 8, ts1, ts0, ts0 - ts1));
  118. } else if (doubleValue0 == 9) {
  119. stateCauseList.add(new StateCause(tbName, 9, ts1, ts0, ts0 - ts1));
  120. } else if (doubleValue0 == 10) {
  121. stateCauseList.add(new StateCause(tbName, 10, ts1, ts0, ts0 - ts1));
  122. } else if (doubleValue0 == 11) {
  123. stateCauseList.add(new StateCause(tbName, 11, ts1, ts0, ts0 - ts1));
  124. } else if (doubleValue0 == 12) {
  125. stateCauseList.add(new StateCause(tbName, 12, ts1, ts0, ts0 - ts1));
  126. } else if (doubleValue0 == 13) {
  127. stateCauseList.add(new StateCause(tbName, 13, ts1, ts0, ts0 - ts1));
  128. } else {
  129. stateCauseList.add(new StateCause(tbName, 0, ts1, ts0, ts0 - ts1));
  130. }
  131. ts0 = ts1;
  132. doubleValue0 = doubleValue1;
  133. }
  134. if (ts0 > starttime) {
  135. stateCauseList.add(new StateCause(tbName, doubleValue1, starttime, ts0, ts0 - starttime));
  136. }
  137. }else {
  138. System.out.println(equipmentId+":14种状态:"+sbztMap.get(equipmentId).getNemCode());
  139. }
  140. }
  141. }
  142. int i = stateCauseService.saveBatch(stateCauseList);
  143. System.out.println("计算完成" + i);
  144. }
  145. private DateRange getDateRange(StateCause sc) {
  146. DateTime startTime;
  147. DateTime endTime = DateUtil.beginOfDay(DateUtil.yesterday());
  148. if (sc == null) {
  149. startTime = endTime;
  150. } else {
  151. startTime = DateUtil.offsetDay(DateUtil.beginOfDay(DateUtil.date(sc.getEndTime())), 1);
  152. if (startTime.isAfter(endTime)) return null;
  153. }
  154. return DateUtil.range(startTime, endTime, DateField.DAY_OF_YEAR);
  155. }
  156. private List<TsDoubleData> getDatas(String nemCode, long starttime, long endtime) {
  157. List<TsDoubleData> rawValuesByKey;
  158. if (nemCode.startsWith("GF")) {
  159. rawValuesByKey = remoteService.adaptergf().getRawValuesByKey(nemCode, starttime, endtime);
  160. } else {
  161. rawValuesByKey = remoteService.adapterfd().getRawValuesByKey(nemCode, starttime, endtime);
  162. }
  163. rawValuesByKey.sort(Comparator.comparing(TsDoubleData::getTs).reversed());
  164. return rawValuesByKey;
  165. }
  166. //秒 分 时 日 月 周
  167. // @Scheduled(cron = "0 11 10 * * ?")
  168. public void creatStables() {
  169. List<ProBasicEquipment> equipmentList = proBasicEquipmentService.getCacheList(null);
  170. String equipmentId, tbName;
  171. List<StateCause> stateCauseList = new ArrayList<>();
  172. for (ProBasicEquipment equipment : equipmentList) {
  173. equipmentId = equipment.getId();
  174. tbName = equipmentId + "_SC_CI08";
  175. StateCause sc1 = new StateCause();
  176. sc1.setEquipmentId(equipmentId);
  177. sc1.setTbname(tbName);
  178. sc1.setStationId(equipment.getWindpowerstationId());
  179. sc1.setStateType("8种状态");
  180. stateCauseList.add(sc1);
  181. tbName = equipmentId + "_SC_CI14";
  182. StateCause sc2 = new StateCause();
  183. sc2.setEquipmentId(equipmentId);
  184. sc2.setTbname(tbName);
  185. sc2.setStationId(equipment.getWindpowerstationId());
  186. sc2.setStateType("14种状态");
  187. stateCauseList.add(sc2);
  188. }
  189. stateCauseService.creatTablesBatch(stateCauseList);
  190. }
  191. }