JavaFunctionJobHandler.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. package com.ruoyi;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.date.DateTime;
  4. import cn.hutool.core.date.DateUtil;
  5. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  6. import com.ruoyi.quartz.handler.IJobHandler;
  7. import com.ruoyi.quartz.task.ChangedSave;
  8. import com.ruoyi.ucp.entity.*;
  9. import com.ruoyi.ucp.feign.AdapterApi;
  10. import com.ruoyi.ucp.service.*;
  11. import com.ruoyi.ucp.util.CalcCache;
  12. import org.springframework.data.redis.core.RedisTemplate;
  13. import javax.annotation.Resource;
  14. import java.net.URI;
  15. import java.util.*;
  16. import java.util.function.Function;
  17. import java.util.stream.Collectors;
  18. public class JavaFunctionJobHandler extends IJobHandler {
  19. @Resource
  20. private AdapterApi adapter;
  21. @Resource
  22. private IPointInfoService pointService;
  23. @Resource
  24. private IStationInfoHourService stationInfoHourService;
  25. @Resource
  26. private IStationInfoDayService stationInfoDayService;
  27. @Resource
  28. private ILineInfoDayService lineInfoDayService;
  29. @Resource
  30. private IStateAiService stateAiService;
  31. @Resource
  32. private RedisTemplate redisTemplate;
  33. @Resource
  34. private IEquipmentModelService equipmentModelService;
  35. @Override
  36. public void execute() throws Exception {
  37. }
  38. @Override
  39. public IJobHandler getFunctionHandler() {
  40. return null;
  41. }
  42. @Override
  43. public void setFunctionHandler(IJobHandler jobHandler) {
  44. }
  45. public void calcAQTS(Date date) {
  46. QueryWrapper<StationInfoDay> wrapper = new QueryWrapper<>();
  47. wrapper.select("min(record_date) record_date");
  48. StationInfoDay one = stationInfoDayService.getOne(wrapper);
  49. wrapper = new QueryWrapper<>();
  50. wrapper.eq("record_date", one.getRecordDate());
  51. //最早时间的列表
  52. List<StationInfoDay> list = stationInfoDayService.list(wrapper);
  53. List<StationInfoDay> today = getStationinfoByDate(date);
  54. if (today.isEmpty()) {
  55. for (StationInfoDay infoDay : list) {
  56. StationInfoDay day = new StationInfoDay();
  57. day.setStationId(infoDay.getStationId());
  58. day.setRecordDate(date);
  59. day.setAqts(infoDay.getAqts() + (int) DateUtil.betweenDay(infoDay.getRecordDate(), date, true));
  60. today.add(day);
  61. }
  62. } else {
  63. Map<String, StationInfoDay> collect = list.stream().collect(Collectors.toMap(StationInfoDay::getStationId, Function.identity()));
  64. for (StationInfoDay day : today) {
  65. StationInfoDay day1 = collect.get(day.getStationId());
  66. day.setAqts(day1.getAqts() + (int) DateUtil.betweenDay(day1.getRecordDate(), date, true));
  67. }
  68. }
  69. stationInfoDayService.saveOrUpdateBatch(today);
  70. }
  71. public List<StationInfoDay> getStationinfoByDate(Date date) {
  72. QueryWrapper<StationInfoDay> wrapper = new QueryWrapper<>();
  73. wrapper.eq("record_date", date);
  74. return stationInfoDayService.list(wrapper);
  75. }
  76. public URI taosGoldenUriTest() {
  77. return URI.create("http://127.0.0.1:8011/ts");
  78. }
  79. public URI taosUri() {
  80. return URI.create("http://172.16.12.101:8012/ts");
  81. }
  82. public void calcStationHourRFDL(Date hour) {
  83. PointInfo pointInfo = new PointInfo();
  84. pointInfo.setInstitutionType("station");
  85. pointInfo.setUniformCode("RFDLSYZ");
  86. List<PointInfo> entity = pointService.getByEntity(pointInfo);
  87. Map<String, PointData> section = adapter.getHistorySection(taosGoldenUriTest()
  88. , pointInfos2Keys(entity), hour.getTime());
  89. List<StationInfoHour> byHours = getStationinfoByHour(hour);
  90. if (byHours.isEmpty()) {
  91. for (PointInfo point : entity) {
  92. StationInfoHour info = new StationInfoHour();
  93. info.setStationId(point.getStationId());
  94. info.setRecordDate(DateUtil.beginOfHour(hour));
  95. info.setRfdl(section.get(point.getPointKey()).getDoubleValue());
  96. byHours.add(info);
  97. }
  98. } else {
  99. Map<String, String> collect = entity.stream().collect(Collectors.toMap(PointInfo::getStationId, PointInfo::getPointKey));
  100. for (StationInfoHour byHour : byHours) {
  101. byHour.setRfdl(section.get(collect.get(byHour.getStationId())).getDoubleValue());
  102. }
  103. }
  104. stationInfoHourService.saveOrUpdateBatch(byHours);
  105. }
  106. public List<StationInfoHour> getStationinfoByHour(Date hour) {
  107. QueryWrapper<StationInfoHour> wrapper = new QueryWrapper<>();
  108. wrapper.eq("record_date", DateUtil.beginOfHour(hour));
  109. return stationInfoHourService.list(wrapper);
  110. }
  111. public String pointInfos2Keys(List<PointInfo> entity) {
  112. return entity.stream().map(PointInfo::getPointKey).collect(Collectors.joining(","));
  113. }
  114. public void calcStationHourPJGL(Date hour) {
  115. PointInfo pointInfo = new PointInfo();
  116. pointInfo.setInstitutionType("station");
  117. pointInfo.setUniformCode("SSZGL");
  118. List<PointInfo> entity = pointService.getByEntity(pointInfo);
  119. List<StationInfoHour> byHours = getStationinfoByHour(hour);
  120. Map<String, StationInfoHour> collect = new HashMap<>();
  121. if (byHours.size() > 0) {
  122. collect = byHours.stream().collect(
  123. Collectors.toMap(StationInfoHour::getStationId, Function.identity()));
  124. }
  125. List<DoubleStatData> stat;
  126. List<StationInfoHour> byHours2 = new ArrayList<>();
  127. for (PointInfo point : entity) {
  128. stat = adapter.getHistoryStat(taosGoldenUriTest(),
  129. point.getPointKey(), DateUtil.offsetHour(hour, -1).getTime(), hour.getTime());
  130. if (byHours.isEmpty()) {
  131. StationInfoHour info = new StationInfoHour();
  132. info.setStationId(point.getStationId());
  133. info.setRecordDate(DateUtil.beginOfHour(hour));
  134. if (stat.isEmpty()) info.setPjgl(0.0);
  135. info.setPjgl(stat.get(0).getAvg().getDoubleValue());
  136. byHours2.add(info);
  137. } else {
  138. collect.get(point.getStationId()).setPjgl(stat.get(0).getAvg().getDoubleValue());
  139. }
  140. }
  141. stationInfoHourService.saveOrUpdateBatch(byHours.isEmpty() ? byHours2 : byHours);
  142. }
  143. public void calcLineDjlRfdl(Date date) {
  144. //date当天零点
  145. DateTime time = DateUtil.beginOfDay(date);
  146. //date昨天零点
  147. DateTime time0 = DateUtil.offsetDay(time, -1);
  148. //date当天零点加三分钟
  149. DateTime time2 = DateUtil.offsetMinute(time, 3);
  150. //date昨天零点加三分钟
  151. DateTime time1 = DateUtil.offsetMinute(time0, 3);
  152. PointInfo pi = new PointInfo();
  153. pi.setUniformCode("Z-ZXYG-JX");
  154. pi.setLineId("all");
  155. List<PointInfo> entity = pointService.getByEntity(pi);
  156. String keys = pointInfos2Keys(entity);
  157. Map<String, PointData> latest2 = adapter.getHistorySection(taosGoldenUriTest(), keys, time2.getTime());
  158. Map<String, PointData> latest1 = adapter.getHistorySection(taosGoldenUriTest(), keys, time1.getTime());
  159. List<LineInfoDay> byDate = getLineinfoByDate(time0.toJdkDate());
  160. Map<String, LineInfoDay> collect = new HashMap<>();
  161. if (byDate.size() > 0) {
  162. collect = byDate.stream().collect(Collectors.toMap(LineInfoDay::getLineId, Function.identity()));
  163. }
  164. List<LineInfoDay> list = new ArrayList<>();
  165. for (PointInfo info : entity) {
  166. LineInfoDay day;
  167. if (byDate.isEmpty()) {
  168. day = new LineInfoDay();
  169. day.setStationId(info.getStationId());
  170. day.setProjectId(info.getProjectId());
  171. day.setLineId(info.getLineId());
  172. day.setRecordDate(time0.toJdkDate());
  173. } else {
  174. day = collect.get(info.getLineId());
  175. }
  176. day.setRfdl((int) ((latest2.get(info.getPointKey()).getDoubleValue() -
  177. latest1.get(info.getPointKey()).getDoubleValue()) * info.getCoef()));
  178. list.add(day);
  179. }
  180. lineInfoDayService.saveOrUpdateBatch(list);
  181. }
  182. public List<LineInfoDay> getLineinfoByDate(Date date) {
  183. QueryWrapper<LineInfoDay> wrapper = new QueryWrapper<>();
  184. wrapper.eq("record_date", date);
  185. return lineInfoDayService.list(wrapper);
  186. }
  187. public void calcStationSwGwCyDl(Date date) {
  188. //date当天零点
  189. DateTime time = DateUtil.beginOfDay(date);
  190. //date昨天零点
  191. DateTime time0 = DateUtil.offsetDay(time, -1);
  192. //date当天零点加三分钟
  193. DateTime time2 = DateUtil.offsetMinute(time, 3);
  194. //date昨天零点加三分钟
  195. DateTime time1 = DateUtil.offsetMinute(time0, 3);
  196. PointInfo swdlPi = new PointInfo();
  197. swdlPi.setUniformCode("Z-ZXYG-CX");
  198. PointInfo gwdlPi = new PointInfo();
  199. gwdlPi.setUniformCode("Z-FXYG-CX");
  200. PointInfo cydlPi = new PointInfo();
  201. cydlPi.setUniformCode("Z-ZXYG-ZYB");
  202. List<PointInfo> swdlEt = pointService.getByEntity(swdlPi);
  203. List<PointInfo> gwdlEt = pointService.getByEntity(gwdlPi);
  204. Map<String, PointInfo> gwdlMap = gwdlEt.stream().collect(Collectors.toMap(PointInfo::getStationId, Function.identity()));
  205. List<PointInfo> cydlEt = pointService.getByEntity(cydlPi);
  206. Map<String, PointInfo> cydlMap = cydlEt.stream().collect(Collectors.toMap(PointInfo::getStationId, Function.identity()));
  207. String swdlK = pointInfos2Keys(swdlEt);
  208. String gwdlK = pointInfos2Keys(gwdlEt);
  209. String cydlK = pointInfos2Keys(cydlEt);
  210. Map<String, PointData> swdlL2 = adapter.getHistorySection(taosGoldenUriTest(), swdlK, time2.getTime());
  211. Map<String, PointData> swdlL1 = adapter.getHistorySection(taosGoldenUriTest(), swdlK, time1.getTime());
  212. Map<String, PointData> gwdlL2 = adapter.getHistorySection(taosGoldenUriTest(), gwdlK, time2.getTime());
  213. Map<String, PointData> gwdlL1 = adapter.getHistorySection(taosGoldenUriTest(), gwdlK, time1.getTime());
  214. Map<String, PointData> cydlL2 = adapter.getHistorySection(taosGoldenUriTest(), cydlK, time2.getTime());
  215. Map<String, PointData> cydlL1 = adapter.getHistorySection(taosGoldenUriTest(), cydlK, time1.getTime());
  216. List<StationInfoDay> byDate = getStationinfoByDate(time0.toJdkDate());
  217. Map<String, StationInfoDay> collect = new HashMap<>();
  218. if (byDate.size() > 0) {
  219. collect = byDate.stream().collect(Collectors.toMap(StationInfoDay::getStationId, Function.identity()));
  220. }
  221. StationInfoDay infoDay;
  222. String stationId;
  223. PointInfo gwdlKey, cydlKey;
  224. List<StationInfoDay> infoDays = new ArrayList<>();
  225. for (PointInfo info : swdlEt) {
  226. stationId = info.getStationId();
  227. if (byDate.isEmpty()) {
  228. infoDay = new StationInfoDay();
  229. infoDay.setStationId(stationId);
  230. infoDay.setRecordDate(time0.toJdkDate());
  231. } else {
  232. infoDay = collect.get(stationId);
  233. }
  234. infoDay.setSwdl((int) ((swdlL2.get(info.getPointKey()).getDoubleValue() -
  235. swdlL1.get(info.getPointKey()).getDoubleValue()) * info.getCoef()));
  236. gwdlKey = gwdlMap.get(stationId);
  237. infoDay.setGwdl((int) ((gwdlL2.get(gwdlKey.getPointKey()).getDoubleValue() -
  238. gwdlL1.get(gwdlKey.getPointKey()).getDoubleValue()) * gwdlKey.getCoef()));
  239. cydlKey = cydlMap.get(stationId);
  240. infoDay.setCydl((int) ((cydlL2.get(cydlKey.getPointKey()).getDoubleValue() -
  241. cydlL1.get(cydlKey.getPointKey()).getDoubleValue()) * cydlKey.getCoef()));
  242. infoDays.add(infoDay);
  243. }
  244. stationInfoDayService.saveOrUpdateBatch(infoDays);
  245. }
  246. public void calcStationZhcydl(Date date) {
  247. //date当天零点
  248. DateTime time = DateUtil.beginOfDay(date);
  249. //date昨天零点
  250. DateTime time0 = DateUtil.offsetDay(time, -1);
  251. QueryWrapper<LineInfoDay> wrapper = new QueryWrapper<>();
  252. wrapper.select("sum(rfdl) rfdl,station_id").eq("record_date", time0.toJdkDate())
  253. .groupBy("station_id");
  254. List<LineInfoDay> list = lineInfoDayService.list(wrapper);
  255. List<StationInfoDay> byDate = getStationinfoByDate(time0.toJdkDate());
  256. Map<String, StationInfoDay> collect = new HashMap<>();
  257. if (byDate.size() > 0) {
  258. collect = byDate.stream().collect(Collectors.toMap(StationInfoDay::getStationId, Function.identity()));
  259. }
  260. StationInfoDay infoDay;
  261. List<StationInfoDay> infoDays = new ArrayList<>();
  262. for (LineInfoDay day : list) {
  263. if (byDate.isEmpty()) {
  264. infoDay = new StationInfoDay();
  265. infoDay.setStationId(day.getStationId());
  266. infoDay.setRecordDate(time0.toJdkDate());
  267. } else {
  268. infoDay = collect.get(day.getStationId());
  269. }
  270. infoDay.setRfdl(day.getRfdl());
  271. infoDay.setZhcydl(infoDay.getRfdl() + infoDay.getGwdl() - infoDay.getSwdl());
  272. infoDays.add(infoDay);
  273. }
  274. stationInfoDayService.saveOrUpdateBatch(infoDays);
  275. }
  276. public void calcTurbine5s(){
  277. }
  278. //此处加公式
  279. public void calcTurbineSsztAi(Date date) {
  280. PointInfo pi = new PointInfo();
  281. pi.setUniformCode("AI422");
  282. pi.setInstitutionType("state");
  283. List<PointInfo> entity = pointService.getByEntity(pi);
  284. Map<String, PointInfo> collect = entity.stream().collect(Collectors.toMap(PointInfo::getTurbineId, Function.identity()));
  285. String keys = pointInfos2Keys(entity);
  286. PointInfo pimx = new PointInfo();
  287. pimx.setUniformCode("MXZT");
  288. pimx.setInstitutionType("turbine");
  289. List<PointInfo> entityMx = pointService.getByEntity(pimx);
  290. Map<String, PointInfo> collectMX = entityMx.stream().collect(Collectors.toMap(PointInfo::getTurbineId, Function.identity()));
  291. PointInfo pisb = new PointInfo();
  292. pisb.setUniformCode("SBZT");
  293. pisb.setInstitutionType("turbine");
  294. List<PointInfo> entitySB = pointService.getByEntity(pimx);
  295. Map<String, PointInfo> collectSB = entitySB.stream().collect(Collectors.toMap(PointInfo::getTurbineId, Function.identity()));
  296. Map<String, PointData> latest = adapter.getLatest(taosGoldenUriTest(), keys);
  297. List<StateAi> list = stateAiService.list();
  298. Map<String, Map<Integer, Integer>> collectAi = list.stream().collect(Collectors.groupingBy(StateAi::getModel, Collectors.toMap(StateAi::getOriginalState, StateAi::getMappingState)));
  299. List<PointData> pointDatas = new ArrayList<>();
  300. int i1;
  301. for (PointInfo info : entity) {
  302. PointData data = latest.get(info.getPointKey());
  303. data.setTagName(collectMX.get(info.getTurbineId()).getPointKey());
  304. i1 = (int) data.getDoubleValue();
  305. //缓存值
  306. Integer i2 = ChangedSave.map.get(info.getPointKey());
  307. if (i2 != null && i1 == i2) continue;
  308. ChangedSave.map.put(info.getPointKey(), i1);
  309. Integer i = collectAi.get(info.getSpare()).get(i1);
  310. data.setDoubleValue(i);
  311. pointDatas.add(data);
  312. }
  313. adapter.writeHistoryBatch(taosGoldenUriTest(), pointDatas);
  314. //redisTemplate.opsForHash().put("state_point",key,value);
  315. }
  316. public void calcStationXd(boolean priorRation){
  317. //限电状态
  318. PointInfo piAgc = new PointInfo();
  319. //AGC
  320. piAgc.setUniformCode("AGC002");
  321. piAgc.setInstitutionType("booster");
  322. List<PointInfo> entityAgc = pointService.getByEntity(piAgc);
  323. Map<String, PointInfo> collectAgc = entityAgc.stream().collect(Collectors.toMap(PointInfo::getTurbineId, Function.identity()));
  324. String keysAgc = pointInfos2Keys(entityAgc);
  325. //出线
  326. PointInfo piCx = new PointInfo();
  327. piCx.setUniformCode("AGC001");
  328. piCx.setInstitutionType("booster");
  329. List<PointInfo> entityCx = pointService.getByEntity(piCx);
  330. Map<String, PointInfo> collectCx = entityCx.stream().collect(Collectors.toMap(PointInfo::getTurbineId, Function.identity()));
  331. String keysCx = pointInfos2Keys(entityCx);
  332. Map<String, PointData> statAgc = adapter.getLatest(goldenUri(), keysAgc);
  333. Map<String, PointData> statCx = adapter.getLatest(goldenUri(), keysCx);
  334. DateTime time5 = DateUtil.offsetMinute(DateUtil.date(), -5);
  335. List<DoubleStatData> statAgc5 = adapter.getHistoryStat(goldenUri(), keysAgc, time5.getTime(), DateUtil.date().getTime());
  336. List<DoubleStatData> statCx5 = adapter.getHistoryStat(goldenUri(), keysCx, time5.getTime(), DateUtil.date().getTime());
  337. //AI066
  338. for (PointInfo info : entityAgc) {
  339. //如果场站限电状态为0
  340. /*if (priorRation) {
  341. if (zsgl5 >= sub.getCapacity() * 0.4) {
  342. if (zsgl / agc > 1.15) {
  343. b = true;
  344. }
  345. if (agc5 <= cxgl5) {
  346. b = true;
  347. }
  348. if (zsgl > agc && agc5 - cxgl5 < 4) {
  349. b = true;
  350. }
  351. }
  352. } else {
  353. if (zsgl5 >= sub.getCapacity() * 0.4) {
  354. if (zsgl / agc >= 1.2 && agc - cxgl <= 2) {
  355. b = true;
  356. }
  357. if (agc5 <= cxgl5) {
  358. b = true;
  359. }
  360. } else {
  361. if (k) {
  362. if (agc5 <= cxgl5) {
  363. b = true;
  364. }
  365. if (zsgl / agc >= 1.1 && agc5 - cxgl5 <= 2) {
  366. b = true;
  367. }
  368. }
  369. }
  370. }*/
  371. }
  372. }
  373. public void calcNhglZs(boolean priorRation){
  374. //拟合功率(自算)
  375. //实时风速
  376. PointInfo piFs = new PointInfo();
  377. piFs.setUniformCode("AI066");
  378. piFs.setInstitutionType("turbine");
  379. List<PointInfo> entityFs = pointService.getByEntity(piFs);
  380. Map<String, PointInfo> collectFs = entityFs.stream().collect(Collectors.toMap(PointInfo::getTurbineId, Function.identity()));
  381. Map<String, List<PointInfo>> collect = entityFs.stream().collect(Collectors.groupingBy(PointInfo::getStationId));
  382. String keysFs = pointInfos2Keys(entityFs);
  383. //设备拟合功率(自算)
  384. PointInfo piFjzsgl = new PointInfo();
  385. piFjzsgl.setUniformCode("ZSGL");
  386. piFjzsgl.setInstitutionType("turbine");
  387. List<PointInfo> entityFjzsgl = pointService.getByEntity(piFjzsgl);
  388. Map<String, String> collectFjzsgl = entityFjzsgl.stream().collect(Collectors.toMap(PointInfo::getTurbineId, PointInfo::getPointKey));
  389. String keysFjzsgl = pointInfos2Keys(entityFjzsgl);
  390. //场站自算功率
  391. PointInfo piCzzsgl = new PointInfo();
  392. piCzzsgl.setUniformCode("AGC010");
  393. piCzzsgl.setInstitutionType("booster");
  394. List<PointInfo> entityCzzsgl = pointService.getByEntity(piCzzsgl);
  395. Map<String, String> collectCzzsgl = entityCzzsgl.stream().collect(Collectors.toMap(PointInfo::getStationId, PointInfo::getPointKey));
  396. String keysCzzsgl = pointInfos2Keys(entityCzzsgl);
  397. DateTime time = DateUtil.beginOfMinute(DateUtil.date());
  398. DateTime time5 = DateUtil.offsetMinute(time, -5);
  399. List<DoubleStatData> statFs5;
  400. PointData avg;
  401. List<PointData> avgs = new ArrayList<>();
  402. double value,v;
  403. for (Map.Entry<String, List<PointInfo>> entry : collect.entrySet()) {
  404. double stV=0;
  405. for (PointInfo info : entry.getValue()) {
  406. //todo
  407. statFs5 = adapter.getHistoryStat(goldenUri(), info.getPointKey(), time5.getTime(), time.getTime());
  408. if(CollUtil.isNotEmpty(statFs5)){
  409. //todo
  410. value = statFs5.get(0).getAvg().getValue();
  411. avg = new PointData();
  412. v = CalcCache.wtMcfMap.get(info.getTurbineId()).get(value);
  413. stV += v;
  414. avg.setTs(time.getTime());
  415. avg.setDoubleValue(v);
  416. avg.setTagName(collectFjzsgl.get(info.getTurbineId()));
  417. avgs.add(avg);
  418. }
  419. }
  420. adapter.writeHistoryBatch(taosUri(), avgs);
  421. PointData data = new PointData();
  422. data.setTs(time.getTime());
  423. data.setDoubleValue(stV);
  424. data.setTagName(collectCzzsgl.get(entry.getKey()));
  425. adapter.writeHistory(taosUri(), data);
  426. }
  427. }
  428. public URI goldenUri(){
  429. return URI.create("http://172.16.12.103:8011/ts");
  430. }
  431. }