TIronVisualScreenServiceImpl.java 44 KB

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