From 1ad80aff65ab3643add3fc79908a7fba0f305876 Mon Sep 17 00:00:00 2001
From: dengzedong <dengzedong@email>
Date: 星期五, 27 六月 2025 10:57:30 +0800
Subject: [PATCH] 焦化异常溯源 指标记录

---
 ansteel-biz/src/main/java/com/iailab/module/ansteel/job/task/RunCokingTraceModelBMTask.java |  300 ++++++++++++++++++++++-------------------------------------
 1 files changed, 111 insertions(+), 189 deletions(-)

diff --git a/ansteel-biz/src/main/java/com/iailab/module/ansteel/job/task/RunCokingTraceModelBMTask.java b/ansteel-biz/src/main/java/com/iailab/module/ansteel/job/task/RunCokingTraceModelBMTask.java
index 3472167..640ca1d 100644
--- a/ansteel-biz/src/main/java/com/iailab/module/ansteel/job/task/RunCokingTraceModelBMTask.java
+++ b/ansteel-biz/src/main/java/com/iailab/module/ansteel/job/task/RunCokingTraceModelBMTask.java
@@ -1,24 +1,22 @@
 package com.iailab.module.ansteel.job.task;
 
 import com.alibaba.fastjson.JSON;
-import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.iailab.framework.common.util.date.DateUtils;
-import com.iailab.module.ansteel.coking.entity.*;
+import com.iailab.module.ansteel.coking.entity.CokingTraceChartEntity;
+import com.iailab.module.ansteel.coking.entity.CokingTraceConfEntity;
 import com.iailab.module.ansteel.coking.service.*;
 import com.iailab.module.ansteel.common.constant.CommonConstant;
-import com.iailab.module.ansteel.common.enums.ProcessConfDataTypeEnum;
-import com.iailab.module.data.api.ind.IndItemApi;
-import com.iailab.module.data.api.ind.dto.ApiIndItemValueDTO;
-import com.iailab.module.data.api.point.DataPointApi;
+import com.iailab.module.ansteel.common.enums.TraceProcessTypeEnum;
 import com.iailab.module.model.api.mcs.McsApi;
-import com.iailab.module.model.api.mcs.dto.ChartParamDTO;
-import org.apache.commons.lang3.StringUtils;
+import com.iailab.module.model.api.mdk.MdkApi;
+import com.iailab.module.model.api.mdk.dto.MdkScheduleReqDTO;
+import com.iailab.module.model.api.mdk.dto.MdkScheduleRespDTO;
+import com.iailab.module.model.api.mdk.dto.StScheduleRecordVO;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
-import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.CollectionUtils;
 
 import java.util.*;
@@ -32,7 +30,7 @@
  * @createTime 2025年04月22日
  */
 @Component("runCokingTraceModelBMTask")
-public class RunCokingTraceModelBMTask implements ITask{
+public class RunCokingTraceModelBMTask implements ITask {
     private Logger logger = LoggerFactory.getLogger(getClass());
 
     @Autowired
@@ -51,45 +49,29 @@
     private CokingTraceIndService cokingTraceIndService;
 
     @Autowired
-    private CokingTraceConfService cokingTraceConfService;
+    private CokingTraceChartService cokingTraceChartService;
 
     @Autowired
-    private CokingTraceChartService cokingTraceChartService;
+    private CokingTraceConfService cokingTraceConfService;
+
 
     @Autowired
     private McsApi mcsApi;
 
     @Autowired
-    private DataPointApi dataPointApi;
+    private MdkApi mdkApi;
 
-    @Autowired
-    private IndItemApi indItemApi;
+    private final static String process = TraceProcessTypeEnum.BM.getProcess();
 
-    private static final HashMap<String, Object> coalColoumMap = new HashMap<String, Object>() {{
-        put("coalColoum0", "一级指标-偏差值");
-        put("coalColoum1", "二级指标-偏差值");
-        put("coalColoum2", "影响因素1-偏差值");
-    }};
+    private final static String reportName = "备煤工序异常溯源";
 
-    private static final HashMap<String, Object> historyPointMap = new HashMap<String, Object>() {{
-        put("BMSY001", "备煤耗电");
-    }};
+    private final static String SugObj = "BM";
 
-    private static final String jsonStr="{\n" +
-            "    \"result\": {\n" +
-            "      \"coalPrepElec\":[1600.8,1613.5],     \n" +
-            "      \"coalPrepElecIndex\":[[0.8,0.73],[723,608],[1782,1782],[752,743],[729,783]],\n" +
-            "      \"coalPrepElecTotal1\":\"1#粉碎机耗电因粉碎机偏高增加220KW/h,经模型计算,建建议调整单班用煤量,预计可使粉碎机耗电量指标降低170KW/h\",\n" +
-            "      \"coalPrepElecTotal2\":\"1#粉碎机耗电量数据异常\",\n" +
-            "     \"coalColoum0\":[[0,450],[0,137],[0,342]],\n" +
-            "     \"coalColoum1\":[[2,306],[3,134],[1,142]],\n" +
-            "     \"coalColoum2\":[[4,142],[4,132],[4,349]],\n" +
-            "\"coalPrepElecHomePage\":[[50,42],[10,10]],\n" +
-            "\"coalHomeIndex\":[[90.4,90.2],[80.3,78],[103,100],[280,270]],\n" +
-            "\"coalIndexInfo\":\"2月18日甲班:备煤耗电异常\",\n" +
-            "\"coalErr\":\"2月18日粉碎机耗电数据异常(无数据)\"\n" +
-            "    }" +
-            "}";
+    private final static String indType = "备煤工序异常溯源";
+
+    private String scheduleCode = "";
+    private final String finalResultStrKey = "finalResultStr";
+    private final String resultListKey = "resultList";
 
     @Override
     public void run(String params) {
@@ -98,46 +80,59 @@
             Calendar calendar = Calendar.getInstance();
             calendar.set(Calendar.MILLISECOND, 0);
             calendar.set(Calendar.SECOND, 0);
-            calendar.set(Calendar.MINUTE,0);
-            calendar.add(Calendar.DAY_OF_YEAR, -1);
-            String yesterday = DateUtils.format(calendar.getTime(), DateUtils.FORMAT_YEAR_MONTH_DAY);
-            int hour = calendar.get(Calendar.HOUR_OF_DAY);
-            Date startDate = calendar.getTime();
-            calendar.add(Calendar.HOUR, 8);
-            Date endDate = calendar.getTime();
-            String clock = "";
-            String analyClass = "";
-            if (hour == 0) {
-                clock = yesterday.concat("-").concat("A");
-                analyClass = "甲班";
-            } else if (hour == 8) {
-                clock = yesterday.concat("-").concat("B");
-                analyClass = "乙班";
-            } else if (hour == 16) {
-                clock = yesterday.concat("-").concat("C");
-                analyClass = "丙班";
-            }
-            Map<String, Object> tMap = new HashMap<>();
-            tMap.put("relId", UUID.randomUUID().toString());
-            tMap.put("process", "备煤工序");
-            tMap.put("reportName", "备煤工序");
-            tMap.put("analyDate", yesterday);
-            tMap.put("analyClass", analyClass);
-            tMap.put("clock", clock);
-            tMap.put("analyContent", "备煤耗电异常");
-            tMap.put("startDate", startDate);
-            tMap.put("endDate", endDate);
+            calendar.set(Calendar.MINUTE, 0);
 
-            JSONObject jsonObject= JSONObject.parseObject(jsonStr);
-            JSONObject result = (JSONObject) JSON.toJSON(jsonObject.get("result"));
-            if(!Objects.isNull(result)){
-                saveTraceReport(tMap,result);
-                saveTraceSuggest(tMap,result);
-                saveTraceDeviation(tMap,result);
-                saveAnalyInd(tMap,result);
-                saveTraceInd(tMap,result);
-                saveTraceChart(tMap,result,startDate,endDate);
+            calendar.set(Calendar.HOUR_OF_DAY, 0);
+            Date endDate = calendar.getTime();
+            calendar.add(Calendar.DAY_OF_YEAR, -1);
+            Date startDate = calendar.getTime();
+            String clock = DateUtils.format(startDate);
+
+            calendar.add(Calendar.DAY_OF_YEAR, 1);
+            calendar.add(Calendar.MINUTE, -3);
+            Date collectStartDate = calendar.getTime();
+
+            // 查询模型结果
+            List<StScheduleRecordVO> lastScheduleData = mcsApi.getLastScheduleData(scheduleCode, 1);
+            if (CollectionUtils.isEmpty(lastScheduleData)) {
+                logger.info("模型结果为空");
+                return;
             }
+            StScheduleRecordVO stScheduleRecordVO = lastScheduleData.get(0);
+            if (stScheduleRecordVO == null) {
+                logger.info("模型结果为空");
+                return;
+            }
+            String jsonStr = stScheduleRecordVO.getResultData();
+
+            JSONObject result = JSONObject.parseObject(jsonStr);
+            if (Objects.isNull(result)) {
+                logger.info("模型结果为空");
+                return;
+            }
+
+            // 保存报告
+            String analyDate = DateUtils.format(startDate);
+            String analyContent = "备煤异常";
+            String relId = cokingTraceReportService.save(process, reportName, analyDate, clock, analyContent);
+
+            // 保存一级分析指标
+            cokingAnalyIndService.saveAnalyInd(relId, process, analyDate, analyContent);
+
+            // 保存优化建议
+            String suggest = result.getString(finalResultStrKey);
+            cokingTraceSuggestService.saveTraceSuggest(relId, process, clock, suggest, SugObj);
+
+            // 保存偏差值
+            String jsonString = result.getString(resultListKey);
+            cokingTraceDeviationService.saveTraceDeviation(relId, process, clock, jsonString, SugObj);
+
+            // 保存溯源指标
+            cokingTraceIndService.saveTraceInd(relId, indType, clock, collectStartDate, endDate);
+
+            // 保存溯源折线图
+            saveTraceChart(relId, clock, indType, startDate, endDate);
+
         } catch (Exception ex) {
             logger.error("RunCokingTraceModelBMTask运行异常");
             ex.printStackTrace();
@@ -145,130 +140,57 @@
         logger.info("RunCokingTraceModelBMTask运行完成");
     }
 
-    public void saveTraceReport(Map<String, Object> tMap,JSONObject result) {
-        CokingTraceReportEntity reportEntity = new CokingTraceReportEntity();
-        reportEntity.setId(tMap.get("relId").toString());
-        reportEntity.setProcess(tMap.get("process").toString());
-        reportEntity.setReportName(tMap.get("reportName").toString());
-        reportEntity.setAnalyDate(tMap.get("analyDate").toString());
-        reportEntity.setAnalyClass(tMap.get("analyClass").toString());
-        reportEntity.setClock(tMap.get("clock").toString());
-        reportEntity.setAnalyContent(tMap.get("analyContent").toString());
-        reportEntity.setCreateDate(new Date());
-        cokingTraceReportService.save(reportEntity);
-    }
+    public void saveTraceChart(String relId, String clock, String indType, Date startDate, Date endDate) {
+        logger.info("查询需要保存chart的配置");
 
-    public void saveTraceSuggest(Map<String, Object> tMap,JSONObject result) {
-            String coalTotal = result.get("coalPrepElecTotal1").toString();
-            if(StringUtils.isNotBlank(coalTotal)){
-                CokingTraceSuggestEntity suggestEntity = new CokingTraceSuggestEntity();
-                suggestEntity.setRelId(tMap.get("relId").toString());
-                suggestEntity.setProcess(tMap.get("process").toString());
-                suggestEntity.setSugObj("BM");
-                suggestEntity.setClock(tMap.get("clock").toString());
-                suggestEntity.setContent(coalTotal);
-                suggestEntity.setCreateDate(new Date());
-                cokingTraceSuggestService.save(suggestEntity);
-            }
-    }
+        Calendar calendar = Calendar.getInstance();
+        calendar.set(Calendar.MILLISECOND, 0);
+        calendar.set(Calendar.SECOND, 0);
+        calendar.set(Calendar.MINUTE, 0);
+        calendar.set(Calendar.HOUR_OF_DAY, 0);
+        calendar.set(Calendar.DAY_OF_MONTH, 1);
+        Date monthStartDate = calendar.getTime();
 
-    @Transactional
-    public void saveTraceDeviation(Map<String, Object> tMap,JSONObject result) {
-        List<ChartParamDTO> list = mcsApi.getChartParamList(CommonConstant.COAL_INDEX_CHARTCODE);
-        Map<String, String> steamIndexMaps = list.stream().collect(Collectors.toMap(ChartParamDTO::getParamCode, e -> e.getParamName()));
-        for(int i=0;i<=2;i++){
-            String coalColoum = result.get("coalColoum"+i).toString();
-            if(StringUtils.isNotBlank(coalColoum)){
-                JSONArray responseArr = JSON.parseArray(coalColoum);
-                for (int j = 0; j < responseArr.size(); j++) {
-                    JSONArray element = JSON.parseArray(responseArr.get(j).toString());
-                    CokingTraceDeviationEntity deviationEntity = new CokingTraceDeviationEntity();
-                    deviationEntity.setRelId(tMap.get("relId").toString());
-                    deviationEntity.setProcess(tMap.get("process").toString());
-                    deviationEntity.setClock(tMap.get("clock").toString());
-                    deviationEntity.setSugObj("BM");
-                    deviationEntity.setGroupName(coalColoumMap.get("coalColoum"+i).toString());
-                    deviationEntity.setIndName(steamIndexMaps.get(element.get(0).toString()));
-                    deviationEntity.setIndValue(element.get(1).toString());
-                    deviationEntity.setCreateDate(new Date());
-                    cokingTraceDeviationService.save(deviationEntity);
-                }
-            }
-        }
-    }
-
-    public void saveAnalyInd(Map<String, Object> tMap,JSONObject result) {
-        String[] analyContent = tMap.get("analyContent").toString().split(";");
-        for(int i=0;i<analyContent.length;i++){
-            CokingAnalyIndEntity analyIndEntity = new CokingAnalyIndEntity();
-            analyIndEntity.setRelId(tMap.get("relId").toString());
-            analyIndEntity.setAnalyType(tMap.get("process").toString());
-            analyIndEntity.setAnalyDate(tMap.get("analyDate").toString());
-            analyIndEntity.setAnalyClass(tMap.get("analyClass").toString());
-            analyIndEntity.setAnalyContent(analyContent[i]);
-            analyIndEntity.setSort(i+1);
-            cokingAnalyIndService.save(analyIndEntity);
-        }
-    }
-
-    public void saveTraceInd(Map<String, Object> tMap,JSONObject result) {
-        Map<String,Object> map = new HashMap<>();
-        map.put("indType","备煤工序异常溯源");
+        Map<String, Object> map = new HashMap<>();
+        map.put("indType", indType);
+        map.put("ext1", "chart");
         List<CokingTraceConfEntity> list = cokingTraceConfService.list(map);
         if (CollectionUtils.isEmpty(list)) {
-            logger.info("ConfLis is Empty");
             return;
         }
-        for (CokingTraceConfEntity conf : list) {
-            if (StringUtils.isBlank(conf.getDataType())) {
-                logger.info("DataType is Empty");
-                continue;
-            }
-            if (StringUtils.isBlank(conf.getPointNo())) {
-                logger.info("PointNo is Empty");
-                continue;
-            }
-            String value = "";
-            switch (ProcessConfDataTypeEnum.getEumByCode(conf.getDataType())) {
-                case DATAPOINT:
-                    List<String> points = new ArrayList<>();
-                    points.add(conf.getPointNo());
-                    Map<String, Object> pointsRealValue = dataPointApi.queryPointsRealValue(points);
-                    value = pointsRealValue.get(conf.getPointNo()).toString();
-                    break;
-                case IND:
-                    List<ApiIndItemValueDTO> indValues = indItemApi.queryIndItemDefaultValue(conf.getPointNo());
-                    if (!CollectionUtils.isEmpty(indValues)) {
-                        value = indValues.get(indValues.size() - 1).getDataValue().toString();
-                    }
-                    break;
-                default:
-                    break;
-            }
-            CokingTraceIndEntity cokingTraceIndEntity = new CokingTraceIndEntity();
-            cokingTraceIndEntity.setRelId(tMap.get("relId").toString());
-            cokingTraceIndEntity.setTitle(conf.getIndType());
-            cokingTraceIndEntity.setClock(tMap.get("clock").toString());
-            cokingTraceIndEntity.setIndCode(conf.getIndCode());
-            cokingTraceIndEntity.setIndName(conf.getIndName());
-            cokingTraceIndEntity.setIndValue(value);
-            cokingTraceIndEntity.setIndUnit(conf.getIndUnit());
-            cokingTraceIndService.save(cokingTraceIndEntity);
-        }
-    }
+        logger.info("list.size={}", list.size());
 
-    public void saveTraceChart(Map<String, Object> tMap, JSONObject result,Date startDate,Date endDate) {
-        historyPointMap.forEach((key,value) ->{
+        List<CokingTraceChartEntity> entityList = new ArrayList<>();
+
+        List<CokingTraceConfEntity> listMin = list.stream().filter(e -> e.getIndCode().equals("BMSY025")).collect(Collectors.toList());
+
+        listMin.forEach(value -> {
             CokingTraceChartEntity cokingTraceChartEntity = new CokingTraceChartEntity();
-            cokingTraceChartEntity.setRelId(tMap.get("relId").toString());
-            cokingTraceChartEntity.setName(value.toString());
-            cokingTraceChartEntity.setClock(tMap.get("clock").toString());
-            cokingTraceChartEntity.setDataNo(key);
-            cokingTraceChartEntity.setDataType("DATAPOINT");
+            cokingTraceChartEntity.setRelId(relId);
+            cokingTraceChartEntity.setName(value.getIndName());
+            cokingTraceChartEntity.setClock(clock);
+            cokingTraceChartEntity.setDataType(value.getDataType());
+            cokingTraceChartEntity.setDataNo(value.getPointNo());
             cokingTraceChartEntity.setStartTime(startDate);
             cokingTraceChartEntity.setEndTime(endDate);
             cokingTraceChartEntity.setCreateDate(new Date());
-            cokingTraceChartService.save(cokingTraceChartEntity);
+            entityList.add(cokingTraceChartEntity);
         });
+
+        List<CokingTraceConfEntity> listMonth = list.stream().filter(e -> e.getIndCode().equals("BMSY026")).collect(Collectors.toList());
+
+        listMonth.forEach(value -> {
+            CokingTraceChartEntity cokingTraceChartEntity = new CokingTraceChartEntity();
+            cokingTraceChartEntity.setRelId(relId);
+            cokingTraceChartEntity.setName(value.getIndName());
+            cokingTraceChartEntity.setClock(clock);
+            cokingTraceChartEntity.setDataType(value.getDataType());
+            cokingTraceChartEntity.setDataNo(value.getPointNo());
+            cokingTraceChartEntity.setStartTime(monthStartDate);
+            cokingTraceChartEntity.setEndTime(endDate);
+            cokingTraceChartEntity.setCreateDate(new Date());
+            entityList.add(cokingTraceChartEntity);
+        });
+        cokingTraceChartService.insert(entityList);
     }
 }
\ No newline at end of file

--
Gitblit v1.9.3