Lius
2025-06-20 2473cd2814fd7c8fa1d132704dd9ab190f43f3ff
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
package org.jeecg.modules.mdc.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.mdc.entity.MdcEquipment;
import org.jeecg.modules.mdc.entity.MdcOeeInfo;
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.MdcOeeComputeVo;
import org.jeecg.modules.mdc.vo.MdcOeeInfoVo;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
/**
 * @Description: OEE表
 * @Author: lius
 * @Date: 2024-12-12
 */
@Service
public class MdcOeeInfoServiceImpl extends ServiceImpl<MdcOeeInfoMapper, MdcOeeInfo> implements IMdcOeeInfoService {
 
    @Resource
    private IMdcEquipmentService mdcEquipmentService;
 
    @Resource
    private IMdcDowntimeService mdcDowntimeService;
 
    @Resource
    private IMdcPartProcessInfoService mdcPartProcessInfoService;
 
    @Resource
    private IMdcEquipmentStatisticalInfoService mdcEquipmentStatisticalInfoService;
 
    /**
     * 分页列表
     *
     * @param userId
     * @param page
     * @param mdcOeeInfoVo
     * @param req
     * @return
     */
    @Override
    public IPage<MdcOeeInfo> pageList(String userId, Page<MdcOeeInfo> page, MdcOeeInfoVo mdcOeeInfoVo, HttpServletRequest req) {
        List<String> equipmentIds = new ArrayList<>();
        if (StringUtils.isNotEmpty(mdcOeeInfoVo.getParentId()) && StringUtils.isEmpty(mdcOeeInfoVo.getEquipmentId())) {
            if ("2".equals(mdcOeeInfoVo.getTypeTree())) {
                //部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, mdcOeeInfoVo.getParentId());
            } else {
                //产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, mdcOeeInfoVo.getParentId());
            }
        } else if (StringUtils.isNotEmpty(mdcOeeInfoVo.getEquipmentId())) {
            //单台设备信息
            mdcOeeInfoVo.setEquipmentIdList(Collections.singletonList(mdcOeeInfoVo.getEquipmentId()));
        } else {
            //查询用户拥有的所有设备信息
            if ("2".equals(mdcOeeInfoVo.getTypeTree())) {
                //部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, null);
            } else {
                //产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, null);
            }
        }
 
        if (mdcOeeInfoVo.getEquipmentIdList() == null || mdcOeeInfoVo.getEquipmentIdList().isEmpty()) {
            mdcOeeInfoVo.setEquipmentIdList(equipmentIds);
        }
 
        if (mdcOeeInfoVo.getEquipmentIdList() == null || mdcOeeInfoVo.getEquipmentIdList().isEmpty()) {
            return null;
        }
        return this.baseMapper.pageList(page, mdcOeeInfoVo);
    }
 
    /**
     * 导出
     *
     * @param userId
     * @param mdcOeeInfoVo
     * @return
     */
    @Override
    public ModelAndView exportXls(String userId, MdcOeeInfoVo mdcOeeInfoVo) {
        LambdaQueryWrapper<MdcOeeInfo> queryWrapper = new LambdaQueryWrapper<>();
        List<String> equipmentIds = new ArrayList<>();
        if (StringUtils.isNotEmpty(mdcOeeInfoVo.getParentId()) && StringUtils.isEmpty(mdcOeeInfoVo.getEquipmentId())) {
            if ("2".equals(mdcOeeInfoVo.getTypeTree())) {
                //部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, mdcOeeInfoVo.getParentId());
            } else {
                //产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, mdcOeeInfoVo.getParentId());
            }
        } else if (StringUtils.isNotEmpty(mdcOeeInfoVo.getEquipmentId())) {
            //单台设备信息
            mdcOeeInfoVo.setEquipmentIdList(Collections.singletonList(mdcOeeInfoVo.getEquipmentId()));
        } else {
            //查询用户拥有的所有设备信息
            if ("2".equals(mdcOeeInfoVo.getTypeTree())) {
                //部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, null);
            } else {
                //产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, null);
            }
        }
 
        if (mdcOeeInfoVo.getEquipmentIdList() == null || mdcOeeInfoVo.getEquipmentIdList().isEmpty()) {
            mdcOeeInfoVo.setEquipmentIdList(equipmentIds);
        }
 
        if (mdcOeeInfoVo.getEquipmentIdList() == null || mdcOeeInfoVo.getEquipmentIdList().isEmpty()) {
            return null;
        } else {
            queryWrapper.in(MdcOeeInfo::getEquipmentId, mdcOeeInfoVo.getEquipmentIdList());
        }
        if (StringUtils.isNotEmpty(mdcOeeInfoVo.getEquipmentId())) {
            queryWrapper.eq(MdcOeeInfo::getEquipmentId, mdcOeeInfoVo.getEquipmentId());
        }
        if (StringUtils.isNotEmpty(mdcOeeInfoVo.getStartTime()) && StringUtils.isNotEmpty(mdcOeeInfoVo.getEndTime())) {
            queryWrapper.between(MdcOeeInfo::getTheDate, mdcOeeInfoVo.getStartTime(), mdcOeeInfoVo.getEndTime());
        }
        queryWrapper.orderByDesc(MdcOeeInfo::getTheDate).orderByDesc(MdcOeeInfo::getEquipmentId);
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        List<MdcOeeInfo> mdcOeeInfos = this.baseMapper.selectList(queryWrapper);
        // 导出文件名称
        mv.addObject(NormalExcelConstants.FILE_NAME, "OEE数据列表");
        mv.addObject(NormalExcelConstants.CLASS, MdcOeeInfo.class);
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("OEE数据列表数据", "导出人:" + user.getRealname(), "OEE数据"));
        //update-end---author:wangshuai ---date:20211227  for:[JTC-116]导出人写死了------------
        mv.addObject(NormalExcelConstants.DATA_LIST, mdcOeeInfos);
        return mv;
    }
 
    /**
     * 计算OEE
     *
     * @param mdcOeeComputeVo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void computeOee(MdcOeeComputeVo mdcOeeComputeVo) {
        List<MdcOeeInfo> result = new ArrayList<>();
        String startDate = mdcOeeComputeVo.getStartDate();
        String endDate = mdcOeeComputeVo.getEndDate();
        List<String> dateList = DateUtils.getDatesStringList(DateUtils.getShortDate(startDate), DateUtils.getShortDate(endDate));
        if (!dateList.isEmpty()) {
            //获取设备列表
            List<MdcEquipment> equipmentList = mdcEquipmentService.list();
 
            for (String validDate : dateList) {
                try {
                    this.remove(new LambdaQueryWrapper<MdcOeeInfo>().eq(MdcOeeInfo::getTheDate, validDate));
                } catch (Exception e) {
                    log.error("参数格式不对", e);
                }
                for (MdcEquipment mdcEquipment : equipmentList) {
                    String equipmentId = mdcEquipment.getEquipmentId();
                    MdcOeeInfo mdcOeeInfo = new MdcOeeInfo();
                    // 设备编号
                    mdcOeeInfo.setEquipmentId(equipmentId);
                    // 计算日期
                    mdcOeeInfo.setTheDate(validDate);
                    // 工作日历时间(min) --- 按每天24小时算
                    mdcOeeInfo.setCalendarLong(1440);
                    // 计划停机时间(min) --- 按维护数据统计当天总时长
                    Integer planCloseLong = mdcDowntimeService.findPlanTimeDuration(equipmentId, validDate, CommonConstant.CLOSE_TYPE_0);
                    mdcOeeInfo.setPlanCloseLong(planCloseLong);
                    // 负荷时间(min) --- 日历工作时间-计划停机时间
                    Integer loadLong = mdcOeeInfo.getCalendarLong() - planCloseLong;
                    mdcOeeInfo.setLoadLong(loadLong);
                    // 非计划停机时间(min) --- 按维护数据统计当天总时长
                    Integer noPlanCloseLong = mdcDowntimeService.findPlanTimeDuration(equipmentId, validDate, CommonConstant.CLOSE_TYPE_1);
                    mdcOeeInfo.setNoplanCloseLong(noPlanCloseLong);
                    // 开动时间(min) --- 负荷时间-非计划停机时间
                    int actuateLong = loadLong - noPlanCloseLong;
                    mdcOeeInfo.setActuateLong(actuateLong);
                    // 时间开动率 --- 开动时间/负荷时间
                    BigDecimal timeActuationRate = new BigDecimal(actuateLong).divide(new BigDecimal(loadLong), 4, RoundingMode.HALF_UP).multiply(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP);
                    mdcOeeInfo.setTimeActuationRate(timeActuationRate);
                    // 有效运行时间 --- 按需求维护零件加工总时长||系统主轴负载时间
                    Integer effectiveRunLong = 0;
                    Integer totalProcessLong = mdcPartProcessInfoService.selectTotalProcessLong(equipmentId, validDate);
                    if (totalProcessLong == 0) {
                        //查询设备运行时间
                        effectiveRunLong = mdcEquipmentStatisticalInfoService.selectProcessLong(equipmentId, validDate.replace("-", ""));
                    } else {
                        effectiveRunLong = totalProcessLong;
                    }
                    mdcOeeInfo.setEffectiveRunLong(effectiveRunLong);
                    // 性能开动率 --- 有效运行时间/开动时间
                    BigDecimal performanceRate = BigDecimal.ZERO;
                    if (effectiveRunLong != 0 && actuateLong != 0) {
                        performanceRate = new BigDecimal(effectiveRunLong).divide(new BigDecimal(actuateLong), 4, RoundingMode.HALF_UP).multiply(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP);
                    }
                    mdcOeeInfo.setPerformanceRate(performanceRate);
                    // 加工零件数量 --- 按维护数据统计当天
                    Integer processCount = mdcPartProcessInfoService.selectTotalProcessCount(equipmentId, validDate);
                    mdcOeeInfo.setProcessCount(processCount);
                    // 合格零件数量 --- 按维护数据统计当天
                    Integer passCount = mdcPartProcessInfoService.selectTotalPassCount(equipmentId, validDate);
                    mdcOeeInfo.setPassCount(passCount);
                    // 合格率 --- 合格数/加工数
                    BigDecimal passRate = BigDecimal.ZERO;
                    if (processCount != 0 && passCount != 0) {
                        passRate = new BigDecimal(passCount).divide(new BigDecimal(processCount), 4, RoundingMode.HALF_UP).multiply(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP);
                    }
                    mdcOeeInfo.setPassRate(passRate);
                    // OEE --- 时间开动率 * 性能开动率 * 一次合格率
                    BigDecimal oee = BigDecimal.ZERO;
                    if (!timeActuationRate.equals(BigDecimal.ZERO) && !performanceRate.equals(BigDecimal.ZERO) && !passRate.equals(BigDecimal.ZERO)) {
                        oee = timeActuationRate.multiply(performanceRate).multiply(passRate).divide(new BigDecimal("10000"), 4, RoundingMode.HALF_UP);
                    }
                    mdcOeeInfo.setOee(oee);
                    result.add(mdcOeeInfo);
                }
            }
        }
        super.saveBatch(result);
    }
 
    @Override
    public BigDecimal findByEquIdAndMonth(List<String> equipmentIdList, String month) {
        return this.baseMapper.findByEquIdAndMonth(equipmentIdList, month);
    }
 
}