WindMachineService.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. package com.gyee.runeconomy.service.WindDirection;
  2. import com.gyee.runeconomy.init.CacheContext;
  3. import com.gyee.runeconomy.model.auto.*;
  4. import com.gyee.runeconomy.service.auto.IStationInfoDayService;
  5. import com.gyee.runeconomy.service.auto.ITurbineInfoDayService;
  6. import org.springframework.stereotype.Service;
  7. import javax.annotation.Resource;
  8. import java.time.LocalDate;
  9. import java.time.ZoneId;
  10. import java.time.ZonedDateTime;
  11. import java.time.format.DateTimeFormatter;
  12. import java.util.*;
  13. import java.util.stream.Collectors;
  14. @Service
  15. public class WindMachineService {
  16. @Resource
  17. private ITurbineInfoDayService turbineInfoDayService;
  18. @Resource
  19. private IStationInfoDayService stationInfoDayService;
  20. public Object machine(String wpid,int year) throws Exception {
  21. // 获取当前日期
  22. int currentYear = LocalDate.now().getYear();
  23. currentYear = year;
  24. List<ProBasicEquipment> collect = CacheContext.wtls.stream()
  25. .filter(wt -> wpid.equals(wt.getWindpowerstationId()))
  26. .collect(Collectors.toList());
  27. Map<String, List> monthlyData = new LinkedHashMap<>();
  28. // 风机循环:每台风机每个月一个 WindData 对象
  29. for (ProBasicEquipment eq : collect) {
  30. // 获取当前年份的开始日期(1月1日)
  31. LocalDate startOfYear = LocalDate.of(currentYear, 1, 1);
  32. // 获取当前年份的结束日期(12月31日)
  33. LocalDate endOfYear = LocalDate.of(currentYear, 12, 31);
  34. // 使用 DateTimeFormatter 格式化日期为字符串
  35. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  36. // 转换为字符串
  37. String startOfYearStr = startOfYear.format(formatter);
  38. String endOfYearStr = endOfYear.format(formatter);
  39. List<TurbineInfoDay> turbineList = turbineInfoDayService.getTurbineList(eq.getId(), startOfYearStr, endOfYearStr);
  40. List<StationInfoDay> infoDays = stationInfoDayService.getTurbineList(wpid, startOfYearStr, endOfYearStr);
  41. List<String> zb = new ArrayList<>();
  42. zb.add("fsavg");
  43. zb.add("cftfsavg");
  44. zb.add("fspc");
  45. zb.add("fxavg");
  46. zb.add("cftfxavg");
  47. zb.add("fxpc");
  48. for (String z : zb) {
  49. // 循环从1月到12月
  50. List<Double> result = new ArrayList<>();
  51. String zbwz = "";
  52. for (int month = 1; month <= 12; month++) {
  53. // 过滤 turbineList 中的记录,筛选出对应月份的数据
  54. int finalMonth = month;
  55. List<TurbineInfoDay> filteredList = turbineList.stream()
  56. .filter(turbine -> {
  57. // 获取 turbineInfoDay 的 recordDate(类型为 LocalDate)
  58. Date dateFromTurbine = turbine.getRecordDate();
  59. // 将 Date 转换为 LocalDate
  60. ZonedDateTime zonedDateTime = dateFromTurbine.toInstant().atZone(ZoneId.systemDefault());
  61. LocalDate recordDate = zonedDateTime.toLocalDate();
  62. // 获取记录的月份(注意,月份从 1 开始,1 表示1月)
  63. int recordMonth = recordDate.getMonthValue();
  64. // 判断记录的月份是否等于给定的 time
  65. return recordMonth == finalMonth; // 比较 int 类型的月份和 time 变量
  66. })
  67. .collect(Collectors.toList());
  68. List<StationInfoDay> dayfilteredList = infoDays.stream()
  69. .filter(turbine -> {
  70. // 获取 turbineInfoDay 的 recordDate(类型为 LocalDate)
  71. LocalDate dateFromTurbine = turbine.getRecordDate();
  72. // 获取记录的月份(注意,月份从 1 开始,1 表示1月)
  73. int recordMonth = dateFromTurbine.getMonthValue();
  74. // 判断记录的月份是否等于给定的 time
  75. return recordMonth == finalMonth; // 比较 int 类型的月份和 time 变量
  76. })
  77. .collect(Collectors.toList());
  78. // 风速
  79. double fsaverage = filteredList.stream()
  80. .mapToDouble(turbineInfoDay -> Optional.ofNullable(turbineInfoDay.getPjfs()).map(Number::doubleValue).orElse(0.0))
  81. .average()
  82. .orElse(0.0); // 默认值0.0
  83. // 风向
  84. double fxaverage = filteredList.stream()
  85. .mapToDouble(turbineInfoDay -> Optional.ofNullable(turbineInfoDay.getFx()).map(Number::doubleValue).orElse(0.0))
  86. .average()
  87. .orElse(0.0); // 默认值0.0
  88. // 测风塔风速
  89. double cftfsaverage = dayfilteredList.stream()
  90. .mapToDouble(turbineInfoDay -> Optional.ofNullable(turbineInfoDay.getCftfs()).map(Number::doubleValue).orElse(0.0))
  91. .average()
  92. .orElse(0.0); // 默认值0.0
  93. // 测风塔风向
  94. double cftfxaverage = dayfilteredList.stream()
  95. .mapToDouble(turbineInfoDay -> Optional.ofNullable(turbineInfoDay.getCftfx()).map(Number::doubleValue).orElse(0.0))
  96. .average()
  97. .orElse(0.0); // 默认值0.0
  98. double zbsj = 0.0;
  99. if (z.toString().equals("fsavg")) {
  100. // 保留两位小数并转换回 double
  101. zbsj = Double.parseDouble(String.format("%.2f", fsaverage));
  102. zbwz = "风速";
  103. } else if (z.toString().equals("fxavg")) {
  104. zbsj = Double.parseDouble(String.format("%.2f", fxaverage));
  105. zbwz = "风向";
  106. } else if (z.toString().equals("cftfsavg")) {
  107. zbsj = Double.parseDouble(String.format("%.2f", cftfsaverage));
  108. zbwz = "测风塔风速";
  109. } else if (z.toString().equals("cftfxavg")) {
  110. zbsj = Double.parseDouble(String.format("%.2f", cftfxaverage));
  111. zbwz = "测风塔风向";
  112. } else if (z.toString().equals("fspc")) {
  113. zbsj = Deviation.calculateSpeedDeviation(
  114. fsaverage,
  115. cftfsaverage
  116. );
  117. zbwz = "风速偏差";
  118. } else if (z.toString().equals("fxpc")) {
  119. zbsj = Deviation.calculateDirectionDeviation(
  120. fxaverage,
  121. cftfxaverage
  122. );
  123. zbwz = "风向偏差";
  124. }
  125. result.add(zbsj);
  126. }
  127. monthlyData.put(eq.getAname() + zbwz, result);
  128. }
  129. }
  130. return monthlyData;
  131. }
  132. /**
  133. * 气象单机偏差分析
  134. * @param wpid
  135. * @return
  136. * @throws Exception
  137. */
  138. public Object weathermachine(String wpid,int year) throws Exception {
  139. // 获取当前日期
  140. int currentYear = LocalDate.now().getYear();
  141. currentYear= year;
  142. List<ProBasicEquipment> collect = CacheContext.wtls.stream()
  143. .filter(wt -> wpid.equals(wt.getWindpowerstationId()))
  144. .collect(Collectors.toList());
  145. Map<String, List> monthlyData = new LinkedHashMap<>();
  146. // 风机循环:每台风机每个月一个 WindData 对象
  147. for (ProBasicEquipment eq : collect) {
  148. // 获取当前年份的开始日期(1月1日)
  149. LocalDate startOfYear = LocalDate.of(currentYear, 1, 1);
  150. // 获取当前年份的结束日期(12月31日)
  151. LocalDate endOfYear = LocalDate.of(currentYear, 12, 31);
  152. // 使用 DateTimeFormatter 格式化日期为字符串
  153. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  154. // 转换为字符串
  155. String startOfYearStr = startOfYear.format(formatter);
  156. String endOfYearStr = endOfYear.format(formatter);
  157. List<TurbineInfoDay> turbineList = turbineInfoDayService.getTurbineList(eq.getId(), startOfYearStr, endOfYearStr);
  158. List<StationInfoDay> infoDays = stationInfoDayService.getTurbineList(wpid, startOfYearStr, endOfYearStr);
  159. List<String> zb = new ArrayList<>();
  160. zb.add("fsavg");
  161. zb.add("cftfsavg");
  162. zb.add("fspc");
  163. zb.add("fxavg");
  164. zb.add("cftfxavg");
  165. zb.add("fxpc");
  166. for (String z : zb) {
  167. // 循环从1月到12月
  168. List<Double> result = new ArrayList<>();
  169. String zbwz = "";
  170. for (int month = 1; month <= 12; month++) {
  171. // 过滤 turbineList 中的记录,筛选出对应月份的数据
  172. int finalMonth = month;
  173. List<TurbineInfoDay> filteredList = turbineList.stream()
  174. .filter(turbine -> {
  175. // 获取 turbineInfoDay 的 recordDate(类型为 LocalDate)
  176. Date dateFromTurbine = turbine.getRecordDate();
  177. // 将 Date 转换为 LocalDate
  178. ZonedDateTime zonedDateTime = dateFromTurbine.toInstant().atZone(ZoneId.systemDefault());
  179. LocalDate recordDate = zonedDateTime.toLocalDate();
  180. // 获取记录的月份(注意,月份从 1 开始,1 表示1月)
  181. int recordMonth = recordDate.getMonthValue();
  182. // 判断记录的月份是否等于给定的 time
  183. return recordMonth == finalMonth; // 比较 int 类型的月份和 time 变量
  184. })
  185. .collect(Collectors.toList());
  186. List<StationInfoDay> dayfilteredList = infoDays.stream()
  187. .filter(turbine -> {
  188. // 获取 turbineInfoDay 的 recordDate(类型为 LocalDate)
  189. LocalDate dateFromTurbine = turbine.getRecordDate();
  190. // 获取记录的月份(注意,月份从 1 开始,1 表示1月)
  191. int recordMonth = dateFromTurbine.getMonthValue();
  192. // 判断记录的月份是否等于给定的 time
  193. return recordMonth == finalMonth; // 比较 int 类型的月份和 time 变量
  194. })
  195. .collect(Collectors.toList());
  196. // 风速
  197. double fsaverage = filteredList.stream()
  198. .mapToDouble(turbineInfoDay -> Optional.ofNullable(turbineInfoDay.getPjfs()).map(Number::doubleValue).orElse(0.0))
  199. .average()
  200. .orElse(0.0); // 默认值0.0
  201. // 风向
  202. double fxaverage = filteredList.stream()
  203. .mapToDouble(turbineInfoDay -> Optional.ofNullable(turbineInfoDay.getFx()).map(Number::doubleValue).orElse(0.0))
  204. .average()
  205. .orElse(0.0); // 默认值0.0
  206. // 测风塔风速
  207. double cftfsaverage = dayfilteredList.stream()
  208. .mapToDouble(turbineInfoDay -> Optional.ofNullable(turbineInfoDay.getCftfs()).map(Number::doubleValue).orElse(0.0))
  209. .average()
  210. .orElse(0.0); // 默认值0.0
  211. // 测风塔风向
  212. double cftfxaverage = dayfilteredList.stream()
  213. .mapToDouble(turbineInfoDay -> Optional.ofNullable(turbineInfoDay.getCftfx()).map(Number::doubleValue).orElse(0.0))
  214. .average()
  215. .orElse(0.0); // 默认值0.0
  216. double zbsj = 0.0;
  217. if (z.toString().equals("fsavg")) {
  218. // 保留两位小数并转换回 double
  219. zbsj = Double.parseDouble(String.format("%.2f", fsaverage));
  220. zbwz = "风速";
  221. } else if (z.toString().equals("fxavg")) {
  222. zbsj = Double.parseDouble(String.format("%.2f", fxaverage));
  223. zbwz = "风向";
  224. } else if (z.toString().equals("cftfsavg")) {
  225. zbsj = Double.parseDouble(String.format("%.2f", cftfsaverage));
  226. zbwz = "测风塔风速";
  227. } else if (z.toString().equals("cftfxavg")) {
  228. zbsj = Double.parseDouble(String.format("%.2f", cftfxaverage));
  229. zbwz = "测风塔风向";
  230. } else if (z.toString().equals("fspc")) {
  231. zbsj = Deviation.calculateSpeedDeviation(
  232. fsaverage,
  233. cftfsaverage
  234. );
  235. zbwz = "风速偏差";
  236. } else if (z.toString().equals("fxpc")) {
  237. zbsj = Deviation.calculateDirectionDeviation(
  238. fxaverage,
  239. cftfxaverage
  240. );
  241. zbwz = "风向偏差";
  242. }
  243. result.add(zbsj);
  244. }
  245. monthlyData.put(eq.getAname() + zbwz, result);
  246. }
  247. }
  248. return monthlyData;
  249. }
  250. }