JavaFunctionJobHandler.java 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. package com.ruoyi;
  2. import cn.hutool.core.date.DateTime;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.thread.ThreadUtil;
  5. import cn.hutool.core.util.NumberUtil;
  6. import com.alibaba.fastjson2.JSON;
  7. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  8. import com.ruoyi.quartz.handler.IJobHandler;
  9. import com.ruoyi.ucp.entity.*;
  10. import com.ruoyi.ucp.feign.AdapterApi;
  11. import com.ruoyi.ucp.service.*;
  12. import com.ruoyi.ucp.util.CalcCache;
  13. import org.apache.commons.math3.fitting.PolynomialCurveFitter;
  14. import org.apache.commons.math3.fitting.WeightedObservedPoints;
  15. import org.springframework.data.redis.core.StringRedisTemplate;
  16. import org.springframework.stereotype.Service;
  17. import javax.annotation.Resource;
  18. import java.net.URI;
  19. import java.util.*;
  20. import java.util.function.Function;
  21. import java.util.stream.Collectors;
  22. @Service
  23. public class JavaFunctionJobHandler extends IJobHandler {
  24. @Resource
  25. private AdapterApi adapter;
  26. @Resource
  27. private IPointInfoService pointService;
  28. @Resource
  29. private IStationInfoHourService stationInfoHourService;
  30. @Resource
  31. private IStationInfoDayService stationInfoDayService;
  32. @Resource
  33. private ILineInfoDayService lineInfoDayService;
  34. @Resource
  35. private IStateAiService stateAiService;
  36. @Resource
  37. private StringRedisTemplate stringRedisTemplate;
  38. @Resource
  39. private IEquipmentModelService equipmentModelService;
  40. @Resource
  41. private ITurbineInfoDayService turbineInfoDayService;
  42. @Resource
  43. private IProEconReportIndicatorPoolService reportIndicatorPoolService;
  44. @Override
  45. public void execute() throws Exception {
  46. }
  47. @Override
  48. public IJobHandler getFunctionHandler() {
  49. return null;
  50. }
  51. @Override
  52. public void setFunctionHandler(IJobHandler jobHandler) {
  53. }
  54. public URI taosGoldenUriTest() {
  55. return URI.create("http://127.0.0.1:8011/ts");
  56. }
  57. public String pointInfos2Keys(List<PointInfo> entity) {
  58. return entity.stream().map(PointInfo::getPointKey).collect(Collectors.joining(","));
  59. }
  60. public List<StationInfoHour> getStationinfoByHour(Date hour) {
  61. QueryWrapper<StationInfoHour> wrapper = new QueryWrapper<>();
  62. wrapper.eq("record_date", DateUtil.beginOfHour(hour));
  63. return stationInfoHourService.list(wrapper);
  64. }
  65. public List<StationInfoDay> getStationinfoByDate(Date date) {
  66. QueryWrapper<StationInfoDay> wrapper = new QueryWrapper<>();
  67. wrapper.eq("record_date", date);
  68. return stationInfoDayService.list(wrapper);
  69. }
  70. public List<LineInfoDay> getLineinfoByDate(Date date) {
  71. QueryWrapper<LineInfoDay> wrapper = new QueryWrapper<>();
  72. wrapper.eq("record_date", date);
  73. return lineInfoDayService.list(wrapper);
  74. }
  75. public URI taosUri() {
  76. return URI.create("http://172.16.12.101:8012/ts");
  77. }
  78. public URI goldenUri() {
  79. return URI.create("http://172.16.12.103:8011/ts");
  80. }
  81. public Map<String, String> getEntityMap(String uniformCode, String institutionType) {
  82. PointInfo pi = new PointInfo();
  83. pi.setUniformCode(uniformCode);
  84. pi.setInstitutionType(institutionType);
  85. List<PointInfo> entity = pointService.getByEntity(pi);
  86. switch (institutionType) {
  87. case "turbine":
  88. case "state":
  89. return entity.stream().collect(Collectors.toMap(PointInfo::getTurbineId, PointInfo::getPointKey));
  90. case "station":
  91. return entity.stream().collect(Collectors.toMap(PointInfo::getStationId, PointInfo::getPointKey));
  92. }
  93. return new HashMap<>();
  94. }
  95. public Map<String, TurbineInfoDay> getTurbineinfoMap(Date date, List<PointInfo> entity) {
  96. QueryWrapper<TurbineInfoDay> wrapper = new QueryWrapper<>();
  97. wrapper.eq("record_date", date);
  98. List<TurbineInfoDay> list = turbineInfoDayService.list(wrapper);
  99. if (list.isEmpty()) {
  100. entity.forEach(pi -> {
  101. TurbineInfoDay day = new TurbineInfoDay();
  102. day.setStationId(pi.getStationId());
  103. day.setProjectId(pi.getProjectId());
  104. day.setLineId(pi.getLineId());
  105. day.setTurbineId(pi.getTurbineId());
  106. day.setRecordDate(date);
  107. list.add(day);
  108. });
  109. }
  110. return list.stream().collect(Collectors.toMap(TurbineInfoDay::getTurbineId, Function.identity()));
  111. }
  112. public List<PointInfo> getEntity(String uniformCode, String institutionType) {
  113. PointInfo pi = new PointInfo();
  114. pi.setUniformCode(uniformCode);
  115. pi.setInstitutionType(institutionType);
  116. return pointService.getByEntity(pi);
  117. }
  118. public Map<String, Double> getTurbinePowerLoss(double rfdl, PointInfo piZt, List<PointData> fsList, List<PointData> glList) {
  119. String tbId = piZt.getTurbineId();
  120. // Map<Double, Long> collect = piZt.getPointDatas().stream().collect(Collectors.groupingBy(PointData::getValue, Collectors.counting()));
  121. double sum1 = glList.stream().mapToDouble(PointData::getValue).sum();
  122. double xs = rfdl / (sum1 / 240);
  123. // double sum2 = fsList.stream().mapToDouble(pd -> {
  124. // Double v = CalcCache.fitcoef.get(tbId).get(pd.getValue());
  125. // if (v == null) v = 0.0;
  126. // return v;
  127. // }).sum();
  128. // sum2=sum2/240;
  129. List<PointData> collectZt = piZt.getPointDatas();
  130. double jxss = 0, sdtj = 0, gzss = 0, djss = 0, xnss = 0, fdjcl = 0, xdjcl = 0, xdtj = 0;
  131. Map<String, Double> map = new HashMap<>();
  132. if (collectZt.size() == fsList.size() && fsList.size() == glList.size()) {
  133. for (int i = 0; i < collectZt.size(); i++) {
  134. Double v = CalcCache.fitcoef.get(tbId).get(fsList.get(i).getValue());
  135. if (v == null) v = 0.0;
  136. switch ((int) collectZt.get(i).getValue()) {
  137. //计划检修损失:
  138. case 6:
  139. //检修
  140. jxss += v;
  141. break;
  142. case 1:
  143. //手动停机
  144. sdtj += v;
  145. break;
  146. //非计划检修损失:
  147. case 4:
  148. //故障
  149. gzss += v;
  150. break;
  151. case 0:
  152. //待机
  153. djss += v;
  154. break;
  155. //性能损失:
  156. case 2:
  157. //性能
  158. xnss += v - glList.get(i).getValue();
  159. break;
  160. case 3:
  161. //发电降出力
  162. fdjcl += v - glList.get(i).getValue();
  163. break;
  164. //限电损失:
  165. case 8:
  166. //限电降出力
  167. xdjcl += v - glList.get(i).getValue();
  168. break;
  169. case 9:
  170. //限电停机
  171. xdtj += v;
  172. break;
  173. }
  174. }
  175. }
  176. //15秒*60分钟
  177. map.put("jxss", jxss > 0 ? jxss / 240 * xs : 0);
  178. map.put("sdtj", sdtj > 0 ? sdtj / 240 * xs : 0);
  179. map.put("gzss", gzss > 0 ? gzss / 240 * xs : 0);
  180. map.put("djss", djss > 0 ? djss / 240 * xs : 0);
  181. map.put("xnss", xnss > 0 ? xnss / 240 * xs : 0);
  182. map.put("fdjcl", fdjcl > 0 ? fdjcl / 240 * xs : 0);
  183. map.put("xdjcl", xdjcl > 0 ? xdjcl / 240 * xs : 0);
  184. map.put("xdtj", xdtj > 0 ? xdtj / 240 * xs : 0);
  185. return map;
  186. }
  187. public void calcAQTS(Date date) {
  188. QueryWrapper<StationInfoDay> wrapper = new QueryWrapper<>();
  189. wrapper.select("min(record_date) record_date");
  190. StationInfoDay one = stationInfoDayService.getOne(wrapper);
  191. wrapper = new QueryWrapper<>();
  192. wrapper.eq("record_date", one.getRecordDate());
  193. //最早时间的列表
  194. List<StationInfoDay> list = stationInfoDayService.list(wrapper);
  195. List<StationInfoDay> today = getStationinfoByDate(date);
  196. if (today.isEmpty()) {
  197. for (StationInfoDay infoDay : list) {
  198. StationInfoDay day = new StationInfoDay();
  199. day.setStationId(infoDay.getStationId());
  200. day.setRecordDate(date);
  201. day.setAqts(infoDay.getAqts() + (int) DateUtil.betweenDay(infoDay.getRecordDate(), date, true));
  202. today.add(day);
  203. }
  204. } else {
  205. Map<String, StationInfoDay> collect = list.stream().collect(Collectors.toMap(StationInfoDay::getStationId, Function.identity()));
  206. for (StationInfoDay day : today) {
  207. StationInfoDay day1 = collect.get(day.getStationId());
  208. day.setAqts(day1.getAqts() + (int) DateUtil.betweenDay(day1.getRecordDate(), date, true));
  209. }
  210. }
  211. stationInfoDayService.saveOrUpdateBatch(today);
  212. }
  213. public void calcStationSwGwCyDl(Date date) {
  214. //date当天零点
  215. DateTime time = DateUtil.beginOfDay(date);
  216. //date昨天零点
  217. DateTime time0 = DateUtil.offsetDay(time, -1);
  218. //date当天零点加三分钟
  219. DateTime time2 = DateUtil.offsetMinute(time, 3);
  220. //date昨天零点加三分钟
  221. DateTime time1 = DateUtil.offsetMinute(time0, 3);
  222. PointInfo swdlPi = new PointInfo();
  223. swdlPi.setUniformCode("Z-ZXYG-CX");
  224. PointInfo gwdlPi = new PointInfo();
  225. gwdlPi.setUniformCode("Z-FXYG-CX");
  226. PointInfo cydlPi = new PointInfo();
  227. cydlPi.setUniformCode("Z-ZXYG-ZYB");
  228. List<PointInfo> swdlEt = pointService.getByEntity(swdlPi);
  229. List<PointInfo> gwdlEt = pointService.getByEntity(gwdlPi);
  230. Map<String, PointInfo> gwdlMap = gwdlEt.stream().collect(Collectors.toMap(PointInfo::getStationId, Function.identity()));
  231. List<PointInfo> cydlEt = pointService.getByEntity(cydlPi);
  232. Map<String, PointInfo> cydlMap = cydlEt.stream().collect(Collectors.toMap(PointInfo::getStationId, Function.identity()));
  233. String swdlK = pointInfos2Keys(swdlEt);
  234. String gwdlK = pointInfos2Keys(gwdlEt);
  235. String cydlK = pointInfos2Keys(cydlEt);
  236. Map<String, PointData> swdlL2 = adapter.getHistorySection(goldenUri(), swdlK, time2.getTime());
  237. Map<String, PointData> swdlL1 = adapter.getHistorySection(goldenUri(), swdlK, time1.getTime());
  238. Map<String, PointData> gwdlL2 = adapter.getHistorySection(goldenUri(), gwdlK, time2.getTime());
  239. Map<String, PointData> gwdlL1 = adapter.getHistorySection(goldenUri(), gwdlK, time1.getTime());
  240. Map<String, PointData> cydlL2 = adapter.getHistorySection(goldenUri(), cydlK, time2.getTime());
  241. Map<String, PointData> cydlL1 = adapter.getHistorySection(goldenUri(), cydlK, time1.getTime());
  242. List<StationInfoDay> byDate = getStationinfoByDate(time0.toJdkDate());
  243. Map<String, StationInfoDay> collect = new HashMap<>();
  244. if (!byDate.isEmpty()) {
  245. collect = byDate.stream().collect(Collectors.toMap(StationInfoDay::getStationId, Function.identity()));
  246. }
  247. StationInfoDay infoDay;
  248. String stationId;
  249. PointInfo gwdlKey, cydlKey;
  250. List<StationInfoDay> infoDays = new ArrayList<>();
  251. for (PointInfo info : swdlEt) {
  252. stationId = info.getStationId();
  253. if (byDate.isEmpty()) {
  254. infoDay = new StationInfoDay();
  255. infoDay.setStationId(stationId);
  256. infoDay.setRecordDate(time0.toJdkDate());
  257. } else {
  258. infoDay = collect.get(stationId);
  259. }
  260. infoDay.setSwdl((int) ((swdlL2.get(info.getPointKey()).getValue() -
  261. swdlL1.get(info.getPointKey()).getValue()) * info.getCoef()));
  262. gwdlKey = gwdlMap.get(stationId);
  263. infoDay.setGwdl((int) ((gwdlL2.get(gwdlKey.getPointKey()).getValue() -
  264. gwdlL1.get(gwdlKey.getPointKey()).getValue()) * gwdlKey.getCoef()));
  265. cydlKey = cydlMap.get(stationId);
  266. infoDay.setCydl((int) ((cydlL2.get(cydlKey.getPointKey()).getValue() -
  267. cydlL1.get(cydlKey.getPointKey()).getValue()) * cydlKey.getCoef()));
  268. infoDays.add(infoDay);
  269. }
  270. stationInfoDayService.saveOrUpdateBatch(infoDays);
  271. }
  272. public void calcStationHourRFDL(Date hour) {
  273. PointInfo pointInfo = new PointInfo();
  274. pointInfo.setInstitutionType("station");
  275. pointInfo.setUniformCode("RFDLSYZ");
  276. List<PointInfo> entity = pointService.getByEntity(pointInfo);
  277. Map<String, PointData> section = adapter.getHistorySection(taosUri(), pointInfos2Keys(entity), hour.getTime());
  278. List<StationInfoHour> byHours = getStationinfoByHour(hour);
  279. if (byHours.isEmpty()) {
  280. for (PointInfo point : entity) {
  281. StationInfoHour info = new StationInfoHour();
  282. info.setStationId(point.getStationId());
  283. info.setRecordDate(DateUtil.beginOfHour(hour));
  284. info.setRfdl(section.get(point.getPointKey()).getValue());
  285. byHours.add(info);
  286. }
  287. } else {
  288. Map<String, String> collect = entity.stream().collect(Collectors.toMap(PointInfo::getStationId, PointInfo::getPointKey));
  289. for (StationInfoHour byHour : byHours) {
  290. byHour.setRfdl(section.get(collect.get(byHour.getStationId())).getValue());
  291. }
  292. }
  293. stationInfoHourService.saveOrUpdateBatch(byHours);
  294. }
  295. public void writeReportPool(Date date) {
  296. //date当天零点
  297. DateTime time = DateUtil.beginOfDay(date);
  298. //date昨天零点
  299. DateTime time0 = DateUtil.offsetDay(time, -1);
  300. DateTime beginOfMonth = DateUtil.beginOfMonth(time0);
  301. DateTime beginOfYear = DateUtil.beginOfYear(time0);
  302. QueryWrapper<StationInfoDay> wrapper = new QueryWrapper<>();
  303. wrapper.eq("record_date", time0);
  304. List<StationInfoDay> days = stationInfoDayService.list(wrapper);
  305. Map<String, StationInfoDay> dayMap = days.stream().collect(Collectors.toMap(StationInfoDay::getStationId, Function.identity()));
  306. wrapper = new QueryWrapper<>();
  307. wrapper.select("sum(rfdl) rfdl,sum(swdl) swdl,sum(gwdl) gwdl,sum(cydl) cydl,sum(zhcydl) zhcydl,station_id")
  308. .between("record_date", beginOfMonth.toJdkDate(), time0.toJdkDate())
  309. .groupBy("station_id");
  310. List<StationInfoDay> monthDays = stationInfoDayService.list(wrapper);
  311. Map<String, StationInfoDay> monthMap = monthDays.stream().collect(Collectors.toMap(StationInfoDay::getStationId, Function.identity()));
  312. wrapper = new QueryWrapper<>();
  313. wrapper.select("sum(rfdl) rfdl,sum(swdl) swdl,sum(gwdl) gwdl,sum(cydl) cydl,sum(zhcydl) zhcydl,station_id")
  314. .between("record_date", beginOfYear.toJdkDate(), time0.toJdkDate())
  315. .groupBy("station_id");
  316. List<StationInfoDay> yearDays = stationInfoDayService.list(wrapper);
  317. Map<String, StationInfoDay> yearMap = yearDays.stream().collect(Collectors.toMap(StationInfoDay::getStationId, Function.identity()));
  318. QueryWrapper<ProEconReportIndicatorPool> wrapperrp = new QueryWrapper<>();
  319. wrapperrp.eq("record_date", time0).last("and foreign_key_id = windpowerstation_id");
  320. List<ProEconReportIndicatorPool> pools = reportIndicatorPoolService.list(wrapperrp);
  321. // pools = pools.stream().filter(p -> p.getWindpowerstationId().equals("GJNY_SXGS_ZZ_FDC_STA")).collect(Collectors.toList());
  322. StationInfoDay day, month, year;
  323. List<ProEconReportIndicatorPool> poolList = new ArrayList<>();
  324. for (ProEconReportIndicatorPool pool : pools) {
  325. String stationId = pool.getWindpowerstationId();
  326. day = dayMap.get(stationId);
  327. pool.setRfdldb(day.getRfdl().doubleValue());
  328. pool.setRswdldb(day.getSwdl().doubleValue());
  329. pool.setRgwgwdldb(day.getGwdl().doubleValue());
  330. pool.setRfdcydldb(day.getCydl().doubleValue());
  331. pool.setRzhcydldb(day.getZhcydl().doubleValue());
  332. month = monthMap.get(stationId);
  333. pool.setYfdldb(month.getRfdl().doubleValue());
  334. pool.setYswdldb(month.getSwdl().doubleValue());
  335. pool.setYgwgwdldb(month.getGwdl().doubleValue());
  336. pool.setYfdcydldb(month.getCydl().doubleValue());
  337. pool.setYzhcydldb(month.getZhcydl().doubleValue());
  338. year = yearMap.get(stationId);
  339. pool.setNfdldb(year.getRfdl().doubleValue());
  340. pool.setNswdldb(year.getSwdl().doubleValue());
  341. pool.setNgwgwdldb(year.getGwdl().doubleValue());
  342. pool.setNfdcydldb(year.getCydl().doubleValue());
  343. pool.setNzhcydldb(year.getZhcydl().doubleValue());
  344. poolList.add(pool);
  345. }
  346. reportIndicatorPoolService.saveOrUpdateBatch(poolList);
  347. }
  348. public void calcLineDjlRfdl(Date date) {
  349. //date当天零点
  350. DateTime time = DateUtil.beginOfDay(date);
  351. //date昨天零点
  352. DateTime time0 = DateUtil.offsetDay(time, -1);
  353. //date当天零点加三分钟
  354. DateTime time2 = DateUtil.offsetMinute(time, 3);
  355. //date昨天零点加三分钟
  356. DateTime time1 = DateUtil.offsetMinute(time0, 3);
  357. PointInfo pi = new PointInfo();
  358. pi.setUniformCode("Z-ZXYG-JX");
  359. pi.setLineId("all");
  360. List<PointInfo> entity = pointService.getByEntity(pi);
  361. // entity = entity.stream().filter(e -> e.getStationId().equals("GJNY_SXGS_ZZ_FDC_STA")).collect(Collectors.toList());
  362. String keys = pointInfos2Keys(entity);
  363. Map<String, PointData> latest2 = adapter.getHistorySection(goldenUri(), keys, time2.getTime());
  364. Map<String, PointData> latest1 = adapter.getHistorySection(goldenUri(), keys, time1.getTime());
  365. List<LineInfoDay> byDate = getLineinfoByDate(time0.toJdkDate());
  366. Map<String, LineInfoDay> collect = new HashMap<>();
  367. if (!byDate.isEmpty()) {
  368. collect = byDate.stream().collect(Collectors.toMap(LineInfoDay::getLineId, Function.identity()));
  369. }
  370. List<LineInfoDay> list = new ArrayList<>();
  371. for (PointInfo info : entity) {
  372. LineInfoDay day;
  373. if (byDate.isEmpty()) {
  374. day = new LineInfoDay();
  375. day.setStationId(info.getStationId());
  376. day.setProjectId(info.getProjectId());
  377. day.setLineId(info.getLineId());
  378. day.setRecordDate(time0.toJdkDate());
  379. } else {
  380. day = collect.get(info.getLineId());
  381. }
  382. day.setRfdl((int) ((latest2.get(info.getPointKey()).getValue() -
  383. latest1.get(info.getPointKey()).getValue()) * info.getCoef()));
  384. if (day.getRfdl() <= 0 || day.getRfdl() > 30000000) {
  385. day.setRfdl(0);
  386. }
  387. list.add(day);
  388. }
  389. lineInfoDayService.saveOrUpdateBatch(list);
  390. }
  391. public void calcStationZhcydl(Date date) {
  392. //date当天零点
  393. DateTime time = DateUtil.beginOfDay(date);
  394. //date昨天零点
  395. DateTime time0 = DateUtil.offsetDay(time, -1);
  396. QueryWrapper<LineInfoDay> wrapper = new QueryWrapper<>();
  397. wrapper.select("sum(rfdl) rfdl,station_id").eq("record_date", time0.toJdkDate())
  398. .groupBy("station_id");
  399. List<LineInfoDay> list = lineInfoDayService.list(wrapper);
  400. List<StationInfoDay> byDate = getStationinfoByDate(time0.toJdkDate());
  401. // list = list.stream().filter(b -> b.getStationId().equals("GJNY_SXGS_ZZ_FDC_STA")).collect(Collectors.toList());
  402. Map<String, StationInfoDay> collect = new HashMap<>();
  403. if (!byDate.isEmpty()) {
  404. collect = byDate.stream().collect(Collectors.toMap(StationInfoDay::getStationId, Function.identity()));
  405. }
  406. StationInfoDay infoDay;
  407. List<StationInfoDay> infoDays = new ArrayList<>();
  408. for (LineInfoDay day : list) {
  409. if (byDate.isEmpty()) {
  410. infoDay = new StationInfoDay();
  411. infoDay.setStationId(day.getStationId());
  412. infoDay.setRecordDate(time0.toJdkDate());
  413. } else {
  414. infoDay = collect.get(day.getStationId());
  415. }
  416. infoDay.setRfdl(day.getRfdl());
  417. infoDay.setZhcydl(infoDay.getRfdl() + infoDay.getGwdl() - infoDay.getSwdl());
  418. infoDays.add(infoDay);
  419. }
  420. stationInfoDayService.saveOrUpdateBatch(infoDays);
  421. }
  422. public void calcStationHourPJGL(Date hour) {
  423. PointInfo pointInfo = new PointInfo();
  424. pointInfo.setInstitutionType("station");
  425. pointInfo.setUniformCode("SSZGL");
  426. List<PointInfo> entity = pointService.getByEntity(pointInfo);
  427. List<StationInfoHour> byHours = getStationinfoByHour(hour);
  428. Map<String, StationInfoHour> collect = new HashMap<>();
  429. if (!byHours.isEmpty()) {
  430. collect = byHours.stream().collect(
  431. Collectors.toMap(StationInfoHour::getStationId, Function.identity()));
  432. }
  433. DoubleStatData stat;
  434. List<StationInfoHour> byHours2 = new ArrayList<>();
  435. for (PointInfo point : entity) {
  436. stat = adapter.getHistoryStat(taosGoldenUriTest(),
  437. point.getPointKey(), DateUtil.offsetHour(hour, -1).getTime(), hour.getTime());
  438. if (byHours.isEmpty()) {
  439. StationInfoHour info = new StationInfoHour();
  440. info.setStationId(point.getStationId());
  441. info.setRecordDate(DateUtil.beginOfHour(hour));
  442. if (stat == null || stat.getAvg() == null) info.setPjgl(0.0);
  443. info.setPjgl(stat.getAvg().getDoubleValue());
  444. byHours2.add(info);
  445. } else {
  446. collect.get(point.getStationId()).setPjgl(stat.getAvg().getDoubleValue());
  447. }
  448. }
  449. stationInfoHourService.saveOrUpdateBatch(byHours.isEmpty() ? byHours2 : byHours);
  450. }
  451. public void calcStationRfdlFj(Date date) {
  452. //date当天零点
  453. DateTime time = DateUtil.beginOfDay(date);
  454. //date昨天零点
  455. DateTime time0 = DateUtil.offsetDay(time, -1);
  456. QueryWrapper<TurbineInfoDay> wrapper = new QueryWrapper<>();
  457. wrapper.select("sum(rfdl) rfdl,station_id").eq("record_date", time0.toJdkDate())
  458. .groupBy("station_id");
  459. List<TurbineInfoDay> list = turbineInfoDayService.list(wrapper);
  460. List<StationInfoDay> byDate = getStationinfoByDate(time0.toJdkDate());
  461. Map<String, StationInfoDay> collect = new HashMap<>();
  462. if (!byDate.isEmpty()) {
  463. collect = byDate.stream().collect(Collectors.toMap(StationInfoDay::getStationId, Function.identity()));
  464. }
  465. StationInfoDay infoDay;
  466. List<StationInfoDay> infoDays = new ArrayList<>();
  467. for (TurbineInfoDay day : list) {
  468. if (byDate.isEmpty()) {
  469. infoDay = new StationInfoDay();
  470. infoDay.setStationId(day.getStationId());
  471. infoDay.setRecordDate(time0.toJdkDate());
  472. } else {
  473. infoDay = collect.get(day.getStationId());
  474. }
  475. infoDay.setRfdlFj(day.getRfdl());
  476. infoDays.add(infoDay);
  477. }
  478. stationInfoDayService.saveOrUpdateBatch(infoDays);
  479. }
  480. public void calcTurbineRFDL(Date date) {
  481. //date当天零点
  482. DateTime time = DateUtil.beginOfDay(date);
  483. //date昨天零点
  484. DateTime time0 = DateUtil.offsetDay(time, -1);
  485. //date当天零点加三分钟
  486. DateTime time2 = DateUtil.offsetMinute(time, 3);
  487. //date昨天零点加三分钟
  488. DateTime time1 = DateUtil.offsetMinute(time0, 3);
  489. PointInfo pi = new PointInfo();
  490. pi.setUniformCode("AI121");
  491. pi.setInstitutionType("turbine");
  492. List<PointInfo> entity = getEntity("AI121", "turbine");
  493. Map<String, String> turMap = entity.stream().collect(Collectors.toMap(PointInfo::getTurbineId, PointInfo::getPointKey));
  494. Map<String, String> fsMap = getEntityMap("AI066", "turbine");
  495. String keys = pointInfos2Keys(entity);
  496. Map<String, PointData> swdlL2 = adapter.getHistorySection(goldenUri(), keys, time2.getTime());
  497. Map<String, PointData> swdlL1 = adapter.getHistorySection(goldenUri(), keys, time1.getTime());
  498. Map<String, TurbineInfoDay> byDate = getTurbineinfoMap(time0.toJdkDate(), entity);
  499. TurbineInfoDay infoDay;
  500. String turbineId, key;
  501. List<TurbineInfoDay> infoDays = new ArrayList<>();
  502. DoubleStatData stat;
  503. for (PointInfo info : entity) {
  504. turbineId = info.getTurbineId();
  505. infoDay = byDate.get(turbineId);
  506. key = turMap.get(turbineId);
  507. double v = (swdlL2.get(key).getValue() - swdlL1.get(key).getValue()) * info.getCoef();
  508. if (v < 0 || v > 1000000) {
  509. v = 0;
  510. } else if (v > 30000) {
  511. v = v / (v % 30000 + 3);
  512. }
  513. infoDay.setRfdl(v);
  514. stat = adapter.getHistoryStat(goldenUri(), fsMap.get(turbineId), time1.getTime(), time2.getTime());
  515. if (stat == null) {
  516. infoDay.setPjfs(0.0);
  517. } else {
  518. infoDay.setPjfs(stat.getAvg().getValue());
  519. }
  520. infoDays.add(infoDay);
  521. }
  522. turbineInfoDayService.saveOrUpdateBatch(infoDays);
  523. }
  524. /*public void calcTurbine5s(Date date) {
  525. //date当天零点
  526. DateTime time = DateUtil.beginOfDay(date);
  527. //date昨天零点
  528. DateTime time0 = DateUtil.offsetDay(time, -1);
  529. //状态
  530. List<PointInfo> entityZt = getEntity("MXZT", "turbine");
  531. //功率
  532. Map<String, String> collectGl = getEntityMap("AI114", "turbine");
  533. //风速
  534. Map<String, String> collectFs = getEntityMap("AI066", "turbine");
  535. Map<String, TurbineInfoDay> dayMap = getTurbineinfoMap(time0.toJdkDate(), entityZt);
  536. String tbId;
  537. TurbineInfoDay infoDay;
  538. List<TurbineInfoDay> infoDays = new ArrayList<>();
  539. for (PointInfo info : entityZt) {
  540. tbId = info.getTurbineId();
  541. List<PointData> snapZt = adapter.getHistorySnap(taosUri(), info.getPointKey(), time0.getTime(), time.getTime(), 60);
  542. //0 待机,1 手动停机,2 正常发电,3 发电降出力,4 故障,5 故障受累,6 检修,7 检修受累,8 限电降出力,
  543. // 9 限电停机,10 电网受累,11 环境受累,12 通讯中断,13 设备离线
  544. List<DoubleStatData> statGl = adapter.getHistoryStat2(goldenUri(), collectGl.get(tbId), time0.getTime(), time.getTime(), 60);
  545. List<DoubleStatData> statFs = adapter.getHistoryStat2(goldenUri(), collectFs.get(tbId), time0.getTime(), time.getTime(), 60);
  546. List<PointData> glList = statGl.stream().map(DoubleStatData::getAvg).collect(Collectors.toList());
  547. List<PointData> fsList = statFs.stream().map(DoubleStatData::getAvg)
  548. .peek(fs -> {
  549. if (fs.getValue() < 25) {
  550. fs.setDoubleValue(NumberUtil.round(fs.getValue(), 2).doubleValue());
  551. } else {
  552. fs.setDoubleValue(25.0);
  553. }
  554. }).collect(Collectors.toList());
  555. // Map<String, Double> loss = getTurbinePowerLoss(snapZt, fsList, tbId, glList);
  556. Map<String, Double> loss = new HashMap<>();
  557. infoDay = dayMap.get(tbId);
  558. infoDay.setJhjxss(loss.get("jxss") + loss.get("sdtj"));
  559. infoDay.setFjhjxss(loss.get("gzss") + loss.get("djss") + loss.get("fdjcl"));
  560. infoDay.setXdss(loss.get("xdjcl") + loss.get("xdtj"));
  561. infoDay.setSlss(0.0);
  562. if (infoDay.getRfdl() == 0) {
  563. infoDay.setXnss(0.0);
  564. } else {
  565. infoDay.setXnss(loss.get("xnss") * 0.1);
  566. }
  567. infoDay.setLlfdl(infoDay.getRfdl() + infoDay.getJhjxss() + infoDay.getFjhjxss() +
  568. infoDay.getXdss() + infoDay.getSlss() + infoDay.getXnss());
  569. infoDays.add(infoDay);
  570. }
  571. turbineInfoDayService.saveOrUpdateBatch(infoDays);
  572. }*/
  573. //此处加公式
  574. public void calcStationSspjfs() {
  575. List<PointInfo> fsEntity = getEntity("AI066", "turbine");
  576. String keys = pointInfos2Keys(fsEntity);
  577. Map<String, PointData> latest = adapter.getLatest(goldenUri(), keys);
  578. Map<String, List<String>> listMap = fsEntity.stream().collect(Collectors.groupingBy(PointInfo::getStationId, Collectors.mapping(PointInfo::getPointKey, Collectors.toList())));
  579. Map<String, String> entityMap = getEntityMap("SSPJFS", "station");
  580. List<PointData> tsData = new ArrayList<>();
  581. listMap.forEach((stId, keyList) -> {
  582. double v = keyList.stream().mapToDouble(key -> latest.get(key).getValue()).average().orElse(0.0);
  583. PointData data = new PointData();
  584. data.setTs(System.currentTimeMillis());
  585. data.setTagName(entityMap.get(stId));
  586. data.setDoubleValue(v);
  587. tsData.add(data);
  588. });
  589. adapter.writeHistoryBatch(taosUri(), tsData);
  590. }
  591. public void calcTurbine5s2(Date date) {
  592. //date当天零点
  593. DateTime time = DateUtil.beginOfDay(date);
  594. //date昨天零点
  595. DateTime time0 = DateUtil.offsetDay(time, -1);
  596. //风速
  597. List<PointInfo> entityFs = getEntity("AI066", "turbine");
  598. //功率
  599. List<PointInfo> entityGl = getEntity("AI114", "turbine");
  600. getSnapDataByEntity(entityFs, time0, time, 15);
  601. getSnapDataByEntity(entityGl, time0, time, 15);
  602. List<PointInfo> ztMapMap = calcTurbineZt(time0, time, entityFs, entityGl);
  603. Map<String, Map<String, PointInfo>> ztMapMapInfos = ztMapMap.stream().collect(
  604. Collectors.groupingBy(PointInfo::getStationId, Collectors.toMap(PointInfo::getTurbineId, Function.identity())));
  605. Map<String, TurbineInfoDay> dayMap = getTurbineinfoMap(time0, entityFs);
  606. List<TurbineInfoDay> infoDays = new ArrayList<>();
  607. Map<String, Map<String, List<PointData>>> fsMapMap = entityFs.stream().collect(Collectors.groupingBy(PointInfo::getStationId,
  608. Collectors.toMap(PointInfo::getTurbineId, PointInfo::getPointDatas)));
  609. Map<String, Map<String, List<PointData>>> glMapMap = entityGl.stream().collect(Collectors.groupingBy(PointInfo::getStationId,
  610. Collectors.toMap(PointInfo::getTurbineId, PointInfo::getPointDatas)));
  611. ztMapMapInfos.forEach((stId, ztMapInfo) -> ztMapInfo.forEach((wtId, ztInfo) -> {
  612. //0 待机,1 手动停机,2 正常发电,3 发电降出力,4 故障,5 故障受累,6 检修,7 检修受累,8 限电降出力,
  613. // 9 限电停机,10 电网受累,11 环境受累,12 通讯中断,13 设备离线
  614. TurbineInfoDay infoDay = dayMap.get(wtId);
  615. Map<String, Double> loss = getTurbinePowerLoss(infoDay.getRfdl(), ztMapMapInfos.get(stId).get(wtId), fsMapMap.get(stId).get(wtId), glMapMap.get(stId).get(wtId));
  616. infoDay.setJhjxss(loss.get("jxss") + loss.get("sdtj"));
  617. infoDay.setFjhjxss(loss.get("gzss") + loss.get("djss") + loss.get("fdjcl"));
  618. infoDay.setXdss(loss.get("xdjcl") + loss.get("xdtj"));
  619. infoDay.setSlss(0.0);
  620. if (infoDay.getRfdl() == 0) {
  621. infoDay.setXnss(0.0);
  622. } else {
  623. infoDay.setXnss(loss.get("xnss"));
  624. }
  625. infoDay.setLlfdl(infoDay.getRfdl() + infoDay.getJhjxss() + infoDay.getFjhjxss() +
  626. infoDay.getXdss() + infoDay.getSlss() + infoDay.getXnss());
  627. infoDays.add(infoDay);
  628. }));
  629. turbineInfoDayService.saveOrUpdateBatch(infoDays);
  630. }
  631. public List<PointInfo> calcTurbineZt(Date start, Date end, List<PointInfo> fsInfos, List<PointInfo> glInfos) {
  632. List<PointInfo> turbineZt = calcTurbineDizt(start, end, 15);
  633. List<PointInfo> turbineAizt = calcTurbineAizt(start, end, 15);
  634. turbineZt.addAll(turbineAizt);
  635. Map<String, Map<String, PointInfo>> ztMapMap = turbineZt.stream().collect(Collectors.groupingBy(PointInfo::getStationId,
  636. Collectors.toMap(PointInfo::getTurbineId, Function.identity())));
  637. //AGC
  638. List<PointInfo> entityAgc = getEntity("AGC002", "booster");
  639. getSnapDataByEntity(entityAgc, start, end, 15);
  640. //出线
  641. List<PointInfo> entityCx = getEntity("AGC001", "booster");
  642. getSnapDataByEntity(entityCx, start, end, 15);
  643. //叶轮转速给定
  644. List<PointInfo> entityYlzsgd = getEntity("AI110", "turbine");
  645. getSnapDataByEntity(entityYlzsgd, start, end, 15);
  646. Map<String, PointInfo> collectAgc = entityAgc.stream().collect(Collectors.toMap(PointInfo::getStationId, Function.identity()));
  647. Map<String, PointInfo> collectCx = entityCx.stream().collect(Collectors.toMap(PointInfo::getStationId, Function.identity()));
  648. Map<String, Map<String, List<PointData>>> collectYlzsgd = entityYlzsgd.stream().collect(Collectors.groupingBy(PointInfo::getStationId,
  649. Collectors.toMap(PointInfo::getTurbineId, PointInfo::getPointDatas)));
  650. Map<String, Map<String, PointInfo>> fsMapMap = fsInfos.stream().collect(Collectors.groupingBy(PointInfo::getStationId,
  651. Collectors.toMap(PointInfo::getTurbineId, Function.identity())));
  652. Map<String, Map<String, List<PointData>>> glMapMap = glInfos.stream().collect(Collectors.groupingBy(PointInfo::getStationId,
  653. Collectors.toMap(PointInfo::getTurbineId, PointInfo::getPointDatas)));
  654. ztMapMap.forEach((stId, ztMap) -> {
  655. PointInfo agcInfo = collectAgc.get(stId);
  656. PointInfo cxInfo = collectCx.get(stId);
  657. Map<String, PointInfo> fsMap = fsMapMap.get(stId);
  658. Map<String, List<PointData>> pdsZsglMap = new HashMap<>();
  659. ztMap.forEach((wtId, zt) -> {
  660. List<PointData> peek = fsMap.get(wtId).getPointDatas().stream().peek(pd -> pd.setDoubleValue(NumberUtil.round(pd.getValue() > 25 ? 25.0
  661. : pd.getValue(), 2).doubleValue())).collect(Collectors.toList());
  662. fsMap.get(wtId).setPointDatas(peek);
  663. List<PointData> zsglSnap = fsMap.get(wtId).getPointDatas().stream().map(pd -> {
  664. Double v = CalcCache.fitcoef.get(wtId).get(pd.getValue());
  665. return new PointData(pd.getTs(), v == null ? 0 : v);
  666. }).collect(Collectors.toList());
  667. pdsZsglMap.put(wtId, zsglSnap);
  668. });
  669. List<PointData> agcc = agcInfo.getPointDatas().stream().peek(agc -> {
  670. agc.setDoubleValue(agc.getValue() * agcInfo.getCoef());
  671. agc.setLongValue(0L);
  672. }).collect(Collectors.toList());
  673. agcInfo.setPointDatas(agcc);
  674. List<PointData> cxc = cxInfo.getPointDatas().stream().peek(cx -> {
  675. cx.setDoubleValue(cx.getValue() * cxInfo.getCoef());
  676. cx.setLongValue(0L);
  677. }).collect(Collectors.toList());
  678. cxInfo.setPointDatas(cxc);
  679. //风机风速、功率->场站风速、功率
  680. // List<PointData> firstFs = new ArrayList<>();
  681. // List<PointData> firstGl = new ArrayList<>();
  682. List<PointData> firstZsgl = new ArrayList<>();
  683. double v1, v2, v3;
  684. for (int i = 0; i < 5760; i++) {
  685. int finalI = i;
  686. long ts = start.getTime() + i * 15000;
  687. // v1 = fsMap.values().stream().mapToDouble(pds -> pds.getPointDatas().get(finalI).getValue()).average().orElse(0.0);
  688. // v2 = glMap.values().stream().mapToDouble(pds -> pds.getPointDatas().get(finalI).getValue()).sum();
  689. v3 = pdsZsglMap.values().stream().mapToDouble(pds -> pds.get(finalI).getValue()).sum();
  690. // firstFs.add(new PointData(ts, v1));
  691. // firstGl.add(new PointData(ts, v2));
  692. firstZsgl.add(new PointData(ts, v3));
  693. }
  694. List<PointData> czxd = calcStationXd2(agcInfo, cxInfo.getPointDatas(), firstZsgl);
  695. ztMap.forEach((wtId, ztInfo) -> {
  696. List<PointData> fss = fsMap.get(wtId).getPointDatas();
  697. List<PointData> gls = glMapMap.get(stId).get(wtId);
  698. //叶轮转速给定
  699. List<PointData> ylzsgds = collectYlzsgd.get(stId).get(wtId);
  700. calcTurbineXd(ztInfo, gls, fss, ylzsgds, czxd);
  701. });
  702. });
  703. return turbineZt;
  704. }
  705. public void calcTurbineXd(PointInfo ztInfo, List<PointData> gls, List<PointData> fss, List<PointData> ylzsgds, List<PointData> czxd) {
  706. Map<String, EquipmentModel> map = equipmentModelService.map();
  707. Double capacity = map.get(ztInfo.getSpare()).getPowerProduction();
  708. List<PointData> zts = ztInfo.getPointDatas();
  709. for (int i = 0; i < 5760; i++) {
  710. double ssgl = gls.get(i).getValue();
  711. double ssfs = fss.get(i).getValue();
  712. double ylzsgd = ylzsgds.get(i).getValue();
  713. if (czxd.get(i).isBooleanValue() && ssgl < capacity * 0.9) {
  714. if (ssfs >= 12) {
  715. zts.get(i).setDoubleValue(8.0);
  716. }
  717. if (ylzsgd != 0 && ylzsgd < 17.3) {//降出力
  718. zts.get(i).setDoubleValue(8.0);
  719. }
  720. }
  721. }
  722. }
  723. //此处加公式
  724. public List<PointData> calcStationXd2(PointInfo agcInfo, List<PointData> cxSnap, List<PointData> zsglSnap) {
  725. String stId = agcInfo.getStationId();
  726. List<PointData> agcSnap = agcInfo.getPointDatas();
  727. List<PointData> zsgl5s = get5Avg(zsglSnap);
  728. List<PointData> agc5s = get5Avg(agcSnap);
  729. List<PointData> cx5s = get5Avg(cxSnap);
  730. //场站限电
  731. List<PointData> czxd = zsglSnap.stream().map(s -> new PointData(s.getTs(), 0.0)).collect(Collectors.toList());
  732. //之前是否限电
  733. boolean iszqxd = false;
  734. for (int i = 0; i < 5760; i++) {
  735. boolean isxd = false;
  736. double zsgl = zsglSnap.get(i).getValue();
  737. double zsgl5 = zsgl5s.get(i).getValue();
  738. double agc = agcSnap.get(i).getValue();
  739. double agc5 = agc5s.get(i).getValue();
  740. double cxgl = cxSnap.get(i).getValue();
  741. double cxgl5 = cx5s.get(i).getValue();
  742. boolean k; //agc一直不变
  743. //第一次初始化复制实时数据到缓存
  744. if (CalcCache.powerCache.get(stId) == null || CalcCache.powerCache.get(stId) != agc) {
  745. CalcCache.powerCache.put(stId, agc);
  746. k = false;
  747. } else {
  748. k = true;
  749. }
  750. if (agc != 0) {
  751. //如果场站限电状态为不限电
  752. if (zsgl5 >= agcInfo.getSpare2() * 40) {
  753. if (agc5 <= cxgl5) {
  754. isxd = true;
  755. } else if (iszqxd) {
  756. if (zsgl / agc > 1.15) {
  757. isxd = true;
  758. } else if (zsgl > agc && agc5 - cxgl5 < 400) {
  759. isxd = true;
  760. }
  761. } else if (zsgl / agc >= 1.2 && agc - cxgl <= 200) {
  762. isxd = true;
  763. }
  764. } else if (k) {
  765. if (zsgl / agc >= 1.1 && agc5 - cxgl5 <= 200) {
  766. isxd = true;
  767. }
  768. }
  769. }
  770. czxd.get(i).setBooleanValue(isxd);
  771. iszqxd = isxd;
  772. }
  773. return czxd;
  774. }
  775. private List<PointData> get5Avg(List<PointData> pointData) {
  776. List<PointData> result = new ArrayList<>();
  777. for (int i = 0; i < pointData.size(); i++) {
  778. PointData data = new PointData();
  779. data.setTs(pointData.get(i).getTs());
  780. double v;
  781. if (i > 4) {
  782. v = (pointData.get(i).getValue() + pointData.get(i - 1).getValue() + pointData.get(i - 2).getValue() + pointData.get(i - 3).getValue() + pointData.get(i - 4).getValue()) / 5;
  783. } else {
  784. v = pointData.get(i).getValue();
  785. }
  786. data.setDoubleValue(v);
  787. result.add(data);
  788. }
  789. return result;
  790. }
  791. public List<PointInfo> calcTurbineAizt(Date start, Date end, int interval) {
  792. List<PointInfo> entityAiZt = getEntity("AI422", "state");
  793. List<StateAi> list = stateAiService.list();
  794. Map<String, Map<Integer, Integer>> collectAi = list.stream().collect(Collectors.groupingBy(
  795. StateAi::getModel, Collectors.toMap(StateAi::getOriginalState, StateAi::getMappingState)));
  796. getSnapDataByEntity(entityAiZt, start, end, interval);
  797. for (PointInfo info : entityAiZt) {
  798. List<PointData> peek = info.getPointDatas().stream().peek(pd -> {
  799. Integer m = collectAi.get(info.getSpare()).get((int) pd.getValue());
  800. if (m == null) m = 2;
  801. pd.setDoubleValue(m);
  802. }).collect(Collectors.toList());
  803. info.setPointDatas(peek);
  804. }
  805. return entityAiZt;
  806. }
  807. public List<PointInfo> calcTurbineDizt(Date start, Date end, int interval) {
  808. List<PointInfo> entity = new ArrayList<>();
  809. List<PointInfo> entity0 = getEntity("MX000", "state");
  810. List<PointInfo> entity1 = getEntity("MX001", "state");
  811. List<PointInfo> entity2 = getEntity("MX002", "state");
  812. List<PointInfo> entity4 = getEntity("MX004", "state");
  813. List<PointInfo> entity6 = getEntity("MX006", "state");
  814. entity.addAll(entity0);
  815. entity.addAll(entity1);
  816. entity.addAll(entity2);
  817. entity.addAll(entity4);
  818. entity.addAll(entity6);
  819. List<String> keys = entity.stream().map(PointInfo::getPointKey).collect(Collectors.toList());
  820. Map<String, PointData> latest = adapter.getHistorySection(goldenUri(), String.join(",", keys), start.getTime());
  821. Map<String, Map<String, PointInfo>> wtUcPis = entity.stream().collect(Collectors.groupingBy(PointInfo::getTurbineId, Collectors.toMap(PointInfo::getUniformCode, Function.identity())));
  822. wtUcPis.forEach((wtId, map) -> {
  823. map.forEach((uc, pi) -> {
  824. List<PointData> raw = adapter.getHistoryRaw(goldenUri(), pi.getPointKey(), start.getTime(), end.getTime());
  825. List<PointData> ds = boolPointDatasFull(latest.get(pi.getPointKey()).isBooleanValue(), raw, start.getTime(), end.getTime(), interval);
  826. pi.setPointDatas(ds);
  827. ThreadUtil.sleep(20);
  828. });
  829. List<PointData> mx000 = map.get("MX000").getPointDatas();
  830. PointData data0;
  831. for (int i = 0; i < mx000.size(); i++) {
  832. data0 = mx000.get(i);
  833. if (data0.getValue() == 1.0) {
  834. data0.setDoubleValue(0.0);
  835. data0.setBooleanValue(false);
  836. } else if (map.get("MX001").getPointDatas().get(i).getValue() == 1.0) {
  837. data0.setDoubleValue(1.0);
  838. } else if (map.get("MX002").getPointDatas().get(i).getValue() == 1.0) {
  839. data0.setDoubleValue(2.0);
  840. } else if (map.get("MX004").getPointDatas().get(i).getValue() == 1.0) {
  841. data0.setDoubleValue(4.0);
  842. } else if (map.get("MX006").getPointDatas().get(i).getValue() == 1.0) {
  843. data0.setDoubleValue(6.0);
  844. } else {
  845. // System.out.println(wtId + data0.getTs() + ":di无对应状态!");
  846. data0.setDoubleValue(2.0);
  847. }
  848. }
  849. map.get("MX001").setPointDatas(null);
  850. map.get("MX002").setPointDatas(null);
  851. map.get("MX004").setPointDatas(null);
  852. map.get("MX006").setPointDatas(null);
  853. });
  854. return entity0;
  855. }
  856. public PointInfo calcTurbineAizt2(PointInfo entityAiZt, Map<String, Map<Integer, Integer>> collectAi, Date start, Date end, int interval) {
  857. if (entityAiZt == null) return null;
  858. getSnapDataByEntity(entityAiZt, start, end, interval);
  859. Map<Integer, Integer> integerMap = collectAi.get(entityAiZt.getSpare());
  860. List<PointData> data = entityAiZt.getPointDatas().stream().peek(pd -> {
  861. Integer m = integerMap.get((int) pd.getValue());
  862. if (m == null) m = 2;
  863. pd.setDoubleValue(m);
  864. }).collect(Collectors.toList());
  865. entityAiZt.setPointDatas(data);
  866. return entityAiZt;
  867. }
  868. public PointInfo calcTurbineDizt2(Map<String, PointInfo> wtucpi, Map<String, PointData> latest, Date start, Date end, int interval) {
  869. if (wtucpi == null) return null;
  870. wtucpi.forEach((uc, pi) -> {
  871. List<PointData> raw = adapter.getHistoryRaw(goldenUri(), pi.getPointKey(), start.getTime(), end.getTime());
  872. List<PointData> ds = boolPointDatasFull(latest.get(pi.getPointKey()).isBooleanValue(), raw, start.getTime(), end.getTime(), interval);
  873. pi.setPointDatas(ds);
  874. ThreadUtil.sleep(20);
  875. });
  876. PointInfo mx000 = wtucpi.get("MX000");
  877. PointData data0;
  878. for (int i = 0; i < mx000.getPointDatas().size(); i++) {
  879. data0 = mx000.getPointDatas().get(i);
  880. if (data0.getValue() == 1.0) {
  881. data0.setDoubleValue(0.0);
  882. data0.setBooleanValue(false);
  883. } else if (wtucpi.get("MX001").getPointDatas().get(i).getValue() == 1.0) {
  884. data0.setDoubleValue(1.0);
  885. } else if (wtucpi.get("MX002").getPointDatas().get(i).getValue() == 1.0) {
  886. data0.setDoubleValue(2.0);
  887. } else if (wtucpi.get("MX004").getPointDatas().get(i).getValue() == 1.0) {
  888. data0.setDoubleValue(4.0);
  889. } else if (wtucpi.get("MX006").getPointDatas().get(i).getValue() == 1.0) {
  890. data0.setDoubleValue(6.0);
  891. } else {
  892. System.out.println(mx000.getTurbineId() + data0.getTs() + ":di无对应状态!");
  893. data0.setDoubleValue(2.0);
  894. }
  895. }
  896. wtucpi.get("MX001").setPointDatas(null);
  897. wtucpi.get("MX002").setPointDatas(null);
  898. wtucpi.get("MX004").setPointDatas(null);
  899. wtucpi.get("MX006").setPointDatas(null);
  900. return mx000;
  901. }
  902. public List<PointData> boolPointDatasFull(boolean v, List<PointData> data, long startTime, long endTime, int interval) {
  903. int j = 0, m = 0;
  904. List<PointData> data2 = new ArrayList<>();
  905. for (long i = startTime; i < endTime; i += interval * 1000L) {
  906. PointData dt = new PointData();
  907. dt.setTs(i);
  908. data2.add(dt);
  909. //超出范围
  910. if (j >= data.size() - 1) continue;
  911. if (i < data.get(j).getTs()) {
  912. if (m == 0) {
  913. dt.setBooleanValue(v);
  914. } else {
  915. dt.setBooleanValue(data.get(j - 1).isBooleanValue());
  916. }
  917. } else {
  918. dt.setBooleanValue(data.get(j).isBooleanValue());
  919. j++;
  920. m++;
  921. }
  922. }
  923. return data2;
  924. }
  925. public void calcGlqxnh(Date start, Date end) {
  926. //风速
  927. List<PointInfo> entityFs = getEntity("AI066", "turbine");
  928. //功率
  929. List<PointInfo> entityGl = getEntity("AI114", "turbine");
  930. Map<String, PointInfo> glMap = entityGl.stream().collect(Collectors.toMap(PointInfo::getTurbineId, Function.identity()));
  931. Map<String, EquipmentModel> map = equipmentModelService.map();
  932. List<PointInfo> entity = new ArrayList<>();
  933. List<PointInfo> entity0 = getEntity("MX000", "state");
  934. List<PointInfo> entity1 = getEntity("MX001", "state");
  935. List<PointInfo> entity2 = getEntity("MX002", "state");
  936. List<PointInfo> entity4 = getEntity("MX004", "state");
  937. List<PointInfo> entity6 = getEntity("MX006", "state");
  938. entity.addAll(entity0);
  939. entity.addAll(entity1);
  940. entity.addAll(entity2);
  941. entity.addAll(entity4);
  942. entity.addAll(entity6);
  943. List<String> keys = entity.stream().map(PointInfo::getPointKey).collect(Collectors.toList());
  944. Map<String, PointData> latest = adapter.getHistorySection(goldenUri(), String.join(",", keys), start.getTime());
  945. Map<String, Map<String, PointInfo>> wtUcPis = entity.stream().collect(Collectors.groupingBy(PointInfo::getTurbineId, Collectors.toMap(PointInfo::getUniformCode, Function.identity())));
  946. List<PointInfo> entityAiZt = getEntity("AI422", "state");
  947. Map<String, PointInfo> aiztMap = entityAiZt.stream().collect(Collectors.toMap(PointInfo::getTurbineId, Function.identity()));
  948. List<StateAi> list = stateAiService.list();
  949. Map<String, Map<Integer, Integer>> collectAi = list.stream().collect(Collectors.groupingBy(
  950. StateAi::getModel, Collectors.toMap(StateAi::getOriginalState, StateAi::getMappingState)));
  951. // Map<String, Map<Double,Double>> fitMap = new HashMap<>();
  952. int i = 0;
  953. for (PointInfo ef : entityFs) {
  954. if (i < 280) {
  955. i++;
  956. continue;
  957. }
  958. String wtId = ef.getTurbineId();
  959. PointInfo glInfo = glMap.get(wtId);
  960. getSnapDataByEntity(ef, start, end, 60);
  961. getSnapDataByEntity(glInfo, start, end, 60);
  962. List<PointData> peek = ef.getPointDatas().stream().peek(pd -> pd.setDoubleValue(
  963. NumberUtil.round(pd.getValue(), 2).doubleValue())).collect(Collectors.toList());
  964. ef.setPointDatas(peek);
  965. PointInfo turbineZt = calcTurbineDizt2(wtUcPis.get(wtId), latest, start, end, 60);
  966. PointInfo turbineAizt = calcTurbineAizt2(aiztMap.get(wtId), collectAi, start, end, 60);
  967. Map<Double, Double> fit = new TreeMap<>();
  968. if (turbineZt != null) {
  969. double capacity = map.get(turbineZt.getSpare()).getPowerProduction();
  970. fit = dataProcessAndFit(ef, glInfo, turbineZt, CalcCache.bzgl.get(turbineZt.getSpare()), capacity);
  971. // fitMap.put(wtId, fit);
  972. turbineZt.setPointDatas(null);
  973. }
  974. if (turbineAizt != null) {
  975. double capacity = map.get(turbineAizt.getSpare()).getPowerProduction();
  976. fit = dataProcessAndFit(ef, glInfo, turbineAizt, CalcCache.bzgl.get(turbineAizt.getSpare()), capacity);
  977. // fitMap.put(wtId, fit);
  978. turbineAizt.setPointDatas(null);
  979. }
  980. ef.setPointDatas(null);
  981. glInfo.setPointDatas(null);
  982. stringRedisTemplate.opsForValue().set("glqxnh:" + end.getMonth() + ":" + wtId, JSON.toJSONString(fit));
  983. }
  984. System.out.println("苹果天涯");
  985. // stringRedisTemplate.opsForValue().set("glqxnh", JSON.toJSONString(fitMap));
  986. }
  987. public Map<Double, Double> dataProcessAndFit(PointInfo fsPI, PointInfo glPI, PointInfo ztPI, Map<Double, Double> modelPowerMap, double capacity) {
  988. List<PointData> fsPds = fsPI.getPointDatas();
  989. List<PointData> glPds = glPI.getPointDatas();
  990. List<PointData> ztPds = ztPI.getPointDatas();
  991. WeightedObservedPoints points = new WeightedObservedPoints();
  992. //数据过滤 0正常,1过滤掉
  993. if (fsPds.size() != glPds.size() || glPds.size() != ztPds.size()) return new HashMap<>();
  994. Double bzv;
  995. for (int i = 0; i < ztPds.size(); i++) {
  996. double fs = fsPds.get(i).getValue();
  997. double gl = glPds.get(i).getValue();
  998. // 过滤非并网值 风机状态不等于2
  999. if (ztPds.get(i).getValue() != 2.0) {
  1000. //
  1001. } else if (fs < 0 || fs > 25 || gl < 0 || gl > capacity * 1.05) {// 按给定风速功率过滤
  1002. //
  1003. } else {
  1004. bzv = modelPowerMap.get(fs);
  1005. if (bzv == null || bzv == 0.0) {
  1006. points.add(fs, gl);
  1007. continue;
  1008. }
  1009. double k = gl / bzv; //功率曲线偏差: 保证功率/实际功率 k:偏差百分比
  1010. if (k < 0.61 || k > 1.25) {
  1011. //
  1012. } else {
  1013. points.add(fs, gl);
  1014. }
  1015. }
  1016. }
  1017. Map<Double, Double> glyc = new TreeMap<>();
  1018. PolynomialCurveFitter fitter = PolynomialCurveFitter.create(4);
  1019. double[] result = fitter.fit(points.toList());
  1020. double min = calcMin(result);
  1021. System.out.println(ztPI.getTurbineId());
  1022. System.out.println(ztPI.getSpare());
  1023. System.out.println(min);
  1024. double[] max = calcMax(result);
  1025. if (min == 0 || max[0] == 0 || max[1] == 0) {
  1026. System.out.println();
  1027. }
  1028. System.out.println(max[0] + "," + max[1]);
  1029. double ru;
  1030. for (double i = 0; i < 25; i += 0.01) {
  1031. i = NumberUtil.round(i, 2).doubleValue();
  1032. if (i < 3) {
  1033. ru = 0.0;
  1034. } else if (i < min) {
  1035. ru = modelPowerMap.get(i);
  1036. } else if (i < max[0]) {
  1037. ru = calcPoly(i, result);
  1038. } else {
  1039. ru = max[1];
  1040. }
  1041. glyc.put(i, NumberUtil.round(ru, 2).doubleValue());
  1042. }
  1043. return glyc;
  1044. }
  1045. public double calcMin(double[] coef) {
  1046. double minX = 0, minY = 9000;
  1047. for (double x = 0; x < 8; x += 0.01) {
  1048. double y = calcPoly(x, coef);
  1049. if (y < minY) {
  1050. minY = y;
  1051. minX = x;
  1052. }
  1053. }
  1054. return minX;
  1055. }
  1056. public double[] calcMax(double[] coef) {
  1057. double[] a = new double[2];
  1058. for (double x = 8; x < 16; x += 0.01) {
  1059. double y = calcPoly(x, coef);
  1060. if (y > a[1]) {
  1061. a[0] = x;
  1062. a[1] = y;
  1063. }
  1064. }
  1065. return a;
  1066. }
  1067. public void printResult(double[] r, double accuracy) {
  1068. for (double index = 0; index < 25; index += accuracy) {
  1069. System.out.println("[" + index + "," + calcPoly(index, r) + "],");
  1070. }
  1071. }
  1072. public void getSnapDataByEntity(List<PointInfo> entity, URI uri, Date start, Date end, int interval) {
  1073. for (PointInfo info : entity) {
  1074. List<PointData> snap = adapter.getHistorySnap(uri, info.getPointKey(), start.getTime(), end.getTime(), interval);
  1075. ThreadUtil.sleep(20);
  1076. info.setPointDatas(snap);
  1077. }
  1078. }
  1079. public void getSnapDataByEntity(PointInfo entity, URI uri, Date start, Date end, int interval) {
  1080. getSnapDataByEntity(Collections.singletonList(entity), uri, start, end, interval);
  1081. }
  1082. public void getSnapDataByEntity(List<PointInfo> entity, Date start, Date end, int interval) {
  1083. getSnapDataByEntity(entity, goldenUri(), start, end, interval);
  1084. }
  1085. public void getSnapDataByEntity(PointInfo entity, Date start, Date end, int interval) {
  1086. getSnapDataByEntity(entity, goldenUri(), start, end, interval);
  1087. }
  1088. public double calcPoly(String wtId, double value) {
  1089. return CalcCache.fitcoef.get(wtId).get(value);
  1090. }
  1091. public double calcPoly(double x, double[] factor) {
  1092. double y = 0;
  1093. for (int deg = 0; deg < factor.length; deg++) {
  1094. y += Math.pow(x, deg) * factor[deg];
  1095. }
  1096. return y;
  1097. }
  1098. }