JavaFunctionJobHandler.java 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230
  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 cn.hutool.core.thread.ThreadUtil;
  6. import cn.hutool.core.util.NumberUtil;
  7. import com.alibaba.fastjson2.JSON;
  8. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  9. import com.ruoyi.quartz.handler.IJobHandler;
  10. import com.ruoyi.ucp.entity.*;
  11. import com.ruoyi.ucp.feign.AdapterApi;
  12. import com.ruoyi.ucp.service.*;
  13. import com.ruoyi.ucp.util.CalcCache;
  14. import org.apache.commons.math3.fitting.PolynomialCurveFitter;
  15. import org.apache.commons.math3.fitting.WeightedObservedPoints;
  16. import org.springframework.data.redis.core.StringRedisTemplate;
  17. import org.springframework.stereotype.Service;
  18. import javax.annotation.Resource;
  19. import java.net.URI;
  20. import java.util.*;
  21. import java.util.concurrent.ConcurrentHashMap;
  22. import java.util.concurrent.atomic.AtomicReference;
  23. import java.util.function.Function;
  24. import java.util.stream.Collectors;
  25. @Service
  26. public class JavaFunctionJobHandler extends IJobHandler {
  27. /* @Resource
  28. private PointInfo pointInfo;*/
  29. @Resource
  30. private AdapterApi adapter;
  31. @Resource
  32. private IPointInfoService pointService;
  33. @Resource
  34. private IStationInfoHourService stationInfoHourService;
  35. @Resource
  36. private IStationInfoDayService stationInfoDayService;
  37. @Resource
  38. private ILineInfoDayService lineInfoDayService;
  39. @Resource
  40. private IStateAiService stateAiService;
  41. @Resource
  42. private StringRedisTemplate stringRedisTemplate;
  43. @Resource
  44. private IEquipmentModelService equipmentModelService;
  45. @Resource
  46. private ITurbineInfoDayService turbineInfoDayService;
  47. @Resource
  48. private IProEconReportIndicatorPoolService reportIndicatorPoolService;
  49. @Override
  50. public void execute() throws Exception {
  51. }
  52. @Override
  53. public IJobHandler getFunctionHandler() {
  54. return null;
  55. }
  56. @Override
  57. public void setFunctionHandler(IJobHandler jobHandler) {
  58. }
  59. public URI taosGoldenUriTest() {
  60. return URI.create("http://127.0.0.1:8011/ts");
  61. }
  62. public String pointInfos2Keys(List<PointInfo> entity) {
  63. return entity.stream().map(PointInfo::getPointKey).collect(Collectors.joining(","));
  64. }
  65. public List<StationInfoHour> getStationinfoByHour(Date hour) {
  66. QueryWrapper<StationInfoHour> wrapper = new QueryWrapper<>();
  67. wrapper.eq("record_date", DateUtil.beginOfHour(hour));
  68. return stationInfoHourService.list(wrapper);
  69. }
  70. public List<StationInfoDay> getStationinfoByDate(Date date) {
  71. QueryWrapper<StationInfoDay> wrapper = new QueryWrapper<>();
  72. wrapper.eq("record_date", date);
  73. return stationInfoDayService.list(wrapper);
  74. }
  75. public List<LineInfoDay> getLineinfoByDate(Date date) {
  76. QueryWrapper<LineInfoDay> wrapper = new QueryWrapper<>();
  77. wrapper.eq("record_date", date);
  78. return lineInfoDayService.list(wrapper);
  79. }
  80. public URI taosUri() {
  81. return URI.create("http://172.16.12.101:8012/ts");
  82. }
  83. public URI goldenUri() {
  84. return URI.create("http://172.16.12.103:8011/ts");
  85. }
  86. public Map<String, String> getEntityMap(String uniformCode, String institutionType) {
  87. PointInfo pi = new PointInfo();
  88. pi.setUniformCode(uniformCode);
  89. pi.setInstitutionType(institutionType);
  90. List<PointInfo> entity = pointService.getByEntity(pi);
  91. switch (institutionType) {
  92. case "turbine":
  93. case "state":
  94. return entity.stream().collect(Collectors.toMap(PointInfo::getTurbineId, PointInfo::getPointKey));
  95. case "station":
  96. return entity.stream().collect(Collectors.toMap(PointInfo::getStationId, PointInfo::getPointKey));
  97. }
  98. return new HashMap<>();
  99. }
  100. public Map<String, TurbineInfoDay> getTurbineinfoMap(Date date, List<PointInfo> entity) {
  101. QueryWrapper<TurbineInfoDay> wrapper = new QueryWrapper<>();
  102. wrapper.eq("record_date", date);
  103. List<TurbineInfoDay> list = turbineInfoDayService.list(wrapper);
  104. if (list.isEmpty()) {
  105. entity.forEach(pi -> {
  106. TurbineInfoDay day = new TurbineInfoDay();
  107. day.setStationId(pi.getStationId());
  108. day.setProjectId(pi.getProjectId());
  109. day.setLineId(pi.getLineId());
  110. day.setTurbineId(pi.getTurbineId());
  111. day.setRecordDate(date);
  112. list.add(day);
  113. });
  114. }
  115. return list.stream().collect(Collectors.toMap(TurbineInfoDay::getTurbineId, Function.identity()));
  116. }
  117. public List<PointInfo> getEntity(String uniformCode, String institutionType) {
  118. PointInfo pi = new PointInfo();
  119. pi.setUniformCode(uniformCode);
  120. pi.setInstitutionType(institutionType);
  121. return pointService.getByEntity(pi);
  122. }
  123. public Map<String, Double> getTurbinePowerLoss(double rfdl, PointInfo piZt, List<PointData> fsList, List<PointData> glList) {
  124. String tbId = piZt.getTurbineId();
  125. // Map<Double, Long> collect = piZt.getPointDatas().stream().collect(Collectors.groupingBy(PointData::getValue, Collectors.counting()));
  126. double sum1 = glList.stream().mapToDouble(PointData::getValue).sum();
  127. double xs = rfdl / (sum1 / 240);
  128. // double sum2 = fsList.stream().mapToDouble(pd -> {
  129. // Double v = CalcCache.fitcoef.get(tbId).get(pd.getValue());
  130. // if (v == null) v = 0.0;
  131. // return v;
  132. // }).sum();
  133. // sum2=sum2/240;
  134. List<PointData> collectZt = piZt.getPointDatas();
  135. double jxss = 0, sdtj = 0, gzss = 0, djss = 0, xnss = 0, fdjcl = 0, xdjcl = 0, xdtj = 0;
  136. Map<String, Double> map = new HashMap<>();
  137. if (collectZt.size() == fsList.size() && fsList.size() == glList.size()) {
  138. for (int i = 0; i < collectZt.size(); i++) {
  139. Double v = CalcCache.fitcoef.get(tbId).get(fsList.get(i).getValue());
  140. if (v == null) v = 0.0;
  141. switch ((int) collectZt.get(i).getValue()) {
  142. //计划检修损失:
  143. case 6:
  144. //检修
  145. jxss += v;
  146. break;
  147. case 1:
  148. //手动停机
  149. sdtj += v;
  150. break;
  151. //非计划检修损失:
  152. case 4:
  153. //故障
  154. gzss += v;
  155. break;
  156. case 0:
  157. //待机
  158. djss += v;
  159. break;
  160. //性能损失:
  161. case 2:
  162. //性能
  163. xnss += v - glList.get(i).getValue();
  164. break;
  165. case 3:
  166. //发电降出力
  167. fdjcl += v - glList.get(i).getValue();
  168. break;
  169. //限电损失:
  170. case 8:
  171. //限电降出力
  172. xdjcl += v - glList.get(i).getValue();
  173. break;
  174. case 9:
  175. //限电停机
  176. xdtj += v;
  177. break;
  178. }
  179. }
  180. }
  181. //15秒*60分钟
  182. map.put("jxss", jxss > 0 ? jxss / 240 * xs : 0);
  183. map.put("sdtj", sdtj > 0 ? sdtj / 240 * xs : 0);
  184. map.put("gzss", gzss > 0 ? gzss / 240 * xs : 0);
  185. map.put("djss", djss > 0 ? djss / 240 * xs : 0);
  186. map.put("xnss", xnss > 0 ? xnss / 240 * xs : 0);
  187. map.put("fdjcl", fdjcl > 0 ? fdjcl / 240 * xs : 0);
  188. map.put("xdjcl", xdjcl > 0 ? xdjcl / 240 * xs : 0);
  189. map.put("xdtj", xdtj > 0 ? xdtj / 240 * xs : 0);
  190. return map;
  191. }
  192. public void calcAQTS(Date date) {
  193. QueryWrapper<StationInfoDay> wrapper = new QueryWrapper<>();
  194. wrapper.select("min(record_date) record_date");
  195. StationInfoDay one = stationInfoDayService.getOne(wrapper);
  196. wrapper = new QueryWrapper<>();
  197. wrapper.eq("record_date", one.getRecordDate());
  198. //最早时间的列表
  199. List<StationInfoDay> list = stationInfoDayService.list(wrapper);
  200. List<StationInfoDay> today = getStationinfoByDate(date);
  201. if (today.isEmpty()) {
  202. for (StationInfoDay infoDay : list) {
  203. StationInfoDay day = new StationInfoDay();
  204. day.setStationId(infoDay.getStationId());
  205. day.setRecordDate(date);
  206. day.setAqts(infoDay.getAqts() + (int) DateUtil.betweenDay(infoDay.getRecordDate(), date, true));
  207. today.add(day);
  208. }
  209. } else {
  210. Map<String, StationInfoDay> collect = list.stream().collect(Collectors.toMap(StationInfoDay::getStationId, Function.identity()));
  211. for (StationInfoDay day : today) {
  212. StationInfoDay day1 = collect.get(day.getStationId());
  213. day.setAqts(day1.getAqts() + (int) DateUtil.betweenDay(day1.getRecordDate(), date, true));
  214. }
  215. }
  216. stationInfoDayService.saveOrUpdateBatch(today);
  217. }
  218. public void calcStationSwGwCyDl(Date date) {
  219. //date当天零点
  220. DateTime time = DateUtil.beginOfDay(date);
  221. //date昨天零点
  222. DateTime time0 = DateUtil.offsetDay(time, -1);
  223. //date当天零点加三分钟
  224. DateTime time2 = DateUtil.offsetMinute(time, 3);
  225. //date昨天零点加三分钟
  226. DateTime time1 = DateUtil.offsetMinute(time0, 3);
  227. PointInfo swdlPi = new PointInfo();
  228. swdlPi.setUniformCode("Z-ZXYG-CX");
  229. PointInfo gwdlPi = new PointInfo();
  230. gwdlPi.setUniformCode("Z-FXYG-CX");
  231. PointInfo cydlPi = new PointInfo();
  232. cydlPi.setUniformCode("Z-ZXYG-ZYB");
  233. List<PointInfo> swdlEt = pointService.getByEntity(swdlPi);
  234. List<PointInfo> gwdlEt = pointService.getByEntity(gwdlPi);
  235. Map<String, PointInfo> gwdlMap = gwdlEt.stream().collect(Collectors.toMap(PointInfo::getStationId, Function.identity()));
  236. List<PointInfo> cydlEt = pointService.getByEntity(cydlPi);
  237. Map<String, PointInfo> cydlMap = cydlEt.stream().collect(Collectors.toMap(PointInfo::getStationId, Function.identity()));
  238. String swdlK = pointInfos2Keys(swdlEt);
  239. String gwdlK = pointInfos2Keys(gwdlEt);
  240. String cydlK = pointInfos2Keys(cydlEt);
  241. Map<String, PointData> swdlL2 = adapter.getHistorySection(goldenUri(), swdlK, time2.getTime());
  242. Map<String, PointData> swdlL1 = adapter.getHistorySection(goldenUri(), swdlK, time1.getTime());
  243. Map<String, PointData> gwdlL2 = adapter.getHistorySection(goldenUri(), gwdlK, time2.getTime());
  244. Map<String, PointData> gwdlL1 = adapter.getHistorySection(goldenUri(), gwdlK, time1.getTime());
  245. Map<String, PointData> cydlL2 = adapter.getHistorySection(goldenUri(), cydlK, time2.getTime());
  246. Map<String, PointData> cydlL1 = adapter.getHistorySection(goldenUri(), cydlK, time1.getTime());
  247. List<StationInfoDay> byDate = getStationinfoByDate(time0.toJdkDate());
  248. Map<String, StationInfoDay> collect = new HashMap<>();
  249. if (!byDate.isEmpty()) {
  250. collect = byDate.stream().collect(Collectors.toMap(StationInfoDay::getStationId, Function.identity()));
  251. }
  252. StationInfoDay infoDay;
  253. String stationId;
  254. PointInfo gwdlKey, cydlKey;
  255. List<StationInfoDay> infoDays = new ArrayList<>();
  256. for (PointInfo info : swdlEt) {
  257. stationId = info.getStationId();
  258. if (byDate.isEmpty()) {
  259. infoDay = new StationInfoDay();
  260. infoDay.setStationId(stationId);
  261. infoDay.setRecordDate(time0.toJdkDate());
  262. } else {
  263. infoDay = collect.get(stationId);
  264. }
  265. infoDay.setSwdl((int) ((swdlL2.get(info.getPointKey()).getValue() -
  266. swdlL1.get(info.getPointKey()).getValue()) * info.getCoef()));
  267. gwdlKey = gwdlMap.get(stationId);
  268. infoDay.setGwdl((int) ((gwdlL2.get(gwdlKey.getPointKey()).getValue() -
  269. gwdlL1.get(gwdlKey.getPointKey()).getValue()) * gwdlKey.getCoef()));
  270. cydlKey = cydlMap.get(stationId);
  271. infoDay.setCydl((int) ((cydlL2.get(cydlKey.getPointKey()).getValue() -
  272. cydlL1.get(cydlKey.getPointKey()).getValue()) * cydlKey.getCoef()));
  273. infoDays.add(infoDay);
  274. }
  275. stationInfoDayService.saveOrUpdateBatch(infoDays);
  276. }
  277. public void calcStationHourRFDL(Date hour) {
  278. PointInfo pointInfo = new PointInfo();
  279. pointInfo.setInstitutionType("station");
  280. pointInfo.setUniformCode("RFDLSYZ");
  281. List<PointInfo> entity = pointService.getByEntity(pointInfo);
  282. Map<String, PointData> section = adapter.getHistorySection(taosUri(), pointInfos2Keys(entity), hour.getTime());
  283. List<StationInfoHour> byHours = getStationinfoByHour(hour);
  284. if (byHours.isEmpty()) {
  285. for (PointInfo point : entity) {
  286. StationInfoHour info = new StationInfoHour();
  287. info.setStationId(point.getStationId());
  288. info.setRecordDate(DateUtil.beginOfHour(hour));
  289. info.setRfdl(section.get(point.getPointKey()).getValue());
  290. byHours.add(info);
  291. }
  292. } else {
  293. Map<String, String> collect = entity.stream().collect(Collectors.toMap(PointInfo::getStationId, PointInfo::getPointKey));
  294. for (StationInfoHour byHour : byHours) {
  295. byHour.setRfdl(section.get(collect.get(byHour.getStationId())).getValue());
  296. }
  297. }
  298. stationInfoHourService.saveOrUpdateBatch(byHours);
  299. }
  300. public void calcLineDjlRfdl(Date date) {
  301. //date当天零点
  302. DateTime time = DateUtil.beginOfDay(date);
  303. //date昨天零点
  304. DateTime time0 = DateUtil.offsetDay(time, -1);
  305. //date当天零点加三分钟
  306. DateTime time2 = DateUtil.offsetMinute(time, 3);
  307. //date昨天零点加三分钟
  308. DateTime time1 = DateUtil.offsetMinute(time0, 3);
  309. PointInfo pi = new PointInfo();
  310. pi.setUniformCode("Z-ZXYG-JX");
  311. pi.setLineId("all");
  312. List<PointInfo> entity = pointService.getByEntity(pi);
  313. // entity = entity.stream().filter(e -> e.getStationId().equals("GJNY_SXGS_ZZ_FDC_STA")).collect(Collectors.toList());
  314. String keys = pointInfos2Keys(entity);
  315. Map<String, PointData> latest2 = adapter.getHistorySection(goldenUri(), keys, time2.getTime());
  316. Map<String, PointData> latest1 = adapter.getHistorySection(goldenUri(), keys, time1.getTime());
  317. List<LineInfoDay> byDate = getLineinfoByDate(time0.toJdkDate());
  318. Map<String, LineInfoDay> collect = new HashMap<>();
  319. if (!byDate.isEmpty()) {
  320. collect = byDate.stream().collect(Collectors.toMap(LineInfoDay::getLineId, Function.identity()));
  321. }
  322. List<LineInfoDay> list = new ArrayList<>();
  323. for (PointInfo info : entity) {
  324. LineInfoDay day;
  325. if (byDate.isEmpty()) {
  326. day = new LineInfoDay();
  327. day.setStationId(info.getStationId());
  328. day.setProjectId(info.getProjectId());
  329. day.setLineId(info.getLineId());
  330. day.setRecordDate(time0.toJdkDate());
  331. } else {
  332. day = collect.get(info.getLineId());
  333. }
  334. day.setRfdl((int) ((latest2.get(info.getPointKey()).getValue() -
  335. latest1.get(info.getPointKey()).getValue()) * info.getCoef()));
  336. if (day.getRfdl() <= 0 || day.getRfdl() > 30000000) {
  337. day.setRfdl(0);
  338. }
  339. list.add(day);
  340. }
  341. lineInfoDayService.saveOrUpdateBatch(list);
  342. }
  343. public void calcStationZhcydl(Date date) {
  344. //date当天零点
  345. DateTime time = DateUtil.beginOfDay(date);
  346. //date昨天零点
  347. DateTime time0 = DateUtil.offsetDay(time, -1);
  348. QueryWrapper<LineInfoDay> wrapper = new QueryWrapper<>();
  349. wrapper.select("sum(rfdl) rfdl,station_id").eq("record_date", time0.toJdkDate())
  350. .groupBy("station_id");
  351. List<LineInfoDay> list = lineInfoDayService.list(wrapper);
  352. List<StationInfoDay> byDate = getStationinfoByDate(time0.toJdkDate());
  353. // list = list.stream().filter(b -> b.getStationId().equals("GJNY_SXGS_ZZ_FDC_STA")).collect(Collectors.toList());
  354. Map<String, StationInfoDay> collect = new HashMap<>();
  355. if (!byDate.isEmpty()) {
  356. collect = byDate.stream().collect(Collectors.toMap(StationInfoDay::getStationId, Function.identity()));
  357. }
  358. StationInfoDay infoDay;
  359. List<StationInfoDay> infoDays = new ArrayList<>();
  360. for (LineInfoDay day : list) {
  361. if (byDate.isEmpty()) {
  362. infoDay = new StationInfoDay();
  363. infoDay.setStationId(day.getStationId());
  364. infoDay.setRecordDate(time0.toJdkDate());
  365. } else {
  366. infoDay = collect.get(day.getStationId());
  367. }
  368. infoDay.setRfdl(day.getRfdl());
  369. infoDay.setZhcydl(infoDay.getRfdl() + infoDay.getGwdl() - infoDay.getSwdl());
  370. infoDays.add(infoDay);
  371. }
  372. stationInfoDayService.saveOrUpdateBatch(infoDays);
  373. }
  374. public void calcStationHourPJGL(Date hour) {
  375. PointInfo pointInfo = new PointInfo();
  376. pointInfo.setInstitutionType("station");
  377. pointInfo.setUniformCode("SSZGL");
  378. List<PointInfo> entity = pointService.getByEntity(pointInfo);
  379. List<StationInfoHour> byHours = getStationinfoByHour(hour);
  380. Map<String, StationInfoHour> collect = new HashMap<>();
  381. if (!byHours.isEmpty()) {
  382. collect = byHours.stream().collect(
  383. Collectors.toMap(StationInfoHour::getStationId, Function.identity()));
  384. }
  385. DoubleStatData stat;
  386. List<StationInfoHour> byHours2 = new ArrayList<>();
  387. for (PointInfo point : entity) {
  388. stat = adapter.getHistoryStat(taosGoldenUriTest(),
  389. point.getPointKey(), DateUtil.offsetHour(hour, -1).getTime(), hour.getTime());
  390. if (byHours.isEmpty()) {
  391. StationInfoHour info = new StationInfoHour();
  392. info.setStationId(point.getStationId());
  393. info.setRecordDate(DateUtil.beginOfHour(hour));
  394. if (stat == null || stat.getAvg() == null) info.setPjgl(0.0);
  395. info.setPjgl(stat.getAvg().getDoubleValue());
  396. byHours2.add(info);
  397. } else {
  398. collect.get(point.getStationId()).setPjgl(stat.getAvg().getDoubleValue());
  399. }
  400. }
  401. stationInfoHourService.saveOrUpdateBatch(byHours.isEmpty() ? byHours2 : byHours);
  402. }
  403. public void calcStationRfdlFj(Date date) {
  404. //date当天零点
  405. DateTime time = DateUtil.beginOfDay(date);
  406. //date昨天零点
  407. DateTime time0 = DateUtil.offsetDay(time, -1);
  408. QueryWrapper<TurbineInfoDay> wrapper = new QueryWrapper<>();
  409. wrapper.select("sum(rfdl) rfdl,station_id").eq("record_date", time0.toJdkDate())
  410. .groupBy("station_id");
  411. List<TurbineInfoDay> list = turbineInfoDayService.list(wrapper);
  412. List<StationInfoDay> byDate = getStationinfoByDate(time0.toJdkDate());
  413. Map<String, StationInfoDay> collect = new HashMap<>();
  414. if (!byDate.isEmpty()) {
  415. collect = byDate.stream().collect(Collectors.toMap(StationInfoDay::getStationId, Function.identity()));
  416. }
  417. StationInfoDay infoDay;
  418. List<StationInfoDay> infoDays = new ArrayList<>();
  419. for (TurbineInfoDay day : list) {
  420. if (byDate.isEmpty()) {
  421. infoDay = new StationInfoDay();
  422. infoDay.setStationId(day.getStationId());
  423. infoDay.setRecordDate(time0.toJdkDate());
  424. } else {
  425. infoDay = collect.get(day.getStationId());
  426. }
  427. infoDay.setRfdlFj(day.getRfdl());
  428. infoDays.add(infoDay);
  429. }
  430. stationInfoDayService.saveOrUpdateBatch(infoDays);
  431. }
  432. public void calcTurbineRFDL(Date date) {
  433. //date当天零点
  434. DateTime time = DateUtil.beginOfDay(date);
  435. //date昨天零点
  436. DateTime time0 = DateUtil.offsetDay(time, -1);
  437. //date当天零点加三分钟
  438. DateTime time2 = DateUtil.offsetMinute(time, 3);
  439. //date昨天零点加三分钟
  440. DateTime time1 = DateUtil.offsetMinute(time0, 3);
  441. PointInfo pi = new PointInfo();
  442. pi.setUniformCode("AI121");
  443. pi.setInstitutionType("turbine");
  444. List<PointInfo> entity = getEntity("AI121", "turbine");
  445. Map<String, String> turMap = entity.stream().collect(Collectors.toMap(PointInfo::getTurbineId, PointInfo::getPointKey));
  446. Map<String, String> fsMap = getEntityMap("AI066", "turbine");
  447. String keys = pointInfos2Keys(entity);
  448. Map<String, PointData> swdlL2 = adapter.getHistorySection(goldenUri(), keys, time2.getTime());
  449. Map<String, PointData> swdlL1 = adapter.getHistorySection(goldenUri(), keys, time1.getTime());
  450. Map<String, TurbineInfoDay> byDate = getTurbineinfoMap(time0.toJdkDate(), entity);
  451. TurbineInfoDay infoDay;
  452. String turbineId, key;
  453. List<TurbineInfoDay> infoDays = new ArrayList<>();
  454. DoubleStatData stat;
  455. for (PointInfo info : entity) {
  456. turbineId = info.getTurbineId();
  457. infoDay = byDate.get(turbineId);
  458. key = turMap.get(turbineId);
  459. double v = (swdlL2.get(key).getValue() - swdlL1.get(key).getValue()) * info.getCoef();
  460. if (v < 0 || v > 1000000) {
  461. v = 0;
  462. } else if (v > 30000) {
  463. v = v / (v % 30000 + 3);
  464. }
  465. infoDay.setRfdl(v);
  466. stat = adapter.getHistoryStat(goldenUri(), fsMap.get(turbineId), time1.getTime(), time2.getTime());
  467. if (stat == null) {
  468. infoDay.setPjfs(0.0);
  469. } else {
  470. infoDay.setPjfs(stat.getAvg().getValue());
  471. }
  472. infoDays.add(infoDay);
  473. }
  474. turbineInfoDayService.saveOrUpdateBatch(infoDays);
  475. }
  476. /*public void calcTurbine5s(Date date) {
  477. //date当天零点
  478. DateTime time = DateUtil.beginOfDay(date);
  479. //date昨天零点
  480. DateTime time0 = DateUtil.offsetDay(time, -1);
  481. //状态
  482. List<PointInfo> entityZt = getEntity("MXZT", "turbine");
  483. //功率
  484. Map<String, String> collectGl = getEntityMap("AI114", "turbine");
  485. //风速
  486. Map<String, String> collectFs = getEntityMap("AI066", "turbine");
  487. Map<String, TurbineInfoDay> dayMap = getTurbineinfoMap(time0.toJdkDate(), entityZt);
  488. String tbId;
  489. TurbineInfoDay infoDay;
  490. List<TurbineInfoDay> infoDays = new ArrayList<>();
  491. for (PointInfo info : entityZt) {
  492. tbId = info.getTurbineId();
  493. List<PointData> snapZt = adapter.getHistorySnap(taosUri(), info.getPointKey(), time0.getTime(), time.getTime(), 60);
  494. //0 待机,1 手动停机,2 正常发电,3 发电降出力,4 故障,5 故障受累,6 检修,7 检修受累,8 限电降出力,
  495. // 9 限电停机,10 电网受累,11 环境受累,12 通讯中断,13 设备离线
  496. List<DoubleStatData> statGl = adapter.getHistoryStat2(goldenUri(), collectGl.get(tbId), time0.getTime(), time.getTime(), 60);
  497. List<DoubleStatData> statFs = adapter.getHistoryStat2(goldenUri(), collectFs.get(tbId), time0.getTime(), time.getTime(), 60);
  498. List<PointData> glList = statGl.stream().map(DoubleStatData::getAvg).collect(Collectors.toList());
  499. List<PointData> fsList = statFs.stream().map(DoubleStatData::getAvg)
  500. .peek(fs -> {
  501. if (fs.getValue() < 25) {
  502. fs.setDoubleValue(NumberUtil.round(fs.getValue(), 2).doubleValue());
  503. } else {
  504. fs.setDoubleValue(25.0);
  505. }
  506. }).collect(Collectors.toList());
  507. // Map<String, Double> loss = getTurbinePowerLoss(snapZt, fsList, tbId, glList);
  508. Map<String, Double> loss = new HashMap<>();
  509. infoDay = dayMap.get(tbId);
  510. infoDay.setJhjxss(loss.get("jxss") + loss.get("sdtj"));
  511. infoDay.setFjhjxss(loss.get("gzss") + loss.get("djss") + loss.get("fdjcl"));
  512. infoDay.setXdss(loss.get("xdjcl") + loss.get("xdtj"));
  513. infoDay.setSlss(0.0);
  514. if (infoDay.getRfdl() == 0) {
  515. infoDay.setXnss(0.0);
  516. } else {
  517. infoDay.setXnss(loss.get("xnss") * 0.1);
  518. }
  519. infoDay.setLlfdl(infoDay.getRfdl() + infoDay.getJhjxss() + infoDay.getFjhjxss() +
  520. infoDay.getXdss() + infoDay.getSlss() + infoDay.getXnss());
  521. infoDays.add(infoDay);
  522. }
  523. turbineInfoDayService.saveOrUpdateBatch(infoDays);
  524. }*/
  525. //此处加公式
  526. public void calcStationSspjfs() {
  527. List<PointInfo> fsEntity = getEntity("AI066", "turbine");
  528. String keys = pointInfos2Keys(fsEntity);
  529. Map<String, PointData> latest = adapter.getLatest(goldenUri(), keys);
  530. Map<String, List<String>> listMap = fsEntity.stream().collect(Collectors.groupingBy(PointInfo::getStationId, Collectors.mapping(PointInfo::getPointKey, Collectors.toList())));
  531. Map<String, String> entityMap = getEntityMap("SSPJFS", "station");
  532. List<PointData> tsData = new ArrayList<>();
  533. listMap.forEach((stId, keyList) -> {
  534. double v = keyList.stream().mapToDouble(key -> latest.get(key).getValue()).average().orElse(0.0);
  535. PointData data = new PointData();
  536. data.setTs(System.currentTimeMillis());
  537. data.setTagName(entityMap.get(stId));
  538. data.setDoubleValue(v);
  539. tsData.add(data);
  540. });
  541. adapter.writeHistoryBatch(taosUri(), tsData);
  542. }
  543. public void calcTurbine5s2(Date date) {
  544. //date当天零点
  545. DateTime time = DateUtil.beginOfDay(date);
  546. //date昨天零点
  547. DateTime time0 = DateUtil.offsetDay(time, -1);
  548. //风速
  549. List<PointInfo> entityFs = getEntity("AI066", "turbine");
  550. //功率
  551. List<PointInfo> entityGl = getEntity("AI114", "turbine");
  552. getSnapDataByEntity(entityFs, time0, time, 15);
  553. getSnapDataByEntity(entityGl, time0, time, 15);
  554. List<PointInfo> ztMapMap = calcTurbineZt(time0, time, entityFs, entityGl);
  555. Map<String, Map<String, PointInfo>> ztMapMapInfos = ztMapMap.stream().collect(
  556. Collectors.groupingBy(PointInfo::getStationId, Collectors.toMap(PointInfo::getTurbineId, Function.identity())));
  557. Map<String, TurbineInfoDay> dayMap = getTurbineinfoMap(time0, entityFs);
  558. List<TurbineInfoDay> infoDays = new ArrayList<>();
  559. Map<String, Map<String, List<PointData>>> fsMapMap = entityFs.stream().collect(Collectors.groupingBy(PointInfo::getStationId,
  560. Collectors.toMap(PointInfo::getTurbineId, PointInfo::getPointDatas)));
  561. Map<String, Map<String, List<PointData>>> glMapMap = entityGl.stream().collect(Collectors.groupingBy(PointInfo::getStationId,
  562. Collectors.toMap(PointInfo::getTurbineId, PointInfo::getPointDatas)));
  563. ztMapMapInfos.forEach((stId, ztMapInfo) -> ztMapInfo.forEach((wtId, ztInfo) -> {
  564. //0 待机,1 手动停机,2 正常发电,3 发电降出力,4 故障,5 故障受累,6 检修,7 检修受累,8 限电降出力,
  565. // 9 限电停机,10 电网受累,11 环境受累,12 通讯中断,13 设备离线
  566. TurbineInfoDay infoDay = dayMap.get(wtId);
  567. Map<String, Double> loss = getTurbinePowerLoss(infoDay.getRfdl(), ztMapMapInfos.get(stId).get(wtId), fsMapMap.get(stId).get(wtId), glMapMap.get(stId).get(wtId));
  568. infoDay.setJhjxss(loss.get("jxss") + loss.get("sdtj"));
  569. infoDay.setFjhjxss(loss.get("gzss") + loss.get("djss") + loss.get("fdjcl"));
  570. infoDay.setXdss(loss.get("xdjcl") + loss.get("xdtj"));
  571. infoDay.setSlss(0.0);
  572. if (infoDay.getRfdl() == 0) {
  573. infoDay.setXnss(0.0);
  574. } else {
  575. infoDay.setXnss(loss.get("xnss"));
  576. }
  577. infoDay.setLlfdl(infoDay.getRfdl() + infoDay.getJhjxss() + infoDay.getFjhjxss() +
  578. infoDay.getXdss() + infoDay.getSlss() + infoDay.getXnss());
  579. infoDays.add(infoDay);
  580. }));
  581. turbineInfoDayService.saveOrUpdateBatch(infoDays);
  582. }
  583. public List<PointInfo> calcTurbineZt(Date start, Date end, List<PointInfo> fsInfos, List<PointInfo> glInfos) {
  584. List<PointInfo> turbineZt = calcTurbineDizt(start, end, 15);
  585. List<PointInfo> turbineAizt = calcTurbineAizt(start, end, 15);
  586. turbineZt.addAll(turbineAizt);
  587. Map<String, Map<String, PointInfo>> ztMapMap = turbineZt.stream().collect(Collectors.groupingBy(PointInfo::getStationId,
  588. Collectors.toMap(PointInfo::getTurbineId, Function.identity())));
  589. //AGC
  590. List<PointInfo> entityAgc = getEntity("AGC002", "booster");
  591. getSnapDataByEntity(entityAgc, start, end, 15);
  592. //出线
  593. List<PointInfo> entityCx = getEntity("AGC001", "booster");
  594. getSnapDataByEntity(entityCx, start, end, 15);
  595. //叶轮转速给定
  596. List<PointInfo> entityYlzsgd = getEntity("AI110", "turbine");
  597. getSnapDataByEntity(entityYlzsgd, start, end, 15);
  598. Map<String, PointInfo> collectAgc = entityAgc.stream().collect(Collectors.toMap(PointInfo::getStationId, Function.identity()));
  599. Map<String, PointInfo> collectCx = entityCx.stream().collect(Collectors.toMap(PointInfo::getStationId, Function.identity()));
  600. Map<String, Map<String, List<PointData>>> collectYlzsgd = entityYlzsgd.stream().collect(Collectors.groupingBy(PointInfo::getStationId,
  601. Collectors.toMap(PointInfo::getTurbineId, PointInfo::getPointDatas)));
  602. Map<String, Map<String, PointInfo>> fsMapMap = fsInfos.stream().collect(Collectors.groupingBy(PointInfo::getStationId,
  603. Collectors.toMap(PointInfo::getTurbineId, Function.identity())));
  604. Map<String, Map<String, List<PointData>>> glMapMap = glInfos.stream().collect(Collectors.groupingBy(PointInfo::getStationId,
  605. Collectors.toMap(PointInfo::getTurbineId, PointInfo::getPointDatas)));
  606. ztMapMap.forEach((stId, ztMap) -> {
  607. PointInfo agcInfo = collectAgc.get(stId);
  608. PointInfo cxInfo = collectCx.get(stId);
  609. Map<String, PointInfo> fsMap = fsMapMap.get(stId);
  610. Map<String, List<PointData>> pdsZsglMap = new HashMap<>();
  611. ztMap.forEach((wtId, zt) -> {
  612. List<PointData> peek = fsMap.get(wtId).getPointDatas().stream().peek(pd -> pd.setDoubleValue(NumberUtil.round(pd.getValue() > 25 ? 25.0
  613. : pd.getValue(), 2).doubleValue())).collect(Collectors.toList());
  614. fsMap.get(wtId).setPointDatas(peek);
  615. List<PointData> zsglSnap = fsMap.get(wtId).getPointDatas().stream().map(pd -> {
  616. Double v = CalcCache.fitcoef.get(wtId).get(pd.getValue());
  617. return new PointData(pd.getTs(), v == null ? 0 : v);
  618. }).collect(Collectors.toList());
  619. pdsZsglMap.put(wtId, zsglSnap);
  620. });
  621. List<PointData> agcc = agcInfo.getPointDatas().stream().peek(agc -> {
  622. agc.setDoubleValue(agc.getValue() * agcInfo.getCoef());
  623. agc.setLongValue(0L);
  624. }).collect(Collectors.toList());
  625. agcInfo.setPointDatas(agcc);
  626. List<PointData> cxc = cxInfo.getPointDatas().stream().peek(cx -> {
  627. cx.setDoubleValue(cx.getValue() * cxInfo.getCoef());
  628. cx.setLongValue(0L);
  629. }).collect(Collectors.toList());
  630. cxInfo.setPointDatas(cxc);
  631. //风机风速、功率->场站风速、功率
  632. // List<PointData> firstFs = new ArrayList<>();
  633. // List<PointData> firstGl = new ArrayList<>();
  634. List<PointData> firstZsgl = new ArrayList<>();
  635. double v1, v2, v3;
  636. for (int i = 0; i < 5760; i++) {
  637. int finalI = i;
  638. long ts = start.getTime() + i * 15000;
  639. // v1 = fsMap.values().stream().mapToDouble(pds -> pds.getPointDatas().get(finalI).getValue()).average().orElse(0.0);
  640. // v2 = glMap.values().stream().mapToDouble(pds -> pds.getPointDatas().get(finalI).getValue()).sum();
  641. v3 = pdsZsglMap.values().stream().mapToDouble(pds -> pds.get(finalI).getValue()).sum();
  642. // firstFs.add(new PointData(ts, v1));
  643. // firstGl.add(new PointData(ts, v2));
  644. firstZsgl.add(new PointData(ts, v3));
  645. }
  646. List<PointData> czxd = calcStationXd2(agcInfo, cxInfo.getPointDatas(), firstZsgl);
  647. ztMap.forEach((wtId, ztInfo) -> {
  648. List<PointData> fss = fsMap.get(wtId).getPointDatas();
  649. List<PointData> gls = glMapMap.get(stId).get(wtId);
  650. //叶轮转速给定
  651. List<PointData> ylzsgds = collectYlzsgd.get(stId).get(wtId);
  652. calcTurbineXd(ztInfo, gls, fss, ylzsgds, czxd);
  653. });
  654. });
  655. return turbineZt;
  656. }
  657. public void calcTurbineXd(PointInfo ztInfo, List<PointData> gls, List<PointData> fss, List<PointData> ylzsgds, List<PointData> czxd) {
  658. Map<String, EquipmentModel> map = equipmentModelService.map();
  659. Double capacity = map.get(ztInfo.getSpare()).getPowerProduction();
  660. List<PointData> zts = ztInfo.getPointDatas();
  661. for (int i = 0; i < fss.size(); i++) {
  662. double ssgl = gls.get(i).getValue();
  663. double ssfs = fss.get(i).getValue();
  664. double ylzsgd = ylzsgds.get(i).getValue();
  665. if (czxd.get(i).isBooleanValue() && ssgl < capacity * 0.9) {
  666. if (ssfs >= 12) {
  667. zts.get(i).setDoubleValue(8.0);
  668. }
  669. if (ylzsgd != 0 && ylzsgd < 17.3) {//降出力
  670. zts.get(i).setDoubleValue(8.0);
  671. }
  672. }
  673. }
  674. }
  675. //此处加公式
  676. public List<PointData> calcStationXd2(PointInfo agcInfo, List<PointData> cxSnap, List<PointData> zsglSnap) {
  677. String stId = agcInfo.getStationId();
  678. List<PointData> agcSnap = agcInfo.getPointDatas();
  679. List<PointData> zsgl5s = get5Avg(zsglSnap);
  680. List<PointData> agc5s = get5Avg(agcSnap);
  681. List<PointData> cx5s = get5Avg(cxSnap);
  682. //场站限电
  683. List<PointData> czxd = zsglSnap.stream().map(s -> new PointData(s.getTs(), 0.0)).collect(Collectors.toList());
  684. //之前是否限电
  685. boolean iszqxd = false;
  686. for (int i = 0; i < zsglSnap.size(); i++) {
  687. boolean isxd = false;
  688. double zsgl = zsglSnap.get(i).getValue();
  689. double zsgl5 = zsgl5s.get(i).getValue();
  690. double agc = agcSnap.get(i).getValue();
  691. double agc5 = agc5s.get(i).getValue();
  692. double cxgl = cxSnap.get(i).getValue();
  693. double cxgl5 = cx5s.get(i).getValue();
  694. boolean k; //agc一直不变
  695. //第一次初始化复制实时数据到缓存
  696. if (CalcCache.powerCache.get(stId) == null || CalcCache.powerCache.get(stId) != agc) {
  697. CalcCache.powerCache.put(stId, agc);
  698. k = false;
  699. } else {
  700. k = true;
  701. }
  702. if (agc != 0) {
  703. //如果场站限电状态为不限电
  704. if (zsgl5 >= agcInfo.getSpare2() * 40) {
  705. if (agc5 <= cxgl5) {
  706. isxd = true;
  707. } else if (iszqxd) {
  708. if (zsgl / agc > 1.15) {
  709. isxd = true;
  710. } else if (zsgl > agc && agc5 - cxgl5 < 400) {
  711. isxd = true;
  712. }
  713. } else if (zsgl / agc >= 1.2 && agc - cxgl <= 200) {
  714. isxd = true;
  715. }
  716. } else if (k) {
  717. if (zsgl / agc >= 1.1 && agc5 - cxgl5 <= 200) {
  718. isxd = true;
  719. }
  720. }
  721. }
  722. czxd.get(i).setBooleanValue(isxd);
  723. iszqxd = isxd;
  724. }
  725. return czxd;
  726. }
  727. private List<PointData> get5Avg(List<PointData> pointData) {
  728. List<PointData> result = new ArrayList<>();
  729. for (int i = 0; i < pointData.size(); i++) {
  730. PointData data = new PointData();
  731. data.setTs(pointData.get(i).getTs());
  732. double v;
  733. if (i > 4) {
  734. 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;
  735. } else {
  736. v = pointData.get(i).getValue();
  737. }
  738. data.setDoubleValue(v);
  739. result.add(data);
  740. }
  741. return result;
  742. }
  743. public List<PointInfo> calcTurbineAizt(Date start, Date end, int interval) {
  744. List<PointInfo> entityAiZt = getEntity("AI422", "state");
  745. List<StateAi> list = stateAiService.list();
  746. Map<String, Map<Integer, Integer>> collectAi = list.stream().collect(Collectors.groupingBy(
  747. StateAi::getModel, Collectors.toMap(StateAi::getOriginalState, StateAi::getMappingState)));
  748. getSnapDataByEntity(entityAiZt, start, end, interval);
  749. for (PointInfo info : entityAiZt) {
  750. List<PointData> peek = info.getPointDatas().stream().peek(pd -> {
  751. Integer m = collectAi.get(info.getSpare()).get((int) pd.getValue());
  752. if (m == null) m = 2;
  753. pd.setDoubleValue(m);
  754. }).collect(Collectors.toList());
  755. info.setPointDatas(peek);
  756. }
  757. return entityAiZt;
  758. }
  759. public List<PointInfo> calcTurbineDizt(Date start, Date end, int interval) {
  760. List<PointInfo> entity = new ArrayList<>();
  761. List<PointInfo> entity0 = getEntity("MX000", "state");
  762. List<PointInfo> entity1 = getEntity("MX001", "state");
  763. List<PointInfo> entity2 = getEntity("MX002", "state");
  764. List<PointInfo> entity4 = getEntity("MX004", "state");
  765. List<PointInfo> entity6 = getEntity("MX006", "state");
  766. entity.addAll(entity0);
  767. entity.addAll(entity1);
  768. entity.addAll(entity2);
  769. entity.addAll(entity4);
  770. entity.addAll(entity6);
  771. List<String> keys = entity.stream().map(PointInfo::getPointKey).collect(Collectors.toList());
  772. Map<String, PointData> latest = adapter.getHistorySection(goldenUri(), String.join(",", keys), start.getTime());
  773. Map<String, Map<String, PointInfo>> wtUcPis = entity.stream().collect(Collectors.groupingBy(PointInfo::getTurbineId, Collectors.toMap(PointInfo::getUniformCode, Function.identity())));
  774. wtUcPis.forEach((wtId, map) -> {
  775. map.forEach((uc, pi) -> {
  776. List<PointData> raw = adapter.getHistoryRaw(goldenUri(), pi.getPointKey(), start.getTime(), end.getTime());
  777. List<PointData> ds = boolPointDatasFull(latest.get(pi.getPointKey()).isBooleanValue(), raw, start.getTime(), end.getTime(), interval);
  778. pi.setPointDatas(ds);
  779. ThreadUtil.sleep(20);
  780. });
  781. List<PointData> mx000 = map.get("MX000").getPointDatas();
  782. PointData data0;
  783. for (int i = 0; i < mx000.size(); i++) {
  784. data0 = mx000.get(i);
  785. if (data0.getValue() == 1.0) {
  786. data0.setDoubleValue(0.0);
  787. data0.setBooleanValue(false);
  788. } else if (map.get("MX001").getPointDatas().get(i).getValue() == 1.0) {
  789. data0.setDoubleValue(1.0);
  790. } else if (map.get("MX002").getPointDatas().get(i).getValue() == 1.0) {
  791. data0.setDoubleValue(2.0);
  792. } else if (map.get("MX004").getPointDatas().get(i).getValue() == 1.0) {
  793. data0.setDoubleValue(4.0);
  794. } else if (map.get("MX006").getPointDatas().get(i).getValue() == 1.0) {
  795. data0.setDoubleValue(6.0);
  796. } else {
  797. // System.out.println(wtId + data0.getTs() + ":di无对应状态!");
  798. data0.setDoubleValue(2.0);
  799. }
  800. }
  801. map.get("MX001").setPointDatas(null);
  802. map.get("MX002").setPointDatas(null);
  803. map.get("MX004").setPointDatas(null);
  804. map.get("MX006").setPointDatas(null);
  805. });
  806. return entity0;
  807. }
  808. public PointInfo calcTurbineAizt2(PointInfo entityAiZt, Map<String, Map<Integer, Integer>> collectAi, Date start, Date end, int interval) {
  809. if (entityAiZt == null) return null;
  810. getSnapDataByEntity(entityAiZt, start, end, interval);
  811. Map<Integer, Integer> integerMap = collectAi.get(entityAiZt.getSpare());
  812. List<PointData> data = entityAiZt.getPointDatas().stream().peek(pd -> {
  813. Integer m = integerMap.get((int) pd.getValue());
  814. if (m == null) m = 2;
  815. pd.setDoubleValue(m);
  816. }).collect(Collectors.toList());
  817. entityAiZt.setPointDatas(data);
  818. return entityAiZt;
  819. }
  820. public PointInfo calcTurbineDizt2(Map<String, PointInfo> wtucpi, Map<String, PointData> latest, Date start, Date end, int interval) {
  821. if (wtucpi == null) return null;
  822. wtucpi.forEach((uc, pi) -> {
  823. List<PointData> raw = adapter.getHistoryRaw(goldenUri(), pi.getPointKey(), start.getTime(), end.getTime());
  824. List<PointData> ds = boolPointDatasFull(latest.get(pi.getPointKey()).isBooleanValue(), raw, start.getTime(), end.getTime(), interval);
  825. pi.setPointDatas(ds);
  826. ThreadUtil.sleep(20);
  827. });
  828. PointInfo mx000 = wtucpi.get("MX000");
  829. PointData data0;
  830. for (int i = 0; i < mx000.getPointDatas().size(); i++) {
  831. data0 = mx000.getPointDatas().get(i);
  832. if (data0.getValue() == 1.0) {
  833. data0.setDoubleValue(0.0);
  834. data0.setBooleanValue(false);
  835. } else if (wtucpi.get("MX001").getPointDatas().get(i).getValue() == 1.0) {
  836. data0.setDoubleValue(1.0);
  837. } else if (wtucpi.get("MX002").getPointDatas().get(i).getValue() == 1.0) {
  838. data0.setDoubleValue(2.0);
  839. } else if (wtucpi.get("MX004").getPointDatas().get(i).getValue() == 1.0) {
  840. data0.setDoubleValue(4.0);
  841. } else if (wtucpi.get("MX006").getPointDatas().get(i).getValue() == 1.0) {
  842. data0.setDoubleValue(6.0);
  843. } else {
  844. System.out.println(mx000.getTurbineId() + data0.getTs() + ":di无对应状态!");
  845. data0.setDoubleValue(2.0);
  846. }
  847. }
  848. wtucpi.get("MX001").setPointDatas(null);
  849. wtucpi.get("MX002").setPointDatas(null);
  850. wtucpi.get("MX004").setPointDatas(null);
  851. wtucpi.get("MX006").setPointDatas(null);
  852. return mx000;
  853. }
  854. public List<PointData> boolPointDatasFull(boolean v, List<PointData> data, long startTime, long endTime, int interval) {
  855. int j = 0, m = 0;
  856. List<PointData> data2 = new ArrayList<>();
  857. for (long i = startTime; i < endTime; i += interval * 1000L) {
  858. PointData dt = new PointData();
  859. dt.setTs(i);
  860. data2.add(dt);
  861. //超出范围
  862. if (j >= data.size() - 1) continue;
  863. if (i < data.get(j).getTs()) {
  864. if (m == 0) {
  865. dt.setBooleanValue(v);
  866. } else {
  867. dt.setBooleanValue(data.get(j - 1).isBooleanValue());
  868. }
  869. } else {
  870. dt.setBooleanValue(data.get(j).isBooleanValue());
  871. j++;
  872. m++;
  873. }
  874. }
  875. return data2;
  876. }
  877. public void calcGlqxnh(Date start, Date end) {
  878. //风速
  879. List<PointInfo> entityFs = getEntity("AI066", "turbine");
  880. //功率
  881. List<PointInfo> entityGl = getEntity("AI114", "turbine");
  882. Map<String, PointInfo> glMap = entityGl.stream().collect(Collectors.toMap(PointInfo::getTurbineId, Function.identity()));
  883. Map<String, EquipmentModel> map = equipmentModelService.map();
  884. List<PointInfo> entity = new ArrayList<>();
  885. List<PointInfo> entity0 = getEntity("MX000", "state");
  886. List<PointInfo> entity1 = getEntity("MX001", "state");
  887. List<PointInfo> entity2 = getEntity("MX002", "state");
  888. List<PointInfo> entity4 = getEntity("MX004", "state");
  889. List<PointInfo> entity6 = getEntity("MX006", "state");
  890. entity.addAll(entity0);
  891. entity.addAll(entity1);
  892. entity.addAll(entity2);
  893. entity.addAll(entity4);
  894. entity.addAll(entity6);
  895. List<String> keys = entity.stream().map(PointInfo::getPointKey).collect(Collectors.toList());
  896. Map<String, PointData> latest = adapter.getHistorySection(goldenUri(), String.join(",", keys), start.getTime());
  897. Map<String, Map<String, PointInfo>> wtUcPis = entity.stream().collect(Collectors.groupingBy(PointInfo::getTurbineId, Collectors.toMap(PointInfo::getUniformCode, Function.identity())));
  898. List<PointInfo> entityAiZt = getEntity("AI422", "state");
  899. Map<String, PointInfo> aiztMap = entityAiZt.stream().collect(Collectors.toMap(PointInfo::getTurbineId, Function.identity()));
  900. List<StateAi> list = stateAiService.list();
  901. Map<String, Map<Integer, Integer>> collectAi = list.stream().collect(Collectors.groupingBy(
  902. StateAi::getModel, Collectors.toMap(StateAi::getOriginalState, StateAi::getMappingState)));
  903. // Map<String, Map<Double,Double>> fitMap = new HashMap<>();
  904. for (PointInfo ef : entityFs) {
  905. String wtId = ef.getTurbineId();
  906. PointInfo glInfo = glMap.get(wtId);
  907. getSnapDataByEntity(ef, start, end, 60);
  908. getSnapDataByEntity(glInfo, start, end, 60);
  909. List<PointData> peek = ef.getPointDatas().stream().peek(pd -> pd.setDoubleValue(
  910. NumberUtil.round(pd.getValue(), 2).doubleValue())).collect(Collectors.toList());
  911. ef.setPointDatas(peek);
  912. PointInfo turbineZt = calcTurbineDizt2(wtUcPis.get(wtId), latest, start, end, 60);
  913. PointInfo turbineAizt = calcTurbineAizt2(aiztMap.get(wtId), collectAi, start, end, 60);
  914. Map<Double, Double> fit = new TreeMap<>();
  915. if (turbineZt != null) {
  916. double capacity = map.get(turbineZt.getSpare()).getPowerProduction();
  917. fit = dataProcessAndFit(ef, glInfo, turbineZt, CalcCache.bzgl.get(turbineZt.getSpare()), capacity);
  918. // fitMap.put(wtId, fit);
  919. turbineZt.setPointDatas(null);
  920. }
  921. if (turbineAizt != null) {
  922. double capacity = map.get(turbineAizt.getSpare()).getPowerProduction();
  923. fit = dataProcessAndFit(ef, glInfo, turbineAizt, CalcCache.bzgl.get(turbineAizt.getSpare()), capacity);
  924. // fitMap.put(wtId, fit);
  925. turbineAizt.setPointDatas(null);
  926. }
  927. ef.setPointDatas(null);
  928. glInfo.setPointDatas(null);
  929. stringRedisTemplate.opsForValue().set("edfs:" + end.getMonth() + ":" + wtId, JSON.toJSONString(fit));
  930. }
  931. System.out.println("苹果天涯");
  932. // stringRedisTemplate.opsForValue().set("glqxnh", JSON.toJSONString(fitMap));
  933. }
  934. public Map<Double, Double> dataProcessAndFit(PointInfo fsPI, PointInfo glPI, PointInfo ztPI, Map<Double, Double> modelPowerMap, double capacity) {
  935. System.out.println("开始拟合");
  936. List<PointData> fsPds = fsPI.getPointDatas();
  937. List<PointData> glPds = glPI.getPointDatas();
  938. List<PointData> ztPds = ztPI.getPointDatas();
  939. WeightedObservedPoints points = new WeightedObservedPoints();
  940. //数据过滤 0正常,1过滤掉
  941. if (fsPds.size() != glPds.size() || glPds.size() != ztPds.size()) return new HashMap<>();
  942. Double bzv;
  943. for (int i = 0; i < ztPds.size(); i++) {
  944. double fs = fsPds.get(i).getValue();
  945. double gl = glPds.get(i).getValue();
  946. // 过滤非并网值 风机状态不等于2
  947. if (ztPds.get(i).getValue() != 2.0) {
  948. //
  949. } else if (fs < 0 || fs > 25 || gl < 0 || gl > capacity * 1.05) {// 按给定风速功率过滤
  950. //
  951. } else {
  952. bzv = modelPowerMap.get(fs);
  953. if (bzv == null || bzv == 0.0) {
  954. points.add(fs, gl);
  955. continue;
  956. }
  957. double k = gl / bzv; //功率曲线偏差: 保证功率/实际功率 k:偏差百分比
  958. if (k < 0.61 || k > 1.25) {
  959. //
  960. } else {
  961. points.add(fs, gl);
  962. }
  963. }
  964. }
  965. Map<Double, Double> glyc = new TreeMap<>();
  966. PolynomialCurveFitter fitter = PolynomialCurveFitter.create(4);
  967. try {
  968. double[] result = fitter.fit(points.toList());
  969. double min = calcMin(result);
  970. System.out.println(ztPI.getTurbineId());
  971. System.out.println(ztPI.getSpare());
  972. System.out.println(min);
  973. double[] max = calcMax(result);
  974. if (min == 0 || max[0] == 0 || max[1] == 0) {
  975. System.out.println();
  976. }
  977. System.out.println(max[0] + "," + max[1]);
  978. double ru;
  979. for (double i = 0; i < 25; i += 0.01) {
  980. i = NumberUtil.round(i, 2).doubleValue();
  981. if (i < 3) {
  982. ru = 0.0;
  983. } else if (i < min) {
  984. ru = modelPowerMap.get(i);
  985. } else if (i < max[0]) {
  986. ru = calcPoly(i, result);
  987. } else {
  988. ru = max[1];
  989. }
  990. glyc.put(i, NumberUtil.round(ru, 2).doubleValue());
  991. }
  992. } catch (Exception e) {
  993. System.out.println(ztPI.getTurbineId());
  994. System.out.println(ztPI.getSpare());
  995. System.out.println(e.getMessage());
  996. }
  997. System.out.println("拟合完成" + glyc);
  998. return glyc;
  999. }
  1000. public double calcMin(double[] coef) {
  1001. double minX = 0, minY = 9000;
  1002. for (double x = 0; x < 8; x += 0.01) {
  1003. double y = calcPoly(x, coef);
  1004. if (y < minY) {
  1005. minY = y;
  1006. minX = x;
  1007. }
  1008. }
  1009. return minX;
  1010. }
  1011. public double[] calcMax(double[] coef) {
  1012. double[] a = new double[2];
  1013. for (double x = 8; x < 16; x += 0.01) {
  1014. double y = calcPoly(x, coef);
  1015. if (y > a[1]) {
  1016. a[0] = x;
  1017. a[1] = y;
  1018. }
  1019. }
  1020. return a;
  1021. }
  1022. public void printResult(double[] r, double accuracy) {
  1023. for (double index = 0; index < 25; index += accuracy) {
  1024. System.out.println("[" + index + "," + calcPoly(index, r) + "],");
  1025. }
  1026. }
  1027. public void getSnapDataByEntity(List<PointInfo> entity, URI uri, Date start, Date end, int interval) {
  1028. for (PointInfo info : entity) {
  1029. List<PointData> snap = adapter.getHistorySnap(uri, info.getPointKey(), start.getTime(), end.getTime(), interval);
  1030. ThreadUtil.sleep(20);
  1031. info.setPointDatas(snap);
  1032. }
  1033. }
  1034. public void getSnapDataByEntity(PointInfo entity, URI uri, Date start, Date end, int interval) {
  1035. getSnapDataByEntity(Collections.singletonList(entity), uri, start, end, interval);
  1036. }
  1037. public void getSnapDataByEntity(List<PointInfo> entity, Date start, Date end, int interval) {
  1038. getSnapDataByEntity(entity, goldenUri(), start, end, interval);
  1039. }
  1040. public void getSnapDataByEntity(PointInfo entity, Date start, Date end, int interval) {
  1041. getSnapDataByEntity(entity, goldenUri(), start, end, interval);
  1042. }
  1043. public double calcPoly(String wtId, double value) {
  1044. return CalcCache.fitcoef.get(wtId).get(value);
  1045. }
  1046. public double calcPoly(double x, double[] factor) {
  1047. double y = 0;
  1048. for (int deg = 0; deg < factor.length; deg++) {
  1049. y += Math.pow(x, deg) * factor[deg];
  1050. }
  1051. return y;
  1052. }
  1053. //切入切出风速
  1054. public void cutInWindSpeed() {
  1055. //date当天零点
  1056. DateTime timeNow00 = DateUtil.beginOfDay(new Date());
  1057. //date昨天零点
  1058. DateTime timeBegin00 = DateUtil.offsetDay(timeNow00, -1);
  1059. //上个月
  1060. DateTime timeBegin2 = DateUtil.offsetMonth(timeBegin00, -1);
  1061. //上个月第一天
  1062. DateTime timeBegin1 = DateUtil.beginOfMonth(timeBegin2);
  1063. //上个月最后一天
  1064. DateTime timeEnd2 = DateUtil.endOfMonth(timeBegin2);
  1065. //上个月天数
  1066. int days = (int) DateUtil.betweenDay(timeBegin1, timeEnd2, false);
  1067. //遍历天数
  1068. for (int i = 1; i <= days; i++) {
  1069. DateTime timeNow = DateUtil.offsetDay(timeBegin1, i);
  1070. DateTime timeBegin = DateUtil.offsetDay(timeBegin1, i - 1);
  1071. List<PointInfo> turbineZt = calcTurbineAizt(timeBegin, timeNow, 60);
  1072. List<PointInfo> turbineZtDI = calcTurbineDizt(timeBegin, timeNow, 60);
  1073. turbineZt.addAll(turbineZtDI);
  1074. //所有风机的风速测点
  1075. QueryWrapper<PointInfo> queryWrapper = new QueryWrapper<>();
  1076. queryWrapper.eq("uniform_code", "AI066");
  1077. List<PointInfo> pointInfos = getEntity("AI066", "turbine");
  1078. // List<PointInfo> pointInfos = pointInfo.selectList(queryWrapper);
  1079. //定义切入风速map集合
  1080. ConcurrentHashMap<String, ConcurrentHashMap<String, Double>> mapIn = new ConcurrentHashMap<>();
  1081. //定义切出风速集合
  1082. ConcurrentHashMap<String, ConcurrentHashMap<String, Double>> mapOut = new ConcurrentHashMap<>();
  1083. //插入集合
  1084. List<TurbineInfoDay> interest = new ArrayList<>();
  1085. //更新集合
  1086. List<TurbineInfoDay> update = new ArrayList<>();
  1087. //遍历每台风机,取出每台风机的pointDatas
  1088. for (PointInfo turbine : turbineZt) {
  1089. mapIn.put(turbine.getTurbineId(), new ConcurrentHashMap<>());
  1090. mapOut.put(turbine.getTurbineId(), new ConcurrentHashMap<>());
  1091. List<PointData> pointDatas = turbine.getPointDatas();
  1092. //风速测点key
  1093. String windSpeedKey = null;
  1094. //遍历pointInfos,找出pointData1的id和pointInfos中相等的,取出pointInfo的pointKey
  1095. for (PointInfo info : pointInfos) {
  1096. if (Objects.equals(turbine.getTurbineId(), info.getTurbineId())) {
  1097. windSpeedKey = info.getPointKey();
  1098. }
  1099. }
  1100. //遍历pointDatas,取出当前状态和后一个状态
  1101. for (int k = 1; k < pointDatas.size(); k++) {
  1102. PointData pointData1 = pointDatas.get(k - 1);
  1103. PointData pointData2 = pointDatas.get(k);
  1104. double value1 = pointData1.getValue();
  1105. double value2 = pointData2.getValue();
  1106. //切入时间
  1107. long ts1;
  1108. //切出时间
  1109. long ts2;
  1110. //如果1的值为0,2的值为2,则为切入状态
  1111. if (value1 == 0 && value2 == 2) {
  1112. ts1 = pointData2.getTs();
  1113. // String stringDate = DateUtil.date(ts1).toString("yyyy-MM-dd HH:mm:ss");
  1114. // System.out.println(turbine.getTurbineId() + "切入" + stringDate);
  1115. //切入时间前5分钟
  1116. long ts11 = ts1 - 300000;
  1117. //根据风速key,从适配器取切入前5分钟的所有风速
  1118. List<PointData> pointDatas1 = adapter.getHistorySnap(goldenUri(), windSpeedKey, ts11, ts1, 10);
  1119. //平均所有风速即为平均切入风速
  1120. double avgCutInWindSpeed = pointDatas1.stream().mapToDouble(PointData::getValue).average().orElse(0);
  1121. // System.out.println(turbine.getTurbineId() + "切入风速" + avgCutInWindSpeed);
  1122. //存入map集合,外层key为turbineId,内层key为ts1,value为平均切入风速
  1123. mapIn.get(turbine.getTurbineId()).put(String.valueOf(ts1), avgCutInWindSpeed);
  1124. }
  1125. //切出
  1126. if (value1 == 2 && value2 != 2) {
  1127. ts2 = pointData2.getTs();
  1128. String stringDate = DateUtil.date(ts2).toString("yyyy-MM-dd HH:mm:ss");
  1129. // System.out.println(turbine.getTurbineId() + "切出" + stringDate);
  1130. //切出时间前5分钟
  1131. long ts22 = ts2 - 300000;
  1132. List<PointData> pointDatas2 = adapter.getHistorySnap(goldenUri(), windSpeedKey, ts22, ts2, 10);
  1133. double avgCutOutWindSpeed = pointDatas2.stream().mapToDouble(PointData::getValue).average().orElse(0);
  1134. // System.out.println(turbine.getTurbineId() + "切出风速" + avgCutOutWindSpeed);
  1135. //存入map集合,外层key为turbineId,内层key为ts2,value为平均切出风速
  1136. mapOut.get(turbine.getTurbineId()).put(String.valueOf(ts2), avgCutOutWindSpeed);
  1137. }
  1138. }
  1139. //遍历map集合,取出每个风机的所有切入风速,算一个平均值
  1140. ConcurrentHashMap<String, Double> mapTurbineValues = mapIn.get(turbine.getTurbineId());
  1141. //平均切入风速
  1142. double avgCutInWindSpeed = 0;
  1143. if (mapTurbineValues != null) {
  1144. AtomicReference<Double> sum = new AtomicReference<>(0.0);
  1145. //遍历mapTurbineValues,V大于5的舍弃,剩余算平均值
  1146. for (String key : mapTurbineValues.keySet()) {
  1147. if (mapTurbineValues.get(key) > 5) {
  1148. mapTurbineValues.remove(key);
  1149. }
  1150. }
  1151. mapTurbineValues.forEach((k1, v) -> {
  1152. sum.updateAndGet(v1 -> v1 + v);
  1153. });
  1154. //如果值为空则置0
  1155. if (sum.get() == 0) {
  1156. avgCutInWindSpeed = 0;
  1157. } else {
  1158. avgCutInWindSpeed = sum.get() / mapTurbineValues.size();
  1159. }
  1160. System.out.println(turbine.getTurbineId() + "切入平均风速" + avgCutInWindSpeed + "时间" + timeBegin);
  1161. }
  1162. //遍历map集合,取出每个风机的所有切出风速,算一个平均值
  1163. ConcurrentHashMap<String, Double> mapTurbineValues2 = mapOut.get(turbine.getTurbineId());
  1164. double avgCutOutWindSpeed;
  1165. if (mapTurbineValues2 != null) {
  1166. AtomicReference<Double> sum = new AtomicReference<>(0.0);
  1167. for (String key : mapTurbineValues2.keySet()) {
  1168. if (mapTurbineValues2.get(key) > 5) {
  1169. mapTurbineValues2.remove(key);
  1170. }
  1171. }
  1172. mapTurbineValues2.forEach((k2, v) -> {
  1173. sum.updateAndGet(v1 -> v1 + v);
  1174. });
  1175. if (sum.get() == 0) {
  1176. avgCutOutWindSpeed = 0;
  1177. } else {
  1178. avgCutOutWindSpeed = sum.get() / mapTurbineValues2.size();
  1179. }
  1180. System.out.println(turbine.getTurbineId() + "切出平均风速" + avgCutOutWindSpeed + "时间" + timeBegin);
  1181. }
  1182. //存入数据库
  1183. String turbineId = turbine.getTurbineId();
  1184. Date jdkDate = timeBegin.toJdkDate();
  1185. System.out.println(turbineId + " " + jdkDate);
  1186. QueryWrapper<TurbineInfoDay> turbineInfoDayQueryWrapper = new QueryWrapper<>();
  1187. turbineInfoDayQueryWrapper.eq("turbine_id", turbine.getTurbineId());
  1188. turbineInfoDayQueryWrapper.eq("record_date", timeBegin.toJdkDate());
  1189. TurbineInfoDay one = turbineInfoDayService.getOne(turbineInfoDayQueryWrapper);
  1190. System.out.println(one);
  1191. if (one == null) {
  1192. TurbineInfoDay turbineInfoDay = new TurbineInfoDay();
  1193. turbineInfoDay.setTurbineId(turbine.getTurbineId());
  1194. turbineInfoDay.setRecordDate(timeBegin.toJdkDate());
  1195. turbineInfoDay.setXfqrfs(avgCutInWindSpeed);
  1196. System.out.println(turbineInfoDay);
  1197. interest.add(turbineInfoDay);
  1198. // turbineInfoDayService.save(turbineInfoDay);
  1199. } else {
  1200. one.setXfqrfs(avgCutInWindSpeed);
  1201. System.out.println(one);
  1202. // turbineInfoDayService.updateById(one);
  1203. update.add(one);
  1204. }
  1205. }
  1206. // //批量插入
  1207. // turbineInfoDayService.saveBatch(interest);
  1208. // //批量更新
  1209. // turbineInfoDayService.updateBatchById(update);
  1210. // //批量插入或更新
  1211. turbineInfoDayService.saveOrUpdateBatch(interest);
  1212. turbineInfoDayService.saveOrUpdateBatch(update);
  1213. }
  1214. }
  1215. //算额定风速
  1216. public void calcGlqxnh_edfs() {
  1217. //date当天零点
  1218. DateTime timeNow00 = DateUtil.beginOfDay(new Date());
  1219. //date昨天零点
  1220. DateTime timeBegin00 = DateUtil.offsetDay(timeNow00, -1);
  1221. //上个月
  1222. DateTime timeBegin2 = DateUtil.offsetMonth(timeBegin00, -1);
  1223. //上个月第一天
  1224. DateTime timeBegin1 = DateUtil.beginOfMonth(timeBegin2);
  1225. //上个月最后一天
  1226. DateTime timeEnd2 = DateUtil.endOfMonth(timeBegin2);
  1227. //上个月天数
  1228. int days = (int) DateUtil.betweenDay(timeBegin1, timeEnd2, false);
  1229. //遍历天数
  1230. for (int i = 1; i <= days; i++) {
  1231. DateTime end = DateUtil.offsetDay(timeBegin1, i);
  1232. DateTime start = DateUtil.offsetDay(timeBegin1, i - 1);
  1233. //风速
  1234. List<PointInfo> entityFs = getEntity("AI066", "turbine");
  1235. //功率
  1236. List<PointInfo> entityGl = getEntity("AI114", "turbine");
  1237. Map<String, PointInfo> glMap = entityGl.stream().collect(Collectors.toMap(PointInfo::getTurbineId, Function.identity()));
  1238. Map<String, EquipmentModel> map = equipmentModelService.map();
  1239. List<PointInfo> entity = new ArrayList<>();
  1240. List<PointInfo> entity0 = getEntity("MX000", "state");
  1241. List<PointInfo> entity1 = getEntity("MX001", "state");
  1242. List<PointInfo> entity2 = getEntity("MX002", "state");
  1243. List<PointInfo> entity4 = getEntity("MX004", "state");
  1244. List<PointInfo> entity6 = getEntity("MX006", "state");
  1245. entity.addAll(entity0);
  1246. entity.addAll(entity1);
  1247. entity.addAll(entity2);
  1248. entity.addAll(entity4);
  1249. entity.addAll(entity6);
  1250. List<String> keys = entity.stream().map(PointInfo::getPointKey).collect(Collectors.toList());
  1251. Map<String, PointData> latest = adapter.getHistorySection(goldenUri(), String.join(",", keys), start.getTime());
  1252. Map<String, Map<String, PointInfo>> wtUcPis = entity.stream().collect(Collectors.groupingBy(PointInfo::getTurbineId, Collectors.toMap(PointInfo::getUniformCode, Function.identity())));
  1253. List<PointInfo> entityAiZt = getEntity("AI422", "state");
  1254. Map<String, PointInfo> aiztMap = entityAiZt.stream().collect(Collectors.toMap(PointInfo::getTurbineId, Function.identity()));
  1255. List<StateAi> list = stateAiService.list();
  1256. Map<String, Map<Integer, Integer>> collectAi = list.stream().collect(Collectors.groupingBy(
  1257. StateAi::getModel, Collectors.toMap(StateAi::getOriginalState, StateAi::getMappingState)));
  1258. // Map<String, Map<Double,Double>> fitMap = new HashMap<>();
  1259. //插入集合
  1260. List<TurbineInfoDay> interest = new ArrayList<>();
  1261. //更新集合
  1262. List<TurbineInfoDay> update = new ArrayList<>();
  1263. for (PointInfo ef : entityFs) {
  1264. String wtId = ef.getTurbineId();
  1265. PointInfo glInfo = glMap.get(wtId);
  1266. getSnapDataByEntity(ef, start, end, 60);
  1267. getSnapDataByEntity(glInfo, start, end, 60);
  1268. List<PointData> peek = ef.getPointDatas().stream().peek(pd -> pd.setDoubleValue(
  1269. NumberUtil.round(pd.getValue(), 2).doubleValue())).collect(Collectors.toList());
  1270. ef.setPointDatas(peek);
  1271. try {
  1272. PointInfo turbineZt = calcTurbineDizt2(wtUcPis.get(wtId), latest, start, end, 60);
  1273. PointInfo turbineAizt = calcTurbineAizt2(aiztMap.get(wtId), collectAi, start, end, 60);
  1274. Map<Double, Double> fit = new TreeMap<>();
  1275. double capacity = 2000;
  1276. if (turbineZt != null) {
  1277. System.out.println("数据处理拟合");
  1278. capacity = map.get(turbineZt.getSpare()).getPowerProduction();
  1279. fit = dataProcessAndFit(ef, glInfo, turbineZt, CalcCache.bzgl.get(turbineZt.getSpare()), capacity);
  1280. // fitMap.put(wtId, fit);
  1281. turbineZt.setPointDatas(null);
  1282. }
  1283. if (turbineAizt != null) {
  1284. System.out.println("数据处理拟合");
  1285. capacity = map.get(turbineAizt.getSpare()).getPowerProduction();
  1286. fit = dataProcessAndFit(ef, glInfo, turbineAizt, CalcCache.bzgl.get(turbineAizt.getSpare()), capacity);
  1287. // fitMap.put(wtId, fit);
  1288. turbineAizt.setPointDatas(null);
  1289. }
  1290. ef.setPointDatas(null);
  1291. glInfo.setPointDatas(null);
  1292. //存redis
  1293. // stringRedisTemplate.opsForValue().set("glqxnh:" + end.getMonth() + ":" + wtId, JSON.toJSONString(fit));
  1294. System.out.println("拟合曲线的大小" + fit.size() + ":" + wtId);
  1295. //符合条件的风速和
  1296. List<Double> listDouble = new ArrayList<>();
  1297. //遍历fit集合,取出值与capacity相等的key
  1298. if (!fit.isEmpty()) {
  1299. for (double key : fit.keySet()) {
  1300. Double v = fit.get(key);
  1301. //如果v-capacity的值小于10大于-10,则把key加入listDouble
  1302. if ((Math.abs(v - capacity) <= 10) && Math.abs(v - capacity) >= -10) {
  1303. listDouble.add(key);
  1304. }
  1305. }
  1306. }
  1307. //取出listDouble的值求平均值
  1308. double agValue = 0;
  1309. if (!listDouble.isEmpty()) {
  1310. for (Double aDouble : listDouble) {
  1311. agValue += aDouble;
  1312. }
  1313. agValue = agValue / listDouble.size();
  1314. }
  1315. System.out.println("wtId:" + wtId + "agValue平均额定风速:" + agValue);
  1316. //存入数据库
  1317. System.out.println(wtId + " " + start);
  1318. QueryWrapper<TurbineInfoDay> turbineInfoDayQueryWrapper = new QueryWrapper<>();
  1319. turbineInfoDayQueryWrapper.eq("turbine_id", wtId);
  1320. turbineInfoDayQueryWrapper.eq("record_date", start);
  1321. TurbineInfoDay one = turbineInfoDayService.getOne(turbineInfoDayQueryWrapper);
  1322. System.out.println(one);
  1323. if (one == null) {
  1324. TurbineInfoDay turbineInfoDay = new TurbineInfoDay();
  1325. turbineInfoDay.setTurbineId(wtId);
  1326. turbineInfoDay.setRecordDate(start);
  1327. turbineInfoDay.setEdfs(agValue);
  1328. System.out.println(turbineInfoDay);
  1329. // turbineInfoDayService.save(turbineInfoDay);
  1330. interest.add(turbineInfoDay);
  1331. } else {
  1332. one.setEdfs(agValue);
  1333. System.out.println(one);
  1334. // turbineInfoDayService.updateById(one);
  1335. update.add(one);
  1336. }
  1337. } catch (Exception e) {
  1338. System.out.println(wtId + " " + start);
  1339. }
  1340. }
  1341. // //批量插入
  1342. // turbineInfoDayService.saveBatch(interest);
  1343. // //批量更新
  1344. // turbineInfoDayService.updateBatchById(update);
  1345. // //批量插入或更新
  1346. turbineInfoDayService.saveOrUpdateBatch(interest);
  1347. turbineInfoDayService.saveOrUpdateBatch(update);
  1348. }
  1349. // stringRedisTemplate.opsForValue().set("glqxnh", JSON.toJSONString(fitMap));
  1350. }
  1351. //环境温度
  1352. public void ambientTemperature() {
  1353. //date当天零点
  1354. DateTime timeNow = DateUtil.beginOfDay(new Date());
  1355. //date昨天零点
  1356. DateTime timeBegin = DateUtil.offsetDay(timeNow, -1);
  1357. //所有风机的温度
  1358. QueryWrapper<PointInfo> queryWrapper = new QueryWrapper<>();
  1359. queryWrapper.eq("uniform_code", "AI072");
  1360. List<PointInfo> pointInfos = getEntity("AI072", "turbine");
  1361. //插入集合
  1362. List<TurbineInfoDay> interest = new ArrayList<>();
  1363. //更新集合
  1364. List<TurbineInfoDay> update = new ArrayList<>();
  1365. //遍历pointInfos
  1366. for (PointInfo turbine : pointInfos) {
  1367. List<PointData> pointDatas = adapter.getHistorySnap(goldenUri(), turbine.getPointKey(), timeBegin.getTime(), timeNow.getTime(), 10);
  1368. //遍历pointDatas,取出数据算平均值
  1369. double avgCutInWindSpeed = 0;
  1370. for (PointData pointData : pointDatas) {
  1371. avgCutInWindSpeed += pointData.getValue();
  1372. }
  1373. avgCutInWindSpeed /= pointDatas.size();
  1374. //存入数据库
  1375. String turbineId = turbine.getTurbineId();
  1376. Date jdkDate = timeBegin.toJdkDate();
  1377. System.out.println(turbineId + " " + jdkDate);
  1378. QueryWrapper<TurbineInfoDay> turbineInfoDayQueryWrapper = new QueryWrapper<>();
  1379. turbineInfoDayQueryWrapper.eq("turbine_id", turbine.getTurbineId());
  1380. turbineInfoDayQueryWrapper.eq("record_date", timeBegin.toJdkDate());
  1381. TurbineInfoDay one = turbineInfoDayService.getOne(turbineInfoDayQueryWrapper);
  1382. System.out.println(one);
  1383. if (one == null) {
  1384. TurbineInfoDay turbineInfoDay = new TurbineInfoDay();
  1385. turbineInfoDay.setTurbineId(turbine.getTurbineId());
  1386. turbineInfoDay.setRecordDate(timeBegin.toJdkDate());
  1387. turbineInfoDay.setHjwd(avgCutInWindSpeed);
  1388. System.out.println(turbineInfoDay);
  1389. interest.add(turbineInfoDay);
  1390. // turbineInfoDayService.save(turbineInfoDay);
  1391. } else {
  1392. one.setHjwd(avgCutInWindSpeed);
  1393. System.out.println(one);
  1394. update.add(one);
  1395. // turbineInfoDayService.updateById(one);
  1396. }
  1397. }
  1398. // //批量插入
  1399. // turbineInfoDayService.saveBatch(interest);
  1400. // //批量更新
  1401. // turbineInfoDayService.updateBatchById(update);
  1402. // //批量插入或更新
  1403. turbineInfoDayService.saveOrUpdateBatch(interest);
  1404. turbineInfoDayService.saveOrUpdateBatch(update);
  1405. }
  1406. public void writeReportPool(Date date) {
  1407. //date当天零点
  1408. DateTime time = DateUtil.beginOfDay(date);
  1409. //date昨天零点
  1410. DateTime time0 = DateUtil.offsetDay(time, -1);
  1411. DateTime beginOfMonth = DateUtil.beginOfMonth(time0);
  1412. DateTime beginOfYear = DateUtil.beginOfYear(time0);
  1413. QueryWrapper<StationInfoDay> wrapper = new QueryWrapper<>();
  1414. wrapper.eq("record_date", time0);
  1415. List<StationInfoDay> days = stationInfoDayService.list(wrapper);
  1416. Map<String, StationInfoDay> dayMap = days.stream().collect(Collectors.toMap(StationInfoDay::getStationId, Function.identity()));
  1417. wrapper = new QueryWrapper<>();
  1418. wrapper.select("sum(rfdl) rfdl,sum(swdl) swdl,sum(gwdl) gwdl,sum(cydl) cydl,sum(zhcydl) zhcydl,station_id")
  1419. .between("record_date", beginOfMonth.toJdkDate(), time0.toJdkDate())
  1420. .groupBy("station_id");
  1421. List<StationInfoDay> monthDays = stationInfoDayService.list(wrapper);
  1422. Map<String, StationInfoDay> monthMap = monthDays.stream().collect(Collectors.toMap(StationInfoDay::getStationId, Function.identity()));
  1423. wrapper = new QueryWrapper<>();
  1424. wrapper.select("sum(rfdl) rfdl,sum(swdl) swdl,sum(gwdl) gwdl,sum(cydl) cydl,sum(zhcydl) zhcydl,station_id")
  1425. .between("record_date", beginOfYear.toJdkDate(), time0.toJdkDate())
  1426. .groupBy("station_id");
  1427. List<StationInfoDay> yearDays = stationInfoDayService.list(wrapper);
  1428. Map<String, StationInfoDay> yearMap = yearDays.stream().collect(Collectors.toMap(StationInfoDay::getStationId, Function.identity()));
  1429. // QueryWrapper<ProEconReportIndicatorPool> wrapperrp = new QueryWrapper<>();
  1430. // wrapperrp.eq("record_date", time0).last("and foreign_key_id = windpowerstation_id");
  1431. // List<ProEconReportIndicatorPool> pools = reportIndicatorPoolService.list(wrapperrp);
  1432. QueryWrapper<ProEconReportIndicatorPool> wrapperrp = new QueryWrapper<>();
  1433. wrapperrp.eq("record_date", time0)
  1434. .in("foreign_key_id", days.stream().map(StationInfoDay::getStationId).collect(Collectors.toList()));
  1435. List<ProEconReportIndicatorPool> pools = reportIndicatorPoolService.list(wrapperrp);
  1436. if (!pools.isEmpty()) {
  1437. StationInfoDay day, month, year;
  1438. List<ProEconReportIndicatorPool> poolList = new ArrayList<>();
  1439. for (ProEconReportIndicatorPool pool : pools) {
  1440. String stationId = pool.getForeignKeyId();
  1441. day = dayMap.get(stationId);
  1442. pool.setRfdldb(day.getRfdl().doubleValue());
  1443. pool.setRswdldb(day.getSwdl().doubleValue());
  1444. pool.setRgwgwdldb(day.getGwdl().doubleValue());
  1445. pool.setRfdcydldb(day.getCydl().doubleValue());
  1446. pool.setRzhcydldb(day.getZhcydl().doubleValue());
  1447. month = monthMap.get(stationId);
  1448. pool.setYfdldb(month.getRfdl().doubleValue());
  1449. pool.setYswdldb(month.getSwdl().doubleValue());
  1450. pool.setYgwgwdldb(month.getGwdl().doubleValue());
  1451. pool.setYfdcydldb(month.getCydl().doubleValue());
  1452. pool.setYzhcydldb(month.getZhcydl().doubleValue());
  1453. year = yearMap.get(stationId);
  1454. pool.setNfdldb(year.getRfdl().doubleValue());
  1455. pool.setNswdldb(year.getSwdl().doubleValue());
  1456. pool.setNgwgwdldb(year.getGwdl().doubleValue());
  1457. pool.setNfdcydldb(year.getCydl().doubleValue());
  1458. pool.setNzhcydldb(year.getZhcydl().doubleValue());
  1459. poolList.add(pool);
  1460. }
  1461. reportIndicatorPoolService.saveOrUpdateBatch(poolList);
  1462. } else {
  1463. List<ProEconReportIndicatorPool> poolList = new ArrayList<>();
  1464. for (StationInfoDay day : days) {
  1465. ProEconReportIndicatorPool pool = new ProEconReportIndicatorPool();
  1466. pool.setRecordDate(time0.toLocalDateTime().toLocalDate());
  1467. pool.setForeignKeyId(day.getStationId());
  1468. poolList.add(pool);
  1469. }
  1470. reportIndicatorPoolService.saveOrUpdateBatch(poolList);
  1471. }
  1472. }
  1473. public void writeReportPoolProject(Date date) {
  1474. //date当天零点
  1475. DateTime time = DateUtil.beginOfDay(date);
  1476. //date昨天零点
  1477. DateTime time0 = DateUtil.offsetDay(time, -1);
  1478. DateTime beginOfMonth = DateUtil.beginOfMonth(time0);
  1479. DateTime beginOfYear = DateUtil.beginOfYear(time0);
  1480. QueryWrapper<LineInfoDay> wrapper = new QueryWrapper<>();
  1481. wrapper.select("sum(rfdl) rfdl,sum(swdl) swdl,sum(gwdl) gwdl,sum(cydl) cydl,project_id")
  1482. .eq("record_date", time0)
  1483. .groupBy("project_id");
  1484. List<LineInfoDay> days = lineInfoDayService.list(wrapper);
  1485. Map<String, LineInfoDay> dayMap = days.stream().collect(Collectors.toMap(LineInfoDay::getProjectId, Function.identity()));
  1486. wrapper = new QueryWrapper<>();
  1487. wrapper.select("sum(rfdl) rfdl,sum(swdl) swdl,sum(gwdl) gwdl,sum(cydl) cydl,project_id")
  1488. .between("record_date", beginOfMonth, time0)
  1489. .groupBy("project_id");
  1490. List<LineInfoDay> monthDays = lineInfoDayService.list(wrapper);
  1491. Map<String, LineInfoDay> monthMap = monthDays.stream().collect(Collectors.toMap(LineInfoDay::getProjectId, Function.identity()));
  1492. wrapper = new QueryWrapper<>();
  1493. wrapper.select("sum(rfdl) rfdl,sum(swdl) swdl,sum(gwdl) gwdl,sum(cydl) cydl,project_id")
  1494. .between("record_date", beginOfYear, time0)
  1495. .groupBy("project_id");
  1496. List<LineInfoDay> yearDays = lineInfoDayService.list(wrapper);
  1497. Map<String, LineInfoDay> yearMap = yearDays.stream().collect(Collectors.toMap(LineInfoDay::getProjectId, Function.identity()));
  1498. QueryWrapper<ProEconReportIndicatorPool> wrapperrp = new QueryWrapper<>();
  1499. wrapperrp.eq("record_date", time0).last("and foreign_key_id = project_id");
  1500. List<ProEconReportIndicatorPool> pools = reportIndicatorPoolService.list(wrapperrp);
  1501. QueryWrapper<ProEconReportIndicatorPool> wrapperrp2 = new QueryWrapper<>();
  1502. wrapperrp2.eq("record_date", time0)
  1503. .in("foreign_key_id", days.stream().map(LineInfoDay::getProjectId).collect(Collectors.toList()));
  1504. List<ProEconReportIndicatorPool> pools2 = reportIndicatorPoolService.list(wrapperrp2);
  1505. if (!pools2.isEmpty()) {
  1506. LineInfoDay day, month, year;
  1507. List<ProEconReportIndicatorPool> poolList = new ArrayList<>();
  1508. String projectId;
  1509. for (ProEconReportIndicatorPool pool : pools2) {
  1510. projectId = pool.getForeignKeyId();
  1511. day = dayMap.get(projectId);
  1512. pool.setRfdldb(day.getRfdl().doubleValue());
  1513. pool.setRswdldb(day.getSwdl());
  1514. pool.setRgwgwdldb(day.getGwdl());
  1515. pool.setRfdcydldb(day.getCydl());
  1516. pool.setRzhcydldb(day.getRfdl().doubleValue() + day.getGwdl() - day.getSwdl());
  1517. month = monthMap.get(projectId);
  1518. pool.setYfdldb(month.getRfdl().doubleValue());
  1519. pool.setYswdldb(month.getSwdl());
  1520. pool.setYgwgwdldb(month.getGwdl());
  1521. pool.setYfdcydldb(month.getCydl());
  1522. pool.setRzhcydldb(month.getRfdl().doubleValue() + month.getGwdl() - month.getSwdl());
  1523. year = yearMap.get(projectId);
  1524. pool.setNfdldb(year.getRfdl().doubleValue());
  1525. pool.setNswdldb(year.getSwdl());
  1526. pool.setNgwgwdldb(year.getGwdl());
  1527. pool.setNfdcydldb(year.getCydl());
  1528. pool.setRzhcydldb(year.getRfdl().doubleValue() + year.getGwdl() - year.getSwdl());
  1529. poolList.add(pool);
  1530. }
  1531. reportIndicatorPoolService.saveOrUpdateBatch(poolList);
  1532. } else {
  1533. List<ProEconReportIndicatorPool> poolList = new ArrayList<>();
  1534. for (LineInfoDay day : days) {
  1535. ProEconReportIndicatorPool pool = new ProEconReportIndicatorPool();
  1536. pool.setRecordDate(time0.toLocalDateTime().toLocalDate());
  1537. pool.setForeignKeyId(day.getProjectId());
  1538. poolList.add(pool);
  1539. }
  1540. reportIndicatorPoolService.saveOrUpdateBatch(poolList);
  1541. }
  1542. }
  1543. public void writeReportPoolPjfs(Date date) {
  1544. //date当天零点
  1545. DateTime time = DateUtil.beginOfDay(date);
  1546. //date昨天零点
  1547. DateTime time0 = DateUtil.offsetDay(time, -1);
  1548. DateTime beginOfMonth = DateUtil.beginOfMonth(time0);
  1549. DateTime beginOfYear = DateUtil.beginOfYear(time0);
  1550. QueryWrapper<TurbineInfoDay> wrapper = new QueryWrapper<>();
  1551. wrapper.select("avg(pjfs) pjfs,project_id")
  1552. .eq("record_date", time0)
  1553. .groupBy("project_id");
  1554. List<TurbineInfoDay> days = turbineInfoDayService.list(wrapper);
  1555. Map<String, TurbineInfoDay> dayMap = days.stream().collect(Collectors.toMap(TurbineInfoDay::getProjectId, Function.identity()));
  1556. wrapper = new QueryWrapper<>();
  1557. wrapper.select("avg(pjfs) pjfs,project_id")
  1558. .between("record_date", beginOfMonth, time0)
  1559. .groupBy("project_id");
  1560. List<TurbineInfoDay> monthDays = turbineInfoDayService.list(wrapper);
  1561. Map<String, TurbineInfoDay> monthMap = monthDays.stream().collect(Collectors.toMap(TurbineInfoDay::getProjectId, Function.identity()));
  1562. wrapper = new QueryWrapper<>();
  1563. wrapper.select("avg(pjfs) pjfs,project_id")
  1564. .between("record_date", beginOfYear, time0)
  1565. .groupBy("project_id");
  1566. List<TurbineInfoDay> yearDays = turbineInfoDayService.list(wrapper);
  1567. Map<String, TurbineInfoDay> yearMap = yearDays.stream().collect(Collectors.toMap(TurbineInfoDay::getProjectId, Function.identity()));
  1568. // QueryWrapper<ProEconReportIndicatorPool> wrapperrp = new QueryWrapper<>();
  1569. // wrapperrp.eq("record_date", time0).last("and foreign_key_id = project_id");
  1570. QueryWrapper<ProEconReportIndicatorPool> wrapperrp = new QueryWrapper<>();
  1571. wrapperrp.eq("record_date", time0)
  1572. .in("foreign_key_id", days.stream().map(TurbineInfoDay::getProjectId).collect(Collectors.toList()));
  1573. List<ProEconReportIndicatorPool> pools = reportIndicatorPoolService.list(wrapperrp);
  1574. QueryWrapper<ProEconReportIndicatorPool> wrapperrp2 = new QueryWrapper<>();
  1575. wrapperrp2.eq("record_date", time0).eq("foreign_key_id", "GJNY_SXGS_DBXNY_ZGS0");
  1576. ProEconReportIndicatorPool pools2 = reportIndicatorPoolService.getOne(wrapperrp2);
  1577. if (pools2 == null){
  1578. ProEconReportIndicatorPool GSPJFS = new ProEconReportIndicatorPool();
  1579. GSPJFS.setRecordDate(time0.toLocalDateTime().toLocalDate());
  1580. GSPJFS.setForeignKeyId("GJNY_SXGS_DBXNY_ZGS0");
  1581. GSPJFS.setCompanyId("GJNY_SXGS_DBXNY_ZGS");
  1582. reportIndicatorPoolService.saveOrUpdate(GSPJFS);
  1583. }
  1584. TurbineInfoDay day, month, year;
  1585. String projectId;
  1586. if (!pools.isEmpty()) {
  1587. for (ProEconReportIndicatorPool pool : pools) {
  1588. projectId = pool.getForeignKeyId();
  1589. day = dayMap.get(projectId);
  1590. pool.setRpjfs(day.getPjfs());
  1591. month = monthMap.get(projectId);
  1592. pool.setYpjfs(month.getPjfs());
  1593. year = yearMap.get(projectId);
  1594. pool.setNpjfs(year.getPjfs());
  1595. }
  1596. double v1 = pools.stream().mapToDouble(ProEconReportIndicatorPool::getRpjfs).average().orElse(0);
  1597. double v2 = pools.stream().mapToDouble(ProEconReportIndicatorPool::getYpjfs).average().orElse(0);
  1598. double v3 = pools.stream().mapToDouble(ProEconReportIndicatorPool::getNpjfs).average().orElse(0);
  1599. if (!(pools2 == null)) {
  1600. pools2.setRpjfs(v1);
  1601. pools2.setYpjfs(v2);
  1602. pools2.setNpjfs(v3);
  1603. reportIndicatorPoolService.saveOrUpdate(pools2);
  1604. }
  1605. reportIndicatorPoolService.saveOrUpdateBatch(pools);
  1606. } else {
  1607. List<ProEconReportIndicatorPool> poolList = new ArrayList<>();
  1608. for (TurbineInfoDay day2 : days) {
  1609. ProEconReportIndicatorPool pool = new ProEconReportIndicatorPool();
  1610. pool.setRecordDate(time0.toLocalDateTime().toLocalDate());
  1611. pool.setForeignKeyId(day2.getProjectId());
  1612. poolList.add(pool);
  1613. }
  1614. reportIndicatorPoolService.saveOrUpdateBatch(poolList);
  1615. }
  1616. }
  1617. public void gongsizhibiao(Date date) {
  1618. //date当天零点
  1619. DateTime time = DateUtil.beginOfDay(date);
  1620. //date昨天零点
  1621. DateTime time0 = DateUtil.offsetDay(time, -1);
  1622. DateTime beginOfMonth = DateUtil.beginOfMonth(time0);
  1623. DateTime beginOfYear = DateUtil.beginOfYear(time0);
  1624. QueryWrapper<ProEconReportIndicatorPool> wrapper = new QueryWrapper<>();
  1625. wrapper.eq("record_date", time0)
  1626. //包含"STA"
  1627. .like("foreign_key_id", "_STA");
  1628. List<ProEconReportIndicatorPool> list = reportIndicatorPoolService.list(wrapper);
  1629. //遍历list,累加指标RFDL
  1630. //公司指标,日发电量
  1631. double rfdl = 0;
  1632. //公司指标,日上网电量
  1633. double swdl = 0;
  1634. //公司指标,日购网电量
  1635. double gwdl = 0;
  1636. //公司指标,日综合厂用电量
  1637. double zhcydl = 0;
  1638. //公司指标,月发电量
  1639. double yfdl = 0;
  1640. //公司指标,月上网电量
  1641. double yswdl = 0;
  1642. //公司指标,月购网电量
  1643. double ygwdl = 0;
  1644. //公司指标,月综合厂用电量
  1645. double yzhcydl = 0;
  1646. //公司指标,年发电量
  1647. double nfdl = 0;
  1648. //公司指标,年上网电量
  1649. double nswdl = 0;
  1650. //公司指标,年购网电量
  1651. double ngwdl = 0;
  1652. //公司指标,年综合厂用电量
  1653. double nzhcydl = 0;
  1654. for (ProEconReportIndicatorPool pool : list) {
  1655. rfdl += pool.getRfdldb();
  1656. swdl += pool.getRswdldb();
  1657. gwdl += pool.getRgwgwdldb();
  1658. zhcydl += pool.getRzhcydldb();
  1659. yfdl += pool.getYfdldb();
  1660. yswdl += pool.getYswdldb();
  1661. ygwdl += pool.getYgwgwdldb();
  1662. yzhcydl += pool.getYzhcydldb();
  1663. nfdl += pool.getNfdldb();
  1664. nswdl += pool.getNswdldb();
  1665. ngwdl += pool.getNgwgwdldb();
  1666. nzhcydl += pool.getNzhcydldb();
  1667. }
  1668. QueryWrapper<ProEconReportIndicatorPool> wrapperrp2 = new QueryWrapper<>();
  1669. wrapperrp2.eq("record_date", time0).eq("foreign_key_id", "GJNY_SXGS_DBXNY_ZGS0");
  1670. ProEconReportIndicatorPool GSZB = reportIndicatorPoolService.getOne(wrapperrp2);
  1671. //公司指标集合
  1672. GSZB.setRfdldb(rfdl);
  1673. GSZB.setRswdldb(swdl);
  1674. GSZB.setRgwgwdldb(gwdl);
  1675. GSZB.setRzhcydldb(zhcydl);
  1676. GSZB.setYfdldb(yfdl);
  1677. GSZB.setYswdldb(yswdl);
  1678. GSZB.setYgwgwdldb(ygwdl);
  1679. GSZB.setYzhcydldb(yzhcydl);
  1680. GSZB.setNfdldb(nfdl);
  1681. GSZB.setNswdldb(nswdl);
  1682. GSZB.setNgwgwdldb(ngwdl);
  1683. GSZB.setNzhcydldb(nzhcydl);
  1684. reportIndicatorPoolService.saveOrUpdate(GSZB);
  1685. }
  1686. //公司指标,累加风机理论发电量
  1687. public void writeReportPoolLLFDL(Date date) {
  1688. //date当天零点
  1689. DateTime time = DateUtil.beginOfDay(date);
  1690. //date昨天零点
  1691. DateTime time0 = DateUtil.offsetDay(time, -1);
  1692. DateTime beginOfMonth = DateUtil.beginOfMonth(time0);
  1693. DateTime beginOfYear = DateUtil.beginOfYear(time0);
  1694. QueryWrapper<TurbineInfoDay> wrapper = new QueryWrapper<>();
  1695. wrapper.select("avg(llfdl) llfdl,project_id")
  1696. .eq("record_date", time0)
  1697. .groupBy("project_id");
  1698. List<TurbineInfoDay> days = turbineInfoDayService.list(wrapper);
  1699. Map<String, TurbineInfoDay> dayMap = days.stream().collect(Collectors.toMap(TurbineInfoDay::getProjectId, Function.identity()));
  1700. wrapper = new QueryWrapper<>();
  1701. wrapper.select("avg(llfdl) llfdl,project_id")
  1702. .between("record_date", beginOfMonth, time0)
  1703. .groupBy("project_id");
  1704. List<TurbineInfoDay> monthDays = turbineInfoDayService.list(wrapper);
  1705. Map<String, TurbineInfoDay> monthMap = monthDays.stream().collect(Collectors.toMap(TurbineInfoDay::getProjectId, Function.identity()));
  1706. wrapper = new QueryWrapper<>();
  1707. wrapper.select("avg(llfdl) llfdl,project_id")
  1708. .between("record_date", beginOfYear, time0)
  1709. .groupBy("project_id");
  1710. List<TurbineInfoDay> yearDays = turbineInfoDayService.list(wrapper);
  1711. Map<String, TurbineInfoDay> yearMap = yearDays.stream().collect(Collectors.toMap(TurbineInfoDay::getProjectId, Function.identity()));
  1712. QueryWrapper<ProEconReportIndicatorPool> wrapperrp = new QueryWrapper<>();
  1713. wrapperrp.eq("record_date", time0)
  1714. .in("foreign_key_id", days.stream().map(TurbineInfoDay::getProjectId).collect(Collectors.toList()));
  1715. List<ProEconReportIndicatorPool> pools = reportIndicatorPoolService.list(wrapperrp);
  1716. QueryWrapper<ProEconReportIndicatorPool> wrapperrp2 = new QueryWrapper<>();
  1717. wrapperrp2.eq("record_date", time0).eq("foreign_key_id", "GJNY_SXGS_DBXNY_ZGS0");
  1718. ProEconReportIndicatorPool pools2 = reportIndicatorPoolService.getOne(wrapperrp2);
  1719. if (pools2 == null){
  1720. ProEconReportIndicatorPool GSPJFS = new ProEconReportIndicatorPool();
  1721. GSPJFS.setRecordDate(time0.toLocalDateTime().toLocalDate());
  1722. GSPJFS.setForeignKeyId("GJNY_SXGS_DBXNY_ZGS0");
  1723. GSPJFS.setCompanyId("GJNY_SXGS_DBXNY_ZGS");
  1724. reportIndicatorPoolService.saveOrUpdate(GSPJFS);
  1725. }
  1726. TurbineInfoDay day, month, year;
  1727. String projectId;
  1728. if (!pools.isEmpty()) {
  1729. for (ProEconReportIndicatorPool pool : pools) {
  1730. projectId = pool.getForeignKeyId();
  1731. day = dayMap.get(projectId);
  1732. pool.setRllfdl(day.getPjfs());
  1733. month = monthMap.get(projectId);
  1734. pool.setYllfdl(month.getPjfs());
  1735. year = yearMap.get(projectId);
  1736. pool.setNllfdl(year.getPjfs());
  1737. }
  1738. double v1 = pools.stream().mapToDouble(ProEconReportIndicatorPool::getRllfdl).average().orElse(0);
  1739. double v2 = pools.stream().mapToDouble(ProEconReportIndicatorPool::getYllfdl).average().orElse(0);
  1740. double v3 = pools.stream().mapToDouble(ProEconReportIndicatorPool::getNllfdl).average().orElse(0);
  1741. if (pools2 != null) {
  1742. pools2.setRpjfs(v1);
  1743. pools2.setYpjfs(v2);
  1744. pools2.setNpjfs(v3);
  1745. reportIndicatorPoolService.saveOrUpdate(pools2);
  1746. }
  1747. reportIndicatorPoolService.saveOrUpdateBatch(pools);
  1748. } else {
  1749. List<ProEconReportIndicatorPool> poolList = new ArrayList<>();
  1750. for (TurbineInfoDay day2 : days) {
  1751. ProEconReportIndicatorPool pool = new ProEconReportIndicatorPool();
  1752. pool.setRecordDate(time0.toLocalDateTime().toLocalDate());
  1753. pool.setForeignKeyId(day2.getProjectId());
  1754. poolList.add(pool);
  1755. }
  1756. reportIndicatorPoolService.saveOrUpdateBatch(poolList);
  1757. }
  1758. }
  1759. public void calcRealtimeTurbineZt() {
  1760. DateTime date = DateUtil.date();
  1761. List<PointInfo> turbineZt = calcRealtimeTurbineDizt();
  1762. List<PointInfo> turbineAizt = calcRealtimeTurbineAizt();
  1763. turbineZt.addAll(turbineAizt);
  1764. turbineZt = turbineZt.stream().peek(t -> {
  1765. if (t.getPointDatas().get(0).getTs() + 9 * 60 * 1000 <= date.getTime()) {
  1766. t.getPointDatas().get(0).setDoubleValue(12.0);
  1767. }
  1768. }).collect(Collectors.toList());
  1769. Map<String, Map<String, PointInfo>> ztMapMap = turbineZt.stream().collect(Collectors.groupingBy(PointInfo::getStationId,
  1770. Collectors.toMap(PointInfo::getTurbineId, Function.identity())));
  1771. //AGC
  1772. List<PointInfo> entityAgc = getEntity("AGC002", "booster");
  1773. getSnapDataByEntity(entityAgc, DateUtil.offsetMinute(date, -5), date, 15);
  1774. //出线
  1775. List<PointInfo> entityCx = getEntity("AGC001", "booster");
  1776. getSnapDataByEntity(entityCx, DateUtil.offsetMinute(date, -5), date, 15);
  1777. //叶轮转速给定
  1778. List<PointInfo> entityYlzsgd = getEntity("AI110", "turbine");
  1779. getLatestByEntity(entityYlzsgd);
  1780. //风速
  1781. List<PointInfo> entityFs = getEntity("AI066", "turbine");
  1782. getSnapDataByEntity(entityFs, DateUtil.offsetMinute(date, -5), date, 15);
  1783. //功率
  1784. List<PointInfo> entityGl = getEntity("AI114", "turbine");
  1785. //getLatestByEntity(entityFs);
  1786. getLatestByEntity(entityGl);
  1787. Map<String, PointInfo> collectAgc = entityAgc.stream().collect(Collectors.toMap(PointInfo::getStationId, Function.identity()));
  1788. Map<String, PointInfo> collectCx = entityCx.stream().collect(Collectors.toMap(PointInfo::getStationId, Function.identity()));
  1789. Map<String, Map<String, List<PointData>>> collectYlzsgd = entityYlzsgd.stream().collect(Collectors.groupingBy(PointInfo::getStationId,
  1790. Collectors.toMap(PointInfo::getTurbineId, PointInfo::getPointDatas)));
  1791. Map<String, Map<String, PointInfo>> fsMapMap = entityFs.stream().collect(Collectors.groupingBy(PointInfo::getStationId,
  1792. Collectors.toMap(PointInfo::getTurbineId, Function.identity())));
  1793. Map<String, Map<String, List<PointData>>> glMapMap = entityGl.stream().collect(Collectors.groupingBy(PointInfo::getStationId,
  1794. Collectors.toMap(PointInfo::getTurbineId, PointInfo::getPointDatas)));
  1795. ztMapMap.forEach((stId, ztMap) -> {
  1796. PointInfo agcInfo = collectAgc.get(stId);
  1797. PointInfo cxInfo = collectCx.get(stId);
  1798. Map<String, PointInfo> fsMap = fsMapMap.get(stId);
  1799. Map<String, List<PointData>> pdsZsglMap = new HashMap<>();
  1800. ztMap.forEach((wtId, zt) -> {
  1801. List<PointData> peek = fsMap.get(wtId).getPointDatas().stream().peek(pd -> pd.setDoubleValue(NumberUtil.round(pd.getValue() > 25 ? 25.0
  1802. : pd.getValue(), 2).doubleValue())).collect(Collectors.toList());
  1803. fsMap.get(wtId).setPointDatas(peek);
  1804. List<PointData> zsglSnap = fsMap.get(wtId).getPointDatas().stream().map(pd -> {
  1805. Double v = CalcCache.fitcoef.get(wtId).get(pd.getValue());
  1806. return new PointData(pd.getTs(), v == null ? 0 : v);
  1807. }).collect(Collectors.toList());
  1808. pdsZsglMap.put(wtId, zsglSnap);
  1809. });
  1810. List<PointData> agcc = agcInfo.getPointDatas().stream().peek(agc -> {
  1811. agc.setDoubleValue(agc.getValue() * agcInfo.getCoef());
  1812. agc.setLongValue(0L);
  1813. }).collect(Collectors.toList());
  1814. agcInfo.setPointDatas(agcc);
  1815. List<PointData> cxc = cxInfo.getPointDatas().stream().peek(cx -> {
  1816. cx.setDoubleValue(cx.getValue() * cxInfo.getCoef());
  1817. cx.setLongValue(0L);
  1818. }).collect(Collectors.toList());
  1819. cxInfo.setPointDatas(cxc);
  1820. //风机风速、功率->场站风速、功率
  1821. List<PointData> firstZsgl = new ArrayList<>();
  1822. double v3 = pdsZsglMap.values().stream().mapToDouble(pds -> pds.get(0).getValue()).sum();
  1823. firstZsgl.add(new PointData(date.getTime(), v3));
  1824. List<PointData> czxd = calcStationXd2(agcInfo, cxInfo.getPointDatas(), firstZsgl);
  1825. ztMap.forEach((wtId, ztInfo) -> {
  1826. List<PointData> fss = fsMap.get(wtId).getPointDatas();
  1827. List<PointData> gls = glMapMap.get(stId).get(wtId);
  1828. //叶轮转速给定
  1829. List<PointData> ylzsgds = collectYlzsgd.get(stId).get(wtId);
  1830. calcRealtimeTurbineXd(ztInfo, gls.get(0), fss.get(fss.size() - 1), ylzsgds.get(0), czxd.get(czxd.size() - 1));
  1831. });
  1832. });
  1833. Map<String, String> entityMxzt = getEntityMap("MXZT", "turbine");
  1834. List<PointData> dataList = turbineZt.stream().map(zt -> {
  1835. PointData data = zt.getPointDatas().get(0);
  1836. data.setTagName(entityMxzt.get(zt.getTurbineId()));
  1837. return data;
  1838. }).collect(Collectors.toList());
  1839. adapter.writeHistoryBatch(taosUri(), dataList);
  1840. ThreadUtil.sleep(1000);
  1841. }
  1842. //此处加公式
  1843. public List<PointInfo> getLatest(String uniformCode, String institutionType) {
  1844. List<PointInfo> entity = getEntity(uniformCode, institutionType);
  1845. String collect = entity.stream().map(PointInfo::getPointKey).collect(Collectors.joining(","));
  1846. Map<String, PointData> latest = adapter.getLatest(goldenUri(), collect);
  1847. entity.forEach(e -> {
  1848. e.setPointDatas(Collections.singletonList(latest.get(e.getPointKey())));
  1849. });
  1850. return entity;
  1851. }
  1852. public void getLatestByEntity(List<PointInfo> entity) {
  1853. String collect = entity.stream().map(PointInfo::getPointKey).collect(Collectors.joining(","));
  1854. Map<String, PointData> latest = adapter.getLatest(goldenUri(), collect);
  1855. entity.forEach(e -> {
  1856. e.setPointDatas(Collections.singletonList(latest.get(e.getPointKey())));
  1857. });
  1858. }
  1859. public List<PointInfo> calcRealtimeTurbineDizt() {
  1860. List<PointInfo> entity = new ArrayList<>();
  1861. List<PointInfo> result = new ArrayList<>();
  1862. List<PointInfo> entity0 = getEntity("MX000", "state");
  1863. List<PointInfo> entity1 = getEntity("MX001", "state");
  1864. List<PointInfo> entity2 = getEntity("MX002", "state");
  1865. List<PointInfo> entity4 = getEntity("MX004", "state");
  1866. List<PointInfo> entity6 = getEntity("MX006", "state");
  1867. entity.addAll(entity0);
  1868. entity.addAll(entity1);
  1869. entity.addAll(entity2);
  1870. entity.addAll(entity4);
  1871. entity.addAll(entity6);
  1872. List<String> keys = entity.stream().map(PointInfo::getPointKey).collect(Collectors.toList());
  1873. Map<String, PointData> latest = adapter.getLatest(goldenUri(), String.join(",", keys));
  1874. Map<String, Map<String, PointInfo>> wtUcPis = entity.stream().collect(Collectors.groupingBy
  1875. (PointInfo::getTurbineId, Collectors.toMap(PointInfo::getUniformCode, Function.identity())));
  1876. wtUcPis.forEach((wtId, map) -> {
  1877. PointInfo info2 = map.get("MX002");
  1878. PointData data2 = latest.get(info2.getPointKey());
  1879. if (data2.getValue() == 1.0) {
  1880. data2.setDoubleValue(2.0);
  1881. data2.setBooleanValue(false);
  1882. info2.setPointDatas(Collections.singletonList(data2));
  1883. result.add(info2);
  1884. } else {
  1885. PointInfo info0 = map.get("MX000");
  1886. PointData data0 = latest.get(info0.getPointKey());
  1887. if (data0.getValue() == 1.0) {
  1888. data0.setDoubleValue(0.0);
  1889. data0.setBooleanValue(false);
  1890. info0.setPointDatas(Collections.singletonList(data0));
  1891. result.add(info0);
  1892. } else {
  1893. PointInfo info6 = map.get("MX006");
  1894. PointData data6 = latest.get(info6.getPointKey());
  1895. if (data6.getValue() == 1.0) {
  1896. data6.setDoubleValue(6.0);
  1897. data6.setBooleanValue(false);
  1898. info6.setPointDatas(Collections.singletonList(data6));
  1899. result.add(info6);
  1900. } else {
  1901. PointInfo info4 = map.get("MX004");
  1902. PointData data4 = latest.get(info4.getPointKey());
  1903. if (data4.getValue() == 1.0) {
  1904. data4.setDoubleValue(4.0);
  1905. data4.setBooleanValue(false);
  1906. info4.setPointDatas(Collections.singletonList(data4));
  1907. result.add(info4);
  1908. } else {
  1909. PointInfo info1 = map.get("MX001");
  1910. PointData data1 = latest.get(info1.getPointKey());
  1911. if (data1.getValue() == 1.0) {
  1912. data1.setDoubleValue(1.0);
  1913. data1.setBooleanValue(false);
  1914. info1.setPointDatas(Collections.singletonList(data1));
  1915. result.add(info1);
  1916. } else {
  1917. data1.setDoubleValue(2.0);
  1918. System.out.println("wtId: " + wtId + ",状态值无批配: " + data1.getValue());
  1919. info1.setPointDatas(Collections.singletonList(data1));
  1920. result.add(info1);
  1921. }
  1922. }
  1923. }
  1924. }
  1925. }
  1926. });
  1927. return result;
  1928. }
  1929. public List<PointInfo> calcRealtimeTurbineAizt() {
  1930. List<PointInfo> entityAiZt = getEntity("AI422", "state");
  1931. List<StateAi> list = stateAiService.list();
  1932. //机型,源状态,目标状态
  1933. Map<String, Map<Integer, Integer>> collectAi = list.stream().collect(Collectors.groupingBy(
  1934. StateAi::getModel, Collectors.toMap(StateAi::getOriginalState, StateAi::getMappingState)));
  1935. String collect = entityAiZt.stream().map(PointInfo::getPointKey).collect(Collectors.joining(","));
  1936. Map<String, PointData> latest = adapter.getLatest(goldenUri(), collect);
  1937. for (PointInfo info : entityAiZt) {
  1938. PointData data = latest.get(info.getPointKey());
  1939. Integer m = collectAi.get(info.getSpare()).get((int) data.getValue());
  1940. if (m == null) m = 2;
  1941. data.setDoubleValue(m);
  1942. info.setPointDatas(Collections.singletonList(data));
  1943. }
  1944. return entityAiZt;
  1945. }
  1946. public void getAgoAvg(List<PointInfo> entity, Date start, Date end) {
  1947. for (PointInfo info : entity) {
  1948. DoubleStatData snap = adapter.getHistoryStat(goldenUri(), info.getPointKey(), start.getTime(), end.getTime());
  1949. ThreadUtil.sleep(20);
  1950. info.setPointDatas(Collections.singletonList(snap.getAvg()));
  1951. }
  1952. }
  1953. public void calcRealtimeTurbineXd(PointInfo ztInfo, PointData gl, PointData fs, PointData ylzsgd, PointData czxd) {
  1954. Map<String, EquipmentModel> map = equipmentModelService.map();
  1955. Double capacity = map.get(ztInfo.getSpare()).getPowerProduction();
  1956. PointData zt = ztInfo.getPointDatas().get(0);
  1957. if (czxd.isBooleanValue() && gl.getValue() < capacity * 0.9) {
  1958. if (fs.getValue() >= 12) {
  1959. zt.setDoubleValue(8.0);
  1960. }
  1961. if (ylzsgd.getValue() != 0 && ylzsgd.getValue() < 17.3) {//降出力
  1962. zt.setDoubleValue(8.0);
  1963. }
  1964. }
  1965. }
  1966. }