TIronVisualScreenServiceImpl.java 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. package com.sckj.iron.service.impl;
  2. import com.sckj.common.util.LocalDateUtils;
  3. import com.sckj.common.util.TimeUtils;
  4. import com.sckj.iron.dto.IronTrendL1DTO;
  5. import com.sckj.iron.dto.IronTrendL2DTO;
  6. import com.sckj.iron.dto.RealtimeData;
  7. import com.sckj.l2.dto.TrendRequest;
  8. import com.sckj.l2.entity.TL2Data;
  9. import com.sckj.l2.service.impl.TL2DataServiceImpl;
  10. import com.sckj.opc.entity.OPCData;
  11. import com.sckj.opc.service.OPCDataServiceImpl;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.apache.commons.lang3.ObjectUtils;
  14. import org.springframework.stereotype.Service;
  15. import javax.annotation.Resource;
  16. import java.math.BigDecimal;
  17. import java.math.RoundingMode;
  18. import java.text.SimpleDateFormat;
  19. import java.time.LocalDateTime;
  20. import java.util.*;
  21. import java.util.concurrent.CompletableFuture;
  22. import java.util.stream.Collectors;
  23. @Slf4j
  24. @Service
  25. public class TIronVisualScreenServiceImpl {
  26. @Resource
  27. OPCDataServiceImpl opcDataService;
  28. @Resource
  29. TL2DataServiceImpl tl2DataService;
  30. private static final SimpleDateFormat sdfMinute = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:00");
  31. /***
  32. * 实时数据图图例
  33. * @param ironChart
  34. * @return
  35. */
  36. public synchronized Map<String, Object> getIronLegend(Map<String, Object> ironChart, int ironPasstime) {
  37. Map<String, Object> result = new LinkedHashMap<>();
  38. if (ObjectUtils.isEmpty(ironChart)) {
  39. return result;
  40. }
  41. // 1. 计算起止时间
  42. int hours = 24;
  43. //给固定时间,24小时之内
  44. java.util.Date endTime = new java.util.Date();
  45. java.util.Calendar cal = java.util.Calendar.getInstance();
  46. cal.setTime(endTime);
  47. cal.add(java.util.Calendar.HOUR_OF_DAY, -hours);
  48. java.util.Date startTime = cal.getTime();
  49. // // 1. 计算起止时间
  50. // // 临时调试用,固定时间范围
  51. // final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  52. // final java.util.Date startTime;
  53. // final java.util.Date endTime;
  54. // try {
  55. // startTime = sdf.parse("2025-07-13 02:15:00");
  56. // endTime = sdf.parse("2025-07-14 00:20:00");
  57. // } catch (Exception e) {
  58. // throw new RuntimeException("时间解析失败", e);
  59. // }
  60. List<TL2Data> tl2DataList = tl2DataService.lambdaQuery()
  61. .between(TL2Data::getIronStarttime,
  62. new SimpleDateFormat(TimeUtils.DATE_TIME_PATTERN).format(startTime),
  63. new SimpleDateFormat(TimeUtils.DATE_TIME_PATTERN).format(endTime))
  64. .eq(TL2Data::getTapholeId, "1")
  65. .orderByAsc(TL2Data::getIronStarttime)
  66. .list();
  67. List<List<Object>> elementS = (List<List<Object>>) ironChart.get("elementS");
  68. List<List<Object>> elementSi = (List<List<Object>>) ironChart.get("elementSi");
  69. List<List<Object>> mudWeight = (List<List<Object>>) ironChart.get("mudWeight");
  70. List<List<Object>> pollMm = (List<List<Object>>) ironChart.get("pollMm");
  71. List<List<Object>> openDepth = (List<List<Object>>) ironChart.get("openDepth");
  72. List<List<Object>> ironCosttime = (List<List<Object>>) ironChart.get("ironCosttime");
  73. List<List<Object>> ironSpeed = (List<List<Object>>) ironChart.get("ironSpeed");
  74. List<List<Object>> ironTemp = (List<List<Object>>) ironChart.get("ironTemp");
  75. List<List<Object>> ironWeight = (List<List<Object>>) ironChart.get("ironWeight");
  76. result.put("elementS", RealtimeData.builder().desc("铁水成分-硫").value(elementS.get(elementS.size() - 1).get(1)).build());
  77. result.put("elementSi", RealtimeData.builder().desc("铁水成分-硅").value(elementSi.get(elementSi.size() - 1).get(1)).build());
  78. result.put("mudWeight", RealtimeData.builder().desc("打泥量").value(mudWeight.get(mudWeight.size() - 1).get(1)).unit("L").build());
  79. result.put("pollMm", RealtimeData.builder().desc("钻杆直径").value(pollMm.get(pollMm.size() - 1).get(1)).unit("mm").build());
  80. result.put("openDepth", RealtimeData.builder().desc("开口深度").value(openDepth.get(openDepth.size() - 1).get(1)).unit("mm").build());
  81. RealtimeData realtimeDataIronConsttime = RealtimeData.builder().desc("出铁时间").unit("min").value(ironPasstime).build();
  82. if (ObjectUtils.isNotEmpty(tl2DataList)) {
  83. List<List<Map<String, Object>>> collect = tl2DataList.stream().map(item -> {
  84. List<Map<String, Object>> mapList = new ArrayList<>();
  85. Map<String, Object> map = new HashMap<>();
  86. map.put("name", String.format("第%s次出铁\n(%s号铁口)%s分钟", item.getIronNo(), item.getTapholeId(), item.getIronCosttime()));
  87. map.put("xAxis", item.getIronStarttime());
  88. mapList.add(map);
  89. map = new HashMap<>();
  90. map.put("xAxis", item.getIronEndtime());
  91. mapList.add(map);
  92. return mapList;
  93. }).collect(Collectors.toList());
  94. Map<String, Object> extraMap = new HashMap<>();
  95. extraMap.put("mark", collect);
  96. realtimeDataIronConsttime.setExtra(extraMap);
  97. }
  98. result.put("ironCosttime", realtimeDataIronConsttime);
  99. result.put("ironWeight", RealtimeData.builder().desc("累计出铁量").value(ironWeight.get(ironWeight.size() - 1).get(1)).unit("t").build());
  100. result.put("ironSpeed", RealtimeData.builder().desc("铁水流速").value(ironSpeed.get(ironSpeed.size() - 1).get(1)).unit("t/min").build());
  101. result.put("ironTemp", RealtimeData.builder().desc("铁水温度").value(ironTemp.get(ironTemp.size() - 1).get(1)).unit("℃").build());
  102. return result;
  103. }
  104. /***
  105. * 实时折线图数据,
  106. * BF4_1_IRONNOTCH_TAPPING表示出铁状态,数值只有0和1,1表示正在出铁,0表示未出铁
  107. * BF4_1TH_1_MIR_NWT表示四高炉1TH-1号车铁水净重,BF4_1TH_2_MIR_NWT表示四高炉1TH-2号车铁水净重,这2车一开始的重量为0,然后装入铁水重量不断增加,直到满载,然后减为0,然后继续增加,以此往复
  108. *
  109. * @param queryDateType
  110. * @return
  111. */
  112. public synchronized Map<String, Object> getIronChart(TrendRequest queryDateType) {
  113. // 1. 计算起止时间
  114. int hours = Objects.isNull(queryDateType.getQueryHourBefore()) ? 24 : queryDateType.getQueryHourBefore();
  115. //给固定时间,24小时之内
  116. java.util.Date endTime = new java.util.Date();
  117. java.util.Calendar cal = java.util.Calendar.getInstance();
  118. cal.setTime(endTime);
  119. cal.add(java.util.Calendar.HOUR_OF_DAY, -hours);
  120. java.util.Date startTime = cal.getTime();
  121. // 临时调试用,固定时间范围
  122. // final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  123. // final java.util.Date startTime;
  124. // final java.util.Date endTime;
  125. // try {
  126. // startTime = sdf.parse("2025-07-19 09:30:00");
  127. // endTime = sdf.parse("2025-07-20 09:30:00");
  128. // } catch (Exception e) {
  129. // throw new RuntimeException("时间解析失败", e);
  130. // }
  131. // 2. 异步查询所有数据
  132. CompletableFuture<List<OPCData>> opcDataFuture = CompletableFuture.supplyAsync(() ->
  133. opcDataService.lambdaQuery()
  134. .in(OPCData::getPointName, Arrays.asList(
  135. "BF4_1_IRONNOTCH_MIR_TEMP", // 温度
  136. "BF4_1_IRONNOTCH_RAILLINE_ACPIRON_SPEED1", // 1车流速
  137. "BF4_1_IRONNOTCH_RAILLINE_ACPIRON_SPEED2", // 2车流速
  138. "BF4_1TH_1_MIR_NWT", // 四高炉1TH-1号车铁水净重
  139. "BF4_1TH_2_MIR_NWT", // 四高炉1TH-2号车铁水净重
  140. "BF4_1_IRONNOTCH_TAPPING", // 1号铁口出铁状态
  141. "BF4_1TH_2_TPC_CARNO", // 1TH-2号车混铁车车号
  142. "BF4_1TH_1_TPC_CARNO" // 1TH-1号车混铁车车号
  143. ))
  144. .between(OPCData::getServerTime, startTime, endTime)
  145. .orderByAsc(OPCData::getServerTime)
  146. .list()
  147. );
  148. CompletableFuture<List<TL2Data>> l2DataFuture = CompletableFuture.supplyAsync(() ->
  149. tl2DataService.lambdaQuery()
  150. .between(TL2Data::getIronStarttime,
  151. new SimpleDateFormat(TimeUtils.DATE_TIME_PATTERN).format(startTime),
  152. new SimpleDateFormat(TimeUtils.DATE_TIME_PATTERN).format(endTime))
  153. .eq(TL2Data::getTapholeId, "1")
  154. .orderByAsc(TL2Data::getIronStarttime)
  155. .list()
  156. );
  157. // 3. 等待所有查询完成并转换数据
  158. try {
  159. // 4. 按pointName分类OPC数据并转换为DTO
  160. Map<String, List<OPCData>> opcDataMap = opcDataFuture.get().stream()
  161. .collect(Collectors.groupingBy(OPCData::getPointName));
  162. List<TL2Data> l2DataList = l2DataFuture.get();
  163. // 并行转换所有数据
  164. //铁水温度
  165. CompletableFuture<List<IronTrendL1DTO>> tempListFuture = CompletableFuture.supplyAsync(() ->
  166. opcDataMap.getOrDefault("BF4_1_IRONNOTCH_MIR_TEMP", new ArrayList<>())
  167. .stream().map(item -> {
  168. IronTrendL1DTO dto = new IronTrendL1DTO();
  169. dto.setData(item.getData());
  170. dto.setCreateTime(item.getSourceTime());
  171. return dto;
  172. }).collect(Collectors.toList())
  173. );
  174. //1TH-1号车受铁速度
  175. CompletableFuture<List<IronTrendL1DTO>> speed1ListFuture = CompletableFuture.supplyAsync(() ->
  176. opcDataMap.getOrDefault("BF4_1_IRONNOTCH_RAILLINE_ACPIRON_SPEED1", new ArrayList<>())
  177. .stream().map(item -> {
  178. IronTrendL1DTO dto = new IronTrendL1DTO();
  179. dto.setData(item.getData());
  180. dto.setCreateTime(item.getSourceTime());
  181. return dto;
  182. }).collect(Collectors.toList())
  183. );
  184. //1TH-2号车受铁速度
  185. CompletableFuture<List<IronTrendL1DTO>> speed2ListFuture = CompletableFuture.supplyAsync(() ->
  186. opcDataMap.getOrDefault("BF4_1_IRONNOTCH_RAILLINE_ACPIRON_SPEED2", new ArrayList<>())
  187. .stream().map(item -> {
  188. IronTrendL1DTO dto = new IronTrendL1DTO();
  189. dto.setData(item.getData());
  190. dto.setCreateTime(item.getSourceTime());
  191. return dto;
  192. }).collect(Collectors.toList())
  193. );
  194. //BF4_1TH_1_MIR_NWT 1号车净重
  195. CompletableFuture<List<IronTrendL1DTO>> weight1ListFuture = CompletableFuture.supplyAsync(() ->
  196. opcDataMap.getOrDefault("BF4_1TH_1_MIR_NWT", new ArrayList<>())
  197. .stream().map(item -> {
  198. IronTrendL1DTO dto = new IronTrendL1DTO();
  199. dto.setData(item.getData());
  200. dto.setCreateTime(item.getSourceTime());
  201. return dto;
  202. }).collect(Collectors.toList())
  203. );
  204. //BF4_1TH_2_MIR_NWT 2号车净重
  205. CompletableFuture<List<IronTrendL1DTO>> weight2ListFuture = CompletableFuture.supplyAsync(() ->
  206. opcDataMap.getOrDefault("BF4_1TH_2_MIR_NWT", new ArrayList<>())
  207. .stream().map(item -> {
  208. IronTrendL1DTO dto = new IronTrendL1DTO();
  209. dto.setData(item.getData());
  210. dto.setCreateTime(item.getSourceTime());
  211. return dto;
  212. }).collect(Collectors.toList())
  213. );
  214. //L2
  215. CompletableFuture<List<IronTrendL2DTO>> elemenListFuture = CompletableFuture.supplyAsync(() ->
  216. l2DataList.stream().map(item -> {
  217. IronTrendL2DTO dto = new IronTrendL2DTO();
  218. try {
  219. dto.setElementS(item.getElementS() != null ? Double.parseDouble(item.getElementS()) : null);
  220. } catch (NumberFormatException e) {
  221. dto.setElementS(null);
  222. }
  223. try {
  224. dto.setElementSi(item.getElementSi() != null ? Double.parseDouble(item.getElementSi()) : null);
  225. } catch (NumberFormatException e) {
  226. dto.setElementSi(null);
  227. }
  228. dto.setIronStarttime(item.getIronStarttime());
  229. dto.setIronEndtime(item.getIronEndtime());
  230. dto.setPollMm(item.getPollMm());
  231. dto.setOpenDepth(item.getOpenDepth());
  232. dto.setMudWeight(item.getMudWeight());
  233. dto.setIronCosttime(item.getIronCosttime());
  234. return dto;
  235. }).collect(Collectors.toList())
  236. );
  237. // 等待所有转换完成
  238. CompletableFuture.allOf(
  239. tempListFuture,
  240. speed1ListFuture,
  241. speed2ListFuture,
  242. weight1ListFuture,
  243. weight2ListFuture,
  244. elemenListFuture
  245. ).join();
  246. // 5. 封装返回
  247. List<IronTrendL1DTO> tempList = tempListFuture.get();
  248. List<IronTrendL1DTO> speed1List = speed1ListFuture.get();
  249. List<IronTrendL1DTO> speed2List = speed2ListFuture.get();
  250. List<IronTrendL1DTO> weight1List = weight1ListFuture.get();
  251. List<IronTrendL1DTO> weight2List = weight2ListFuture.get();
  252. List<IronTrendL2DTO> elemenList = elemenListFuture.get();
  253. // 1. 生成完整的每一分钟时间点
  254. java.util.List<String> xAxis = new java.util.ArrayList<>();
  255. java.util.Calendar cursor = java.util.Calendar.getInstance();
  256. cursor.setTime(startTime);
  257. while (!cursor.getTime().after(endTime)) {
  258. xAxis.add(sdfMinute.format(cursor.getTime()));
  259. cursor.add(java.util.Calendar.MINUTE, 1);
  260. }
  261. // 查询区间前的最近一条历史数据(前置值)
  262. // OPC类
  263. OPCData preTemp = opcDataService.lambdaQuery()
  264. .eq(OPCData::getPointName, "BF4_1_IRONNOTCH_MIR_TEMP")
  265. .isNotNull(OPCData::getData)
  266. .lt(OPCData::getServerTime, startTime)
  267. .orderByDesc(OPCData::getServerTime)
  268. .last("limit 1").one();
  269. OPCData preSpeed1 = opcDataService.lambdaQuery()
  270. .eq(OPCData::getPointName, "BF4_1_IRONNOTCH_RAILLINE_ACPIRON_SPEED1")
  271. .isNotNull(OPCData::getData)
  272. .lt(OPCData::getServerTime, startTime)
  273. .orderByDesc(OPCData::getServerTime)
  274. .last("limit 1").one();
  275. OPCData preSpeed2 = opcDataService.lambdaQuery()
  276. .eq(OPCData::getPointName, "BF4_1_IRONNOTCH_RAILLINE_ACPIRON_SPEED2")
  277. .isNotNull(OPCData::getData)
  278. .lt(OPCData::getServerTime, startTime)
  279. .orderByDesc(OPCData::getServerTime)
  280. .last("limit 1").one();
  281. OPCData preWeight1 = opcDataService.lambdaQuery()
  282. .eq(OPCData::getPointName, "BF4_1TH_1_MIR_NWT")
  283. .isNotNull(OPCData::getData)
  284. .lt(OPCData::getServerTime, startTime)
  285. .orderByDesc(OPCData::getServerTime)
  286. .last("limit 1").one();
  287. OPCData preWeight2 = opcDataService.lambdaQuery()
  288. .eq(OPCData::getPointName, "BF4_1TH_2_MIR_NWT")
  289. .isNotNull(OPCData::getData)
  290. .lt(OPCData::getServerTime, startTime)
  291. .orderByDesc(OPCData::getServerTime)
  292. .last("limit 1").one();
  293. // tappingMap补齐每一分钟,前面无数据用前置值补齐
  294. // 先查区间前最近一条tapping
  295. Integer preTappingVal = 0;
  296. OPCData preTapping = opcDataService.lambdaQuery()
  297. .eq(OPCData::getPointName, "BF4_1_IRONNOTCH_TAPPING")
  298. .isNotNull(OPCData::getData)
  299. .lt(OPCData::getServerTime, startTime)
  300. .orderByDesc(OPCData::getServerTime)
  301. .last("limit 1").one();
  302. // L2类
  303. TL2Data preL2 = tl2DataService.lambdaQuery()
  304. .lt(TL2Data::getIronStarttime, new SimpleDateFormat(TimeUtils.DATE_TIME_PATTERN).format(startTime))
  305. .eq(TL2Data::getTapholeId, "1")
  306. .orderByDesc(TL2Data::getIronStarttime)
  307. .last("limit 1").one();
  308. // 2. 构建每分钟补全的二维数组
  309. java.util.Map<String, Object> result = new java.util.LinkedHashMap<>();
  310. //
  311. if (preTapping != null && preTapping.getData() != null) {
  312. Object v = preTapping.getData();
  313. if (v instanceof Number) preTappingVal = ((Number) v).intValue();
  314. else if (v instanceof String) {
  315. String str = ((String) v).trim();
  316. if (!str.isEmpty()) {
  317. try {
  318. preTappingVal = Integer.parseInt(str);
  319. } catch (Exception ignore) {
  320. }
  321. }
  322. }
  323. }
  324. // 构建每分钟tappingMap
  325. Map<String, Integer> tappingMap = new LinkedHashMap<>();
  326. List<OPCData> tappingList = opcDataMap.getOrDefault("BF4_1_IRONNOTCH_TAPPING", new ArrayList<>());
  327. Map<String, Integer> rawTappingMap = new LinkedHashMap<>();
  328. for (OPCData item : tappingList) {
  329. if (item.getServerTime() != null && item.getData() != null) {
  330. String min = sdfMinute.format(item.getServerTime());
  331. Integer val = null;
  332. Object v = item.getData();
  333. if (v instanceof Number) val = ((Number) v).intValue();
  334. else if (v instanceof String) {
  335. String str = ((String) v).trim();
  336. if (!str.isEmpty()) {
  337. try {
  338. val = Integer.parseInt(str);
  339. } catch (Exception ignore) {
  340. }
  341. }
  342. }
  343. if (val != null) rawTappingMap.put(min, val);
  344. }
  345. }
  346. Integer last = preTappingVal;
  347. for (String t : xAxis) {
  348. Integer v = rawTappingMap.getOrDefault(t, last);
  349. tappingMap.put(t, v);
  350. if (v != null) last = v;
  351. }
  352. int ironWeightScale = 2; //小数位数,可调整为0、1、2等
  353. List<List<Object>> ironFlowArr = buildIronWeightMinuteArray(xAxis, tappingMap, weight1List, weight2List, ironWeightScale);
  354. result.put("ironWeight", ironFlowArr);
  355. // 处理铁水流速,出铁中按原逻辑,出铁结束后为0
  356. List<List<Object>> ironSpeedArr = buildMinuteArrayForSpeed(xAxis, speed1List, speed2List, tappingMap);
  357. List<List<Object>> ironTempArr = buildMinuteArray(tempList, xAxis, preTemp != null ? preTemp.getData() : null);
  358. for (int i = 0; i < xAxis.size(); i++) {
  359. String t = xAxis.get(i);
  360. Integer tapping = tappingMap.getOrDefault(t, 0);
  361. if (tapping == 0 && ironSpeedArr.get(i).size() > 1) {
  362. ironSpeedArr.get(i).set(1, 0);
  363. ironTempArr.get(i).set(1, 0);
  364. }
  365. }
  366. result.put("ironSpeed", ironSpeedArr);
  367. result.put("ironTemp", ironTempArr);
  368. // 处理L2类数据,tapping=0时设为0
  369. List<List<Object>> elementSArr = buildL2MinuteArray(elemenList, xAxis, tappingMap, "elementS", preL2 != null ? preL2.getElementS() : null);
  370. List<List<Object>> elementSiArr = buildL2MinuteArray(elemenList, xAxis, tappingMap, "elementSi", preL2 != null ? preL2.getElementSi() : null);
  371. List<List<Object>> mudWeightArr = buildL2MinuteArray(elemenList, xAxis, tappingMap, "mudWeight", preL2 != null ? preL2.getMudWeight() : null);
  372. List<List<Object>> pollMmArr = buildL2MinuteArray(elemenList, xAxis, tappingMap, "pollMm", preL2 != null ? preL2.getPollMm() : null);
  373. List<List<Object>> openDepthArr = buildL2MinuteArray(elemenList, xAxis, tappingMap, "openDepth", preL2 != null ? preL2.getOpenDepth() : null);
  374. List<List<Object>> ironCosttimeArr = buildL2MinuteArray(elemenList, xAxis, tappingMap, "ironCosttime", preL2 != null ? preL2.getIronCosttime() : null);
  375. // for (int i = 0; i < xAxis.size(); i++) {
  376. // String t = xAxis.get(i);
  377. // Integer tapping = tappingMap.getOrDefault(t, 0);
  378. // if (tapping == 0) {
  379. // if (elementSArr.get(i).size() > 1) elementSArr.get(i).set(1, 0);
  380. // if (elementSiArr.get(i).size() > 1) elementSiArr.get(i).set(1, 0);
  381. // if (mudWeightArr.get(i).size() > 1) mudWeightArr.get(i).set(1, 0);
  382. // if (pollMmArr.get(i).size() > 1) pollMmArr.get(i).set(1, 0);
  383. // if (openDepthArr.get(i).size() > 1) openDepthArr.get(i).set(1, 0);
  384. // if (ironCosttimeArr.get(i).size() > 1) ironCosttimeArr.get(i).set(1, 0);
  385. // }
  386. // }
  387. result.put("elementS", elementSArr);
  388. result.put("elementSi", elementSiArr);
  389. result.put("mudWeight", mudWeightArr);
  390. result.put("pollMm", pollMmArr);
  391. result.put("openDepth", openDepthArr);
  392. result.put("ironCosttime", ironCosttimeArr);
  393. return result;
  394. } catch (Exception e) {
  395. e.printStackTrace();
  396. }
  397. return null;
  398. }
  399. // 新增工具方法:L1类型
  400. private java.util.List<java.util.List<Object>> buildMinuteArray(java.util.List<IronTrendL1DTO> list, java.util.List<String> xAxis, Object preValue) {
  401. java.util.Map<String, Object> timeValueMap = new java.util.LinkedHashMap<>();
  402. for (IronTrendL1DTO e : list) {
  403. if (e.getCreateTime() != null) {
  404. String min = sdfMinute.format(e.getCreateTime());
  405. timeValueMap.put(min, e.getData());
  406. }
  407. }
  408. java.util.List<java.util.List<Object>> arr = new java.util.ArrayList<>();
  409. Object last = preValue;
  410. for (String t : xAxis) {
  411. Object v = timeValueMap.getOrDefault(t, last);
  412. arr.add(java.util.Arrays.asList(t, v));
  413. if (v != null) last = v;
  414. }
  415. return arr;
  416. }
  417. // 铁水流速(重写:合并取最大值,出铁周期内补齐空值,且出铁周期内流速不允许为0)
  418. private java.util.List<java.util.List<Object>> buildMinuteArrayForSpeed(java.util.List<String> xAxis, java.util.List<IronTrendL1DTO> speed1List, java.util.List<IronTrendL1DTO> speed2List, java.util.Map<String, Integer> tappingMap) {
  419. java.util.Map<String, Double> speed1Map = new java.util.LinkedHashMap<>();
  420. java.util.Map<String, Double> speed2Map = new java.util.LinkedHashMap<>();
  421. for (IronTrendL1DTO e : speed1List) {
  422. if (e.getCreateTime() != null) {
  423. String min = sdfMinute.format(e.getCreateTime());
  424. Object v = e.getData();
  425. Double value = null;
  426. if (v instanceof Number) value = ((Number) v).doubleValue();
  427. else if (v instanceof String) {
  428. try {
  429. value = Double.parseDouble((String) v);
  430. } catch (Exception ignore) {
  431. }
  432. }
  433. if (value != null) speed1Map.put(min, value);
  434. }
  435. }
  436. for (IronTrendL1DTO e : speed2List) {
  437. if (e.getCreateTime() != null) {
  438. String min = sdfMinute.format(e.getCreateTime());
  439. Object v = e.getData();
  440. Double value = null;
  441. if (v instanceof Number) value = ((Number) v).doubleValue();
  442. else if (v instanceof String) {
  443. try {
  444. value = Double.parseDouble((String) v);
  445. } catch (Exception ignore) {
  446. }
  447. }
  448. if (value != null) speed2Map.put(min, value);
  449. }
  450. }
  451. // 合并为最大值map
  452. java.util.Map<String, Double> speedMap = new java.util.LinkedHashMap<>();
  453. for (String key : speed1Map.keySet()) {
  454. speedMap.put(key, speed1Map.get(key));
  455. }
  456. for (String key : speed2Map.keySet()) {
  457. double v2 = speed2Map.get(key);
  458. speedMap.put(key, Math.max(speedMap.getOrDefault(key, 0d), v2));
  459. }
  460. java.util.List<java.util.List<Object>> arr = new java.util.ArrayList<>();
  461. Double last = null;
  462. for (String t : xAxis) {
  463. Integer tapping = tappingMap.getOrDefault(t, 0);
  464. Double v = speedMap.get(t);
  465. if (tapping == 1) {
  466. if (v == null || v == 0) {
  467. // 出铁周期内流速不允许为0,用上一个非0值
  468. v = last != null ? last : 0d;
  469. }
  470. arr.add(java.util.Arrays.asList(t, v));
  471. if (v != null && v != 0) last = v;
  472. } else {
  473. arr.add(java.util.Arrays.asList(t, 0));
  474. }
  475. }
  476. return arr;
  477. }
  478. //L2
  479. private java.util.List<java.util.List<Object>> buildL2MinuteArray(java.util.List<IronTrendL2DTO> list, java.util.List<String> xAxis, Map<String, Integer> tappingMap, String field, Object preValue) {
  480. java.util.Map<String, Object> timeValueMap = new java.util.LinkedHashMap<>();
  481. for (IronTrendL2DTO e : list) {
  482. String startMin = e.getIronStarttime();
  483. String endMin = e.getIronEndtime();
  484. if (startMin != null && endMin != null) {
  485. Object v = null;
  486. switch (field) {
  487. case "elementS":
  488. v = e.getElementS();
  489. break;
  490. case "elementSi":
  491. v = e.getElementSi();
  492. break;
  493. case "mudWeight":
  494. v = e.getMudWeight();
  495. break;
  496. case "pollMm":
  497. v = e.getPollMm();
  498. break;
  499. case "openDepth":
  500. v = e.getOpenDepth();
  501. break;
  502. case "ironCosttime":
  503. v = e.getIronCosttime();
  504. break;
  505. }
  506. // 定义时间格式
  507. // DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  508. // 解析字符串为LocalDateTime对象
  509. LocalDateTime startTime = LocalDateTime.parse(startMin, LocalDateUtils.DATE_TIME_FORMATTER).withSecond(0);
  510. LocalDateTime endTime = LocalDateTime.parse(endMin, LocalDateUtils.DATE_TIME_FORMATTER).withSecond(0);
  511. // 遍历每一分钟
  512. LocalDateTime currentTime = startTime;
  513. while (!currentTime.isAfter(endTime)) {
  514. timeValueMap.put(currentTime.format(LocalDateUtils.DATE_TIME_FORMATTER), v);
  515. // 增加一分钟
  516. currentTime = currentTime.plusMinutes(1);
  517. }
  518. }
  519. }
  520. List<java.util.List<Object>> arr = new ArrayList<>();
  521. Object last = preValue;
  522. // for (String time : xAxis) {
  523. // Integer tapping = tappingMap.getOrDefault(time, 0);
  524. // Object v = timeValueMap.getOrDefault(time, last);
  525. // double value = Double.parseDouble(v.toString());
  526. // if (tapping == 1) {
  527. // if (value == 0 ) {
  528. // //
  529. // value = last != null ? Double.parseDouble(last.toString()) : 0;
  530. // }
  531. // arr.add(java.util.Arrays.asList(time,value));
  532. // if (value != 0){
  533. // last = value;
  534. // }
  535. // } else {
  536. // arr.add(java.util.Arrays.asList(time, 0));
  537. // last = 0;
  538. // }
  539. // }
  540. for (String time : xAxis) {
  541. Object value = timeValueMap.getOrDefault(time, last);
  542. List item = new ArrayList();
  543. item.add(time);
  544. Integer tapping = tappingMap.getOrDefault(time, 0);
  545. if (tapping == 0) {
  546. item.add(0);
  547. last = null;
  548. } else {
  549. if (null == value) {
  550. item.add(0);
  551. last = null;
  552. } else {
  553. item.add(value);
  554. last = value;
  555. }
  556. }
  557. arr.add(item);
  558. }
  559. //处理正在出铁的情况
  560. if (tappingMap.getOrDefault(xAxis.get(xAxis.size() - 1), 0) > 0) {
  561. //表明正在出铁中
  562. for (int i = xAxis.size() - 1; i >= 0; i--) {
  563. Integer tapping = tappingMap.getOrDefault(xAxis.get(i), 0);
  564. if (tapping <= 0) {
  565. break;
  566. } else {
  567. if (arr.get(i).size() > 1) arr.get(i).set(1, 0);
  568. }
  569. }
  570. }
  571. return arr;
  572. }
  573. //铁水重量
  574. private List<List<Object>> buildIronWeightMinuteArray(List<String> xAxis, Map<String, Integer> tappingMap, List<IronTrendL1DTO> weight1List, List<IronTrendL1DTO> weight2List, int scale) {
  575. Map<String, Double> weight1Map = new LinkedHashMap<>();
  576. for (IronTrendL1DTO e : weight1List) {
  577. if (e.getCreateTime() != null) {
  578. String minute = sdfMinute.format(e.getCreateTime());
  579. Object v = e.getData();
  580. Double value = null;
  581. if (v instanceof Number) value = ((Number) v).doubleValue();
  582. else if (v instanceof String) {
  583. String str = ((String) v).trim();
  584. if (!str.isEmpty()) {
  585. try {
  586. value = Double.parseDouble(str);
  587. } catch (Exception ignore) {
  588. }
  589. }
  590. }
  591. if (value != null) {
  592. weight1Map.put(minute, value);
  593. }
  594. }
  595. }
  596. Map<String, Double> weight2Map = new LinkedHashMap<>();
  597. for (IronTrendL1DTO e : weight2List) {
  598. if (e.getCreateTime() != null) {
  599. String minute = sdfMinute.format(e.getCreateTime());
  600. Object v = e.getData();
  601. Double value = null;
  602. if (v instanceof Number) value = ((Number) v).doubleValue();
  603. else if (v instanceof String) {
  604. String str = ((String) v).trim();
  605. if (!str.isEmpty()) {
  606. try {
  607. value = Double.parseDouble(str);
  608. } catch (Exception ignore) {
  609. }
  610. }
  611. }
  612. if (value != null) {
  613. weight2Map.put(minute, value);
  614. }
  615. }
  616. }
  617. // 合并weight1Map和weight2Map到weightMap,取最大值
  618. Map<String, Double> weightMap = new LinkedHashMap<>();
  619. //先将weight1Map复制到weightMap中
  620. for (Map.Entry<String, Double> entry : weight1Map.entrySet()) {
  621. weightMap.put(entry.getKey(), entry.getValue());
  622. }
  623. //然后将weight2Map复制到weightMap中,重复的取最大值
  624. for (Map.Entry<String, Double> entry : weight2Map.entrySet()) {
  625. weightMap.put(entry.getKey(), Math.max(weightMap.getOrDefault(entry.getKey(), 0d), entry.getValue()));
  626. }
  627. // 按key正序排序
  628. weightMap = weightMap.entrySet().stream()
  629. .sorted(Map.Entry.comparingByKey())
  630. .collect(java.util.LinkedHashMap::new, (m, e) -> m.put(e.getKey(), e.getValue()), java.util.LinkedHashMap::putAll);
  631. List<List<Object>> arr = new ArrayList<>();
  632. boolean inTappingCycle = false;
  633. //
  634. BigDecimal cycleTotal = BigDecimal.ZERO;
  635. //
  636. BigDecimal lastValue = BigDecimal.ZERO;
  637. BigDecimal bigDecimal = BigDecimal.ZERO;
  638. for (int i = 0; i < xAxis.size(); i++) {
  639. String time = xAxis.get(i);
  640. Integer tapping = tappingMap.getOrDefault(time, 0);
  641. bigDecimal = new BigDecimal(weightMap.getOrDefault(time, 0d).toString());
  642. if (tapping == 1) {
  643. //切换到出铁状态
  644. if (!inTappingCycle) {
  645. //刚刚开始出铁
  646. inTappingCycle = true;
  647. log.info("[出铁周期开始] 开始于: " + time);
  648. if(bigDecimal.compareTo(new BigDecimal("70")) > 0){
  649. bigDecimal = BigDecimal.ZERO;
  650. lastValue = BigDecimal.ZERO;
  651. }
  652. }
  653. if (bigDecimal.compareTo(BigDecimal.ZERO) == 0) {
  654. //复用上次的数据
  655. bigDecimal = lastValue;
  656. weightMap.put(time, bigDecimal.doubleValue());
  657. } else {
  658. lastValue = bigDecimal;
  659. }
  660. if (i > 0) {
  661. //按照时间正序(10:00:00,10:01:00,10:02:00)
  662. //将10:00:00对应的数据 - 10:01:00对应的数据
  663. BigDecimal prevW = new BigDecimal(weightMap.getOrDefault(xAxis.get(i - 1), 0d).toString());
  664. //上一个减去当前的
  665. if (prevW.subtract(bigDecimal).compareTo(new BigDecimal("70")) > 0) {
  666. log.info("[出铁周期满载情况] 时间: " + xAxis.get(i - 1) + ", 铁水满载值: " + prevW);
  667. cycleTotal = cycleTotal.add(prevW);
  668. }
  669. }
  670. arr.add(java.util.Arrays.asList(time, bigDecimal.add(cycleTotal).setScale(scale, RoundingMode.HALF_UP).doubleValue()));
  671. } else {
  672. //切换到出铁结束
  673. if (inTappingCycle) {
  674. //刚刚从出铁结束切换过来
  675. cycleTotal = cycleTotal.add(bigDecimal);
  676. log.info("[出铁周期结束] 最后时间点的铁水重量: " + bigDecimal);
  677. bigDecimal = BigDecimal.ZERO;
  678. double value = cycleTotal.setScale(scale, RoundingMode.HALF_UP).doubleValue();
  679. log.info("[出铁周期结束] 结束于: " + time + ", 本周期铁水总净重: " + value);
  680. arr.add(java.util.Arrays.asList(time, value));
  681. inTappingCycle = false;
  682. cycleTotal = BigDecimal.ZERO;
  683. // for (List<Object> objects : arr) {
  684. // System.out.println(objects.get(0)+" >>> "+objects.get(1));
  685. // }
  686. } else {
  687. arr.add(java.util.Arrays.asList(time, 0));
  688. }
  689. }
  690. }
  691. return arr;
  692. }
  693. }