yangbin
2025-02-20 ec077979f2d55b2a54e893085f4b845b729e69c3
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
package org.jeecg.modules.mdc.subcontrol.service.impl;
 
import org.apache.commons.lang.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.jeecg.modules.mdc.entity.EquipmentLog;
import org.jeecg.modules.mdc.service.*;
import org.jeecg.modules.mdc.subcontrol.service.MdcBigScreenService;
import org.jeecg.modules.mdc.subcontrol.vo.LastWeekDataRankingVo;
import org.jeecg.modules.mdc.subcontrol.vo.LastWeekDataVo;
import org.jeecg.modules.mdc.subcontrol.vo.MdcEquipmentOpVo;
import org.jeecg.modules.mdc.util.DateUtils;
import org.jeecg.modules.mdc.vo.MdcEquipmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @author: LiuS
 * @create: 2023-01-05 11:42
 */
@Service
public class MdcBigScreenServiceImpl implements MdcBigScreenService {
 
    @Resource
    private IMdcEquipmentService mdcEquipmentService;
 
    @Resource
    private IEquipmentStatisticalInfoService equipmentStatisticalInfoService;
    @Autowired
    private IMdcEquipmentStatisticalShiftInfoService mdcEquipmentStatisticalShiftInfoService;
    @Autowired
    private IMdcEquipmentStatisticalInfoService mdcEquipmentStatisticalInfoService;
 
 
    @Resource
    private IEquipmentLogService equipmentLogService;
 
 
    /**
     * 根据厂区id获取上周数据
     */
    @Override
    public Map<String, Object> findDataByLastWeek() {
        Map<String, Object> result = new HashMap<>();
        List<BigDecimal> openingRateList = new ArrayList<>();
        List<BigDecimal> processingRateList = new ArrayList<>();
        List<BigDecimal> shiftRateList = new ArrayList<>();
        //获取上周周一日期和周六日期
        LocalDate today = LocalDate.now().minusDays(7L);
        LocalDate monday = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        LocalDate saturday = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SATURDAY));
        List<String> dates = getMiddleDate(monday, saturday);
        List<String> equipmentIdList = new ArrayList<>();
        //根据厂区id获取厂区设备信息
        equipmentIdList = mdcEquipmentService.listEquipmentId(null);
        //上周利用率和开机率数据
        List<LastWeekDataVo> lastWeekDataVos = equipmentStatisticalInfoService.findDataForBigScreen(equipmentIdList, monday.format(DateTimeFormatter.BASIC_ISO_DATE), saturday.format(DateTimeFormatter.BASIC_ISO_DATE));
        if (lastWeekDataVos.isEmpty()) {
            for (int i = 0; i < dates.size(); i++) {
                openingRateList.add(new BigDecimal("0"));
                processingRateList.add(new BigDecimal("0"));
            }
        } else {
            for (int i = 0; i < dates.size(); i++) {
                Integer size = equipmentStatisticalInfoService.findEquipmentId(equipmentIdList, dates.get(i));
                if (dates.get(i).equals(lastWeekDataVos.get(i).getTheDate())) {
                    openingRateList.add(new BigDecimal(String.format("%.2f", (Float.parseFloat(lastWeekDataVos.get(i).getOpeningLong()) / (86400 * size) * 100))));
                    processingRateList.add(new BigDecimal(String.format("%.2f", (Float.parseFloat(lastWeekDataVos.get(i).getProcessingLong()) / (86400 * size) * 100))));
                } else {
                    openingRateList.add(new BigDecimal("0"));
                    processingRateList.add(new BigDecimal("0"));
                }
            }
        }
 
        //上周班次利用率数据
        List<LastWeekDataVo> lastWeekDataList = mdcEquipmentStatisticalShiftInfoService.
                findDataForBigScreen(equipmentIdList, monday.toString(), saturday.toString());
        List<String> middleDates = getMiddleDates(monday, saturday);
 
        if (lastWeekDataList.isEmpty()) {
            for (int i = 0; i < middleDates.size(); i++) {
                shiftRateList.add(new BigDecimal("0"));
            }
        } else {
            Map<String,LastWeekDataVo> map = new HashMap<>();
            for (LastWeekDataVo s : lastWeekDataList) {
                map.put(s.getTheDate(),s);
            }
            for (int i = 0; i < middleDates.size(); i++) {
                if (i < lastWeekDataList.size()) {
                    if ( map.containsKey(middleDates.get(i))) {
                        LastWeekDataVo vo =  map.get(middleDates.get(i));
                        if (!"0".equals(vo.getTotalLong())) {
                            shiftRateList.add(new BigDecimal(vo.getProcessingLong()).
                                    divide(new BigDecimal(vo.getTotalLong()),
                                            4, BigDecimal.ROUND_HALF_UP)
                                    .setScale(4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)));
                        } else {
                            shiftRateList.add(new BigDecimal("0"));
                        }
                    }
 
                }
                 else {
                    shiftRateList.add(new BigDecimal("0"));
                }
            }
        }
        result.put("openingRateList", openingRateList);
        result.put("processingRateList", processingRateList);
        result.put("shiftRateList", shiftRateList);
 
        return result;
    }
 
    /**
     * 根据厂区id获取上周运行效率排名
     */
    @Override
    public List<LastWeekDataRankingVo> findDataRankingByLastWeek() {
        List<LastWeekDataRankingVo> voList = new ArrayList<>();
 
        //获取上周周一日期和周六日期
        LocalDate today = LocalDate.now();
        String monday = today.minusDays(7).format(DateTimeFormatter.BASIC_ISO_DATE);
        String saturday = today.minusDays(1).format(DateTimeFormatter.BASIC_ISO_DATE);
        //根据id获取设备id集合
        List<String> equipmentIdList = mdcEquipmentService.listEquipmentId(null);
        if (!equipmentIdList.isEmpty()) {
            List<LastWeekDataVo> lastWeekDataVos = equipmentStatisticalInfoService.findDataRankingForBigScreen(equipmentIdList, monday, saturday);
            if (!lastWeekDataVos.isEmpty()) {
                for (LastWeekDataVo lastWeekDataVo : lastWeekDataVos) {
                    Long size = mdcEquipmentStatisticalInfoService.selectSize(lastWeekDataVo.getEquipment(), monday, saturday);
                    if (StringUtils.isNotEmpty(lastWeekDataVo.getProcessingLong()) && Float.parseFloat(lastWeekDataVo.getProcessingLong()) >0) {
                        lastWeekDataVo.setUtilization(new BigDecimal(String.format("%.2f",
                                (Float.parseFloat(lastWeekDataVo.getProcessingLong()) / (86400 * size) * 100))));
                    } else {
                        lastWeekDataVo.setUtilization(new BigDecimal(0));
                    }
 
                }
                List<LastWeekDataVo> collect = lastWeekDataVos.stream().sorted(Comparator.comparing(LastWeekDataVo::getUtilization).reversed()).collect(Collectors.toList());
                if (collect.size() > 6) {
                    List<LastWeekDataVo> topList = collect.subList(0, 6);
                    for (LastWeekDataVo vo : topList) {
                        LastWeekDataRankingVo rankingVo = new LastWeekDataRankingVo();
                        rankingVo.setTopName(vo.getEquipment());
                        rankingVo.setTopRate(vo.getUtilization().toString());
                        voList.add(rankingVo);
                    }
                } else {
                    for (LastWeekDataVo vo : collect) {
                        LastWeekDataRankingVo rankingVo = new LastWeekDataRankingVo();
                        rankingVo.setTopName(vo.getEquipment());
                        rankingVo.setTopRate(vo.getUtilization().toString());
                        voList.add(rankingVo);
                    }
                }
 
            }
        }
 
        return voList;
 
    }
 
    /**
     * 获取设备当前状态
     */
    @Override
    public List<MdcEquipmentOpVo> findEquipmentStatus() {
        List<MdcEquipmentOpVo> result = new ArrayList<>();
        //根据id获取设备id集合
        List<String> equipmentIdList = mdcEquipmentService.listEquipmentId(null);
 
        if (!equipmentIdList.isEmpty()) {
            List<EquipmentLog> logs = equipmentLogService.getEquipmentStatusList(equipmentIdList);
            //根据设备id集合查询设备实时状态
            for (EquipmentLog equipmentId : logs) {
                if (equipmentId != null) {
                    MdcEquipmentOpVo vo = new MdcEquipmentOpVo();
                    vo.setEquipmentId(equipmentId.getEquipmentId());
                    if (equipmentId.getOporation() != null) {
                        switch (equipmentId.getOporation()) {
                            case 1:
                                vo.setOporation(1);
                                vo.setOporationDict("待机");
                                break;
                            case 2:
                                vo.setOporationDict("待机");
                                vo.setOporation(2);
                                break;
                            case 3:
                                vo.setOporationDict("运行");
                                vo.setOporation(3);
                                break;
                            case 22:
                                vo.setOporationDict("报警");
                                vo.setOporation(22);
                                break;
                            default:
                                vo.setOporationDict("关机");
                                vo.setOporation(0);
                                break;
                        }
                    } else {
                        vo.setOporation(0);
                        vo.setOporationDict("关机");
                    }
                    result.add(vo);
                } else {
                    MdcEquipmentOpVo vo = new MdcEquipmentOpVo();
                    vo.setOporation(0);
                    vo.setOporationDict("关机");
                    result.add(vo);
                }
            }
        }
        return result;
    }
 
    /**
     * 获取全厂当前设备状态信息
     */
    @Override
    public Map<String, Object> findAllEquipmentStatus() {
        int runCount = 0;
        int standbyCount = 0;
        int alarmCount = 0;
        int offCount = 0;
        List<String> equipmentIdList = mdcEquipmentService.listEquipmentId(null);
        List<EquipmentLog> equipmentLogs = new ArrayList<>();
        for (String equipmentId : equipmentIdList) {
            EquipmentLog equipmentLog = equipmentLogService.selectEquipmentOporation(equipmentId);
            if (equipmentLog != null) {
                equipmentLogs.add(equipmentLog);
            } else {
                offCount += 1;
            }
        }
        if (!equipmentLogs.isEmpty()) {
            for (EquipmentLog equipmentLog : equipmentLogs) {
                if (equipmentLog.getOporation() != null) {
                    switch (equipmentLog.getOporation()) {
                        case 1:
                        case 2:
                            standbyCount += 1;
                            break;
                        case 3:
                            runCount += 1;
                            break;
                        case 22:
                            alarmCount += 1;
                            break;
                        default:
                            offCount += 1;
                            break;
                    }
                } else {
                    offCount += 1;
                }
            }
        }
        Map<String, Object> result = new HashMap<>();
        result.put("runCount", runCount);
        result.put("standbyCount", standbyCount);
        result.put("alarmCount", alarmCount);
        result.put("offCount", offCount);
 
        return result;
    }
 
    /**
     * 20230101
     *
     * @param begin 开始日期
     * @param end   结束日期
     * @return 开始与结束之间的所有日期,包括起止
     */
    public static List<String> getMiddleDate(LocalDate begin, LocalDate end) {
        List<String> localDateList = new ArrayList<>();
        long length = end.toEpochDay() - begin.toEpochDay();
        for (long i = length; i >= 0; i--) {
            localDateList.add(end.minusDays(i).format(DateTimeFormatter.BASIC_ISO_DATE));
        }
        return localDateList;
    }
 
    /**
     * 2023-01-01
     *
     * @param begin 开始日期
     * @param end   结束日期
     * @return 开始与结束之间的所有日期,包括起止
     */
    public static List<String> getMiddleDates(LocalDate begin, LocalDate end) {
        List<String> localDateList = new ArrayList<>();
        long length = end.toEpochDay() - begin.toEpochDay();
        for (long i = length; i >= 0; i--) {
            String date = end.minusDays(i).toString().replace("-","");
            localDateList.add(date);
        }
        return localDateList;
    }
 
}