zhangherong
2025-05-09 f9a9fb9090c2e9e1a4e00374d48181efe2463a56
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
package org.jeecg.modules.mdc.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.modules.mdc.constant.MdcConstant;
import org.jeecg.modules.mdc.entity.Equipment;
import org.jeecg.modules.mdc.entity.MdcEquipment;
import org.jeecg.modules.mdc.entity.MdcEquipmentStatisticalInfo;
import org.jeecg.modules.mdc.entity.MdcOeeInfo;
import org.jeecg.modules.mdc.service.*;
import org.jeecg.modules.mdc.util.DateUtils;
import org.jeecg.modules.mdc.vo.MdcBoardRateVo;
import org.jeecg.modules.mdc.vo.MdcEquipmentStatusVo;
import org.jeecg.modules.mdcJc.service.IMdcJcRcJobreportService;
import org.jeecg.modules.system.entity.MdcProduction;
import org.jeecg.modules.system.service.IMdcProductionService;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @Author: Lius
 * @CreateTime: 2025-05-06
 * @Description: 看板接口
 */
@Service
public class MdcBoardServiceImpl implements IMdcBoardService {
 
    @Resource
    private IMdcProductionService mdcProductionService;
 
    @Resource
    private IEquipmentService equipmentService;
 
    @Resource
    private IMdcEquipmentService mdcEquipmentService;
 
    @Resource
    private IMdcEquipmentStatisticalInfoService mdcEquipmentStatisticalInfoService;
 
    @Resource
    private IMdcOeeInfoService mdcOeeInfoService;
 
    @Resource
    private IMdcJcRcJobreportService mdcJcRcJobreportService;
 
    /**
     * 设备状态
     */
    @Override
    public Map<String, Object> equipmentStatusStatistics(String productionId) {
        Map<String, Object> result = new HashMap<>();
        if (StringUtils.isBlank(productionId)) {
            MdcProduction mdcProduction = mdcProductionService.getOne(new LambdaQueryWrapper<MdcProduction>().eq(MdcProduction::getParentId, ""));
            productionId = mdcProduction.getId();
        }
        List<String> proIds = mdcProductionService.findChildByProId(productionId);
        if (proIds == null || proIds.isEmpty()) {
            return result;
        }
        List<Equipment> equipmentList = equipmentService.listByProds(proIds);
        if (equipmentList == null || equipmentList.isEmpty()) {
            return result;
        }
 
        MdcEquipmentStatusVo mdcEquipmentStatusVo = new MdcEquipmentStatusVo();
        for (Equipment equipment : equipmentList) {
            if (equipment.getOporation() != null) {
                switch (equipment.getOporation()) {
                    case 1:
                    case 2:
                        mdcEquipmentStatusVo.setWaitCount(mdcEquipmentStatusVo.getWaitCount() + 1);
                        break;
                    case 3:
                        mdcEquipmentStatusVo.setRunCount(mdcEquipmentStatusVo.getRunCount() + 1);
                        break;
                    case 22:
                        mdcEquipmentStatusVo.setAlarmCount(mdcEquipmentStatusVo.getAlarmCount() + 1);
                        break;
                    default:
                        mdcEquipmentStatusVo.setCloseCount(mdcEquipmentStatusVo.getCloseCount() + 1);
                        break;
                }
            } else {
                mdcEquipmentStatusVo.setCloseCount(mdcEquipmentStatusVo.getCloseCount() + 1);
            }
        }
        result.put("equipmentStatus", mdcEquipmentStatusVo);
 
        return result;
    }
 
    /**
     * 获取产线列表
     */
    @Override
    public List<MdcProduction> proList(String productionId) {
        if (StringUtils.isBlank(productionId)) {
            MdcProduction mdcProduction = mdcProductionService.getOne(new LambdaQueryWrapper<MdcProduction>().eq(MdcProduction::getParentId, ""));
            productionId = mdcProduction.getId();
        }
        return mdcProductionService.list(new LambdaQueryWrapper<MdcProduction>().eq(MdcProduction::getParentId, productionId).eq(MdcProduction::getMdcFlag, "1").eq(MdcProduction::getDelFlag, CommonConstant.DEL_FLAG_0).orderByAsc(MdcProduction::getProductionOrder));
    }
 
    /**
     * 率分析走势
     */
    @Override
    public Map<String, Object> rateAnalysisTrend(String productionId) {
        Map<String, Object> result = new HashMap<>();
        if (StringUtils.isBlank(productionId)) {
            MdcProduction mdcProduction = mdcProductionService.getOne(new LambdaQueryWrapper<MdcProduction>().eq(MdcProduction::getParentId, ""));
            productionId = mdcProduction.getId();
        }
        List<String> proIds = mdcProductionService.findChildByProId(productionId);
        if (proIds == null || proIds.isEmpty()) {
            return result;
        }
        List<String> equipmentIdList = mdcEquipmentService.getEquIdsByProIds(proIds);
        if (equipmentIdList == null || equipmentIdList.isEmpty()) {
            return result;
        }
        LocalDate now = LocalDate.now();
        Date startDate = DateUtils.toDate(now.plusDays(-15).toString(), DateUtils.STR_DATE);
        Date endDate = DateUtils.toDate(now.plusDays(-1).toString(), DateUtils.STR_DATE);
        String start = DateUtils.format(startDate, DateUtils.STR_DATE);
        String end = DateUtils.format(endDate, DateUtils.STR_DATE);
        List<String> dateList = DateUtils.getDatesStringList(startDate, endDate);
        List<String> dates = dateList.stream().map(date -> date.substring(5, 10)).collect(Collectors.toList());
        result.put("dateList", dates);
        Map<String, MdcBoardRateVo> statisticsMap = new LinkedHashMap<>();
        dateList.forEach(date -> {
            statisticsMap.put(date, new MdcBoardRateVo(date.substring(5, 10)));
        });
 
        // TEEP 开机率 开动率
        List<MdcEquipmentStatisticalInfo> mdcEquipmentStatisticalInfo = mdcEquipmentStatisticalInfoService.findByEquipmentAndDate(equipmentIdList, start.replaceAll("-", ""), end.replaceAll("-", ""));
        if (mdcEquipmentStatisticalInfo != null && !mdcEquipmentStatisticalInfo.isEmpty()) {
            mdcEquipmentStatisticalInfo.forEach(equipmentStatisticalInfo -> {
                String date = DateUtils.format(DateUtils.toDate(equipmentStatisticalInfo.getTheDate(), DateUtils.STRDATE), DateUtils.STR_DATE);
                if (statisticsMap.containsKey(date)) {
                    MdcBoardRateVo mdcBoardRateVo = statisticsMap.get(date);
                    if (equipmentStatisticalInfo.getProcessLong().compareTo(BigDecimal.ZERO) > 0) {
                        mdcBoardRateVo.setUtilizationRate(equipmentStatisticalInfo.getProcessLong().divide(new BigDecimal("864"), 2, RoundingMode.HALF_UP));
                    }
                    if (equipmentStatisticalInfo.getOpenLong().compareTo(BigDecimal.ZERO) > 0) {
                        mdcBoardRateVo.setOpenRate(equipmentStatisticalInfo.getOpenLong().divide(new BigDecimal("864"), 2, RoundingMode.HALF_UP));
                    }
                    if (equipmentStatisticalInfo.getOpenLong().compareTo(BigDecimal.ZERO) > 0) {
                        mdcBoardRateVo.setStartRate(equipmentStatisticalInfo.getProcessLong().multiply(new BigDecimal("100").divide(equipmentStatisticalInfo.getOpenLong(), 2, RoundingMode.HALF_UP)));
                    }
                    statisticsMap.put(date, mdcBoardRateVo);
                }
            });
        }
        // OEE
        List<MdcOeeInfo> oeeInfo = mdcOeeInfoService.findByEquIdsAndDate(equipmentIdList, start, end);
        if (oeeInfo != null && !oeeInfo.isEmpty()) {
            oeeInfo.forEach(mdcOeeInfo -> {
                if (statisticsMap.containsKey(mdcOeeInfo.getTheDate())) {
                    MdcBoardRateVo mdcBoardRateVo = statisticsMap.get(mdcOeeInfo.getTheDate());
                    if (mdcOeeInfo.getOee().compareTo(BigDecimal.ZERO) > 0) {
                        mdcBoardRateVo.setOee(mdcOeeInfo.getOee().setScale(2, RoundingMode.HALF_UP));
                    }
                    statisticsMap.put(mdcOeeInfo.getTheDate(), mdcBoardRateVo);
                }
            });
        }
        List<MdcBoardRateVo> dataList = new ArrayList<>(statisticsMap.values());
        result.put("dataList", dataList);
        return result;
    }
 
    /**
     * 设备列表
     */
    @Override
    public List<MdcEquipment> equipmentList(String productionId) {
        List<String> proIds = mdcProductionService.findChildByProId(productionId);
        if (proIds == null || proIds.isEmpty()) {
            return null;
        }
        return mdcEquipmentService.findByProIds(proIds);
    }
 
    /**
     * 设备日利用率分析
     */
    @Override
    public Map<String, Object> rateAnalysisTrendDay(String equipmentId) {
        Map<String, Object> result = new HashMap<>();
        LocalDate now = LocalDate.now();
        Date startDate = DateUtils.toDate(now.plusDays(-7).toString(), DateUtils.STR_DATE);
        Date endDate = DateUtils.toDate(now.plusDays(-1).toString(), DateUtils.STR_DATE);
        String start = DateUtils.format(startDate, DateUtils.STR_DATE);
        String end = DateUtils.format(endDate, DateUtils.STR_DATE);
        List<String> dayBetween = DateUtils.getDatesStringList(startDate, endDate);
        List<String> dateList = dayBetween.stream().map(date -> date.substring(5, 10)).collect(Collectors.toList());
        result.put("dateList", dateList);
        Map<String, MdcBoardRateVo> statisticsMap = new LinkedHashMap<>();
        dayBetween.forEach(date -> {
            statisticsMap.put(date, new MdcBoardRateVo(date.substring(5, 10)));
        });
        // TEEP
        List<MdcEquipmentStatisticalInfo> mdcEquipmentStatisticalInfo = mdcEquipmentStatisticalInfoService.findByEquIdAndDate(equipmentId, start.replaceAll("-", ""), end.replaceAll("-", ""));
        if (mdcEquipmentStatisticalInfo != null && !mdcEquipmentStatisticalInfo.isEmpty()) {
            mdcEquipmentStatisticalInfo.forEach(equipmentStatisticalInfo -> {
                String date = DateUtils.format(DateUtils.toDate(equipmentStatisticalInfo.getTheDate(), DateUtils.STRDATE), DateUtils.STR_DATE);
                if (statisticsMap.containsKey(date)) {
                    MdcBoardRateVo mdcBoardRateVo = statisticsMap.get(date);
                    if (equipmentStatisticalInfo.getProcessLong().compareTo(BigDecimal.ZERO) > 0) {
                        mdcBoardRateVo.setUtilizationRate(equipmentStatisticalInfo.getProcessLong().divide(new BigDecimal("864"), 2, RoundingMode.HALF_UP));
                    }
                    statisticsMap.put(date, mdcBoardRateVo);
                }
            });
        }
        // OEE
        List<MdcOeeInfo> oeeInfo = mdcOeeInfoService.findByEquIdAndDate(equipmentId, start, end);
        if (oeeInfo != null && !oeeInfo.isEmpty()) {
            oeeInfo.forEach(mdcOeeInfo -> {
                if (statisticsMap.containsKey(mdcOeeInfo.getTheDate())) {
                    MdcBoardRateVo mdcBoardRateVo = statisticsMap.get(mdcOeeInfo.getTheDate());
                    if (mdcOeeInfo.getOee().compareTo(BigDecimal.ZERO) > 0) {
                        mdcBoardRateVo.setOee(mdcOeeInfo.getOee().setScale(2, RoundingMode.HALF_UP));
                    }
                    statisticsMap.put(mdcOeeInfo.getTheDate(), mdcBoardRateVo);
                }
            });
        }
 
        List<MdcBoardRateVo> dataList = new ArrayList<>(statisticsMap.values());
        result.put("dataList", dataList);
        return result;
    }
 
    /**
     * 设备月率分析
     */
    @Override
    public Map<String, Object> rateAnalysisTrendMonth(String equipmentId) {
        Map<String, Object> result = new HashMap<>();
        LocalDate now = LocalDate.now();
        Date start = DateUtils.toDate(now.plusMonths(-6).toString(), DateUtils.STR_DATE);
        Date end = DateUtils.toDate(now.plusMonths(-1).toString(), DateUtils.STR_DATE);
        List<String> monthBetween = DateUtils.getMonthBetween(start, end);
        List<String> dateList = new ArrayList<>();
        List<MdcBoardRateVo> dataList = new ArrayList<>();
        for (String month : monthBetween) {
            String name = month.substring(month.lastIndexOf("-") + 1).replaceFirst("^0*", "") + "月";
            dateList.add(name);
            MdcBoardRateVo mdcBoardRateVo = new MdcBoardRateVo();
            mdcBoardRateVo.setDate(name);
            // TEEP
            MdcEquipmentStatisticalInfo mdcEquipmentStatisticalInfo = mdcEquipmentStatisticalInfoService.findByEquIdAndMonth(equipmentId, month.replaceAll("-", ""));
            if (mdcEquipmentStatisticalInfo != null) {
                mdcBoardRateVo.setUtilizationRate(mdcEquipmentStatisticalInfo.getProcessLong().divide(new BigDecimal("864"), 2, RoundingMode.HALF_UP));
            }
            // OEE
            BigDecimal oee = mdcOeeInfoService.findByEquIdAndMonth(equipmentId, month);
            if (oee != null) {
                mdcBoardRateVo.setOee(oee.setScale(2, RoundingMode.HALF_UP));
            }
            // 合格率
            BigDecimal passRate = mdcJcRcJobreportService.findRateByMonth(equipmentId, month);
            if (passRate != null) {
                mdcBoardRateVo.setPassRate(passRate);
            }
            dataList.add(mdcBoardRateVo);
        }
        result.put("dateList", dateList);
        result.put("dataList", dataList);
        return result;
    }
 
}