Lius
2024-10-25 f50b0315234274547f2b1e6e590eeeebe6b3fe2f
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
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.lang3.StringUtils;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.modules.mdc.dto.MdcOeeListDto;
import org.jeecg.modules.mdc.dto.MdcOeeResultDto;
import org.jeecg.modules.mdc.entity.*;
import org.jeecg.modules.mdc.mapper.MdcOeeInfoMapper;
import org.jeecg.modules.mdc.service.*;
import org.jeecg.modules.mdc.util.DateUtils;
import org.jeecg.modules.mdc.vo.MdcOeeInfoQueryVo;
import org.jeecg.modules.mdc.vo.MdcOeeVo;
import org.jeecg.modules.mdcJc.entity.MdcProductDayschedule;
import org.jeecg.modules.mdcJc.service.IMdcProductDayScheduleService;
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;
 
/**
 * @Description: OEE
 * @Author: lius
 * @Date: 2024-10-22
 * @Version: V1.0
 */
@Service
public class MdcOeeInfoServiceImpl extends ServiceImpl<MdcOeeInfoMapper, MdcOeeInfo> implements IMdcOeeInfoService {
 
    @Resource
    private IMdcEquipmentService mdcEquipmentService;
 
    @Resource
    private IMdcShiftSubService mdcShiftSubService;
 
    @Resource
    private IMdcUtilizationRateService mdcUtilizationRateService;
 
    @Resource
    private IMdcComponentInfoService mdcComponentInfoService;
 
    @Resource
    private IMdcProductDayScheduleService mdcProductDayScheduleService;
 
    @Resource
    private IEquipmentService equipmentService;
 
    @Resource
    private IMdcEquipmentStatisticalShiftInfoService mdcEquipmentStatisticalShiftInfoService;
 
    /**
     * oee列表
     * @param userId
     * @param vo
     * @return
     */
    @Override
    public MdcOeeVo pageList(String userId, MdcOeeInfoQueryVo vo) {
        MdcOeeVo result = new MdcOeeVo();
        List<MdcOeeListDto> listDtos = new ArrayList<>();
        List<String> equipmentIds = new ArrayList<>();
        if (StringUtils.isNotEmpty(vo.getParentId()) && StringUtils.isEmpty(vo.getEquipmentId())) {
            if ("2".equals(vo.getTypeTree())) {
                // 部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, vo.getParentId());
            } else {
                // 产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, vo.getParentId());
            }
        } else if (StringUtils.isNotEmpty(vo.getEquipmentId())) {
            // 单台设备信息
            vo.setEquipmentIdList(Collections.singletonList(vo.getEquipmentId()));
        } else {
            // 查询用户拥有的所有设备信息
            if ("2".equals(vo.getTypeTree())) {
                // 部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, null);
            } else {
                // 产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, null);
            }
        }
        if (vo.getEquipmentIdList() == null || vo.getEquipmentIdList().isEmpty()) {
            vo.setEquipmentIdList(equipmentIds);
        }
 
        List<String> dates = DateUtils.getDatesStringList2(DateUtils.getShortDate2(vo.getStartTime()), DateUtils.getShortDate2(vo.getEndTime()));
        result.setDates(dates);
 
        // oee等级
        List<MdcUtilizationRate> mdcUtilizationRateList = mdcUtilizationRateService.listByType("oee");
 
        if (vo.getEquipmentIdList() == null || vo.getEquipmentIdList().isEmpty()) {
            result.setMdcOeeListDtoList(listDtos);
        } else {
            LambdaQueryWrapper<MdcEquipment> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.isNotNull(MdcEquipment::getEquipmentId);
            if (vo.getEquipmentIdList() != null && !vo.getEquipmentIdList().isEmpty()) {
                queryWrapper.in(MdcEquipment::getEquipmentId, vo.getEquipmentIdList());
            }
            queryWrapper.orderByAsc(MdcEquipment::getEquipmentName);
            List<MdcEquipment> equipmentList = mdcEquipmentService.list(queryWrapper);
            //查询oee数据
            List<MdcOeeInfo> oeeList = this.baseMapper.oeeList(vo);
            //数据处理
            String shiftSubName = "全部";
            List<MdcShiftSub> mdcShiftSubList = new ArrayList<>();
            if (StringUtils.isNotBlank(vo.getShiftSubId())) {
                mdcShiftSubList = mdcShiftSubService.list(new LambdaQueryWrapper<MdcShiftSub>().eq(MdcShiftSub::getId, vo.getShiftSubId()).eq(MdcShiftSub::getShiftSubStatus, "1"));
                //oeeList = oeeList.stream().filter(mdcOeeInfo -> mdcOeeInfo.getShiftId().equals(vo.getShiftId()) && mdcOeeInfo.getShiftSubId().equals(vo.getShiftSubId())).collect(Collectors.toList());
                List<String> shiftSubNames = mdcShiftSubList.stream().map(MdcShiftSub::getShiftSubName).collect(Collectors.toList());
                shiftSubName = String.join(",", shiftSubNames);
            }
 
 
            for (MdcEquipment mdcEquipment : equipmentList) {
                MdcOeeListDto mdcOeeListDto = new MdcOeeListDto();
                mdcOeeListDto.setEquipmentId(mdcEquipment.getEquipmentId());
                mdcOeeListDto.setEquipmentName(mdcEquipment.getEquipmentName());
                mdcOeeListDto.setEquipmentType(mdcEquipment.getEquipmentType());
                mdcOeeListDto.setShiftSubName(shiftSubName);
                mdcOeeListDto.setClazz(mdcProductDayScheduleService.findClazz(mdcEquipment.getEquipmentId()));
                List<MdcOeeResultDto> mdcOeeResultList = new ArrayList<>();
                for (String date : dates) {
                    MdcOeeResultDto mdcOeeResultDto = new MdcOeeResultDto();
                    mdcOeeResultDto.setTheDate(date);
                    String format = DateUtils.format(DateUtils.toDate(date, DateUtils.STRDATE), DateUtils.STR_DATE);
                    List<MdcOeeInfo> mdcOeeInfoList = oeeList.stream().filter(mdcOeeInfo -> mdcOeeInfo.getEquipmentId().equals(mdcEquipment.getEquipmentId()) && mdcOeeInfo.getWorkDate().equals(format)).collect(Collectors.toList());
                    BigDecimal oeeRate = BigDecimal.ZERO;
                    if (!mdcOeeInfoList.isEmpty()) {
                        BigDecimal count = mdcOeeInfoList.stream().map(MdcOeeInfo::getOeeRate).reduce(BigDecimal.ZERO, BigDecimal::add);
                        oeeRate = count.divide(new BigDecimal(mdcOeeInfoList.size()), 2, RoundingMode.HALF_UP);
                        mdcOeeResultDto.setOeeRate(oeeRate);
                    }
                    long rate = oeeRate.multiply(new BigDecimal("100")).longValue();
                    for (MdcUtilizationRate mdcUtilizationRate : mdcUtilizationRateList) {
                        if (rate >= mdcUtilizationRate.getMinimumRange() && rate < mdcUtilizationRate.getMaximumRange()) {
                            mdcOeeResultDto.setColor(mdcUtilizationRate.getRateParameterColor());
                        }
                    }
                    mdcOeeResultList.add(mdcOeeResultDto);
                }
                mdcOeeListDto.setDataList(mdcOeeResultList);
                listDtos.add(mdcOeeListDto);
            }
            result.setMdcOeeListDtoList(listDtos);
        }
 
        return result;
    }
 
    /**
     * 计算oee
     * @param date
     */
    @Override
    public void runningOverallEquipmentEfficiency(String date) {
        // 获取有效日期 格式 yyyy-MM-dd
        String validDate = LocalDate.now().plusDays(-1).toString();
        if (StringUtils.isNotBlank(date)) {
            validDate = DateUtils.format(DateUtils.toDate(date, "yyyyMMdd"), DateUtils.STR_DATE);
            try {
                if (validDate != null) {
                    this.remove(new LambdaQueryWrapper<MdcOeeInfo>().
                            eq(MdcOeeInfo::getWorkDate, validDate));
                }
            } catch (Exception e) {
                log.error("参数格式不对", e);
            }
        }
        //获取班次
        List<MdcShiftSub> mdcShiftSubList = mdcShiftSubService.list(new LambdaQueryWrapper<MdcShiftSub>().eq(MdcShiftSub::getShiftSubStatus, CommonConstant.STATUS_1));
        if (mdcShiftSubList == null || mdcShiftSubList.isEmpty()) {
            return;
        }
        List<Equipment> equipmentList = equipmentService.list();
        if (equipmentList == null || equipmentList.isEmpty()) {
            return;
        }
        List<MdcOeeInfo> result = new ArrayList<>();
        for (Equipment equipment : equipmentList) {
            //获取零部件计划
            List<MdcProductDayschedule> productDayScheduleList = mdcProductDayScheduleService.list(new LambdaQueryWrapper<MdcProductDayschedule>()
                    .eq(MdcProductDayschedule::getPlanDate, validDate)
                    .eq(MdcProductDayschedule::getEquipmentId, equipment.getEquipmentid()));
            if (productDayScheduleList != null && !productDayScheduleList.isEmpty()) {
                MdcProductDayschedule mdcProductDayschedule;
                MdcOeeInfo mdcOeeInfo = new MdcOeeInfo();
                for (MdcShiftSub mdcShiftSub : mdcShiftSubList) {
                    String id = mdcShiftSub.getId();
                    if (productDayScheduleList.stream().anyMatch(productDayschedule -> productDayschedule.getOrderId().substring(8, 10).equals(id))) {
                        mdcOeeInfo.setEquipmentId(equipment.getEquipmentid());
                        mdcOeeInfo.setWorkDate(validDate);
                        mdcOeeInfo.setStartTime(mdcShiftSub.getStartDate());
                        mdcOeeInfo.setEndTime(mdcShiftSub.getEndDate());
                        mdcOeeInfo.setInterDayFlag(mdcShiftSub.getIsDaySpan());
                        mdcOeeInfo.setShiftId(mdcShiftSub.getShiftId());
                        mdcOeeInfo.setShiftSubId(id);
                        //获取设备运行数据
                        MdcEquipmentStatisticalShiftInfo mdcEquipmentStatisticalShiftInfo = mdcEquipmentStatisticalShiftInfoService.getOne(new LambdaQueryWrapper<MdcEquipmentStatisticalShiftInfo>()
                                .eq(MdcEquipmentStatisticalShiftInfo::getEquipmentId, equipment.getEquipmentid())
                                .eq(MdcEquipmentStatisticalShiftInfo::getTheDate, validDate)
                                .eq(MdcEquipmentStatisticalShiftInfo::getShiftSubId, id));
                        if (mdcEquipmentStatisticalShiftInfo == null) {
                            continue;
                        }
                        mdcOeeInfo.setProcessLong(mdcEquipmentStatisticalShiftInfo.getProcessLong());
                        mdcOeeInfo.setWaitLong(mdcEquipmentStatisticalShiftInfo.getWaitLong());
                        mdcOeeInfo.setOpenLong(mdcEquipmentStatisticalShiftInfo.getOpenLong());
                        mdcOeeInfo.setCloseLong(mdcEquipmentStatisticalShiftInfo.getCloseLong());
                        //获取设备生产计划
                        mdcProductDayschedule = productDayScheduleList.stream().filter(productDayschedule -> productDayschedule.getOrderId().substring(8, 10).equals(id)).findAny().orElse(null);
                        if (mdcProductDayschedule == null) {
                            continue;
                        }
                        mdcOeeInfo.setWorkTeam(mdcProductDayschedule.getClazz());
                        mdcOeeInfo.setPlanNum(mdcProductDayschedule.getPlanCount());
                        mdcOeeInfo.setCompleteNum(mdcProductDayschedule.getCompletionCount());
                        mdcOeeInfo.setQualityNum(mdcProductDayschedule.getQualifiedCount());
                        mdcOeeInfo.setComponentNo(mdcProductDayschedule.getProductNo());
                        //查询班产能
                        MdcComponentInfo mdcComponentInfo = null;
                        if (mdcProductDayschedule.getIsAutomaticLine().equals(CommonConstant.STATUS_1)) {
                            //自动线
                            mdcComponentInfo = mdcComponentInfoService.findByComponentNo(equipment.getEquipmentid(), mdcProductDayschedule.getProductNo());
                        } else {
                            //非自动线
                            List<MdcComponentInfo> componentInfoList = mdcComponentInfoService.list(new LambdaQueryWrapper<MdcComponentInfo>()
                                    .eq(MdcComponentInfo::getComponentNo, mdcProductDayschedule.getProductNo())
                                    .like(MdcComponentInfo::getEquipmentId, equipment.getEquipmentid()));
                            if (componentInfoList != null && !componentInfoList.isEmpty()) {
                                mdcComponentInfo = componentInfoList.get(0);
                            }
                        }
                        if (mdcComponentInfo == null) {
                            continue;
                        }
                        mdcOeeInfo.setScheduleNum(mdcComponentInfo.getScheduleNum());
 
                        BigDecimal oeeRate = mdcOeeInfo.getProcessLong().multiply(new BigDecimal(mdcOeeInfo.getPlanNum())).multiply(new BigDecimal(mdcOeeInfo.getQualityNum()))
                                        .divide(mdcEquipmentStatisticalShiftInfo.getTotalLong(), 2, RoundingMode.HALF_UP)
                                        .divide(new BigDecimal(mdcOeeInfo.getScheduleNum()), 2, RoundingMode.HALF_UP)
                                        .divide(new BigDecimal(mdcOeeInfo.getCompleteNum()), 2, RoundingMode.HALF_UP);
                        mdcOeeInfo.setOeeRate(oeeRate);
                        result.add(mdcOeeInfo);
                    }
                }
            }
        }
        if (!result.isEmpty()) {
            this.saveBatch(result);
        }
    }
 
}