zhangherong
2025-05-30 4c50c77f3a5cf63746068d4a703e65792701bc09
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package org.jeecg.modules.mdc.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.modules.mdc.dto.MdcEquipmentStatisticalDto;
import org.jeecg.modules.mdc.entity.*;
import org.jeecg.modules.mdc.mapper.MdcEquipmentStatisticalInfoMapper;
import org.jeecg.modules.mdc.service.*;
import org.jeecg.modules.mdc.util.DateUtils;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
 
/**
 * @Description: 设备单日运行数据表
 * @Author: lius
 * @Date: 2023-04-14
 * @Version: V1.0
 */
@Service
public class MdcEquipmentStatisticalInfoServiceImpl extends ServiceImpl<MdcEquipmentStatisticalInfoMapper, MdcEquipmentStatisticalInfo> implements IMdcEquipmentStatisticalInfoService {
 
    @Resource
    private IEquipmentService equipmentService;
 
    @Resource
    private IMdcEquipmentRunningSectionService mdcEquipmentRunningSectionService;
 
    @Resource
    private IMdcSystemParametersService mdcSystemParametersService;
 
    @Resource
    private ISysDictService sysDictService;
 
    @Resource
    private IEquipmentStatisticalInfoService equipmentStatisticalInfoService;
 
    /**
     * 计算设备单日运行数据
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void runningAllEquipmentStatisticalProcess(String dateTime) {
        if (StringUtils.isNotBlank(dateTime)) {
            try {
                Date initDate = DateUtils.toDate(dateTime, "yyyyMMdd");
                if (initDate != null) {
                    this.remove(new LambdaQueryWrapper<MdcEquipmentStatisticalInfo>().
                            eq(MdcEquipmentStatisticalInfo::getTheDate, dateTime));
                }
            } catch (Exception e) {
                log.error("参数格式不对", e);
            }
        }
        List<Equipment> equipmentList = equipmentService.list();
        List<MdcEquipmentStatisticalInfo> result = new ArrayList<>();
        for (Equipment equipment : equipmentList) {
            List<MdcEquipmentStatisticalInfo> equipmentStatisticalInfoList = equipmentStatisticalProcess(equipment, dateTime);
            if (equipmentStatisticalInfoList != null && !equipmentStatisticalInfoList.isEmpty()) {
                result.addAll(equipmentStatisticalInfoList);
            }
        }
        if (!result.isEmpty()) {
            this.saveBatch(result);
            List<DictModel> dictList = sysDictService.queryDictItemsByCode("data_synchronization_flag");
            if (dictList != null && !dictList.isEmpty() && "0".equals(dictList.get(0).getValue())) {
                List<EquipmentStatisticalInfo> list = this.dataHandle(result);
                equipmentStatisticalInfoService.saveBatch(list);
            }
        }
    }
 
    private List<EquipmentStatisticalInfo> dataHandle(List<MdcEquipmentStatisticalInfo> list) {
        List<EquipmentStatisticalInfo> result = new ArrayList<>();
        list.forEach(item -> {
            EquipmentStatisticalInfo equipmentStatisticalInfo = new EquipmentStatisticalInfo();
            equipmentStatisticalInfo.setEquipment(item.getEquipmentId());
            equipmentStatisticalInfo.setTheDate(item.getTheDate());
            equipmentStatisticalInfo.setClosedLong(item.getCloseLong());
            equipmentStatisticalInfo.setOpeningLong(item.getOpenLong());
            equipmentStatisticalInfo.setErroringLong(item.getErrorLong());
            equipmentStatisticalInfo.setProcessingLong(item.getProcessLong());
            equipmentStatisticalInfo.setWaitingLong(item.getWaitLong());
            result.add(equipmentStatisticalInfo);
        });
        return result;
    }
 
    private List<MdcEquipmentStatisticalInfo> equipmentStatisticalProcess(Equipment equipment, String dateTime) {
        Date initDate = null;
        //取最后的统计数据
        if (StringUtils.isBlank(dateTime)) {
            MdcEquipmentStatisticalInfo nearestDate = this.baseMapper.getMaxStaticsData(equipment.getEquipmentid());
            if (nearestDate != null) {
                initDate = DateUtils.toDate(nearestDate.getTheDate(), "yyyyMMdd");
                initDate = DateUtils.plusTime(initDate, 1);
            } else {
                //初次取值 取最早时间
                MdcEquipmentRunningSection equipmentRunningSection = mdcEquipmentRunningSectionService.getFirstData(equipment.getEquipmentid());
                if (equipmentRunningSection != null) {
                    initDate = equipmentRunningSection.getStartTime();
                }
            }
        } else {
            try {
                initDate = DateUtils.toDate(dateTime, "yyyyMMdd");
                initDate = DateUtils.plusTime(initDate, 0);
            } catch (Exception e) {
                log.error("参数格式不对", null);
                return null;
            }
        }
        if (initDate == null) {
            return null;
        }
        MdcSystemParameters mdcSystemParameters = mdcSystemParametersService.getOne(new LambdaQueryWrapper<MdcSystemParameters>().eq(MdcSystemParameters::getCode, "equip_log_statis_time"));
        if (mdcSystemParameters == null) {
            log.error("mdc_system_parameters 表中数据缺失");
            return null;
        } else {
            String planTime = mdcSystemParameters.getValue();
            Date startDate = DateUtils.setTimeForDay(initDate, planTime);
            //问题点 判定当前时间的小时分钟是否超过系统规定时间
            Date now = new Date();
            Date endDate;
            if (StringUtils.isBlank(dateTime)) {
                Date planDate = DateUtils.setTimeForDay(DateUtils.plusTime(now, 0), planTime);
                long second = DateUtils.differentSecond(now, planDate);
                if (second <= 0) {
                    endDate = planDate;
                } else {
                    endDate = DateUtils.setTimeForDay(DateUtils.plusTime(now, -1), planTime);
                }
                List<MdcEquipmentRunningSection> equipmentRunningSectionList = mdcEquipmentRunningSectionService.listForEquipmentStatisticalInfo(equipment.getEquipmentid(), startDate, endDate);
                return this.statisticsData(equipmentRunningSectionList, planTime, startDate, endDate, equipment);
            } else {
                //待验证
                Date end = DateUtils.plusTime(initDate, 1);
                endDate = DateUtils.setTimeForDay(end, planTime);
                List<MdcEquipmentRunningSection> equipmentRunningSectionList = mdcEquipmentRunningSectionService.listForEquipmentStatisticalInfo(equipment.getEquipmentid(), startDate, endDate);
                return this.statisticsData(equipmentRunningSectionList, planTime, startDate,
                        DateUtils.setTimeForDay(DateUtils.plusTime(initDate, 0), planTime), equipment);
            }
        }
    }
 
    /**
     * 时间切割
     */
    private List<MdcEquipmentStatisticalInfo> statisticsData(List<MdcEquipmentRunningSection> equipmentRunningSectionList, String planTime, Date startDate, Date endDate, Equipment equipment) {
        Map<String, List<Map<String, Long>>> map = new HashMap<>();
        for (MdcEquipmentRunningSection temp : equipmentRunningSectionList) {
            Date s1 = temp.getStartTime();
            //s2设置的节点时间
            Date s2 = DateUtils.setTimeForDay(s1, planTime);
            Date e1 = temp.getEndTime();
            //e2 设置的节点时间
            Date e2 = DateUtils.getNextDay(s2);
            if (s1.getTime() < s2.getTime()) {
                if (e1.getTime() < s2.getTime()) {
                    String previousDay = DateUtils.format(DateUtils.getPreviousDay(s1), "yyyyMMdd");
                    List list = map.get(previousDay);
                    if (list == null) {
                        list = new ArrayList();
                    }
                    Map<String, Long> previous = new HashMap<>();
                    previous.put("status", (long) temp.getStatus());
                    previous.put("duration", (e1.getTime() - s1.getTime()) / 1000);
                    list.add(previous);
                    map.put(previousDay, list);
                } else {
                    String previousDay = DateUtils.format(DateUtils.getPreviousDay(s1), "yyyyMMdd");
                    List list = map.get(previousDay);
                    if (list == null) {
                        list = new ArrayList();
                    }
                    Map<String, Long> previous = new HashMap<>();
                    previous.put("status", (long) temp.getStatus());
                    previous.put("duration", (s2.getTime() - s1.getTime()) / 1000);
                    list.add(previous);
                    map.put(previousDay, list);
                    String day = DateUtils.format(s1, "yyyyMMdd");
                    List list1 = map.get(day);
                    if (list1 == null) {
                        list1 = new ArrayList();
                    }
                    Map<String, Long> now = new HashMap<>();
                    now.put("status", (long) temp.getStatus());
                    now.put("duration", (e1.getTime() - s2.getTime()) / 1000);
                    list1.add(now);
                    map.put(day, list1);
                }
            } else {
                if (e2.getTime() > e1.getTime()) {
                    //今天
                    String nowDay = DateUtils.format(s1, "yyyyMMdd");
                    List list1 = map.get(nowDay);
                    if (list1 == null) {
                        list1 = new ArrayList();
                    }
                    Map<String, Long> now = new HashMap<>();
                    now.put("status", (long) temp.getStatus());
                    now.put("duration", (e1.getTime() - s1.getTime()) / 1000);
                    list1.add(now);
                    map.put(nowDay, list1);
                } else {
                    // 今天
                    String nowDay = DateUtils.format(s1, "yyyyMMdd");
                    List list1 = map.get(nowDay);
                    if (list1 == null) {
                        list1 = new ArrayList();
                    }
                    Map<String, Long> now = new HashMap<>();
                    now.put("status", (long) temp.getStatus());
                    now.put("duration", (e2.getTime() - s1.getTime()) / 1000);
                    list1.add(now);
                    map.put(nowDay, list1);
                    //明天
                    String nextDay = DateUtils.format(DateUtils.getNextDay(s1), "yyyyMMdd");
                    List list2 = map.get(nextDay);
                    if (list2 == null) {
                        list2 = new ArrayList();
                    }
                    Map<String, Long> next = new HashMap<>();
                    next.put("status", (long) temp.getStatus());
                    next.put("duration", (e1.getTime() - e2.getTime()) / 1000);
                    list2.add(next);
                    map.put(nextDay, list2);
                }
            }
        }
        int maxday = 0;
        Date now = new Date();
        int dateInt1 = Integer.parseInt(DateUtils.format(now, "yyyyMMdd"));
        int lastDay;
        Date planDate = DateUtils.setTimeForDay(DateUtils.plusTime(now, 0), planTime);
        Long second = DateUtils.differentSecond(planDate, now);
        if (second < 0) {
            lastDay = Integer.parseInt(DateUtils.format(DateUtils.plusTime(now, -1), "yyyyMMdd"));
        } else {
            lastDay = dateInt1;
        }
        Set<String> keys = map.keySet();
        for (String key : keys) {
            int tmp = Integer.parseInt(key);
            if (tmp == dateInt1 || tmp == lastDay) {
                continue;
            }
            if (tmp > maxday) {
                maxday = tmp;
            }
        }
        boolean flag = true;
        Date date = startDate;
        List<MdcEquipmentStatisticalInfo> equipmentStatisticalInfos = new ArrayList<>();
        while (flag) {
            int dateInt = Integer.parseInt(DateUtils.format(date, "yyyyMMdd"));
            if (date.getTime() <= endDate.getTime() && dateInt <= maxday) {
                List<Map<String, Long>> dayList = map.get(String.valueOf(dateInt));
                MdcEquipmentStatisticalInfo equipmentStatisticalInfo = new MdcEquipmentStatisticalInfo();
                equipmentStatisticalInfo.setEquipmentId(equipment.getEquipmentid());
                equipmentStatisticalInfo.setTheDate(String.valueOf(dateInt));
                long waitingLong = 0L;
                long processingLong = 0L;
                long closedLong = 0L;
                long errorLong = 0L;
                for (Map<String, Long> n : dayList) {
                    long du = n.get("duration");
                    int status = new Long(n.get("status")).intValue();
                    if (status == 2) {
                        waitingLong += du;
                    }
                    if (status == 3) {
                        processingLong += du;
                    }
                    if (status == 0) {
                        closedLong += du;
                    }
                    if (status == 22) {
                        errorLong += du;
                    }
                }
 
                //如待机时间+加工时间+关机时间不等于86400,需将多余时间进行补充
                long remainingDate = 86400 - waitingLong - processingLong - closedLong;
                if (remainingDate != 0L) {
                    //如果关机时间大于待机时间+加工时间,将多余时间加至关机时间,否则,加入待机时间
                    if (waitingLong + processingLong < closedLong) {
                        closedLong += remainingDate;
                    }else {
                        waitingLong += remainingDate;
                    }
                }
 
                equipmentStatisticalInfo.setWaitLong(new BigDecimal(waitingLong));
                equipmentStatisticalInfo.setProcessLong(new BigDecimal(processingLong));
                equipmentStatisticalInfo.setCloseLong(new BigDecimal(closedLong));
                equipmentStatisticalInfo.setOpenLong(equipmentStatisticalInfo.getWaitLong().add(equipmentStatisticalInfo.getProcessLong()));
                equipmentStatisticalInfo.setErrorLong(new BigDecimal(errorLong));
                equipmentStatisticalInfo.setCreateTime(new Date());
                equipmentStatisticalInfos.add(equipmentStatisticalInfo);
            } else {
                flag = false;
            }
            date = DateUtils.getNextDay(date);
        }
        return equipmentStatisticalInfos;
    }
 
    /**
     * 查询设备运行时间
     *
     * @param equipmentId
     * @param validDate
     * @return
     */
    @Override
    public Integer selectProcessLong(String equipmentId, String validDate) {
        Integer processLong = this.baseMapper.selectProcessLong(equipmentId, validDate);
        if (processLong == null) {
            return 0;
        } else {
            return Integer.parseInt(new BigDecimal(processLong).divide(new BigDecimal("60"), 0, RoundingMode.HALF_UP).toString());
        }
    }
 
    @Override
    public MdcEquipmentStatisticalDto findByEquipmentAndMonth(String equipmentId, String date) {
        return this.baseMapper.findByEquipmentAndMonth(equipmentId, date);
    }
 
    @Override
    public List<MdcEquipmentStatisticalInfo> findByEquipmentAndDate(List<String> equipmentList, String start, String end) {
        return this.baseMapper.findByEquipmentAndDate(equipmentList, start, end);
    }
 
    @Override
    public List<MdcEquipmentStatisticalInfo> findByEquIdAndDate(String equipmentId, String start, String end) {
        return this.baseMapper.findByEquIdAndDate(equipmentId, start, end);
    }
 
    @Override
    public MdcEquipmentStatisticalInfo findByEquIdAndMonth(String equipmentId, String month) {
        return this.baseMapper.findByEquIdAndMonth(equipmentId, month);
    }
}