| | |
| | | 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.transaction.annotation.Transactional; |
| | | 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("runCokingTraceModelBMTask") |
| | | public class RunCokingTraceModelBMTask implements ITask{ |
| | | public class RunCokingTraceModelBMTask 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.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 final static String row = "coalRow"; |
| | | |
| | | private final static String total = "coalPrepElecTotal1"; |
| | | |
| | | private static final String jsonStr = "{\n" + " " + |
| | | "\"result\": {\n" + |
| | | "\"coalHomeIndexInfo\":\"备煤耗电偏高\"," + |
| | | "\"coalPrepElec\":[1600.8,1613.5]," + |
| | | "\"coalPrepElecTotal1\":\"备煤耗电量偏高,经模型计算,原因和调整建议如下:煤量异常,当前值2000, 建议调整煤量至区间[765.0,1020.0]\"," + |
| | | "\"coalPrepElecTime\":[[0.3,0.3],[1700.31,1900.2],[120.3]]," + |
| | | "\"coalPrepElecIndex\":[133527.2,283517.6,83451.5,233461.2,83564.4]," + |
| | | "\"coalRow0\":[[0.0,1000.3],[1.0,120.2],[7.0,1150.32]]," + |
| | | "\"coalRow1\":[[0.0,1000.5],[2.0,200.56],[7.0,120.2]]," + |
| | | "\"coalRow2\":[[0.0,1000.6],[3.0,261.7],[7.0,170.52]]," + |
| | | "\"coalPrepElecHomePage\":[503000.6,84.04]," + |
| | | "\"coalHomeIndex\":[30.0,4.6,523000.6]" + |
| | | " }" + |
| | | "}"; |
| | | |
| | | @Override |
| | |
| | | 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); |
| | | 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); |
| | | |
| | | // 调用模型 |
| | | /* MdkScheduleReqDTO dto = new MdkScheduleReqDTO(); |
| | | dto.setScheduleTime(calendar.getTime()); |
| | | dto.setScheduleCode(params); |
| | | MdkScheduleRespDTO mdkScheduleRespDTO = mdkApi.doSchedule(dto); |
| | | logger.info(params + "调度方案执行完成," + mdkScheduleRespDTO); |
| | | Map<String, Object> result = mdkScheduleRespDTO.getResult(); |
| | | |
| | | JSONObject jsonObject = new JSONObject(result); |
| | | JSONObject result2 = (JSONObject) JSON.toJSON(jsonObject.get("result"));*/ |
| | | |
| | | 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("coalHomeIndexInfo"); |
| | | 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("RunCokingTraceModelBMTask运行异常"); |
| | | ex.printStackTrace(); |
| | | } |
| | | 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 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); |
| | | } |
| | | } |
| | | |
| | | @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","备煤工序异常溯源"); |
| | | 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); |
| | | } |
| | | } |
| | | |
| | | 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); |
| | | }); |
| | | } |
| | | } |