JavaFunctionJobHandler.java 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620
  1. package com.ruoyi;
  2. import cn.hutool.core.date.DateTime;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.thread.ThreadUtil;
  5. import cn.hutool.core.util.NumberUtil;
  6. import com.alibaba.fastjson2.JSON;
  7. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  8. import com.ruoyi.quartz.handler.IJobHandler;
  9. import com.ruoyi.ucp.entity.*;
  10. import com.ruoyi.ucp.feign.AdapterApi;
  11. import com.ruoyi.ucp.service.*;
  12. import com.ruoyi.ucp.util.CalcCache;
  13. import org.apache.commons.math3.fitting.PolynomialCurveFitter;
  14. import org.apache.commons.math3.fitting.WeightedObservedPoints;
  15. import org.junit.Test;
  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 writeReportPool(Date date) {
  301. //date当天零点
  302. DateTime time = DateUtil.beginOfDay(date);
  303. //date昨天零点
  304. DateTime time0 = DateUtil.offsetDay(time, -1);
  305. DateTime beginOfMonth = DateUtil.beginOfMonth(time0);
  306. DateTime beginOfYear = DateUtil.beginOfYear(time0);
  307. QueryWrapper<StationInfoDay> wrapper = new QueryWrapper<>();
  308. wrapper.eq("record_date", time0);
  309. List<StationInfoDay> days = stationInfoDayService.list(wrapper);
  310. Map<String, StationInfoDay> dayMap = days.stream().collect(Collectors.toMap(StationInfoDay::getStationId, Function.identity()));
  311. wrapper = new QueryWrapper<>();
  312. wrapper.select("sum(rfdl) rfdl,sum(swdl) swdl,sum(gwdl) gwdl,sum(cydl) cydl,sum(zhcydl) zhcydl,station_id")
  313. .between("record_date", beginOfMonth.toJdkDate(), time0.toJdkDate())
  314. .groupBy("station_id");
  315. List<StationInfoDay> monthDays = stationInfoDayService.list(wrapper);
  316. Map<String, StationInfoDay> monthMap = monthDays.stream().collect(Collectors.toMap(StationInfoDay::getStationId, Function.identity()));
  317. wrapper = new QueryWrapper<>();
  318. wrapper.select("sum(rfdl) rfdl,sum(swdl) swdl,sum(gwdl) gwdl,sum(cydl) cydl,sum(zhcydl) zhcydl,station_id")
  319. .between("record_date", beginOfYear.toJdkDate(), time0.toJdkDate())
  320. .groupBy("station_id");
  321. List<StationInfoDay> yearDays = stationInfoDayService.list(wrapper);
  322. Map<String, StationInfoDay> yearMap = yearDays.stream().collect(Collectors.toMap(StationInfoDay::getStationId, Function.identity()));
  323. QueryWrapper<ProEconReportIndicatorPool> wrapperrp = new QueryWrapper<>();
  324. wrapperrp.eq("record_date", time0).last("and foreign_key_id = windpowerstation_id");
  325. List<ProEconReportIndicatorPool> pools = reportIndicatorPoolService.list(wrapperrp);
  326. // pools = pools.stream().filter(p -> p.getWindpowerstationId().equals("GJNY_SXGS_ZZ_FDC_STA")).collect(Collectors.toList());
  327. StationInfoDay day, month, year;
  328. List<ProEconReportIndicatorPool> poolList = new ArrayList<>();
  329. for (ProEconReportIndicatorPool pool : pools) {
  330. String stationId = pool.getWindpowerstationId();
  331. day = dayMap.get(stationId);
  332. pool.setRfdldb(day.getRfdl().doubleValue());
  333. pool.setRswdldb(day.getSwdl().doubleValue());
  334. pool.setRgwgwdldb(day.getGwdl().doubleValue());
  335. pool.setRfdcydldb(day.getCydl().doubleValue());
  336. pool.setRzhcydldb(day.getZhcydl().doubleValue());
  337. month = monthMap.get(stationId);
  338. pool.setYfdldb(month.getRfdl().doubleValue());
  339. pool.setYswdldb(month.getSwdl().doubleValue());
  340. pool.setYgwgwdldb(month.getGwdl().doubleValue());
  341. pool.setYfdcydldb(month.getCydl().doubleValue());
  342. pool.setYzhcydldb(month.getZhcydl().doubleValue());
  343. year = yearMap.get(stationId);
  344. pool.setNfdldb(year.getRfdl().doubleValue());
  345. pool.setNswdldb(year.getSwdl().doubleValue());
  346. pool.setNgwgwdldb(year.getGwdl().doubleValue());
  347. pool.setNfdcydldb(year.getCydl().doubleValue());
  348. pool.setNzhcydldb(year.getZhcydl().doubleValue());
  349. poolList.add(pool);
  350. }
  351. reportIndicatorPoolService.saveOrUpdateBatch(poolList);
  352. }
  353. public void calcLineDjlRfdl(Date date) {
  354. //date当天零点
  355. DateTime time = DateUtil.beginOfDay(date);
  356. //date昨天零点
  357. DateTime time0 = DateUtil.offsetDay(time, -1);
  358. //date当天零点加三分钟
  359. DateTime time2 = DateUtil.offsetMinute(time, 3);
  360. //date昨天零点加三分钟
  361. DateTime time1 = DateUtil.offsetMinute(time0, 3);
  362. PointInfo pi = new PointInfo();
  363. pi.setUniformCode("Z-ZXYG-JX");
  364. pi.setLineId("all");
  365. List<PointInfo> entity = pointService.getByEntity(pi);
  366. // entity = entity.stream().filter(e -> e.getStationId().equals("GJNY_SXGS_ZZ_FDC_STA")).collect(Collectors.toList());
  367. String keys = pointInfos2Keys(entity);
  368. Map<String, PointData> latest2 = adapter.getHistorySection(goldenUri(), keys, time2.getTime());
  369. Map<String, PointData> latest1 = adapter.getHistorySection(goldenUri(), keys, time1.getTime());
  370. List<LineInfoDay> byDate = getLineinfoByDate(time0.toJdkDate());
  371. Map<String, LineInfoDay> collect = new HashMap<>();
  372. if (!byDate.isEmpty()) {
  373. collect = byDate.stream().collect(Collectors.toMap(LineInfoDay::getLineId, Function.identity()));
  374. }
  375. List<LineInfoDay> list = new ArrayList<>();
  376. for (PointInfo info : entity) {
  377. LineInfoDay day;
  378. if (byDate.isEmpty()) {
  379. day = new LineInfoDay();
  380. day.setStationId(info.getStationId());
  381. day.setProjectId(info.getProjectId());
  382. day.setLineId(info.getLineId());
  383. day.setRecordDate(time0.toJdkDate());
  384. } else {
  385. day = collect.get(info.getLineId());
  386. }
  387. day.setRfdl((int) ((latest2.get(info.getPointKey()).getValue() -
  388. latest1.get(info.getPointKey()).getValue()) * info.getCoef()));
  389. if (day.getRfdl() <= 0 || day.getRfdl() > 30000000) {
  390. day.setRfdl(0);
  391. }
  392. list.add(day);
  393. }
  394. lineInfoDayService.saveOrUpdateBatch(list);
  395. }
  396. public void calcStationZhcydl(Date date) {
  397. //date当天零点
  398. DateTime time = DateUtil.beginOfDay(date);
  399. //date昨天零点
  400. DateTime time0 = DateUtil.offsetDay(time, -1);
  401. QueryWrapper<LineInfoDay> wrapper = new QueryWrapper<>();
  402. wrapper.select("sum(rfdl) rfdl,station_id").eq("record_date", time0.toJdkDate())
  403. .groupBy("station_id");
  404. List<LineInfoDay> list = lineInfoDayService.list(wrapper);
  405. List<StationInfoDay> byDate = getStationinfoByDate(time0.toJdkDate());
  406. // list = list.stream().filter(b -> b.getStationId().equals("GJNY_SXGS_ZZ_FDC_STA")).collect(Collectors.toList());
  407. Map<String, StationInfoDay> collect = new HashMap<>();
  408. if (!byDate.isEmpty()) {
  409. collect = byDate.stream().collect(Collectors.toMap(StationInfoDay::getStationId, Function.identity()));
  410. }
  411. StationInfoDay infoDay;
  412. List<StationInfoDay> infoDays = new ArrayList<>();
  413. for (LineInfoDay day : list) {
  414. if (byDate.isEmpty()) {
  415. infoDay = new StationInfoDay();
  416. infoDay.setStationId(day.getStationId());
  417. infoDay.setRecordDate(time0.toJdkDate());
  418. } else {
  419. infoDay = collect.get(day.getStationId());
  420. }
  421. infoDay.setRfdl(day.getRfdl());
  422. infoDay.setZhcydl(infoDay.getRfdl() + infoDay.getGwdl() - infoDay.getSwdl());
  423. infoDays.add(infoDay);
  424. }
  425. stationInfoDayService.saveOrUpdateBatch(infoDays);
  426. }
  427. public void calcStationHourPJGL(Date hour) {
  428. PointInfo pointInfo = new PointInfo();
  429. pointInfo.setInstitutionType("station");
  430. pointInfo.setUniformCode("SSZGL");
  431. List<PointInfo> entity = pointService.getByEntity(pointInfo);
  432. List<StationInfoHour> byHours = getStationinfoByHour(hour);
  433. Map<String, StationInfoHour> collect = new HashMap<>();
  434. if (!byHours.isEmpty()) {
  435. collect = byHours.stream().collect(
  436. Collectors.toMap(StationInfoHour::getStationId, Function.identity()));
  437. }
  438. DoubleStatData stat;
  439. List<StationInfoHour> byHours2 = new ArrayList<>();
  440. for (PointInfo point : entity) {
  441. stat = adapter.getHistoryStat(taosGoldenUriTest(),
  442. point.getPointKey(), DateUtil.offsetHour(hour, -1).getTime(), hour.getTime());
  443. if (byHours.isEmpty()) {
  444. StationInfoHour info = new StationInfoHour();
  445. info.setStationId(point.getStationId());
  446. info.setRecordDate(DateUtil.beginOfHour(hour));
  447. if (stat == null || stat.getAvg() == null) info.setPjgl(0.0);
  448. info.setPjgl(stat.getAvg().getDoubleValue());
  449. byHours2.add(info);
  450. } else {
  451. collect.get(point.getStationId()).setPjgl(stat.getAvg().getDoubleValue());
  452. }
  453. }
  454. stationInfoHourService.saveOrUpdateBatch(byHours.isEmpty() ? byHours2 : byHours);
  455. }
  456. public void calcStationRfdlFj(Date date) {
  457. //date当天零点
  458. DateTime time = DateUtil.beginOfDay(date);
  459. //date昨天零点
  460. DateTime time0 = DateUtil.offsetDay(time, -1);
  461. QueryWrapper<TurbineInfoDay> wrapper = new QueryWrapper<>();
  462. wrapper.select("sum(rfdl) rfdl,station_id").eq("record_date", time0.toJdkDate())
  463. .groupBy("station_id");
  464. List<TurbineInfoDay> list = turbineInfoDayService.list(wrapper);
  465. List<StationInfoDay> byDate = getStationinfoByDate(time0.toJdkDate());
  466. Map<String, StationInfoDay> collect = new HashMap<>();
  467. if (!byDate.isEmpty()) {
  468. collect = byDate.stream().collect(Collectors.toMap(StationInfoDay::getStationId, Function.identity()));
  469. }
  470. StationInfoDay infoDay;
  471. List<StationInfoDay> infoDays = new ArrayList<>();
  472. for (TurbineInfoDay day : list) {
  473. if (byDate.isEmpty()) {
  474. infoDay = new StationInfoDay();
  475. infoDay.setStationId(day.getStationId());
  476. infoDay.setRecordDate(time0.toJdkDate());
  477. } else {
  478. infoDay = collect.get(day.getStationId());
  479. }
  480. infoDay.setRfdlFj(day.getRfdl());
  481. infoDays.add(infoDay);
  482. }
  483. stationInfoDayService.saveOrUpdateBatch(infoDays);
  484. }
  485. public void calcTurbineRFDL(Date date) {
  486. //date当天零点
  487. DateTime time = DateUtil.beginOfDay(date);
  488. //date昨天零点
  489. DateTime time0 = DateUtil.offsetDay(time, -1);
  490. //date当天零点加三分钟
  491. DateTime time2 = DateUtil.offsetMinute(time, 3);
  492. //date昨天零点加三分钟
  493. DateTime time1 = DateUtil.offsetMinute(time0, 3);
  494. PointInfo pi = new PointInfo();
  495. pi.setUniformCode("AI121");
  496. pi.setInstitutionType("turbine");
  497. List<PointInfo> entity = getEntity("AI121", "turbine");
  498. Map<String, String> turMap = entity.stream().collect(Collectors.toMap(PointInfo::getTurbineId, PointInfo::getPointKey));
  499. Map<String, String> fsMap = getEntityMap("AI066", "turbine");
  500. String keys = pointInfos2Keys(entity);
  501. Map<String, PointData> swdlL2 = adapter.getHistorySection(goldenUri(), keys, time2.getTime());
  502. Map<String, PointData> swdlL1 = adapter.getHistorySection(goldenUri(), keys, time1.getTime());
  503. Map<String, TurbineInfoDay> byDate = getTurbineinfoMap(time0.toJdkDate(), entity);
  504. TurbineInfoDay infoDay;
  505. String turbineId, key;
  506. List<TurbineInfoDay> infoDays = new ArrayList<>();
  507. DoubleStatData stat;
  508. for (PointInfo info : entity) {
  509. turbineId = info.getTurbineId();
  510. infoDay = byDate.get(turbineId);
  511. key = turMap.get(turbineId);
  512. double v = (swdlL2.get(key).getValue() - swdlL1.get(key).getValue()) * info.getCoef();
  513. if (v < 0 || v > 1000000) {
  514. v = 0;
  515. } else if (v > 30000) {
  516. v = v / (v % 30000 + 3);
  517. }
  518. infoDay.setRfdl(v);
  519. stat = adapter.getHistoryStat(goldenUri(), fsMap.get(turbineId), time1.getTime(), time2.getTime());
  520. if (stat == null) {
  521. infoDay.setPjfs(0.0);
  522. } else {
  523. infoDay.setPjfs(stat.getAvg().getValue());
  524. }
  525. infoDays.add(infoDay);
  526. }
  527. turbineInfoDayService.saveOrUpdateBatch(infoDays);
  528. }
  529. /*public void calcTurbine5s(Date date) {
  530. //date当天零点
  531. DateTime time = DateUtil.beginOfDay(date);
  532. //date昨天零点
  533. DateTime time0 = DateUtil.offsetDay(time, -1);
  534. //状态
  535. List<PointInfo> entityZt = getEntity("MXZT", "turbine");
  536. //功率
  537. Map<String, String> collectGl = getEntityMap("AI114", "turbine");
  538. //风速
  539. Map<String, String> collectFs = getEntityMap("AI066", "turbine");
  540. Map<String, TurbineInfoDay> dayMap = getTurbineinfoMap(time0.toJdkDate(), entityZt);
  541. String tbId;
  542. TurbineInfoDay infoDay;
  543. List<TurbineInfoDay> infoDays = new ArrayList<>();
  544. for (PointInfo info : entityZt) {
  545. tbId = info.getTurbineId();
  546. List<PointData> snapZt = adapter.getHistorySnap(taosUri(), info.getPointKey(), time0.getTime(), time.getTime(), 60);
  547. //0 待机,1 手动停机,2 正常发电,3 发电降出力,4 故障,5 故障受累,6 检修,7 检修受累,8 限电降出力,
  548. // 9 限电停机,10 电网受累,11 环境受累,12 通讯中断,13 设备离线
  549. List<DoubleStatData> statGl = adapter.getHistoryStat2(goldenUri(), collectGl.get(tbId), time0.getTime(), time.getTime(), 60);
  550. List<DoubleStatData> statFs = adapter.getHistoryStat2(goldenUri(), collectFs.get(tbId), time0.getTime(), time.getTime(), 60);
  551. List<PointData> glList = statGl.stream().map(DoubleStatData::getAvg).collect(Collectors.toList());
  552. List<PointData> fsList = statFs.stream().map(DoubleStatData::getAvg)
  553. .peek(fs -> {
  554. if (fs.getValue() < 25) {
  555. fs.setDoubleValue(NumberUtil.round(fs.getValue(), 2).doubleValue());
  556. } else {
  557. fs.setDoubleValue(25.0);
  558. }
  559. }).collect(Collectors.toList());
  560. // Map<String, Double> loss = getTurbinePowerLoss(snapZt, fsList, tbId, glList);
  561. Map<String, Double> loss = new HashMap<>();
  562. infoDay = dayMap.get(tbId);
  563. infoDay.setJhjxss(loss.get("jxss") + loss.get("sdtj"));
  564. infoDay.setFjhjxss(loss.get("gzss") + loss.get("djss") + loss.get("fdjcl"));
  565. infoDay.setXdss(loss.get("xdjcl") + loss.get("xdtj"));
  566. infoDay.setSlss(0.0);
  567. if (infoDay.getRfdl() == 0) {
  568. infoDay.setXnss(0.0);
  569. } else {
  570. infoDay.setXnss(loss.get("xnss") * 0.1);
  571. }
  572. infoDay.setLlfdl(infoDay.getRfdl() + infoDay.getJhjxss() + infoDay.getFjhjxss() +
  573. infoDay.getXdss() + infoDay.getSlss() + infoDay.getXnss());
  574. infoDays.add(infoDay);
  575. }
  576. turbineInfoDayService.saveOrUpdateBatch(infoDays);
  577. }*/
  578. //此处加公式
  579. public void calcStationSspjfs() {
  580. List<PointInfo> fsEntity = getEntity("AI066", "turbine");
  581. String keys = pointInfos2Keys(fsEntity);
  582. Map<String, PointData> latest = adapter.getLatest(goldenUri(), keys);
  583. Map<String, List<String>> listMap = fsEntity.stream().collect(Collectors.groupingBy(PointInfo::getStationId, Collectors.mapping(PointInfo::getPointKey, Collectors.toList())));
  584. Map<String, String> entityMap = getEntityMap("SSPJFS", "station");
  585. List<PointData> tsData = new ArrayList<>();
  586. listMap.forEach((stId, keyList) -> {
  587. double v = keyList.stream().mapToDouble(key -> latest.get(key).getValue()).average().orElse(0.0);
  588. PointData data = new PointData();
  589. data.setTs(System.currentTimeMillis());
  590. data.setTagName(entityMap.get(stId));
  591. data.setDoubleValue(v);
  592. tsData.add(data);
  593. });
  594. adapter.writeHistoryBatch(taosUri(), tsData);
  595. }
  596. public void calcTurbine5s2(Date date) {
  597. //date当天零点
  598. DateTime time = DateUtil.beginOfDay(date);
  599. //date昨天零点
  600. DateTime time0 = DateUtil.offsetDay(time, -1);
  601. //风速
  602. List<PointInfo> entityFs = getEntity("AI066", "turbine");
  603. //功率
  604. List<PointInfo> entityGl = getEntity("AI114", "turbine");
  605. getSnapDataByEntity(entityFs, time0, time, 15);
  606. getSnapDataByEntity(entityGl, time0, time, 15);
  607. List<PointInfo> ztMapMap = calcTurbineZt(time0, time, entityFs, entityGl);
  608. Map<String, Map<String, PointInfo>> ztMapMapInfos = ztMapMap.stream().collect(
  609. Collectors.groupingBy(PointInfo::getStationId, Collectors.toMap(PointInfo::getTurbineId, Function.identity())));
  610. Map<String, TurbineInfoDay> dayMap = getTurbineinfoMap(time0, entityFs);
  611. List<TurbineInfoDay> infoDays = new ArrayList<>();
  612. Map<String, Map<String, List<PointData>>> fsMapMap = entityFs.stream().collect(Collectors.groupingBy(PointInfo::getStationId,
  613. Collectors.toMap(PointInfo::getTurbineId, PointInfo::getPointDatas)));
  614. Map<String, Map<String, List<PointData>>> glMapMap = entityGl.stream().collect(Collectors.groupingBy(PointInfo::getStationId,
  615. Collectors.toMap(PointInfo::getTurbineId, PointInfo::getPointDatas)));
  616. ztMapMapInfos.forEach((stId, ztMapInfo) -> ztMapInfo.forEach((wtId, ztInfo) -> {
  617. //0 待机,1 手动停机,2 正常发电,3 发电降出力,4 故障,5 故障受累,6 检修,7 检修受累,8 限电降出力,
  618. // 9 限电停机,10 电网受累,11 环境受累,12 通讯中断,13 设备离线
  619. TurbineInfoDay infoDay = dayMap.get(wtId);
  620. Map<String, Double> loss = getTurbinePowerLoss(infoDay.getRfdl(), ztMapMapInfos.get(stId).get(wtId), fsMapMap.get(stId).get(wtId), glMapMap.get(stId).get(wtId));
  621. infoDay.setJhjxss(loss.get("jxss") + loss.get("sdtj"));
  622. infoDay.setFjhjxss(loss.get("gzss") + loss.get("djss") + loss.get("fdjcl"));
  623. infoDay.setXdss(loss.get("xdjcl") + loss.get("xdtj"));
  624. infoDay.setSlss(0.0);
  625. if (infoDay.getRfdl() == 0) {
  626. infoDay.setXnss(0.0);
  627. } else {
  628. infoDay.setXnss(loss.get("xnss"));
  629. }
  630. infoDay.setLlfdl(infoDay.getRfdl() + infoDay.getJhjxss() + infoDay.getFjhjxss() +
  631. infoDay.getXdss() + infoDay.getSlss() + infoDay.getXnss());
  632. infoDays.add(infoDay);
  633. }));
  634. turbineInfoDayService.saveOrUpdateBatch(infoDays);
  635. }
  636. public List<PointInfo> calcTurbineZt(Date start, Date end, List<PointInfo> fsInfos, List<PointInfo> glInfos) {
  637. List<PointInfo> turbineZt = calcTurbineDizt(start, end, 15);
  638. List<PointInfo> turbineAizt = calcTurbineAizt(start, end, 15);
  639. turbineZt.addAll(turbineAizt);
  640. Map<String, Map<String, PointInfo>> ztMapMap = turbineZt.stream().collect(Collectors.groupingBy(PointInfo::getStationId,
  641. Collectors.toMap(PointInfo::getTurbineId, Function.identity())));
  642. //AGC
  643. List<PointInfo> entityAgc = getEntity("AGC002", "booster");
  644. getSnapDataByEntity(entityAgc, start, end, 15);
  645. //出线
  646. List<PointInfo> entityCx = getEntity("AGC001", "booster");
  647. getSnapDataByEntity(entityCx, start, end, 15);
  648. //叶轮转速给定
  649. List<PointInfo> entityYlzsgd = getEntity("AI110", "turbine");
  650. getSnapDataByEntity(entityYlzsgd, start, end, 15);
  651. Map<String, PointInfo> collectAgc = entityAgc.stream().collect(Collectors.toMap(PointInfo::getStationId, Function.identity()));
  652. Map<String, PointInfo> collectCx = entityCx.stream().collect(Collectors.toMap(PointInfo::getStationId, Function.identity()));
  653. Map<String, Map<String, List<PointData>>> collectYlzsgd = entityYlzsgd.stream().collect(Collectors.groupingBy(PointInfo::getStationId,
  654. Collectors.toMap(PointInfo::getTurbineId, PointInfo::getPointDatas)));
  655. Map<String, Map<String, PointInfo>> fsMapMap = fsInfos.stream().collect(Collectors.groupingBy(PointInfo::getStationId,
  656. Collectors.toMap(PointInfo::getTurbineId, Function.identity())));
  657. Map<String, Map<String, List<PointData>>> glMapMap = glInfos.stream().collect(Collectors.groupingBy(PointInfo::getStationId,
  658. Collectors.toMap(PointInfo::getTurbineId, PointInfo::getPointDatas)));
  659. ztMapMap.forEach((stId, ztMap) -> {
  660. PointInfo agcInfo = collectAgc.get(stId);
  661. PointInfo cxInfo = collectCx.get(stId);
  662. Map<String, PointInfo> fsMap = fsMapMap.get(stId);
  663. Map<String, List<PointData>> pdsZsglMap = new HashMap<>();
  664. ztMap.forEach((wtId, zt) -> {
  665. List<PointData> peek = fsMap.get(wtId).getPointDatas().stream().peek(pd -> pd.setDoubleValue(NumberUtil.round(pd.getValue() > 25 ? 25.0
  666. : pd.getValue(), 2).doubleValue())).collect(Collectors.toList());
  667. fsMap.get(wtId).setPointDatas(peek);
  668. List<PointData> zsglSnap = fsMap.get(wtId).getPointDatas().stream().map(pd -> {
  669. Double v = CalcCache.fitcoef.get(wtId).get(pd.getValue());
  670. return new PointData(pd.getTs(), v == null ? 0 : v);
  671. }).collect(Collectors.toList());
  672. pdsZsglMap.put(wtId, zsglSnap);
  673. });
  674. List<PointData> agcc = agcInfo.getPointDatas().stream().peek(agc -> {
  675. agc.setDoubleValue(agc.getValue() * agcInfo.getCoef());
  676. agc.setLongValue(0L);
  677. }).collect(Collectors.toList());
  678. agcInfo.setPointDatas(agcc);
  679. List<PointData> cxc = cxInfo.getPointDatas().stream().peek(cx -> {
  680. cx.setDoubleValue(cx.getValue() * cxInfo.getCoef());
  681. cx.setLongValue(0L);
  682. }).collect(Collectors.toList());
  683. cxInfo.setPointDatas(cxc);
  684. //风机风速、功率->场站风速、功率
  685. // List<PointData> firstFs = new ArrayList<>();
  686. // List<PointData> firstGl = new ArrayList<>();
  687. List<PointData> firstZsgl = new ArrayList<>();
  688. double v1, v2, v3;
  689. for (int i = 0; i < 5760; i++) {
  690. int finalI = i;
  691. long ts = start.getTime() + i * 15000;
  692. // v1 = fsMap.values().stream().mapToDouble(pds -> pds.getPointDatas().get(finalI).getValue()).average().orElse(0.0);
  693. // v2 = glMap.values().stream().mapToDouble(pds -> pds.getPointDatas().get(finalI).getValue()).sum();
  694. v3 = pdsZsglMap.values().stream().mapToDouble(pds -> pds.get(finalI).getValue()).sum();
  695. // firstFs.add(new PointData(ts, v1));
  696. // firstGl.add(new PointData(ts, v2));
  697. firstZsgl.add(new PointData(ts, v3));
  698. }
  699. List<PointData> czxd = calcStationXd2(agcInfo, cxInfo.getPointDatas(), firstZsgl);
  700. ztMap.forEach((wtId, ztInfo) -> {
  701. List<PointData> fss = fsMap.get(wtId).getPointDatas();
  702. List<PointData> gls = glMapMap.get(stId).get(wtId);
  703. //叶轮转速给定
  704. List<PointData> ylzsgds = collectYlzsgd.get(stId).get(wtId);
  705. calcTurbineXd(ztInfo, gls, fss, ylzsgds, czxd);
  706. });
  707. });
  708. return turbineZt;
  709. }
  710. public void calcTurbineXd(PointInfo ztInfo, List<PointData> gls, List<PointData> fss, List<PointData> ylzsgds, List<PointData> czxd) {
  711. Map<String, EquipmentModel> map = equipmentModelService.map();
  712. Double capacity = map.get(ztInfo.getSpare()).getPowerProduction();
  713. List<PointData> zts = ztInfo.getPointDatas();
  714. for (int i = 0; i < 5760; i++) {
  715. double ssgl = gls.get(i).getValue();
  716. double ssfs = fss.get(i).getValue();
  717. double ylzsgd = ylzsgds.get(i).getValue();
  718. if (czxd.get(i).isBooleanValue() && ssgl < capacity * 0.9) {
  719. if (ssfs >= 12) {
  720. zts.get(i).setDoubleValue(8.0);
  721. }
  722. if (ylzsgd != 0 && ylzsgd < 17.3) {//降出力
  723. zts.get(i).setDoubleValue(8.0);
  724. }
  725. }
  726. }
  727. }
  728. //此处加公式
  729. public List<PointData> calcStationXd2(PointInfo agcInfo, List<PointData> cxSnap, List<PointData> zsglSnap) {
  730. String stId = agcInfo.getStationId();
  731. List<PointData> agcSnap = agcInfo.getPointDatas();
  732. List<PointData> zsgl5s = get5Avg(zsglSnap);
  733. List<PointData> agc5s = get5Avg(agcSnap);
  734. List<PointData> cx5s = get5Avg(cxSnap);
  735. //场站限电
  736. List<PointData> czxd = zsglSnap.stream().map(s -> new PointData(s.getTs(), 0.0)).collect(Collectors.toList());
  737. //之前是否限电
  738. boolean iszqxd = false;
  739. for (int i = 0; i < 5760; i++) {
  740. boolean isxd = false;
  741. double zsgl = zsglSnap.get(i).getValue();
  742. double zsgl5 = zsgl5s.get(i).getValue();
  743. double agc = agcSnap.get(i).getValue();
  744. double agc5 = agc5s.get(i).getValue();
  745. double cxgl = cxSnap.get(i).getValue();
  746. double cxgl5 = cx5s.get(i).getValue();
  747. boolean k; //agc一直不变
  748. //第一次初始化复制实时数据到缓存
  749. if (CalcCache.powerCache.get(stId) == null || CalcCache.powerCache.get(stId) != agc) {
  750. CalcCache.powerCache.put(stId, agc);
  751. k = false;
  752. } else {
  753. k = true;
  754. }
  755. if (agc != 0) {
  756. //如果场站限电状态为不限电
  757. if (zsgl5 >= agcInfo.getSpare2() * 40) {
  758. if (agc5 <= cxgl5) {
  759. isxd = true;
  760. } else if (iszqxd) {
  761. if (zsgl / agc > 1.15) {
  762. isxd = true;
  763. } else if (zsgl > agc && agc5 - cxgl5 < 400) {
  764. isxd = true;
  765. }
  766. } else if (zsgl / agc >= 1.2 && agc - cxgl <= 200) {
  767. isxd = true;
  768. }
  769. } else if (k) {
  770. if (zsgl / agc >= 1.1 && agc5 - cxgl5 <= 200) {
  771. isxd = true;
  772. }
  773. }
  774. }
  775. czxd.get(i).setBooleanValue(isxd);
  776. iszqxd = isxd;
  777. }
  778. return czxd;
  779. }
  780. private List<PointData> get5Avg(List<PointData> pointData) {
  781. List<PointData> result = new ArrayList<>();
  782. for (int i = 0; i < pointData.size(); i++) {
  783. PointData data = new PointData();
  784. data.setTs(pointData.get(i).getTs());
  785. double v;
  786. if (i > 4) {
  787. 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;
  788. } else {
  789. v = pointData.get(i).getValue();
  790. }
  791. data.setDoubleValue(v);
  792. result.add(data);
  793. }
  794. return result;
  795. }
  796. public List<PointInfo> calcTurbineAizt(Date start, Date end, int interval) {
  797. List<PointInfo> entityAiZt = getEntity("AI422", "state");
  798. List<StateAi> list = stateAiService.list();
  799. Map<String, Map<Integer, Integer>> collectAi = list.stream().collect(Collectors.groupingBy(
  800. StateAi::getModel, Collectors.toMap(StateAi::getOriginalState, StateAi::getMappingState)));
  801. getSnapDataByEntity(entityAiZt, start, end, interval);
  802. for (PointInfo info : entityAiZt) {
  803. List<PointData> peek = info.getPointDatas().stream().peek(pd -> {
  804. Integer m = collectAi.get(info.getSpare()).get((int) pd.getValue());
  805. if (m == null) m = 2;
  806. pd.setDoubleValue(m);
  807. }).collect(Collectors.toList());
  808. info.setPointDatas(peek);
  809. }
  810. return entityAiZt;
  811. }
  812. public List<PointInfo> calcTurbineDizt(Date start, Date end, int interval) {
  813. List<PointInfo> entity = new ArrayList<>();
  814. List<PointInfo> entity0 = getEntity("MX000", "state");
  815. List<PointInfo> entity1 = getEntity("MX001", "state");
  816. List<PointInfo> entity2 = getEntity("MX002", "state");
  817. List<PointInfo> entity4 = getEntity("MX004", "state");
  818. List<PointInfo> entity6 = getEntity("MX006", "state");
  819. entity.addAll(entity0);
  820. entity.addAll(entity1);
  821. entity.addAll(entity2);
  822. entity.addAll(entity4);
  823. entity.addAll(entity6);
  824. List<String> keys = entity.stream().map(PointInfo::getPointKey).collect(Collectors.toList());
  825. Map<String, PointData> latest = adapter.getHistorySection(goldenUri(), String.join(",", keys), start.getTime());
  826. Map<String, Map<String, PointInfo>> wtUcPis = entity.stream().collect(Collectors.groupingBy(PointInfo::getTurbineId, Collectors.toMap(PointInfo::getUniformCode, Function.identity())));
  827. wtUcPis.forEach((wtId, map) -> {
  828. map.forEach((uc, pi) -> {
  829. List<PointData> raw = adapter.getHistoryRaw(goldenUri(), pi.getPointKey(), start.getTime(), end.getTime());
  830. List<PointData> ds = boolPointDatasFull(latest.get(pi.getPointKey()).isBooleanValue(), raw, start.getTime(), end.getTime(), interval);
  831. pi.setPointDatas(ds);
  832. ThreadUtil.sleep(20);
  833. });
  834. List<PointData> mx000 = map.get("MX000").getPointDatas();
  835. PointData data0;
  836. for (int i = 0; i < mx000.size(); i++) {
  837. data0 = mx000.get(i);
  838. if (data0.getValue() == 1.0) {
  839. data0.setDoubleValue(0.0);
  840. data0.setBooleanValue(false);
  841. } else if (map.get("MX001").getPointDatas().get(i).getValue() == 1.0) {
  842. data0.setDoubleValue(1.0);
  843. } else if (map.get("MX002").getPointDatas().get(i).getValue() == 1.0) {
  844. data0.setDoubleValue(2.0);
  845. } else if (map.get("MX004").getPointDatas().get(i).getValue() == 1.0) {
  846. data0.setDoubleValue(4.0);
  847. } else if (map.get("MX006").getPointDatas().get(i).getValue() == 1.0) {
  848. data0.setDoubleValue(6.0);
  849. } else {
  850. // System.out.println(wtId + data0.getTs() + ":di无对应状态!");
  851. data0.setDoubleValue(2.0);
  852. }
  853. }
  854. map.get("MX001").setPointDatas(null);
  855. map.get("MX002").setPointDatas(null);
  856. map.get("MX004").setPointDatas(null);
  857. map.get("MX006").setPointDatas(null);
  858. });
  859. return entity0;
  860. }
  861. public PointInfo calcTurbineAizt2(PointInfo entityAiZt, Map<String, Map<Integer, Integer>> collectAi, Date start, Date end, int interval) {
  862. if (entityAiZt == null) return null;
  863. getSnapDataByEntity(entityAiZt, start, end, interval);
  864. Map<Integer, Integer> integerMap = collectAi.get(entityAiZt.getSpare());
  865. List<PointData> data = entityAiZt.getPointDatas().stream().peek(pd -> {
  866. Integer m = integerMap.get((int) pd.getValue());
  867. if (m == null) m = 2;
  868. pd.setDoubleValue(m);
  869. }).collect(Collectors.toList());
  870. entityAiZt.setPointDatas(data);
  871. return entityAiZt;
  872. }
  873. public PointInfo calcTurbineDizt2(Map<String, PointInfo> wtucpi, Map<String, PointData> latest, Date start, Date end, int interval) {
  874. if (wtucpi == null) return null;
  875. wtucpi.forEach((uc, pi) -> {
  876. List<PointData> raw = adapter.getHistoryRaw(goldenUri(), pi.getPointKey(), start.getTime(), end.getTime());
  877. List<PointData> ds = boolPointDatasFull(latest.get(pi.getPointKey()).isBooleanValue(), raw, start.getTime(), end.getTime(), interval);
  878. pi.setPointDatas(ds);
  879. ThreadUtil.sleep(20);
  880. });
  881. PointInfo mx000 = wtucpi.get("MX000");
  882. PointData data0;
  883. for (int i = 0; i < mx000.getPointDatas().size(); i++) {
  884. data0 = mx000.getPointDatas().get(i);
  885. if (data0.getValue() == 1.0) {
  886. data0.setDoubleValue(0.0);
  887. data0.setBooleanValue(false);
  888. } else if (wtucpi.get("MX001").getPointDatas().get(i).getValue() == 1.0) {
  889. data0.setDoubleValue(1.0);
  890. } else if (wtucpi.get("MX002").getPointDatas().get(i).getValue() == 1.0) {
  891. data0.setDoubleValue(2.0);
  892. } else if (wtucpi.get("MX004").getPointDatas().get(i).getValue() == 1.0) {
  893. data0.setDoubleValue(4.0);
  894. } else if (wtucpi.get("MX006").getPointDatas().get(i).getValue() == 1.0) {
  895. data0.setDoubleValue(6.0);
  896. } else {
  897. System.out.println(mx000.getTurbineId() + data0.getTs() + ":di无对应状态!");
  898. data0.setDoubleValue(2.0);
  899. }
  900. }
  901. wtucpi.get("MX001").setPointDatas(null);
  902. wtucpi.get("MX002").setPointDatas(null);
  903. wtucpi.get("MX004").setPointDatas(null);
  904. wtucpi.get("MX006").setPointDatas(null);
  905. return mx000;
  906. }
  907. public List<PointData> boolPointDatasFull(boolean v, List<PointData> data, long startTime, long endTime, int interval) {
  908. int j = 0, m = 0;
  909. List<PointData> data2 = new ArrayList<>();
  910. for (long i = startTime; i < endTime; i += interval * 1000L) {
  911. PointData dt = new PointData();
  912. dt.setTs(i);
  913. data2.add(dt);
  914. //超出范围
  915. if (j >= data.size() - 1) continue;
  916. if (i < data.get(j).getTs()) {
  917. if (m == 0) {
  918. dt.setBooleanValue(v);
  919. } else {
  920. dt.setBooleanValue(data.get(j - 1).isBooleanValue());
  921. }
  922. } else {
  923. dt.setBooleanValue(data.get(j).isBooleanValue());
  924. j++;
  925. m++;
  926. }
  927. }
  928. return data2;
  929. }
  930. public void calcGlqxnh(Date start, Date end) {
  931. //风速
  932. List<PointInfo> entityFs = getEntity("AI066", "turbine");
  933. //功率
  934. List<PointInfo> entityGl = getEntity("AI114", "turbine");
  935. Map<String, PointInfo> glMap = entityGl.stream().collect(Collectors.toMap(PointInfo::getTurbineId, Function.identity()));
  936. Map<String, EquipmentModel> map = equipmentModelService.map();
  937. List<PointInfo> entity = new ArrayList<>();
  938. List<PointInfo> entity0 = getEntity("MX000", "state");
  939. List<PointInfo> entity1 = getEntity("MX001", "state");
  940. List<PointInfo> entity2 = getEntity("MX002", "state");
  941. List<PointInfo> entity4 = getEntity("MX004", "state");
  942. List<PointInfo> entity6 = getEntity("MX006", "state");
  943. entity.addAll(entity0);
  944. entity.addAll(entity1);
  945. entity.addAll(entity2);
  946. entity.addAll(entity4);
  947. entity.addAll(entity6);
  948. List<String> keys = entity.stream().map(PointInfo::getPointKey).collect(Collectors.toList());
  949. Map<String, PointData> latest = adapter.getHistorySection(goldenUri(), String.join(",", keys), start.getTime());
  950. Map<String, Map<String, PointInfo>> wtUcPis = entity.stream().collect(Collectors.groupingBy(PointInfo::getTurbineId, Collectors.toMap(PointInfo::getUniformCode, Function.identity())));
  951. List<PointInfo> entityAiZt = getEntity("AI422", "state");
  952. Map<String, PointInfo> aiztMap = entityAiZt.stream().collect(Collectors.toMap(PointInfo::getTurbineId, Function.identity()));
  953. List<StateAi> list = stateAiService.list();
  954. Map<String, Map<Integer, Integer>> collectAi = list.stream().collect(Collectors.groupingBy(
  955. StateAi::getModel, Collectors.toMap(StateAi::getOriginalState, StateAi::getMappingState)));
  956. // Map<String, Map<Double,Double>> fitMap = new HashMap<>();
  957. for (PointInfo ef : entityFs) {
  958. String wtId = ef.getTurbineId();
  959. PointInfo glInfo = glMap.get(wtId);
  960. getSnapDataByEntity(ef, start, end, 60);
  961. getSnapDataByEntity(glInfo, start, end, 60);
  962. List<PointData> peek = ef.getPointDatas().stream().peek(pd -> pd.setDoubleValue(
  963. NumberUtil.round(pd.getValue(), 2).doubleValue())).collect(Collectors.toList());
  964. ef.setPointDatas(peek);
  965. PointInfo turbineZt = calcTurbineDizt2(wtUcPis.get(wtId), latest, start, end, 60);
  966. PointInfo turbineAizt = calcTurbineAizt2(aiztMap.get(wtId), collectAi, start, end, 60);
  967. Map<Double, Double> fit = new TreeMap<>();
  968. if (turbineZt != null) {
  969. double capacity = map.get(turbineZt.getSpare()).getPowerProduction();
  970. fit = dataProcessAndFit(ef, glInfo, turbineZt, CalcCache.bzgl.get(turbineZt.getSpare()), capacity);
  971. // fitMap.put(wtId, fit);
  972. turbineZt.setPointDatas(null);
  973. }
  974. if (turbineAizt != null) {
  975. double capacity = map.get(turbineAizt.getSpare()).getPowerProduction();
  976. fit = dataProcessAndFit(ef, glInfo, turbineAizt, CalcCache.bzgl.get(turbineAizt.getSpare()), capacity);
  977. // fitMap.put(wtId, fit);
  978. turbineAizt.setPointDatas(null);
  979. }
  980. ef.setPointDatas(null);
  981. glInfo.setPointDatas(null);
  982. stringRedisTemplate.opsForValue().set("edfs:" + end.getMonth() + ":" + wtId, JSON.toJSONString(fit));
  983. }
  984. System.out.println("苹果天涯");
  985. // stringRedisTemplate.opsForValue().set("glqxnh", JSON.toJSONString(fitMap));
  986. }
  987. public Map<Double, Double> dataProcessAndFit(PointInfo fsPI, PointInfo glPI, PointInfo ztPI, Map<Double, Double> modelPowerMap, double capacity) {
  988. List<PointData> fsPds = fsPI.getPointDatas();
  989. List<PointData> glPds = glPI.getPointDatas();
  990. List<PointData> ztPds = ztPI.getPointDatas();
  991. WeightedObservedPoints points = new WeightedObservedPoints();
  992. //数据过滤 0正常,1过滤掉
  993. if (fsPds.size() != glPds.size() || glPds.size() != ztPds.size()) return new HashMap<>();
  994. Double bzv;
  995. for (int i = 0; i < ztPds.size(); i++) {
  996. double fs = fsPds.get(i).getValue();
  997. double gl = glPds.get(i).getValue();
  998. // 过滤非并网值 风机状态不等于2
  999. if (ztPds.get(i).getValue() != 2.0) {
  1000. //
  1001. } else if (fs < 0 || fs > 25 || gl < 0 || gl > capacity * 1.05) {// 按给定风速功率过滤
  1002. //
  1003. } else {
  1004. bzv = modelPowerMap.get(fs);
  1005. if (bzv == null || bzv == 0.0) {
  1006. points.add(fs, gl);
  1007. continue;
  1008. }
  1009. double k = gl / bzv; //功率曲线偏差: 保证功率/实际功率 k:偏差百分比
  1010. if (k < 0.61 || k > 1.25) {
  1011. //
  1012. } else {
  1013. points.add(fs, gl);
  1014. }
  1015. }
  1016. }
  1017. Map<Double, Double> glyc = new TreeMap<>();
  1018. PolynomialCurveFitter fitter = PolynomialCurveFitter.create(4);
  1019. try {
  1020. double[] result = fitter.fit(points.toList());
  1021. double min = calcMin(result);
  1022. System.out.println(ztPI.getTurbineId());
  1023. System.out.println(ztPI.getSpare());
  1024. System.out.println(min);
  1025. double[] max = calcMax(result);
  1026. if (min == 0 || max[0] == 0 || max[1] == 0) {
  1027. System.out.println();
  1028. }
  1029. System.out.println(max[0] + "," + max[1]);
  1030. double ru;
  1031. for (double i = 0; i < 25; i += 0.01) {
  1032. i = NumberUtil.round(i, 2).doubleValue();
  1033. if (i < 3) {
  1034. ru = 0.0;
  1035. } else if (i < min) {
  1036. ru = modelPowerMap.get(i);
  1037. } else if (i < max[0]) {
  1038. ru = calcPoly(i, result);
  1039. } else {
  1040. ru = max[1];
  1041. }
  1042. glyc.put(i, NumberUtil.round(ru, 2).doubleValue());
  1043. }
  1044. } catch (Exception e) {
  1045. System.out.println(ztPI.getTurbineId());
  1046. System.out.println(ztPI.getSpare());
  1047. System.out.println(e.getMessage());
  1048. }
  1049. return glyc;
  1050. }
  1051. public double calcMin(double[] coef) {
  1052. double minX = 0, minY = 9000;
  1053. for (double x = 0; x < 8; x += 0.01) {
  1054. double y = calcPoly(x, coef);
  1055. if (y < minY) {
  1056. minY = y;
  1057. minX = x;
  1058. }
  1059. }
  1060. return minX;
  1061. }
  1062. public double[] calcMax(double[] coef) {
  1063. double[] a = new double[2];
  1064. for (double x = 8; x < 16; x += 0.01) {
  1065. double y = calcPoly(x, coef);
  1066. if (y > a[1]) {
  1067. a[0] = x;
  1068. a[1] = y;
  1069. }
  1070. }
  1071. return a;
  1072. }
  1073. public void printResult(double[] r, double accuracy) {
  1074. for (double index = 0; index < 25; index += accuracy) {
  1075. System.out.println("[" + index + "," + calcPoly(index, r) + "],");
  1076. }
  1077. }
  1078. public void getSnapDataByEntity(List<PointInfo> entity, URI uri, Date start, Date end, int interval) {
  1079. for (PointInfo info : entity) {
  1080. List<PointData> snap = adapter.getHistorySnap(uri, info.getPointKey(), start.getTime(), end.getTime(), interval);
  1081. ThreadUtil.sleep(20);
  1082. info.setPointDatas(snap);
  1083. }
  1084. }
  1085. public void getSnapDataByEntity(PointInfo entity, URI uri, Date start, Date end, int interval) {
  1086. getSnapDataByEntity(Collections.singletonList(entity), uri, start, end, interval);
  1087. }
  1088. public void getSnapDataByEntity(List<PointInfo> entity, Date start, Date end, int interval) {
  1089. getSnapDataByEntity(entity, goldenUri(), start, end, interval);
  1090. }
  1091. public void getSnapDataByEntity(PointInfo entity, Date start, Date end, int interval) {
  1092. getSnapDataByEntity(entity, goldenUri(), start, end, interval);
  1093. }
  1094. public double calcPoly(String wtId, double value) {
  1095. return CalcCache.fitcoef.get(wtId).get(value);
  1096. }
  1097. public double calcPoly(double x, double[] factor) {
  1098. double y = 0;
  1099. for (int deg = 0; deg < factor.length; deg++) {
  1100. y += Math.pow(x, deg) * factor[deg];
  1101. }
  1102. return y;
  1103. }
  1104. //切入切出风速
  1105. public void cutInWindSpeed() {
  1106. //date当天零点
  1107. DateTime timeNow = DateUtil.beginOfDay(new Date());
  1108. //date昨天零点
  1109. DateTime timeBegin = DateUtil.offsetDay(timeNow, -1);
  1110. List<PointInfo> turbineZt = calcTurbineAizt(timeBegin, timeNow, 60);
  1111. List<PointInfo> turbineZtDI = calcTurbineDizt(timeBegin, timeNow, 60);
  1112. turbineZt.addAll(turbineZtDI);
  1113. //所有风机的风速测点
  1114. QueryWrapper<PointInfo> queryWrapper = new QueryWrapper<>();
  1115. queryWrapper.eq("uniform_code", "AI066");
  1116. List<PointInfo> pointInfos = getEntity("AI066", "turbine");
  1117. // List<PointInfo> pointInfos = pointInfo.selectList(queryWrapper);
  1118. //定义切入风速map集合
  1119. ConcurrentHashMap<String, ConcurrentHashMap<String, Double>> mapIn = new ConcurrentHashMap<>();
  1120. //定义切出风速集合
  1121. ConcurrentHashMap<String, ConcurrentHashMap<String, Double>> mapOut = new ConcurrentHashMap<>();
  1122. //插入集合
  1123. List<TurbineInfoDay > interest = new ArrayList<>();
  1124. //更新集合
  1125. List<TurbineInfoDay > update = new ArrayList<>();
  1126. //遍历每台风机,取出每台风机的pointDatas
  1127. for (PointInfo turbine : turbineZt) {
  1128. mapIn.put(turbine.getTurbineId(), new ConcurrentHashMap<>());
  1129. mapOut.put(turbine.getTurbineId(), new ConcurrentHashMap<>());
  1130. List<PointData> pointDatas = turbine.getPointDatas();
  1131. //风速测点key
  1132. String windSpeedKey = null;
  1133. //遍历pointInfos,找出pointData1的id和pointInfos中相等的,取出pointInfo的pointKey
  1134. for (PointInfo info : pointInfos) {
  1135. if (Objects.equals(turbine.getTurbineId(), info.getTurbineId())) {
  1136. windSpeedKey = info.getPointKey();
  1137. }
  1138. }
  1139. //遍历pointDatas,取出当前状态和后一个状态
  1140. for (int k = 1; k < pointDatas.size(); k++) {
  1141. PointData pointData1 = pointDatas.get(k - 1);
  1142. PointData pointData2 = pointDatas.get(k);
  1143. double value1 = pointData1.getValue();
  1144. double value2 = pointData2.getValue();
  1145. //切入时间
  1146. long ts1;
  1147. //切出时间
  1148. long ts2;
  1149. //如果1的值为0,2的值为2,则为切入状态
  1150. if (value1 == 0 && value2 == 2) {
  1151. ts1 = pointData2.getTs();
  1152. // String stringDate = DateUtil.date(ts1).toString("yyyy-MM-dd HH:mm:ss");
  1153. // System.out.println(turbine.getTurbineId() + "切入" + stringDate);
  1154. //切入时间前5分钟
  1155. long ts11 = ts1 - 300000;
  1156. //根据风速key,从适配器取切入前5分钟的所有风速
  1157. List<PointData> pointDatas1 = adapter.getHistorySnap(goldenUri(), windSpeedKey, ts11, ts1, 10);
  1158. //平均所有风速即为平均切入风速
  1159. double avgCutInWindSpeed = pointDatas1.stream().mapToDouble(PointData::getValue).average().orElse(0);
  1160. // System.out.println(turbine.getTurbineId() + "切入风速" + avgCutInWindSpeed);
  1161. //存入map集合,外层key为turbineId,内层key为ts1,value为平均切入风速
  1162. mapIn.get(turbine.getTurbineId()).put(String.valueOf(ts1), avgCutInWindSpeed);
  1163. }
  1164. //切出
  1165. if (value1 == 2 && value2 != 2) {
  1166. ts2 = pointData2.getTs();
  1167. String stringDate = DateUtil.date(ts2).toString("yyyy-MM-dd HH:mm:ss");
  1168. // System.out.println(turbine.getTurbineId() + "切出" + stringDate);
  1169. //切出时间前5分钟
  1170. long ts22 = ts2 - 300000;
  1171. List<PointData> pointDatas2 = adapter.getHistorySnap(goldenUri(), windSpeedKey, ts22, ts2, 10);
  1172. double avgCutOutWindSpeed = pointDatas2.stream().mapToDouble(PointData::getValue).average().orElse(0);
  1173. // System.out.println(turbine.getTurbineId() + "切出风速" + avgCutOutWindSpeed);
  1174. //存入map集合,外层key为turbineId,内层key为ts2,value为平均切出风速
  1175. mapOut.get(turbine.getTurbineId()).put(String.valueOf(ts2), avgCutOutWindSpeed);
  1176. }
  1177. }
  1178. //遍历map集合,取出每个风机的所有切入风速,算一个平均值
  1179. ConcurrentHashMap<String, Double> mapTurbineValues = mapIn.get(turbine.getTurbineId());
  1180. //平均切入风速
  1181. double avgCutInWindSpeed = 0;
  1182. if (mapTurbineValues != null) {
  1183. AtomicReference<Double> sum = new AtomicReference<>(0.0);
  1184. //遍历mapTurbineValues,V大于5的舍弃,剩余算平均值
  1185. for (String key : mapTurbineValues.keySet()) {
  1186. if (mapTurbineValues.get(key) > 5) {
  1187. mapTurbineValues.remove(key);
  1188. }
  1189. }
  1190. mapTurbineValues.forEach((k1, v) -> {
  1191. sum.updateAndGet(v1 -> v1 + v);
  1192. });
  1193. //如果值为空则置0
  1194. if (sum.get() == 0) {
  1195. avgCutInWindSpeed = 0;
  1196. } else {
  1197. avgCutInWindSpeed = sum.get() / mapTurbineValues.size();
  1198. }
  1199. System.out.println(turbine.getTurbineId() + "切入平均风速" + avgCutInWindSpeed + "时间" + timeBegin);
  1200. }
  1201. //遍历map集合,取出每个风机的所有切出风速,算一个平均值
  1202. ConcurrentHashMap<String, Double> mapTurbineValues2 = mapOut.get(turbine.getTurbineId());
  1203. double avgCutOutWindSpeed;
  1204. if (mapTurbineValues2 != null) {
  1205. AtomicReference<Double> sum = new AtomicReference<>(0.0);
  1206. for (String key : mapTurbineValues2.keySet()) {
  1207. if (mapTurbineValues2.get(key) > 5) {
  1208. mapTurbineValues2.remove(key);
  1209. }
  1210. }
  1211. mapTurbineValues2.forEach((k2, v) -> {
  1212. sum.updateAndGet(v1 -> v1 + v);
  1213. });
  1214. if (sum.get() == 0) {
  1215. avgCutOutWindSpeed = 0;
  1216. } else {
  1217. avgCutOutWindSpeed = sum.get() / mapTurbineValues2.size();
  1218. }
  1219. System.out.println(turbine.getTurbineId() + "切出平均风速" + avgCutOutWindSpeed + "时间" + timeBegin);
  1220. }
  1221. //存入数据库
  1222. String turbineId = turbine.getTurbineId();
  1223. Date jdkDate = timeBegin.toJdkDate();
  1224. System.out.println(turbineId + " " + jdkDate);
  1225. QueryWrapper<TurbineInfoDay> turbineInfoDayQueryWrapper = new QueryWrapper<>();
  1226. turbineInfoDayQueryWrapper.eq("turbine_id", turbine.getTurbineId());
  1227. turbineInfoDayQueryWrapper.eq("record_date", timeBegin.toJdkDate());
  1228. TurbineInfoDay one = turbineInfoDayService.getOne(turbineInfoDayQueryWrapper);
  1229. System.out.println(one);
  1230. if (one == null) {
  1231. TurbineInfoDay turbineInfoDay = new TurbineInfoDay();
  1232. turbineInfoDay.setTurbineId(turbine.getTurbineId());
  1233. turbineInfoDay.setRecordDate(timeBegin.toJdkDate());
  1234. turbineInfoDay.setXfqrfs(avgCutInWindSpeed);
  1235. System.out.println(turbineInfoDay);
  1236. interest.add(turbineInfoDay);
  1237. // turbineInfoDayService.save(turbineInfoDay);
  1238. } else {
  1239. one.setXfqrfs(avgCutInWindSpeed);
  1240. System.out.println(one);
  1241. // turbineInfoDayService.updateById(one);
  1242. update.add(one);
  1243. }
  1244. }
  1245. // //批量插入
  1246. // turbineInfoDayService.saveBatch(interest);
  1247. // //批量更新
  1248. // turbineInfoDayService.updateBatchById(update);
  1249. // //批量插入或更新
  1250. turbineInfoDayService.saveOrUpdateBatch(interest);
  1251. turbineInfoDayService.saveOrUpdateBatch(update);
  1252. }
  1253. //算额定风速
  1254. public void calcGlqxnh_edfs() {
  1255. //date当天零点
  1256. Date end = DateUtil.beginOfDay(new Date()).toJdkDate();
  1257. //date昨天零点
  1258. Date start = DateUtil.offsetDay(end, -1).toJdkDate();
  1259. //风速
  1260. List<PointInfo> entityFs = getEntity("AI066", "turbine");
  1261. //功率
  1262. List<PointInfo> entityGl = getEntity("AI114", "turbine");
  1263. Map<String, PointInfo> glMap = entityGl.stream().collect(Collectors.toMap(PointInfo::getTurbineId, Function.identity()));
  1264. Map<String, EquipmentModel> map = equipmentModelService.map();
  1265. List<PointInfo> entity = new ArrayList<>();
  1266. List<PointInfo> entity0 = getEntity("MX000", "state");
  1267. List<PointInfo> entity1 = getEntity("MX001", "state");
  1268. List<PointInfo> entity2 = getEntity("MX002", "state");
  1269. List<PointInfo> entity4 = getEntity("MX004", "state");
  1270. List<PointInfo> entity6 = getEntity("MX006", "state");
  1271. entity.addAll(entity0);
  1272. entity.addAll(entity1);
  1273. entity.addAll(entity2);
  1274. entity.addAll(entity4);
  1275. entity.addAll(entity6);
  1276. List<String> keys = entity.stream().map(PointInfo::getPointKey).collect(Collectors.toList());
  1277. Map<String, PointData> latest = adapter.getHistorySection(goldenUri(), String.join(",", keys), start.getTime());
  1278. Map<String, Map<String, PointInfo>> wtUcPis = entity.stream().collect(Collectors.groupingBy(PointInfo::getTurbineId, Collectors.toMap(PointInfo::getUniformCode, Function.identity())));
  1279. List<PointInfo> entityAiZt = getEntity("AI422", "state");
  1280. Map<String, PointInfo> aiztMap = entityAiZt.stream().collect(Collectors.toMap(PointInfo::getTurbineId, Function.identity()));
  1281. List<StateAi> list = stateAiService.list();
  1282. Map<String, Map<Integer, Integer>> collectAi = list.stream().collect(Collectors.groupingBy(
  1283. StateAi::getModel, Collectors.toMap(StateAi::getOriginalState, StateAi::getMappingState)));
  1284. // Map<String, Map<Double,Double>> fitMap = new HashMap<>();
  1285. int i = 0;
  1286. //插入集合
  1287. List<TurbineInfoDay > interest = new ArrayList<>();
  1288. //更新集合
  1289. List<TurbineInfoDay > update = new ArrayList<>();
  1290. for (PointInfo ef : entityFs) {
  1291. String wtId = ef.getTurbineId();
  1292. PointInfo glInfo = glMap.get(wtId);
  1293. getSnapDataByEntity(ef, start, end, 60);
  1294. getSnapDataByEntity(glInfo, start, end, 60);
  1295. List<PointData> peek = ef.getPointDatas().stream().peek(pd -> pd.setDoubleValue(
  1296. NumberUtil.round(pd.getValue(), 2).doubleValue())).collect(Collectors.toList());
  1297. ef.setPointDatas(peek);
  1298. try {
  1299. PointInfo turbineZt = calcTurbineDizt2(wtUcPis.get(wtId), latest, start, end, 60);
  1300. PointInfo turbineAizt = calcTurbineAizt2(aiztMap.get(wtId), collectAi, start, end, 60);
  1301. Map<Double, Double> fit = new TreeMap<>();
  1302. double capacity = 2000;
  1303. if (turbineZt != null) {
  1304. capacity = map.get(turbineZt.getSpare()).getPowerProduction();
  1305. fit = dataProcessAndFit(ef, glInfo, turbineZt, CalcCache.bzgl.get(turbineZt.getSpare()), capacity);
  1306. // fitMap.put(wtId, fit);
  1307. turbineZt.setPointDatas(null);
  1308. }
  1309. if (turbineAizt != null) {
  1310. capacity = map.get(turbineAizt.getSpare()).getPowerProduction();
  1311. fit = dataProcessAndFit(ef, glInfo, turbineAizt, CalcCache.bzgl.get(turbineAizt.getSpare()), capacity);
  1312. // fitMap.put(wtId, fit);
  1313. turbineAizt.setPointDatas(null);
  1314. }
  1315. ef.setPointDatas(null);
  1316. glInfo.setPointDatas(null);
  1317. //存redis
  1318. // stringRedisTemplate.opsForValue().set("glqxnh:" + end.getMonth() + ":" + wtId, JSON.toJSONString(fit));
  1319. //符合条件的风速和
  1320. List<Double> listDouble = new ArrayList<>();
  1321. //遍历fit集合,取出值与capacity相等的key
  1322. if (!fit.isEmpty()) {
  1323. for (double key : fit.keySet()) {
  1324. Double v = fit.get(key);
  1325. //如果v-capacity的值小于10大于-10,则把key加入listDouble
  1326. if ((Math.abs(v - capacity) <= 10) && Math.abs(v - capacity) >= -10) {
  1327. listDouble.add(key);
  1328. }
  1329. }
  1330. }
  1331. //取出listDouble的值求平均值
  1332. double agValue = 0;
  1333. if (!listDouble.isEmpty()) {
  1334. for (Double aDouble : listDouble) {
  1335. agValue += aDouble;
  1336. }
  1337. agValue = agValue / listDouble.size();
  1338. }
  1339. System.out.println("wtId:" + wtId + "agValue平均额定风速:" + agValue);
  1340. //存入数据库
  1341. System.out.println(wtId + " " + start);
  1342. QueryWrapper<TurbineInfoDay> turbineInfoDayQueryWrapper = new QueryWrapper<>();
  1343. turbineInfoDayQueryWrapper.eq("turbine_id", wtId);
  1344. turbineInfoDayQueryWrapper.eq("record_date", start);
  1345. TurbineInfoDay one = turbineInfoDayService.getOne(turbineInfoDayQueryWrapper);
  1346. System.out.println(one);
  1347. if (one == null) {
  1348. TurbineInfoDay turbineInfoDay = new TurbineInfoDay();
  1349. turbineInfoDay.setTurbineId(wtId);
  1350. turbineInfoDay.setRecordDate(start);
  1351. turbineInfoDay.setEdfs(agValue);
  1352. System.out.println(turbineInfoDay);
  1353. // turbineInfoDayService.save(turbineInfoDay);
  1354. interest.add(turbineInfoDay);
  1355. } else {
  1356. one.setEdfs(agValue);
  1357. System.out.println(one);
  1358. // turbineInfoDayService.updateById(one);
  1359. update.add(one);
  1360. }
  1361. }catch (Exception e) {
  1362. System.out.println(wtId + " " + start);
  1363. }
  1364. }
  1365. // //批量插入
  1366. // turbineInfoDayService.saveBatch(interest);
  1367. // //批量更新
  1368. // turbineInfoDayService.updateBatchById(update);
  1369. // //批量插入或更新
  1370. turbineInfoDayService.saveOrUpdateBatch(interest);
  1371. turbineInfoDayService.saveOrUpdateBatch(update);
  1372. // stringRedisTemplate.opsForValue().set("glqxnh", JSON.toJSONString(fitMap));
  1373. }
  1374. //环境温度
  1375. public void ambientTemperature() {
  1376. //date当天零点
  1377. DateTime timeNow = DateUtil.beginOfDay(new Date());
  1378. //date昨天零点
  1379. DateTime timeBegin = DateUtil.offsetDay(timeNow, -1);
  1380. //所有风机的温度
  1381. QueryWrapper<PointInfo> queryWrapper = new QueryWrapper<>();
  1382. queryWrapper.eq("uniform_code", "AI072");
  1383. List<PointInfo> pointInfos = getEntity("AI072", "turbine");
  1384. //插入集合
  1385. List<TurbineInfoDay > interest = new ArrayList<>();
  1386. //更新集合
  1387. List<TurbineInfoDay > update = new ArrayList<>();
  1388. //遍历pointInfos
  1389. for (PointInfo turbine : pointInfos) {
  1390. List<PointData> pointDatas = adapter.getHistorySnap(goldenUri(), turbine.getPointKey(), timeBegin.getTime(), timeNow.getTime(), 10);
  1391. //遍历pointDatas,取出数据算平均值
  1392. double avgCutInWindSpeed = 0;
  1393. for (PointData pointData : pointDatas) {
  1394. avgCutInWindSpeed += pointData.getValue();
  1395. }
  1396. avgCutInWindSpeed /= pointDatas.size();
  1397. //存入数据库
  1398. String turbineId = turbine.getTurbineId();
  1399. Date jdkDate = timeBegin.toJdkDate();
  1400. System.out.println(turbineId + " " + jdkDate);
  1401. QueryWrapper<TurbineInfoDay> turbineInfoDayQueryWrapper = new QueryWrapper<>();
  1402. turbineInfoDayQueryWrapper.eq("turbine_id", turbine.getTurbineId());
  1403. turbineInfoDayQueryWrapper.eq("record_date", timeBegin.toJdkDate());
  1404. TurbineInfoDay one = turbineInfoDayService.getOne(turbineInfoDayQueryWrapper);
  1405. System.out.println(one);
  1406. if (one == null) {
  1407. TurbineInfoDay turbineInfoDay = new TurbineInfoDay();
  1408. turbineInfoDay.setTurbineId(turbine.getTurbineId());
  1409. turbineInfoDay.setRecordDate(timeBegin.toJdkDate());
  1410. turbineInfoDay.setHjwd(avgCutInWindSpeed);
  1411. System.out.println(turbineInfoDay);
  1412. interest.add(turbineInfoDay);
  1413. // turbineInfoDayService.save(turbineInfoDay);
  1414. } else {
  1415. one.setHjwd(avgCutInWindSpeed);
  1416. System.out.println(one);
  1417. update.add(one);
  1418. // turbineInfoDayService.updateById(one);
  1419. }
  1420. }
  1421. // //批量插入
  1422. // turbineInfoDayService.saveBatch(interest);
  1423. // //批量更新
  1424. // turbineInfoDayService.updateBatchById(update);
  1425. // //批量插入或更新
  1426. turbineInfoDayService.saveOrUpdateBatch(interest);
  1427. turbineInfoDayService.saveOrUpdateBatch(update);
  1428. }
  1429. }