Lius
2025-06-10 96be94fc3c33c49e15b538bebbea455e839a7a7b
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
package org.jeecg.modules.board.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import liquibase.pro.packaged.I;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.modules.board.service.IDtBoardService;
import org.jeecg.modules.board.vo.*;
import org.jeecg.modules.mdc.constant.MdcConstant;
import org.jeecg.modules.mdc.entity.*;
import org.jeecg.modules.mdc.service.*;
import org.jeecg.modules.mdc.util.DateUtils;
import org.jeecg.modules.system.entity.MdcProduction;
import org.jeecg.modules.system.service.IMdcProductionService;
import org.jeecg.modules.system.service.ISysDictService;
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-30
 * @Description:
 */
@Service
public class DtBoardServiceImpl implements IDtBoardService {
 
    @Resource
    private IMdcProductionService mdcProductionService;
 
    @Resource
    private IMdcEquipmentService mdcEquipmentService;
 
    @Resource
    private IMdcEquipmentStatisticalInfoService mdcEquipmentStatisticalInfoService;
 
    @Resource
    private IMdcOeeInfoService mdcOeeInfoService;
 
    @Resource
    private IEquipmentService equipmentService;
 
    @Resource
    private IEquipmentWorkLineService equipmentWorkLineService;
 
    @Resource
    private IMdcDriveTypeParamConfigService mdcDriveTypeParamConfigService;
 
    @Resource
    private ISysDictService sysDictService;
 
    @Resource
    private IMdcDowntimeService mdcDowntimeService;
 
    @Resource
    private IEquipmentAlarmService equipmentAlarmService;
 
    @Resource
    private IMdcAlarmInfoService mdcAlarmInfoService;
 
    /**
     * 车间信息
     */
    @Override
    public List<MdcProduction> productionList() {
        return mdcProductionService.list(new LambdaQueryWrapper<MdcProduction>().eq(MdcProduction::getOrgType, CommonConstant.ORG_TYPE_2).eq(MdcProduction::getDelFlag, CommonConstant.DEL_FLAG_0).orderByAsc(MdcProduction::getProductionOrder));
    }
 
    /**
     * 设备月度利用率
     */
    @Override
    public List<EquUtilRateMonth> equipmentMonthUtilizationRate(String productionId) {
        // 组装返回数据
        LocalDate now = LocalDate.now();
        Date start = DateUtils.toDate(now.plusMonths(-12).toString(), DateUtils.STR_DATE);
        Date end = DateUtils.toDate(now.plusMonths(-1).toString(), DateUtils.STR_DATE);
        List<String> monthBetween = DateUtils.getMonthBetween(start, end);
        Map<String, EquUtilRateMonth> resultMap = monthBetween.stream().collect(Collectors.toMap(
                date -> date,
                date -> new EquUtilRateMonth(date.substring(date.lastIndexOf("-") + 1).replaceFirst("^0*", "") + "月"),
                (existing, replacement) -> existing, // 处理键冲突的合并函数(通常不会冲突)
                LinkedHashMap::new // 指定使用LinkedHashMap保持插入顺序
        ));
        List<String> proIds = mdcProductionService.findChildByProId(productionId);
        if (proIds == null || proIds.isEmpty()) {
            return new ArrayList<>(resultMap.values());
        }
        List<String> equipmentIdList = mdcEquipmentService.getEquIdsByProIds(proIds);
        if (equipmentIdList == null || equipmentIdList.isEmpty()) {
            return new ArrayList<>(resultMap.values());
        }
        for (String month : monthBetween) {
            MdcEquipmentStatisticalInfo mdcEquipmentStatisticalInfo = mdcEquipmentStatisticalInfoService.findByEquIdsAndMonth(equipmentIdList, month.replaceAll("-", ""));
            if (mdcEquipmentStatisticalInfo != null) {
                if (resultMap.containsKey(month)) {
                    EquUtilRateMonth equUtilRateMonth = resultMap.get(month);
                    if (mdcEquipmentStatisticalInfo.getProcessLong().compareTo(BigDecimal.ZERO) > 0) {
                        equUtilRateMonth.setUtilizationRate(mdcEquipmentStatisticalInfo.getProcessLong().divide(new BigDecimal("864"), 2, RoundingMode.HALF_UP));
                    }
                    resultMap.put(month, equUtilRateMonth);
                }
            }
        }
        return new ArrayList<>(resultMap.values());
    }
 
    /**
     * 设备利用率(昨天)
     */
    @Override
    public List<EquUtilRate> equipmentUtilizationRate(String productionId) {
        List<String> proIds = mdcProductionService.findChildByProId(productionId);
        if (proIds == null || proIds.isEmpty()) {
            return null;
        }
        List<String> equipmentIdList = mdcEquipmentService.getEquIdsByProIds(proIds);
        if (equipmentIdList == null || equipmentIdList.isEmpty()) {
            return null;
        }
        Map<String, EquUtilRate> resultMap = new LinkedHashMap<>();
        equipmentIdList.forEach(equipmentId -> {
            EquUtilRate equUtilRate = new EquUtilRate(equipmentId);
            resultMap.put(equipmentId, equUtilRate);
        });
        String yesterday = LocalDate.now().plusDays(-1).toString();
        List<MdcEquipmentStatisticalInfo> mdcEquipmentStatisticalInfoList = mdcEquipmentStatisticalInfoService.findByEquipmentAndDate(equipmentIdList, yesterday.replaceAll("-", ""));
        if (mdcEquipmentStatisticalInfoList != null && !mdcEquipmentStatisticalInfoList.isEmpty()) {
            mdcEquipmentStatisticalInfoList.forEach(mdcEquipmentStatisticalInfo -> {
                if (resultMap.containsKey(mdcEquipmentStatisticalInfo.getEquipmentId())) {
                    EquUtilRate equUtilRate = resultMap.get(mdcEquipmentStatisticalInfo.getEquipmentId());
                    if (mdcEquipmentStatisticalInfo.getProcessLong().compareTo(BigDecimal.ZERO) > 0) {
                        equUtilRate.setUtilizationRate(mdcEquipmentStatisticalInfo.getProcessLong().divide(new BigDecimal("864"), 2, RoundingMode.HALF_UP));
                    }
                    resultMap.put(mdcEquipmentStatisticalInfo.getEquipmentId(), equUtilRate);
                }
            });
        }
        return new ArrayList<>(resultMap.values());
    }
 
    /**
     * 月度设备综合效率
     */
    @Override
    public List<EquOeeMonth> equipmentMonthOee(String productionId) {
        LocalDate now = LocalDate.now();
        Date start = DateUtils.toDate(now.plusMonths(-12).toString(), DateUtils.STR_DATE);
        Date end = DateUtils.toDate(now.plusMonths(-1).toString(), DateUtils.STR_DATE);
        List<String> monthBetween = DateUtils.getMonthBetween(start, end);
        Map<String, EquOeeMonth> resultMap = monthBetween.stream().collect(Collectors.toMap(
                date -> date,
                date -> new EquOeeMonth(date.substring(date.lastIndexOf("-") + 1).replaceFirst("^0*", "") + "月"),
                (existing, replacement) -> existing, // 处理键冲突的合并函数(通常不会冲突)
                LinkedHashMap::new // 指定使用LinkedHashMap保持插入顺序
        ));
        List<String> proIds = mdcProductionService.findChildByProId(productionId);
        if (proIds == null || proIds.isEmpty()) {
            return new ArrayList<>(resultMap.values());
        }
        List<String> equipmentIdList = mdcEquipmentService.getEquIdsByProIds(proIds);
        if (equipmentIdList == null || equipmentIdList.isEmpty()) {
            return new ArrayList<>(resultMap.values());
        }
        for (String month : monthBetween) {
            BigDecimal oee = mdcOeeInfoService.findByEquIdAndMonth(equipmentIdList, month);
            if (oee != null) {
                EquOeeMonth equOeeMonth = resultMap.get(month);
                equOeeMonth.setOee(oee.setScale(2, RoundingMode.HALF_UP));
                resultMap.put(month, equOeeMonth);
            }
        }
        return new ArrayList<>(resultMap.values());
    }
 
    /**
     * 设备状态统计
     */
    @Override
    public EquOperation equipmentOperationStatistics(String productionId) {
        EquOperation equOperation = new EquOperation();
        List<String> proIds = mdcProductionService.findChildByProId(productionId);
        if (proIds == null || proIds.isEmpty()) {
            return equOperation;
        }
        List<Equipment> equipmentList = equipmentService.listByProds(proIds);
        if (equipmentList == null || equipmentList.isEmpty()) {
            return equOperation;
        }
        for (Equipment equipment : equipmentList) {
            if (equipment.getOporation() != null) {
                switch (equipment.getOporation()) {
                    case 1:
                    case 2:
                        equOperation.setStandby(equOperation.getStandby() + 1);
                        break;
                    case 3:
                        equOperation.setRun(equOperation.getRun() + 1);
                        break;
                    case 22:
                        equOperation.setAlarm(equOperation.getAlarm() + 1);
                        break;
                    default:
                        equOperation.setShutdown(equOperation.getShutdown() + 1);
                        break;
                }
            } else {
                equOperation.setShutdown(equOperation.getShutdown() + 1);
            }
        }
        return equOperation;
    }
 
    /**
     * 设备运行信息
     */
    @Override
    public List<EquRunInfo> equipmentRunInfo(String equipmentId) {
        List<EquRunInfo> equRunInfoList = new ArrayList<>();
        Equipment equipment = equipmentService.findByEquId(equipmentId);
        if (equipment != null) {
            //填充设备基础信息
            equRunInfoList.add(new EquRunInfo("设备名称", equipment.getEquipmentname(), ""));
            equRunInfoList.add(new EquRunInfo("设备编号", equipment.getEquipmentid(), ""));
            if (equipment.getOporation() != null && equipment.getOporation() != 0) {
                String saveTableName = equipment.getSavetablename();
                Map<String, Object> mapData = equipmentWorkLineService.getDataList(saveTableName);
                if (mapData != null) {
                    //获取 MDC 驱动对应的展示参数   并根据key 拼装从 workData  查询的数据
                    List<MdcDriveTypeParamConfig> mdcDriveTypeParamList = mdcDriveTypeParamConfigService.getShowDriveParam(equipment.getDrivetype());
                    if (mdcDriveTypeParamList != null && !mdcDriveTypeParamList.isEmpty()) {
                        List<DictModel> dictItems = sysDictService.getDictItems(CommonConstant.DICT_EQUIPMENT_RUN_UNIT);
                        Map<String, DictModel> resultMap = new HashMap<>();
                        dictItems.forEach(dictModel -> {
                            resultMap.put(dictModel.getText(), dictModel);
                        });
                        for (MdcDriveTypeParamConfig mdcDriveTypeParamConfig : mdcDriveTypeParamList) {
                            EquRunInfo equRunInfo = new EquRunInfo();
                            String englishName = mdcDriveTypeParamConfig.getEnglishName();
                            String chineseName = mdcDriveTypeParamConfig.getChineseName();
                            equRunInfo.setKey(chineseName);
                            if (mapData.containsKey(englishName)) {
                                Object object = mapData.get(englishName);
                                String value = "";
                                if ("CollectTime".equals(englishName)) {
                                    Date date = object == null ? null : (Date) object;
                                    value = DateUtils.format(date, DateUtils.STR_DATE_TIME_SMALL);
                                } else if ("ZUOLAN".equals(equipment.getDrivetype()) && "spindlespeed".equals(englishName) && equipment.getOporation() == 3) {
                                    // ZUOLAN设备主轴转速字段spindlespeed
                                    value = String.valueOf(((new Random().nextInt(35)) + 1) * 100);
                                } else if ("ZUOLAN".equals(equipment.getDrivetype()) && "spindleload".equals(englishName) && equipment.getOporation() == 3) {
                                    // ZUOLAN设备主轴负荷字段spindleload
                                    value = String.valueOf(Integer.valueOf(new Random().nextInt(21)));
                                } else if ("ZUOLAN".equals(equipment.getDrivetype()) && "spindlebeilv".equals(englishName) && equipment.getOporation() == 3) {
                                    // ZUOLAN设备主轴倍率字段spindlebeilv
                                    value = String.valueOf((new Random().nextInt(13)) * 10);
                                } else if ("ZUOLAN".equals(equipment.getDrivetype()) && "feedbeilv".equals(englishName) && equipment.getOporation() == 3) {
                                    // ZUOLAN设备进给倍率字段feedbeilv
                                    value = String.valueOf((new Random().nextInt(13)) * 10);
                                } else {
                                    value = object == null ? "" : object.toString();
                                }
                                equRunInfo.setValue(value);
                                // 设置单位
                                if (resultMap.containsKey(chineseName)) {
                                    DictModel dictModel = resultMap.get(chineseName);
                                    equRunInfo.setUnit(dictModel.getValue());
                                }
                                equRunInfoList.add(equRunInfo);
                            }
                        }
 
                    }
                }
            }
        }
        return equRunInfoList;
    }
 
    /**
     * 设备停机统计
     */
    @Override
    public List<EquDowntimeInfo> equDowntimeStatistics(String productionId) {
        List<String> proIds = mdcProductionService.findChildByProId(productionId);
        if (proIds == null || proIds.isEmpty()) {
            return null;
        }
        List<String> equipmentIdList = mdcEquipmentService.getEquIdsByProIds(proIds);
        if (equipmentIdList == null || equipmentIdList.isEmpty()) {
            return null;
        }
        LocalDate end = LocalDate.now();
        LocalDate start = end.plusDays(-30);
        List<EquDowntimeInfo> result = mdcDowntimeService.equDowntimeStatistics(equipmentIdList, start.toString(), end.toString());
        result.forEach(equDowntimeInfo -> {
            equDowntimeInfo.setDuration(equDowntimeInfo.getDuration().setScale(2, RoundingMode.HALF_UP));
        });
        return result;
    }
 
    /**
     * 设备报警列表
     */
    @Override
    public List<EquAlarm> equAlarmList(String productionId) {
        List<EquAlarm> result = new ArrayList<>();
        List<String> proIds = mdcProductionService.findChildByProId(productionId);
        if (proIds == null || proIds.isEmpty()) {
            return null;
        }
        List<String> equipmentIdList = mdcEquipmentService.getEquIdsByProIds(proIds);
        if (equipmentIdList == null || equipmentIdList.isEmpty()) {
            return null;
        }
        List<EquipmentAlarm> equipmentAlarmList =  equipmentAlarmService.list(new LambdaQueryWrapper<EquipmentAlarm>().in(EquipmentAlarm::getEquipmentid, equipmentIdList).orderByDesc(EquipmentAlarm::getCollecttime).isNotNull(EquipmentAlarm::getAlarmNo).last("TOP 15"));
        if (equipmentAlarmList == null || equipmentAlarmList.isEmpty()) {
            return null;
        }
        for (EquipmentAlarm equipmentAlarm : equipmentAlarmList) {
            MdcAlarmInfo mdcAlarmInfo = mdcAlarmInfoService.findAlarmContent(equipmentAlarm.getAlarmNo(), equipmentAlarm.getEquipmentid());
            EquAlarm equAlarm = new EquAlarm();
            equAlarm.setEquipmentId(equipmentAlarm.getEquipmentid());
            if (mdcAlarmInfo != null) {
                equAlarm.setAlarmInfo(mdcAlarmInfo.getAlarmContent());
            } else {
                equAlarm.setAlarmInfo(equipmentAlarm.getAlarmContent());
            }
            result.add(equAlarm);
        }
        return result;
    }
 
}