| | |
| | | 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.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 org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.CollectionUtils; |
| | | |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | import java.util.Calendar; |
| | | import java.util.Date; |
| | | import java.util.Objects; |
| | | |
| | | /** |
| | | * 化产工序异常溯源 |
| | |
| | | * @createTime 2025年04月22日 |
| | | */ |
| | | @Component("runCokingTraceModelHCTask") |
| | | public class RunCokingTraceModelHCTask implements ITask{ |
| | | public class RunCokingTraceModelHCTask implements ITask { |
| | | private Logger logger = LoggerFactory.getLogger(getClass()); |
| | | |
| | | @Autowired |
| | |
| | | private CokingTraceIndService cokingTraceIndService; |
| | | |
| | | @Autowired |
| | | private CokingTraceConfService cokingTraceConfService; |
| | | |
| | | @Autowired |
| | | private CokingTraceChartService cokingTraceChartService; |
| | | |
| | | @Autowired |
| | | private McsApi mcsApi; |
| | | |
| | | @Autowired |
| | | private DataPointApi dataPointApi; |
| | | private MdkApi mdkApi; |
| | | |
| | | @Autowired |
| | | private IndItemApi indItemApi; |
| | | private final static String process = TraceProcessTypeEnum.HC.getProcess(); |
| | | |
| | | private static final HashMap<String, Object> chemProdColoumMap = new HashMap<String, Object>() {{ |
| | | put("chemProdFirstColoum0", "一级指标-偏差值"); |
| | | put("chemProdFirstColoum1", "二级指标-偏差值"); |
| | | put("chemProdFirstColoum2", "影响因素1-偏差值"); |
| | | }}; |
| | | private final static String reportName = "化产工序异常溯源"; |
| | | |
| | | private static final HashMap<String, Object> historyPointMap = new HashMap<String, Object>() {{ |
| | | put("F0000101006", "蒸汽消耗"); |
| | | put("F0000101006", "初冷器出口煤气温度"); |
| | | put("F0000101006", "洗苯塔后煤气含苯"); |
| | | put("F0000101006", "脱硫后H2S含量"); |
| | | }}; |
| | | private final static String SugObj = "HC"; |
| | | |
| | | private static final String jsonStr="{\n" + |
| | | " \"result\":{\n" + |
| | | " \"chemProdSteam\":[[33836,33925]], \n" + |
| | | " \"chemProdIndex\":[[352,355],[68,66.3],[82.5,87.6],[50.7,56.5],[73.5,73.1]],\n" + |
| | | " \"chemProdTotal\":\"蒸汽消耗量异常,当前值1342,经模型计算,指标1异常,当前值1342,建议调整指标1至区间[90,110],指标2异常,当前值1342,建议调整指标2至区间[50,80]\",\n" + |
| | | " \"chemProdFirstColoum0\":[[0,42]],\n" + |
| | | " \"chemProdFirstColoum1\":[[5,1342],[7,1342],[9,1342]],\n" + |
| | | " \"chemProdFirstColoum2\":[[5,10,1342],[5,16,1342],[7,18,1342]],\n" + |
| | | " \"chemProdHomePage\":[[9,1342],[9,1342]],\n" + |
| | | " \"chemProdHomeIndex\":[[90.4,90.2],[80.3,78],[103,100],[280,270]],\n" + |
| | | " \"chemProdHomeIndexInfo\":\"2月18日甲班:化产蒸汽消耗量异常\",\n" + |
| | | " \"chemProdHomeErr\":\"2月18日化产蒸汽消耗量数据异常(无数据)\"\n" + |
| | | " }" + |
| | | private final static String indType = "化产工序异常溯源"; |
| | | |
| | | private final static String row = "chemProdRow"; |
| | | |
| | | private final static String total = "chemProdTotal"; |
| | | |
| | | private static final String jsonStr = "{\n" + |
| | | "\"result\":{\n" + |
| | | "\"chemProdHomePage\":[1782.7,0.8]," + |
| | | "\"chemProdElec\":[794680.52]," + |
| | | "\"chemProdIndex\":[16.3,2.57,0.017,0.745,658731.5]," + |
| | | "\"chemProdRow0\":[[0.0,42.3],[5.0,1342.5],[10.0,122.5]]," + |
| | | "\"chemProdRow1\":[[0.0,42.3],[7.0,12.5],[9.0,17.45]]," + |
| | | "\"chemProdRow2\":[[0.0,42.3],[12.0,23.5],[35.0,54.33]]," + |
| | | "\"chemProdSteam\":[46.3]," + |
| | | "\"chemProdTotal\":\"蒸汽消耗量异常,经模型计算,原因和调整建议如下:电捕绝缘箱温度异常,当前值190,建议调整电捕绝缘箱温度至区间[80,110], 蒸氨塔塔顶温度异常,当前值190,建议调整蒸氨塔塔顶温度至区间[101,103]\"," + |
| | | "\"chemProdHomeIndexInfo\":\"化产蒸汽消耗量异常\""+ |
| | | " }" + |
| | | "}"; |
| | | |
| | | @Override |
| | |
| | | calendar.set(Calendar.MILLISECOND, 0); |
| | | calendar.set(Calendar.SECOND, 0); |
| | | calendar.set(Calendar.MINUTE, 0); |
| | | int hour = calendar.get(Calendar.HOUR_OF_DAY); |
| | | Date startDate = calendar.getTime(); |
| | | Date endDate = calendar.getTime(); |
| | | String clock = ""; |
| | | String analyClass = ""; |
| | | if (hour >= 8 && hour < 16) { |
| | | calendar.set(Calendar.HOUR_OF_DAY, 0); |
| | | startDate = calendar.getTime(); |
| | | calendar.add(Calendar.HOUR_OF_DAY, 8); |
| | | endDate = calendar.getTime(); |
| | | clock = DateUtils.format(startDate).concat("-").concat("A"); |
| | | analyClass = "甲班"; |
| | | } else if (hour >= 16 && hour < 24) { |
| | | calendar.set(Calendar.HOUR_OF_DAY, 8); |
| | | startDate = calendar.getTime(); |
| | | calendar.add(Calendar.HOUR_OF_DAY, 8); |
| | | endDate = calendar.getTime(); |
| | | clock = DateUtils.format(startDate).concat("-").concat("B"); |
| | | analyClass = "乙班"; |
| | | } else if (hour >= 0 && hour < 8) { |
| | | calendar.add(Calendar.DAY_OF_YEAR, -1); |
| | | calendar.set(Calendar.HOUR_OF_DAY, 16); |
| | | startDate = calendar.getTime(); |
| | | calendar.add(Calendar.HOUR_OF_DAY, 8); |
| | | endDate = calendar.getTime(); |
| | | clock = DateUtils.format(startDate).concat("-").concat("B"); |
| | | analyClass = "丙班"; |
| | | } |
| | | Map<String, Object> tMap = new HashMap<>(); |
| | | tMap.put("relId", UUID.randomUUID().toString()); |
| | | tMap.put("process", "化产工序"); |
| | | tMap.put("reportName", "化产工序"); |
| | | tMap.put("analyDate", DateUtils.format(startDate)); |
| | | tMap.put("analyClass", analyClass); |
| | | tMap.put("clock", clock); |
| | | tMap.put("analyContent", "化产蒸汽消耗量异常"); |
| | | tMap.put("startDate", startDate); |
| | | tMap.put("endDate", endDate); |
| | | |
| | | JSONObject jsonObject= JSONObject.parseObject(jsonStr); |
| | | 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); |
| | | |
| | | // 调用模型 |
| | | 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); |
| | | |
| | | if (Objects.isNull(result)) { |
| | | logger.info("模型结果为空"); |
| | | return; |
| | | } |
| | | |
| | | // 保存报告 |
| | | String analyDate = DateUtils.format(startDate); |
| | | String analyContent = clock + " " + result.getString("chemProdHomeIndexInfo"); |
| | | String content = result.getString(total); |
| | | String relId = cokingTraceReportService.save(process, reportName, analyDate, clock, analyContent); |
| | | |
| | | // 保存一级分析指标 |
| | | cokingAnalyIndService.saveAnalyInd(relId, process, analyDate, analyContent); |
| | | |
| | | // 保存优化建议 |
| | | cokingTraceSuggestService.saveTraceSuggest(relId, process, clock, content, SugObj); |
| | | |
| | | // 保存偏差值 |
| | | cokingTraceDeviationService.saveTraceDeviation(relId, process, clock, result, CommonConstant.COKE_INDEX_CHARTCODE, row, SugObj); |
| | | |
| | | // 保存溯源指标 |
| | | cokingTraceIndService.saveTraceInd(relId, indType, clock); |
| | | |
| | | // 保存溯源折线图 |
| | | cokingTraceChartService.save(relId, clock, indType, startDate, endDate); |
| | | |
| | | } catch (Exception ex) { |
| | | logger.error("RunCokingTraceModelHCTask运行异常"); |
| | | ex.printStackTrace(); |
| | | } |
| | | logger.info("RunCokingTraceModelHCTask运行完成"); |
| | | } |
| | | |
| | | 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 saveTraceSuggest(Map<String, Object> tMap,JSONObject result) { |
| | | String chemProdTotal = result.get("chemProdTotal").toString(); |
| | | if(StringUtils.isNotBlank(chemProdTotal)){ |
| | | CokingTraceSuggestEntity suggestEntity = new CokingTraceSuggestEntity(); |
| | | suggestEntity.setRelId(tMap.get("relId").toString()); |
| | | suggestEntity.setProcess(tMap.get("process").toString()); |
| | | suggestEntity.setSugObj("HC"); |
| | | suggestEntity.setClock(tMap.get("clock").toString()); |
| | | suggestEntity.setContent(chemProdTotal); |
| | | suggestEntity.setCreateDate(new Date()); |
| | | cokingTraceSuggestService.save(suggestEntity); |
| | | } |
| | | } |
| | | |
| | | public void saveTraceDeviation(Map<String, Object> tMap,JSONObject result) { |
| | | List<ChartParamDTO> list = mcsApi.getChartParamList(CommonConstant.CHEMPROD_INDEX_CHARTCODE); |
| | | Map<String, String> steamIndexMaps = list.stream().collect(Collectors.toMap(ChartParamDTO::getParamCode, e -> e.getParamName())); |
| | | for(int i=0;i<=2;i++){ |
| | | String chemProdFirstColoum = result.get("chemProdFirstColoum"+i).toString(); |
| | | if(StringUtils.isNotBlank(chemProdFirstColoum)){ |
| | | JSONArray responseArr = JSON.parseArray(chemProdFirstColoum); |
| | | List<CokingTraceDeviationEntity> cokingTraceDeviationEntities = new ArrayList<>(); |
| | | 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("HC"); |
| | | deviationEntity.setGroupName(chemProdColoumMap.get("chemProdFirstColoum"+i).toString()); |
| | | deviationEntity.setIndName(steamIndexMaps.get(element.get(0).toString())); |
| | | deviationEntity.setIndValue(element.get(1).toString()); |
| | | deviationEntity.setCreateDate(new Date()); |
| | | cokingTraceDeviationEntities.add(deviationEntity); |
| | | } |
| | | cokingTraceDeviationService.save(cokingTraceDeviationEntities); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public void saveAnalyInd(Map<String, Object> tMap,JSONObject result) { |
| | | String[] analyContent = tMap.get("analyContent").toString().split(";"); |
| | | List<CokingAnalyIndEntity> list = new ArrayList<>(); |
| | | 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); |
| | | list.add(analyIndEntity); |
| | | } |
| | | cokingAnalyIndService.save(list); |
| | | } |
| | | |
| | | public void saveTraceInd(Map<String, Object> tMap,JSONObject result) { |
| | | Map<String,Object> map = new HashMap<>(); |
| | | map.put("indType","化产工序异常溯源"); |
| | | List<CokingTraceConfEntity> list = cokingTraceConfService.list(map); |
| | | if (CollectionUtils.isEmpty(list)) { |
| | | logger.info("ConfLis is Empty"); |
| | | return; |
| | | } |
| | | List<CokingTraceIndEntity> cokingTraceIndEntities = new ArrayList<>(); |
| | | 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()); |
| | | cokingTraceIndEntities.add(cokingTraceIndEntity); |
| | | } |
| | | cokingTraceIndService.save(cokingTraceIndEntities); |
| | | } |
| | | |
| | | public void saveTraceChart(Map<String, Object> 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); |
| | | }); |
| | | } |
| | | } |