123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759 |
- package com.sckj.iron.service.impl;
- import com.sckj.common.util.LocalDateUtils;
- import com.sckj.common.util.TimeUtils;
- import com.sckj.iron.dto.IronTrendL1DTO;
- import com.sckj.iron.dto.IronTrendL2DTO;
- import com.sckj.iron.dto.RealtimeData;
- import com.sckj.l2.dto.TrendRequest;
- import com.sckj.l2.entity.TL2Data;
- import com.sckj.l2.service.impl.TL2DataServiceImpl;
- import com.sckj.opc.entity.OPCData;
- import com.sckj.opc.service.OPCDataServiceImpl;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang3.ObjectUtils;
- import org.springframework.stereotype.Service;
- import javax.annotation.Resource;
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- import java.text.SimpleDateFormat;
- import java.time.LocalDateTime;
- import java.util.*;
- import java.util.concurrent.CompletableFuture;
- import java.util.stream.Collectors;
- @Slf4j
- @Service
- public class TIronVisualScreenServiceImpl {
- @Resource
- OPCDataServiceImpl opcDataService;
- @Resource
- TL2DataServiceImpl tl2DataService;
- private static final SimpleDateFormat sdfMinute = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:00");
- /***
- * 实时数据图图例
- * @param ironChart
- * @return
- */
- public synchronized Map<String, Object> getIronLegend(Map<String, Object> ironChart, int ironPasstime) {
- Map<String, Object> result = new LinkedHashMap<>();
- if (ObjectUtils.isEmpty(ironChart)) {
- return result;
- }
- // 1. 计算起止时间
- int hours = 24;
- //给固定时间,24小时之内
- java.util.Date endTime = new java.util.Date();
- java.util.Calendar cal = java.util.Calendar.getInstance();
- cal.setTime(endTime);
- cal.add(java.util.Calendar.HOUR_OF_DAY, -hours);
- java.util.Date startTime = cal.getTime();
- // // 1. 计算起止时间
- // // 临时调试用,固定时间范围
- // final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- // final java.util.Date startTime;
- // final java.util.Date endTime;
- // try {
- // startTime = sdf.parse("2025-07-13 02:15:00");
- // endTime = sdf.parse("2025-07-14 00:20:00");
- // } catch (Exception e) {
- // throw new RuntimeException("时间解析失败", e);
- // }
- List<TL2Data> tl2DataList = tl2DataService.lambdaQuery()
- .between(TL2Data::getIronStarttime,
- new SimpleDateFormat(TimeUtils.DATE_TIME_PATTERN).format(startTime),
- new SimpleDateFormat(TimeUtils.DATE_TIME_PATTERN).format(endTime))
- .eq(TL2Data::getTapholeId, "1")
- .orderByAsc(TL2Data::getIronStarttime)
- .list();
- List<List<Object>> elementS = (List<List<Object>>) ironChart.get("elementS");
- List<List<Object>> elementSi = (List<List<Object>>) ironChart.get("elementSi");
- List<List<Object>> mudWeight = (List<List<Object>>) ironChart.get("mudWeight");
- List<List<Object>> pollMm = (List<List<Object>>) ironChart.get("pollMm");
- List<List<Object>> openDepth = (List<List<Object>>) ironChart.get("openDepth");
- List<List<Object>> ironCosttime = (List<List<Object>>) ironChart.get("ironCosttime");
- List<List<Object>> ironSpeed = (List<List<Object>>) ironChart.get("ironSpeed");
- List<List<Object>> ironTemp = (List<List<Object>>) ironChart.get("ironTemp");
- List<List<Object>> ironWeight = (List<List<Object>>) ironChart.get("ironWeight");
- result.put("elementS", RealtimeData.builder().desc("铁水成分-硫").value(elementS.get(elementS.size() - 1).get(1)).build());
- result.put("elementSi", RealtimeData.builder().desc("铁水成分-硅").value(elementSi.get(elementSi.size() - 1).get(1)).build());
- result.put("mudWeight", RealtimeData.builder().desc("打泥量").value(mudWeight.get(mudWeight.size() - 1).get(1)).unit("L").build());
- result.put("pollMm", RealtimeData.builder().desc("钻杆直径").value(pollMm.get(pollMm.size() - 1).get(1)).unit("mm").build());
- result.put("openDepth", RealtimeData.builder().desc("开口深度").value(openDepth.get(openDepth.size() - 1).get(1)).unit("mm").build());
- RealtimeData realtimeDataIronConsttime = RealtimeData.builder().desc("出铁时间").unit("min").value(ironPasstime).build();
- if (ObjectUtils.isNotEmpty(tl2DataList)) {
- List<List<Map<String, Object>>> collect = tl2DataList.stream().map(item -> {
- List<Map<String, Object>> mapList = new ArrayList<>();
- Map<String, Object> map = new HashMap<>();
- map.put("name", String.format("第%s次出铁\n(%s号铁口)%s分钟", item.getIronNo(), item.getTapholeId(), item.getIronCosttime()));
- map.put("xAxis", item.getIronStarttime());
- mapList.add(map);
- map = new HashMap<>();
- map.put("xAxis", item.getIronEndtime());
- mapList.add(map);
- return mapList;
- }).collect(Collectors.toList());
- Map<String, Object> extraMap = new HashMap<>();
- extraMap.put("mark", collect);
- realtimeDataIronConsttime.setExtra(extraMap);
- }
- result.put("ironCosttime", realtimeDataIronConsttime);
- result.put("ironWeight", RealtimeData.builder().desc("累计出铁量").value(ironWeight.get(ironWeight.size() - 1).get(1)).unit("t").build());
- result.put("ironSpeed", RealtimeData.builder().desc("铁水流速").value(ironSpeed.get(ironSpeed.size() - 1).get(1)).unit("t/min").build());
- result.put("ironTemp", RealtimeData.builder().desc("铁水温度").value(ironTemp.get(ironTemp.size() - 1).get(1)).unit("℃").build());
- return result;
- }
- /***
- * 实时折线图数据,
- * BF4_1_IRONNOTCH_TAPPING表示出铁状态,数值只有0和1,1表示正在出铁,0表示未出铁
- * BF4_1TH_1_MIR_NWT表示四高炉1TH-1号车铁水净重,BF4_1TH_2_MIR_NWT表示四高炉1TH-2号车铁水净重,这2车一开始的重量为0,然后装入铁水重量不断增加,直到满载,然后减为0,然后继续增加,以此往复
- *
- * @param queryDateType
- * @return
- */
- public synchronized Map<String, Object> getIronChart(TrendRequest queryDateType) {
- // 1. 计算起止时间
- int hours = Objects.isNull(queryDateType.getQueryHourBefore()) ? 24 : queryDateType.getQueryHourBefore();
- //给固定时间,24小时之内
- java.util.Date endTime = new java.util.Date();
- java.util.Calendar cal = java.util.Calendar.getInstance();
- cal.setTime(endTime);
- cal.add(java.util.Calendar.HOUR_OF_DAY, -hours);
- java.util.Date startTime = cal.getTime();
- // 临时调试用,固定时间范围
- // final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- // final java.util.Date startTime;
- // final java.util.Date endTime;
- // try {
- // startTime = sdf.parse("2025-07-19 09:30:00");
- // endTime = sdf.parse("2025-07-20 09:30:00");
- // } catch (Exception e) {
- // throw new RuntimeException("时间解析失败", e);
- // }
- // 2. 异步查询所有数据
- CompletableFuture<List<OPCData>> opcDataFuture = CompletableFuture.supplyAsync(() ->
- opcDataService.lambdaQuery()
- .in(OPCData::getPointName, Arrays.asList(
- "BF4_1_IRONNOTCH_MIR_TEMP", // 温度
- "BF4_1_IRONNOTCH_RAILLINE_ACPIRON_SPEED1", // 1车流速
- "BF4_1_IRONNOTCH_RAILLINE_ACPIRON_SPEED2", // 2车流速
- "BF4_1TH_1_MIR_NWT", // 四高炉1TH-1号车铁水净重
- "BF4_1TH_2_MIR_NWT", // 四高炉1TH-2号车铁水净重
- "BF4_1_IRONNOTCH_TAPPING", // 1号铁口出铁状态
- "BF4_1TH_2_TPC_CARNO", // 1TH-2号车混铁车车号
- "BF4_1TH_1_TPC_CARNO" // 1TH-1号车混铁车车号
- ))
- .between(OPCData::getServerTime, startTime, endTime)
- .orderByAsc(OPCData::getServerTime)
- .list()
- );
- CompletableFuture<List<TL2Data>> l2DataFuture = CompletableFuture.supplyAsync(() ->
- tl2DataService.lambdaQuery()
- .between(TL2Data::getIronStarttime,
- new SimpleDateFormat(TimeUtils.DATE_TIME_PATTERN).format(startTime),
- new SimpleDateFormat(TimeUtils.DATE_TIME_PATTERN).format(endTime))
- .eq(TL2Data::getTapholeId, "1")
- .orderByAsc(TL2Data::getIronStarttime)
- .list()
- );
- // 3. 等待所有查询完成并转换数据
- try {
- // 4. 按pointName分类OPC数据并转换为DTO
- Map<String, List<OPCData>> opcDataMap = opcDataFuture.get().stream()
- .collect(Collectors.groupingBy(OPCData::getPointName));
- List<TL2Data> l2DataList = l2DataFuture.get();
- // 并行转换所有数据
- //铁水温度
- CompletableFuture<List<IronTrendL1DTO>> tempListFuture = CompletableFuture.supplyAsync(() ->
- opcDataMap.getOrDefault("BF4_1_IRONNOTCH_MIR_TEMP", new ArrayList<>())
- .stream().map(item -> {
- IronTrendL1DTO dto = new IronTrendL1DTO();
- dto.setData(item.getData());
- dto.setCreateTime(item.getSourceTime());
- return dto;
- }).collect(Collectors.toList())
- );
- //1TH-1号车受铁速度
- CompletableFuture<List<IronTrendL1DTO>> speed1ListFuture = CompletableFuture.supplyAsync(() ->
- opcDataMap.getOrDefault("BF4_1_IRONNOTCH_RAILLINE_ACPIRON_SPEED1", new ArrayList<>())
- .stream().map(item -> {
- IronTrendL1DTO dto = new IronTrendL1DTO();
- dto.setData(item.getData());
- dto.setCreateTime(item.getSourceTime());
- return dto;
- }).collect(Collectors.toList())
- );
- //1TH-2号车受铁速度
- CompletableFuture<List<IronTrendL1DTO>> speed2ListFuture = CompletableFuture.supplyAsync(() ->
- opcDataMap.getOrDefault("BF4_1_IRONNOTCH_RAILLINE_ACPIRON_SPEED2", new ArrayList<>())
- .stream().map(item -> {
- IronTrendL1DTO dto = new IronTrendL1DTO();
- dto.setData(item.getData());
- dto.setCreateTime(item.getSourceTime());
- return dto;
- }).collect(Collectors.toList())
- );
- //BF4_1TH_1_MIR_NWT 1号车净重
- CompletableFuture<List<IronTrendL1DTO>> weight1ListFuture = CompletableFuture.supplyAsync(() ->
- opcDataMap.getOrDefault("BF4_1TH_1_MIR_NWT", new ArrayList<>())
- .stream().map(item -> {
- IronTrendL1DTO dto = new IronTrendL1DTO();
- dto.setData(item.getData());
- dto.setCreateTime(item.getSourceTime());
- return dto;
- }).collect(Collectors.toList())
- );
- //BF4_1TH_2_MIR_NWT 2号车净重
- CompletableFuture<List<IronTrendL1DTO>> weight2ListFuture = CompletableFuture.supplyAsync(() ->
- opcDataMap.getOrDefault("BF4_1TH_2_MIR_NWT", new ArrayList<>())
- .stream().map(item -> {
- IronTrendL1DTO dto = new IronTrendL1DTO();
- dto.setData(item.getData());
- dto.setCreateTime(item.getSourceTime());
- return dto;
- }).collect(Collectors.toList())
- );
- //L2
- CompletableFuture<List<IronTrendL2DTO>> elemenListFuture = CompletableFuture.supplyAsync(() ->
- l2DataList.stream().map(item -> {
- IronTrendL2DTO dto = new IronTrendL2DTO();
- try {
- dto.setElementS(item.getElementS() != null ? Double.parseDouble(item.getElementS()) : null);
- } catch (NumberFormatException e) {
- dto.setElementS(null);
- }
- try {
- dto.setElementSi(item.getElementSi() != null ? Double.parseDouble(item.getElementSi()) : null);
- } catch (NumberFormatException e) {
- dto.setElementSi(null);
- }
- dto.setIronStarttime(item.getIronStarttime());
- dto.setIronEndtime(item.getIronEndtime());
- dto.setPollMm(item.getPollMm());
- dto.setOpenDepth(item.getOpenDepth());
- dto.setMudWeight(item.getMudWeight());
- dto.setIronCosttime(item.getIronCosttime());
- return dto;
- }).collect(Collectors.toList())
- );
- // 等待所有转换完成
- CompletableFuture.allOf(
- tempListFuture,
- speed1ListFuture,
- speed2ListFuture,
- weight1ListFuture,
- weight2ListFuture,
- elemenListFuture
- ).join();
- // 5. 封装返回
- List<IronTrendL1DTO> tempList = tempListFuture.get();
- List<IronTrendL1DTO> speed1List = speed1ListFuture.get();
- List<IronTrendL1DTO> speed2List = speed2ListFuture.get();
- List<IronTrendL1DTO> weight1List = weight1ListFuture.get();
- List<IronTrendL1DTO> weight2List = weight2ListFuture.get();
- List<IronTrendL2DTO> elemenList = elemenListFuture.get();
- // 1. 生成完整的每一分钟时间点
- java.util.List<String> xAxis = new java.util.ArrayList<>();
- java.util.Calendar cursor = java.util.Calendar.getInstance();
- cursor.setTime(startTime);
- while (!cursor.getTime().after(endTime)) {
- xAxis.add(sdfMinute.format(cursor.getTime()));
- cursor.add(java.util.Calendar.MINUTE, 1);
- }
- // 查询区间前的最近一条历史数据(前置值)
- // OPC类
- OPCData preTemp = opcDataService.lambdaQuery()
- .eq(OPCData::getPointName, "BF4_1_IRONNOTCH_MIR_TEMP")
- .isNotNull(OPCData::getData)
- .lt(OPCData::getServerTime, startTime)
- .orderByDesc(OPCData::getServerTime)
- .last("limit 1").one();
- OPCData preSpeed1 = opcDataService.lambdaQuery()
- .eq(OPCData::getPointName, "BF4_1_IRONNOTCH_RAILLINE_ACPIRON_SPEED1")
- .isNotNull(OPCData::getData)
- .lt(OPCData::getServerTime, startTime)
- .orderByDesc(OPCData::getServerTime)
- .last("limit 1").one();
- OPCData preSpeed2 = opcDataService.lambdaQuery()
- .eq(OPCData::getPointName, "BF4_1_IRONNOTCH_RAILLINE_ACPIRON_SPEED2")
- .isNotNull(OPCData::getData)
- .lt(OPCData::getServerTime, startTime)
- .orderByDesc(OPCData::getServerTime)
- .last("limit 1").one();
- OPCData preWeight1 = opcDataService.lambdaQuery()
- .eq(OPCData::getPointName, "BF4_1TH_1_MIR_NWT")
- .isNotNull(OPCData::getData)
- .lt(OPCData::getServerTime, startTime)
- .orderByDesc(OPCData::getServerTime)
- .last("limit 1").one();
- OPCData preWeight2 = opcDataService.lambdaQuery()
- .eq(OPCData::getPointName, "BF4_1TH_2_MIR_NWT")
- .isNotNull(OPCData::getData)
- .lt(OPCData::getServerTime, startTime)
- .orderByDesc(OPCData::getServerTime)
- .last("limit 1").one();
- // tappingMap补齐每一分钟,前面无数据用前置值补齐
- // 先查区间前最近一条tapping
- Integer preTappingVal = 0;
- OPCData preTapping = opcDataService.lambdaQuery()
- .eq(OPCData::getPointName, "BF4_1_IRONNOTCH_TAPPING")
- .isNotNull(OPCData::getData)
- .lt(OPCData::getServerTime, startTime)
- .orderByDesc(OPCData::getServerTime)
- .last("limit 1").one();
- // L2类
- TL2Data preL2 = tl2DataService.lambdaQuery()
- .lt(TL2Data::getIronStarttime, new SimpleDateFormat(TimeUtils.DATE_TIME_PATTERN).format(startTime))
- .eq(TL2Data::getTapholeId, "1")
- .orderByDesc(TL2Data::getIronStarttime)
- .last("limit 1").one();
- // 2. 构建每分钟补全的二维数组
- java.util.Map<String, Object> result = new java.util.LinkedHashMap<>();
- //
- if (preTapping != null && preTapping.getData() != null) {
- Object v = preTapping.getData();
- if (v instanceof Number) preTappingVal = ((Number) v).intValue();
- else if (v instanceof String) {
- String str = ((String) v).trim();
- if (!str.isEmpty()) {
- try {
- preTappingVal = Integer.parseInt(str);
- } catch (Exception ignore) {
- }
- }
- }
- }
- // 构建每分钟tappingMap
- Map<String, Integer> tappingMap = new LinkedHashMap<>();
- List<OPCData> tappingList = opcDataMap.getOrDefault("BF4_1_IRONNOTCH_TAPPING", new ArrayList<>());
- Map<String, Integer> rawTappingMap = new LinkedHashMap<>();
- for (OPCData item : tappingList) {
- if (item.getServerTime() != null && item.getData() != null) {
- String min = sdfMinute.format(item.getServerTime());
- Integer val = null;
- Object v = item.getData();
- if (v instanceof Number) val = ((Number) v).intValue();
- else if (v instanceof String) {
- String str = ((String) v).trim();
- if (!str.isEmpty()) {
- try {
- val = Integer.parseInt(str);
- } catch (Exception ignore) {
- }
- }
- }
- if (val != null) rawTappingMap.put(min, val);
- }
- }
- Integer last = preTappingVal;
- for (String t : xAxis) {
- Integer v = rawTappingMap.getOrDefault(t, last);
- tappingMap.put(t, v);
- if (v != null) last = v;
- }
- int ironWeightScale = 2; //小数位数,可调整为0、1、2等
- List<List<Object>> ironFlowArr = buildIronWeightMinuteArray(xAxis, tappingMap, weight1List, weight2List, ironWeightScale);
- result.put("ironWeight", ironFlowArr);
- // 处理铁水流速,出铁中按原逻辑,出铁结束后为0
- List<List<Object>> ironSpeedArr = buildMinuteArrayForSpeed(xAxis, speed1List, speed2List, tappingMap);
- List<List<Object>> ironTempArr = buildMinuteArray(tempList, xAxis, preTemp != null ? preTemp.getData() : null);
- for (int i = 0; i < xAxis.size(); i++) {
- String t = xAxis.get(i);
- Integer tapping = tappingMap.getOrDefault(t, 0);
- if (tapping == 0 && ironSpeedArr.get(i).size() > 1) {
- ironSpeedArr.get(i).set(1, 0);
- ironTempArr.get(i).set(1, 0);
- }
- }
- result.put("ironSpeed", ironSpeedArr);
- result.put("ironTemp", ironTempArr);
- // 处理L2类数据,tapping=0时设为0
- List<List<Object>> elementSArr = buildL2MinuteArray(elemenList, xAxis, tappingMap, "elementS", preL2 != null ? preL2.getElementS() : null);
- List<List<Object>> elementSiArr = buildL2MinuteArray(elemenList, xAxis, tappingMap, "elementSi", preL2 != null ? preL2.getElementSi() : null);
- List<List<Object>> mudWeightArr = buildL2MinuteArray(elemenList, xAxis, tappingMap, "mudWeight", preL2 != null ? preL2.getMudWeight() : null);
- List<List<Object>> pollMmArr = buildL2MinuteArray(elemenList, xAxis, tappingMap, "pollMm", preL2 != null ? preL2.getPollMm() : null);
- List<List<Object>> openDepthArr = buildL2MinuteArray(elemenList, xAxis, tappingMap, "openDepth", preL2 != null ? preL2.getOpenDepth() : null);
- List<List<Object>> ironCosttimeArr = buildL2MinuteArray(elemenList, xAxis, tappingMap, "ironCosttime", preL2 != null ? preL2.getIronCosttime() : null);
- // for (int i = 0; i < xAxis.size(); i++) {
- // String t = xAxis.get(i);
- // Integer tapping = tappingMap.getOrDefault(t, 0);
- // if (tapping == 0) {
- // if (elementSArr.get(i).size() > 1) elementSArr.get(i).set(1, 0);
- // if (elementSiArr.get(i).size() > 1) elementSiArr.get(i).set(1, 0);
- // if (mudWeightArr.get(i).size() > 1) mudWeightArr.get(i).set(1, 0);
- // if (pollMmArr.get(i).size() > 1) pollMmArr.get(i).set(1, 0);
- // if (openDepthArr.get(i).size() > 1) openDepthArr.get(i).set(1, 0);
- // if (ironCosttimeArr.get(i).size() > 1) ironCosttimeArr.get(i).set(1, 0);
- // }
- // }
- result.put("elementS", elementSArr);
- result.put("elementSi", elementSiArr);
- result.put("mudWeight", mudWeightArr);
- result.put("pollMm", pollMmArr);
- result.put("openDepth", openDepthArr);
- result.put("ironCosttime", ironCosttimeArr);
- return result;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- // 新增工具方法:L1类型
- private java.util.List<java.util.List<Object>> buildMinuteArray(java.util.List<IronTrendL1DTO> list, java.util.List<String> xAxis, Object preValue) {
- java.util.Map<String, Object> timeValueMap = new java.util.LinkedHashMap<>();
- for (IronTrendL1DTO e : list) {
- if (e.getCreateTime() != null) {
- String min = sdfMinute.format(e.getCreateTime());
- timeValueMap.put(min, e.getData());
- }
- }
- java.util.List<java.util.List<Object>> arr = new java.util.ArrayList<>();
- Object last = preValue;
- for (String t : xAxis) {
- Object v = timeValueMap.getOrDefault(t, last);
- arr.add(java.util.Arrays.asList(t, v));
- if (v != null) last = v;
- }
- return arr;
- }
- // 铁水流速(重写:合并取最大值,出铁周期内补齐空值,且出铁周期内流速不允许为0)
- 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) {
- java.util.Map<String, Double> speed1Map = new java.util.LinkedHashMap<>();
- java.util.Map<String, Double> speed2Map = new java.util.LinkedHashMap<>();
- for (IronTrendL1DTO e : speed1List) {
- if (e.getCreateTime() != null) {
- String min = sdfMinute.format(e.getCreateTime());
- Object v = e.getData();
- Double value = null;
- if (v instanceof Number) value = ((Number) v).doubleValue();
- else if (v instanceof String) {
- try {
- value = Double.parseDouble((String) v);
- } catch (Exception ignore) {
- }
- }
- if (value != null) speed1Map.put(min, value);
- }
- }
- for (IronTrendL1DTO e : speed2List) {
- if (e.getCreateTime() != null) {
- String min = sdfMinute.format(e.getCreateTime());
- Object v = e.getData();
- Double value = null;
- if (v instanceof Number) value = ((Number) v).doubleValue();
- else if (v instanceof String) {
- try {
- value = Double.parseDouble((String) v);
- } catch (Exception ignore) {
- }
- }
- if (value != null) speed2Map.put(min, value);
- }
- }
- // 合并为最大值map
- java.util.Map<String, Double> speedMap = new java.util.LinkedHashMap<>();
- for (String key : speed1Map.keySet()) {
- speedMap.put(key, speed1Map.get(key));
- }
- for (String key : speed2Map.keySet()) {
- double v2 = speed2Map.get(key);
- speedMap.put(key, Math.max(speedMap.getOrDefault(key, 0d), v2));
- }
- java.util.List<java.util.List<Object>> arr = new java.util.ArrayList<>();
- Double last = null;
- for (String t : xAxis) {
- Integer tapping = tappingMap.getOrDefault(t, 0);
- Double v = speedMap.get(t);
- if (tapping == 1) {
- if (v == null || v == 0) {
- // 出铁周期内流速不允许为0,用上一个非0值
- v = last != null ? last : 0d;
- }
- arr.add(java.util.Arrays.asList(t, v));
- if (v != null && v != 0) last = v;
- } else {
- arr.add(java.util.Arrays.asList(t, 0));
- }
- }
- return arr;
- }
- //L2
- 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) {
- java.util.Map<String, Object> timeValueMap = new java.util.LinkedHashMap<>();
- for (IronTrendL2DTO e : list) {
- String startMin = e.getIronStarttime();
- String endMin = e.getIronEndtime();
- if (startMin != null && endMin != null) {
- Object v = null;
- switch (field) {
- case "elementS":
- v = e.getElementS();
- break;
- case "elementSi":
- v = e.getElementSi();
- break;
- case "mudWeight":
- v = e.getMudWeight();
- break;
- case "pollMm":
- v = e.getPollMm();
- break;
- case "openDepth":
- v = e.getOpenDepth();
- break;
- case "ironCosttime":
- v = e.getIronCosttime();
- break;
- }
- // 定义时间格式
- // DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
- // 解析字符串为LocalDateTime对象
- LocalDateTime startTime = LocalDateTime.parse(startMin, LocalDateUtils.DATE_TIME_FORMATTER).withSecond(0);
- LocalDateTime endTime = LocalDateTime.parse(endMin, LocalDateUtils.DATE_TIME_FORMATTER).withSecond(0);
- // 遍历每一分钟
- LocalDateTime currentTime = startTime;
- while (!currentTime.isAfter(endTime)) {
- timeValueMap.put(currentTime.format(LocalDateUtils.DATE_TIME_FORMATTER), v);
- // 增加一分钟
- currentTime = currentTime.plusMinutes(1);
- }
- }
- }
- List<java.util.List<Object>> arr = new ArrayList<>();
- Object last = preValue;
- // for (String time : xAxis) {
- // Integer tapping = tappingMap.getOrDefault(time, 0);
- // Object v = timeValueMap.getOrDefault(time, last);
- // double value = Double.parseDouble(v.toString());
- // if (tapping == 1) {
- // if (value == 0 ) {
- // //
- // value = last != null ? Double.parseDouble(last.toString()) : 0;
- // }
- // arr.add(java.util.Arrays.asList(time,value));
- // if (value != 0){
- // last = value;
- // }
- // } else {
- // arr.add(java.util.Arrays.asList(time, 0));
- // last = 0;
- // }
- // }
- for (String time : xAxis) {
- Object value = timeValueMap.getOrDefault(time, last);
- List item = new ArrayList();
- item.add(time);
- Integer tapping = tappingMap.getOrDefault(time, 0);
- if (tapping == 0) {
- item.add(0);
- last = null;
- } else {
- if (null == value) {
- item.add(0);
- last = null;
- } else {
- item.add(value);
- last = value;
- }
- }
- arr.add(item);
- }
- //处理正在出铁的情况
- if (tappingMap.getOrDefault(xAxis.get(xAxis.size() - 1), 0) > 0) {
- //表明正在出铁中
- for (int i = xAxis.size() - 1; i >= 0; i--) {
- Integer tapping = tappingMap.getOrDefault(xAxis.get(i), 0);
- if (tapping <= 0) {
- break;
- } else {
- if (arr.get(i).size() > 1) arr.get(i).set(1, 0);
- }
- }
- }
- return arr;
- }
- //铁水重量
- private List<List<Object>> buildIronWeightMinuteArray(List<String> xAxis, Map<String, Integer> tappingMap, List<IronTrendL1DTO> weight1List, List<IronTrendL1DTO> weight2List, int scale) {
- Map<String, Double> weight1Map = new LinkedHashMap<>();
- for (IronTrendL1DTO e : weight1List) {
- if (e.getCreateTime() != null) {
- String minute = sdfMinute.format(e.getCreateTime());
- Object v = e.getData();
- Double value = null;
- if (v instanceof Number) value = ((Number) v).doubleValue();
- else if (v instanceof String) {
- String str = ((String) v).trim();
- if (!str.isEmpty()) {
- try {
- value = Double.parseDouble(str);
- } catch (Exception ignore) {
- }
- }
- }
- if (value != null) {
- weight1Map.put(minute, value);
- }
- }
- }
- Map<String, Double> weight2Map = new LinkedHashMap<>();
- for (IronTrendL1DTO e : weight2List) {
- if (e.getCreateTime() != null) {
- String minute = sdfMinute.format(e.getCreateTime());
- Object v = e.getData();
- Double value = null;
- if (v instanceof Number) value = ((Number) v).doubleValue();
- else if (v instanceof String) {
- String str = ((String) v).trim();
- if (!str.isEmpty()) {
- try {
- value = Double.parseDouble(str);
- } catch (Exception ignore) {
- }
- }
- }
- if (value != null) {
- weight2Map.put(minute, value);
- }
- }
- }
- // 合并weight1Map和weight2Map到weightMap,取最大值
- Map<String, Double> weightMap = new LinkedHashMap<>();
- //先将weight1Map复制到weightMap中
- for (Map.Entry<String, Double> entry : weight1Map.entrySet()) {
- weightMap.put(entry.getKey(), entry.getValue());
- }
- //然后将weight2Map复制到weightMap中,重复的取最大值
- for (Map.Entry<String, Double> entry : weight2Map.entrySet()) {
- weightMap.put(entry.getKey(), Math.max(weightMap.getOrDefault(entry.getKey(), 0d), entry.getValue()));
- }
- // 按key正序排序
- weightMap = weightMap.entrySet().stream()
- .sorted(Map.Entry.comparingByKey())
- .collect(java.util.LinkedHashMap::new, (m, e) -> m.put(e.getKey(), e.getValue()), java.util.LinkedHashMap::putAll);
- List<List<Object>> arr = new ArrayList<>();
- boolean inTappingCycle = false;
- //
- BigDecimal cycleTotal = BigDecimal.ZERO;
- //
- BigDecimal lastValue = BigDecimal.ZERO;
- BigDecimal bigDecimal = BigDecimal.ZERO;
- for (int i = 0; i < xAxis.size(); i++) {
- String time = xAxis.get(i);
- Integer tapping = tappingMap.getOrDefault(time, 0);
- bigDecimal = new BigDecimal(weightMap.getOrDefault(time, 0d).toString());
- if (tapping == 1) {
- //切换到出铁状态
- if (!inTappingCycle) {
- //刚刚开始出铁
- inTappingCycle = true;
- log.info("[出铁周期开始] 开始于: " + time);
- if(bigDecimal.compareTo(new BigDecimal("70")) > 0){
- bigDecimal = BigDecimal.ZERO;
- lastValue = BigDecimal.ZERO;
- }
- }
- if (bigDecimal.compareTo(BigDecimal.ZERO) == 0) {
- //复用上次的数据
- bigDecimal = lastValue;
- weightMap.put(time, bigDecimal.doubleValue());
- } else {
- lastValue = bigDecimal;
- }
- if (i > 0) {
- //按照时间正序(10:00:00,10:01:00,10:02:00)
- //将10:00:00对应的数据 - 10:01:00对应的数据
- BigDecimal prevW = new BigDecimal(weightMap.getOrDefault(xAxis.get(i - 1), 0d).toString());
- //上一个减去当前的
- if (prevW.subtract(bigDecimal).compareTo(new BigDecimal("70")) > 0) {
- log.info("[出铁周期满载情况] 时间: " + xAxis.get(i - 1) + ", 铁水满载值: " + prevW);
- cycleTotal = cycleTotal.add(prevW);
- }
- }
- arr.add(java.util.Arrays.asList(time, bigDecimal.add(cycleTotal).setScale(scale, RoundingMode.HALF_UP).doubleValue()));
- } else {
- //切换到出铁结束
- if (inTappingCycle) {
- //刚刚从出铁结束切换过来
- cycleTotal = cycleTotal.add(bigDecimal);
- log.info("[出铁周期结束] 最后时间点的铁水重量: " + bigDecimal);
- bigDecimal = BigDecimal.ZERO;
- double value = cycleTotal.setScale(scale, RoundingMode.HALF_UP).doubleValue();
- log.info("[出铁周期结束] 结束于: " + time + ", 本周期铁水总净重: " + value);
- arr.add(java.util.Arrays.asList(time, value));
- inTappingCycle = false;
- cycleTotal = BigDecimal.ZERO;
- // for (List<Object> objects : arr) {
- // System.out.println(objects.get(0)+" >>> "+objects.get(1));
- // }
- } else {
- arr.add(java.util.Arrays.asList(time, 0));
- }
- }
- }
- return arr;
- }
- }
|