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.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.model.api.mcs.McsApi; import com.iailab.module.model.api.mcs.dto.ChartParamDTO; import org.apache.commons.lang3.StringUtils; 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.*; import java.util.stream.Collectors; /** * 炼焦工序-异常溯源 * * @author lirm * @Description * @createTime 2025年04月21日 */ @Component("runCokingTraceModel3Task") public class RunCokingTraceModel3Task implements ITask { private Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private CokingTraceReportService cokingTraceReportService; @Autowired private CokingTraceSuggestService cokingTraceSuggestService; @Autowired private CokingTraceDeviationService cokingTraceDeviationService; @Autowired private CokingAnalyIndService cokingAnalyIndService; @Autowired private CokingTraceIndService cokingTraceIndService; @Autowired private CokingTraceConfService cokingTraceConfService; @Autowired private CokingTraceChartService cokingTraceChartService; @Autowired private McsApi mcsApi; @Autowired private DataPointApi dataPointApi; @Autowired private IndItemApi indItemApi; private static final HashMap cokeColoumMap = new HashMap() {{ put("cokeColoum0", "一级指标-偏差值"); put("cokeColoum1", "二级指标-偏差值"); put("cokeColoum2", "影响因素1-偏差值"); put("cokeColoum3", "影响因素2-偏差值"); put("cokeColoum4", "影响因素3-偏差值"); put("cokeColoum5", "影响因素n-偏差值"); }}; private static final HashMap historyPointMap = new HashMap() {{ put("LJSY001", "荒煤气发生"); put("LJSY005", "耗热量"); }}; private static final String jsonStr="{\n" + " \"result\": {\n" + " \"cokeCOG\":[100.5,99.8],\n" + " \"cokeHeat\":[3.7,3.8],\n" + " \"cokeIndex\":[[0.8,0.7],[608,608],[1440,1460],[1346,1293],[0.8,0.7],[1372,1293]],\n" + " \"cokeTotal\":\"炼焦吨焦耗热量因煤气流量偏高、煤气水分偏低而增加23,经模型计算,延建议调整煤气流量,预计可使吨焦耗热量指标降低17\",\n" + " \"cokeColoum0\":[[0,13],[0,42],[0,34]],\n" + " \"cokeColoum1\":[[13,12],[12,34],[24,42]],\n" + " \"cokeColoum2\":[[27,45],[58,42],[70,31]],\n" + " \"cokeColoum3\":[[108,56],[110,89],[185,21]],\n" + " \"cokeColoum4\":[[199,13],[200,51],[197,34]],\n" + " \"cokeColoum5\":[[147,33],[131,12],[122,13]],\n" + " \"cokeHomePage\":[[202,42],[203,13],[204,45],[205,34],[206,17]],\n" + " \"cokeHomeIndex\":[[90.4,90.2],[80.3,78],[103,100],[280,270]],\n" + " \"cokeHomeIndexInfo\":\"2月18日甲班:吨焦耗热量异常\",\n" + " \"cokeHomeErr\":\"2月18日煤气预热器前数据异常(无数据)\"\n" + " }\n" + "}"; @Override @Transactional public void run(String params) { logger.info("RunCokingTraceModel3Task,参数为:{}", params); try { 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(); int hour = 8; 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 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", "COG回收量异常;吨焦耗热量异常"); tMap.put("startDate", startDate); tMap.put("endDate", endDate); 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); } } catch (Exception ex) { logger.error("RunCokingTraceModel3Task运行异常"); ex.printStackTrace(); } logger.info("RunCokingTraceModel3Task运行完成"); } private void saveTraceReport(Map 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); } private void saveTraceSuggest(Map tMap,JSONObject result) { String cokeTotal = result.get("cokeTotal").toString(); if(StringUtils.isNotBlank(cokeTotal)){ CokingTraceSuggestEntity suggestEntity = new CokingTraceSuggestEntity(); suggestEntity.setRelId(tMap.get("relId").toString()); suggestEntity.setProcess(tMap.get("process").toString()); suggestEntity.setSugObj("LJ"); suggestEntity.setClock(tMap.get("clock").toString()); suggestEntity.setContent(cokeTotal); suggestEntity.setCreateDate(new Date()); cokingTraceSuggestService.save(suggestEntity); } } private void saveTraceDeviation(Map tMap,JSONObject result) { List list = mcsApi.getChartParamList(CommonConstant.COKE_INDEX_CHARTCODE); Map steamIndexMaps = list.stream().collect(Collectors.toMap(ChartParamDTO::getParamCode, e -> e.getParamValue())); for(int i=0;i<=5;i++){ String cokeColoum = result.get("cokeColoum"+i).toString(); if(StringUtils.isNotBlank(cokeColoum)){ JSONArray responseArr = JSON.parseArray(cokeColoum); 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("LJ"); deviationEntity.setGroupName(cokeColoumMap.get("cokeColoum"+i).toString()); deviationEntity.setIndName(steamIndexMaps.get(element.get(0).toString())); // deviationEntity.setIndName(element.get(0).toString()); deviationEntity.setIndValue(element.get(1).toString()); deviationEntity.setCreateDate(new Date()); cokingTraceDeviationService.save(deviationEntity); } } } } private void saveAnalyInd(Map tMap,JSONObject result) { String[] analyContent = tMap.get("analyContent").toString().split(";"); for(int i=0;i tMap,JSONObject result) { Map map = new HashMap<>(); map.put("indType","炼焦工序异常溯源"); List 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 points = new ArrayList<>(); points.add(conf.getPointNo()); Map pointsRealValue = dataPointApi.queryPointsRealValue(points); value = pointsRealValue.get(conf.getPointNo()).toString(); break; case IND: List 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.setIndName(conf.getIndName()); cokingTraceIndEntity.setIndValue(value); cokingTraceIndEntity.setIndUnit(conf.getIndUnit()); cokingTraceIndService.save(cokingTraceIndEntity); } } private void saveTraceChart(Map tMap, JSONObject result,Date startDate,Date endDate) { historyPointMap.forEach((key,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.setStartTime(startDate); cokingTraceChartEntity.setEndTime(endDate); cokingTraceChartEntity.setCreateDate(new Date()); cokingTraceChartService.save(cokingTraceChartEntity); }); } }