鞍钢鲅鱼圈能源管控系统后端代码
dongyukun
2 天以前 843ad1df68939a637c02c79965d74c4b723e42b7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package com.iailab.module.ansteel.job.task;
 
import cn.hutool.core.bean.BeanUtil;
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.enums.ProcessConfDataTypeEnum;
import com.iailab.module.ansteel.common.enums.ProcessTypeEnum;
import com.iailab.module.data.api.ind.IndItemApi;
import com.iailab.module.data.api.ind.dto.ApiIndItemQueryDTO;
import com.iailab.module.data.api.ind.dto.ApiIndItemValueDTO;
import com.iailab.module.data.api.point.DataPointApi;
import com.iailab.module.data.api.point.dto.ApiPointValueDTO;
import com.iailab.module.data.api.point.dto.ApiPointValueQueryDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
 
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 备煤工序-概况
 *
 * @author PanZhibao
 * @Description
 * @createTime 2025年04月21日
 */
@Slf4j
@Component("runCokingOverviewTask")
public class RunCokingOverviewTask implements ITask{
    private Logger logger = LoggerFactory.getLogger(getClass());
 
    @Resource
    private CokingProcessConfService cokingProcessConfService;
    @Resource
    private CokingOverviewService cokingOverviewService;
    @Resource
    private DataPointApi dataPointApi;
    @Resource
    private IndItemApi indItemApi;
    @Resource
    private CokingTraceReportService cokingTraceReportService;
    @Resource
    private CokingTraceChartService cokingTraceChartService;
    @Resource
    private CokingTraceSuggestService cokingTraceSuggestService;
 
    @Override
    public void run(String processTypes) {
        logger.info("runCokingOverviewTask,参数为:{}", processTypes);
        try {
 
            String[] split = processTypes.split(",");
 
            for (String processType : split) {
                CokingProcessConfEntity queryParams = new CokingProcessConfEntity();
                queryParams.setIndType(processType);
                List<CokingProcessConfEntity> list = cokingProcessConfService.list(queryParams);
                if (CollectionUtils.isEmpty(list)) {
                    logger.info("ConfLis is Empty");
                    return;
                }
 
                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.add(Calendar.DAY_OF_YEAR, -1);
                String clock = DateUtils.format(calendar.getTime(), DateUtils.FORMAT_YEAR_MONTH_DAY);
                // 溯源报告
                CokingTraceReportEntity reportEntity = new CokingTraceReportEntity();
                String relId = UUID.randomUUID().toString();
                reportEntity.setId(relId);
                reportEntity.setProcess(ProcessTypeEnum.getEumByCode(processType).getProcess());
                reportEntity.setReportName(ProcessTypeEnum.getEumByCode(processType).getReportName());
                reportEntity.setAnalyDate(clock);
                reportEntity.setClock(clock);
                reportEntity.setCreateDate(new Date());
                cokingTraceReportService.save(reportEntity);
 
                // 整体情况
                // 筛选info_type = 2,按照子工序类型分组
                Map<String, List<CokingProcessConfEntity>> processTypeMap = list.stream().filter(e -> e.getInfoType().equals("2")).collect(Collectors.groupingBy(CokingProcessConfEntity::getExt2));
                // 结果 <子工序类型,<数据key,数据value>>
                List<CokingOverviewEntity> result = new ArrayList<>(processTypeMap.size());
 
                for (Map.Entry<String, List<CokingProcessConfEntity>> entry : processTypeMap.entrySet()) {
                    Map<String,Double> values = new HashMap<>(entry.getValue().size());
                    for (CokingProcessConfEntity conf : entry.getValue()) {
                        if (StringUtils.isBlank(conf.getDataType())) {
                            logger.info("DataType is Empty");
                            continue;
                        }
                        if (StringUtils.isBlank(conf.getPointNo())) {
                            logger.info("PointNo is Empty");
                            continue;
                        }
                        Double value = 0.0;
                        switch (ProcessConfDataTypeEnum.getEumByCode(conf.getDataType())) {
                            case DATAPOINT:
                                List<String> points = new ArrayList<>();
                                points.add(conf.getPointNo());
                                Map<String, Object> pointsRealValue = dataPointApi.queryPointsRealValue(points);
                                value = Double.valueOf(pointsRealValue.get(conf.getPointNo()).toString());
                                break;
                            case IND:
                                List<ApiIndItemValueDTO> indValues = indItemApi.queryIndItemDefaultValue(conf.getPointNo());
                                if (!CollectionUtils.isEmpty(indValues)) {
                                    value = Double.valueOf(indValues.get(indValues.size() - 1).getDataValue().toString());
                                }
                                break;
                            case MODEL:
                                break;
                            default:
                                break;
 
                        }
                        values.put(conf.getExt1(),value);
                    }
                    CokingOverviewEntity overviewEntity = new CokingOverviewEntity();
                    BeanUtil.fillBeanWithMap(values,overviewEntity,true);
                    overviewEntity.setId(UUID.randomUUID().toString());
                    overviewEntity.setRelId(relId);
                    overviewEntity.setProcessType(processType);
                    overviewEntity.setSubProcessType(entry.getKey());
                    overviewEntity.setClock(clock);
                    result.add(overviewEntity);
                }
                cokingOverviewService.insert(result);
 
                // 指标运行趋势
                List<CokingProcessConfEntity> indRunTrend = list.stream().filter(e -> e.getInfoType().equals("1")).collect(Collectors.toList());
                List<CokingTraceChartEntity> cokingTraceChartEntityList = new ArrayList<>(indRunTrend.size());
                List<CokingTraceSuggestEntity> suggestEntitieList = new ArrayList<>();
                indRunTrend.forEach(e -> {
                    CokingTraceChartEntity cokingTraceChartEntity = new CokingTraceChartEntity();
                    cokingTraceChartEntity.setRelId(relId);
                    cokingTraceChartEntity.setName(e.getIndName());
                    cokingTraceChartEntity.setClock(clock);
                    cokingTraceChartEntity.setDataNo(e.getPointNo());
                    cokingTraceChartEntity.setDataType(e.getDataType());
 
                    Calendar clone = (Calendar) calendar.clone();
                    cokingTraceChartEntity.setEndTime(clone.getTime());
                    if (e.getExt2().equals("month")) {
                        clone.add(Calendar.MONTH,-1 * Integer.parseInt(e.getExt1()));
                    } else if (e.getExt2().equals("day")) {
                        clone.add(Calendar.DAY_OF_YEAR,-1 * Integer.parseInt(e.getExt1()));
                    }
                    cokingTraceChartEntity.setStartTime(clone.getTime());
                    cokingTraceChartEntity.setCreateDate(new Date());
                    cokingTraceChartEntityList.add(cokingTraceChartEntity);
 
                    // 异常数据处理
                    suggestEntitieList.addAll(handleAbnormalData(e, cokingTraceChartEntity.getStartTime(), cokingTraceChartEntity.getEndTime(),relId,clock));
                });
                cokingTraceChartService.insert(cokingTraceChartEntityList);
                cokingTraceSuggestService.insert(suggestEntitieList);
 
            }
        } catch (Exception ex) {
            logger.error("runCokingOverviewTask运行异常",ex);
        }
        logger.info("runCokingOverviewTask运行完成");
 
    }
 
    private List<CokingTraceSuggestEntity> handleAbnormalData(CokingProcessConfEntity e, Date startTime, Date endTime, String relId, String clock) {
        List<CokingTraceSuggestEntity> suggestEntities = new ArrayList<>();
        switch (ProcessConfDataTypeEnum.getEumByCode(e.getDataType())) {
            case DATAPOINT:
                ApiPointValueQueryDTO queryDTO = new ApiPointValueQueryDTO();
                queryDTO.setPointNo(e.getPointNo());
                queryDTO.setStart(startTime);
                queryDTO.setEnd(endTime);
                List<ApiPointValueDTO> pointValues = dataPointApi.queryPointHistoryValue(queryDTO);
                Map<Date, Double> pointValueMap = pointValues.stream().collect(Collectors.toMap(ApiPointValueDTO::getT, ApiPointValueDTO::getV, (e1, e2) -> e1));
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(startTime);
                while (!calendar.getTime().after(endTime)) {
                    String content = null;
                    if (pointValueMap.containsKey(calendar.getTime())) {
                        Double value = pointValueMap.get(calendar.getTime());
                        if (value.equals(0.0)) {
                            content = DateUtils.format(calendar.getTime()) + " " + e.getIndName() + "数据异常(数据为0)";
                        }else if (value.compareTo(Double.valueOf(e.getExt3())) > 0) {
                            content = DateUtils.format(calendar.getTime()) + " " + e.getIndName() + "数据异常(超上限)";
                        }else if (value.compareTo(Double.valueOf(e.getExt4())) < 0) {
                            content = DateUtils.format(calendar.getTime()) + " " + e.getIndName() + "数据异常(超下限)";
                        }
                    }else {
                        content = DateUtils.format(calendar.getTime()) + " " + e.getIndName() + "数据异常(无数据)";
                    }
                    if (StringUtils.isNotBlank(content)) {
                        suggestEntities.add(new CokingTraceSuggestEntity(UUID.randomUUID().toString(),relId,ProcessTypeEnum.getEumByCode(e.getIndType()).getReportName(),e.getIndType()+"_AD",clock,content,new Date()));
                    }
                    calendar.add(Calendar.DAY_OF_YEAR,1);
                }
                break;
            case IND:
                ApiIndItemQueryDTO query = new ApiIndItemQueryDTO();
                query.setItemNo(e.getPointNo());
                query.setStart(startTime);
                query.setEnd(endTime);
                List<ApiIndItemValueDTO> indValues = indItemApi.queryIndItemHistoryValue(query);
                Map<String, Double> indValueMap = indValues.stream().collect(Collectors.toMap(ApiIndItemValueDTO::getDataTime, ind -> Double.valueOf(ind.getDataValue().toString()), (e1, e2) -> e1));
                Calendar intCalendar = Calendar.getInstance();
                intCalendar.setTime(startTime);
                while (!intCalendar.getTime().after(endTime)) {
                    String content = null;
                    String time = DateUtils.format(intCalendar.getTime());
                    if (indValueMap.containsKey(time)) {
                        Double value = indValueMap.get(time);
                        if (value.equals(0.0)) {
                            content = time + " " + e.getIndName() + "数据异常(数据为0)";
                        }else if (value.compareTo(Double.valueOf(e.getExt3())) > 0) {
                            content = time + " " + e.getIndName() + "数据异常(超上限)";
                        }else if (value.compareTo(Double.valueOf(e.getExt4())) < 0) {
                            content = time + " " + e.getIndName() + "数据异常(超下限)";
                        }
                    }else {
                        content = time + " " + e.getIndName() + "数据异常(无数据)";
                    }
                    if (StringUtils.isNotBlank(content)) {
                        suggestEntities.add(new CokingTraceSuggestEntity(UUID.randomUUID().toString(),relId,ProcessTypeEnum.getEumByCode(e.getIndType()).getReportName(),e.getIndType()+"_AD",clock,content,new Date()));
                    }
                    intCalendar.add(Calendar.DAY_OF_YEAR,1);
                }
                break;
            default:
                break;
 
        }
        return suggestEntities;
    }
}