Lius
2025-02-18 3423bb9ee5b25d270a00763b69ed73970d790f63
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
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.system.vo.LoginUser;
import org.jeecg.modules.mdc.entity.MdcDownTime;
import org.jeecg.modules.mdc.entity.MdcEquipment;
import org.jeecg.modules.mdc.entity.MdcMttrInfo;
import org.jeecg.modules.mdc.entity.MdcOeeInfo;
import org.jeecg.modules.mdc.mapper.MdcDownTimeMapper;
import org.jeecg.modules.mdc.service.IMdcDownTimeService;
import org.jeecg.modules.mdc.service.IMdcEquipmentService;
import org.jeecg.modules.mdc.service.IMdcMttrInfoService;
import org.jeecg.modules.mdc.util.DateUtils;
import org.jeecg.modules.mdc.vo.MdcDownTimeVo;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
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.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
/**
 * @Description: 设备故障停机时长表
 * @Author: Lius
 * @Date: 2025-01-22
 */
@Service
public class MdcDownTimeServiceImpl extends ServiceImpl<MdcDownTimeMapper, MdcDownTime> implements IMdcDownTimeService {
 
    @Resource
    private IMdcEquipmentService mdcEquipmentService;
 
    @Resource
    private IMdcMttrInfoService mdcMttrInfoService;
 
    @Override
    public IPage<MdcDownTime> pageList(String userId, Page<MdcDownTime> page, MdcDownTimeVo mdcDownTimeVo, HttpServletRequest req) {
        List<String> equipmentIds = new ArrayList<>();
        if (StringUtils.isNotEmpty(mdcDownTimeVo.getParentId()) && StringUtils.isEmpty(mdcDownTimeVo.getEquipmentId())) {
            if ("2".equals(mdcDownTimeVo.getTypeTree())) {
                //部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, mdcDownTimeVo.getParentId());
            } else {
                //产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, mdcDownTimeVo.getParentId());
            }
        } else if (StringUtils.isNotEmpty(mdcDownTimeVo.getEquipmentId())) {
            //单台设备信息
            mdcDownTimeVo.setEquipmentIdList(Collections.singletonList(mdcDownTimeVo.getEquipmentId()));
        } else {
            //查询用户拥有的所有设备信息
            if ("2".equals(mdcDownTimeVo.getTypeTree())) {
                //部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, null);
            } else {
                //产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, null);
            }
        }
 
        if (mdcDownTimeVo.getEquipmentIdList() == null || mdcDownTimeVo.getEquipmentIdList().isEmpty()) {
            mdcDownTimeVo.setEquipmentIdList(equipmentIds);
        }
 
        if (mdcDownTimeVo.getEquipmentIdList() == null || mdcDownTimeVo.getEquipmentIdList().isEmpty()) {
            return null;
        }
        return this.baseMapper.pageList(page, mdcDownTimeVo);
    }
 
    /**
     * 添加
     *
     * @param mdcDownTimeVo
     * @return
     */
    @Override
    public boolean addDownTime(MdcDownTimeVo mdcDownTimeVo) {
        String[] equipmentIdList = mdcDownTimeVo.getEquipmentIds().split(",");
        List<MdcDownTime> downTimeList = new ArrayList<>();
        for (String equipmentId : equipmentIdList) {
            MdcDownTime mdcDownTime = new MdcDownTime();
            BeanUtils.copyProperties(mdcDownTimeVo, mdcDownTime);
            mdcDownTime.setEquipmentId(equipmentId);
            downTimeList.add(mdcDownTime);
        }
        this.saveBatch(downTimeList);
        return true;
    }
 
    /**
     * 导出
     *
     * @param userId
     * @param mdcDownTimeVo
     * @return
     */
    @Override
    public ModelAndView exportXls(String userId, MdcDownTimeVo mdcDownTimeVo) {
        LambdaQueryWrapper<MdcDownTime> queryWrapper = new LambdaQueryWrapper<>();
        List<String> equipmentIds = new ArrayList<>();
        if (StringUtils.isNotEmpty(mdcDownTimeVo.getParentId()) && StringUtils.isEmpty(mdcDownTimeVo.getEquipmentId())) {
            if ("2".equals(mdcDownTimeVo.getTypeTree())) {
                //部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, mdcDownTimeVo.getParentId());
            } else {
                //产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, mdcDownTimeVo.getParentId());
            }
        } else if (StringUtils.isNotEmpty(mdcDownTimeVo.getEquipmentId())) {
            //单台设备信息
            mdcDownTimeVo.setEquipmentIdList(Collections.singletonList(mdcDownTimeVo.getEquipmentId()));
        } else {
            //查询用户拥有的所有设备信息
            if ("2".equals(mdcDownTimeVo.getTypeTree())) {
                //部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, null);
            } else {
                //产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, null);
            }
        }
 
        if (mdcDownTimeVo.getEquipmentIdList() == null || mdcDownTimeVo.getEquipmentIdList().isEmpty()) {
            mdcDownTimeVo.setEquipmentIdList(equipmentIds);
        }
 
        if (mdcDownTimeVo.getEquipmentIdList() == null || mdcDownTimeVo.getEquipmentIdList().isEmpty()) {
            return null;
        } else {
            queryWrapper.in(MdcDownTime::getEquipmentId, mdcDownTimeVo.getEquipmentIdList());
        }
        if (StringUtils.isNotEmpty(mdcDownTimeVo.getEquipmentId())) {
            queryWrapper.eq(MdcDownTime::getEquipmentId, mdcDownTimeVo.getEquipmentId());
        }
        if (StringUtils.isNotEmpty(mdcDownTimeVo.getStartTime()) && StringUtils.isNotEmpty(mdcDownTimeVo.getEndTime())) {
            queryWrapper.between(MdcDownTime::getTheDate, mdcDownTimeVo.getStartTime(), mdcDownTimeVo.getEndTime());
        }
        queryWrapper.orderByDesc(MdcDownTime::getTheDate).orderByDesc(MdcDownTime::getEquipmentId);
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        List<MdcDownTime> downTimes = this.baseMapper.selectList(queryWrapper);
        // 导出文件名称
        mv.addObject(NormalExcelConstants.FILE_NAME, "设备故障停机时长列表");
        mv.addObject(NormalExcelConstants.CLASS, MdcDownTime.class);
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("设备故障停机时长表数据", "导出人:" + user.getRealname(), "设备故障停机时长数据"));
        //update-end---author:wangshuai ---date:20211227  for:[JTC-116]导出人写死了------------
        mv.addObject(NormalExcelConstants.DATA_LIST, downTimes);
        return mv;
    }
 
    /**
     * 计算MTBF
     *
     * @param month
     */
    @Override
    public void computeMtbf(String month) {
        /*
        设备平均故障间隔期(MTBF) = (日历时间 - 总故障停机时间) / 总故障次数
         */
        // step.1
        List<MdcMttrInfo> mdcMttrInfos = mdcMttrInfoService.list(new LambdaQueryWrapper<MdcMttrInfo>().eq(MdcMttrInfo::getTheDate, month));
        DateTimeFormatter df = DateTimeFormatter.ofPattern(DateUtils.STR_DATE);
        LocalDate parse = LocalDate.parse(month + "-01", df);
        int calendarLong = parse.lengthOfMonth() * 24;
        if (mdcMttrInfos != null && !mdcMttrInfos.isEmpty()) {
            for (MdcMttrInfo mdcMttrInfo : mdcMttrInfos) {
                // 日历时间
                mdcMttrInfo.setCalendarLong(new BigDecimal(calendarLong));
                // 总故障停机时间
                BigDecimal totalDownLong = this.baseMapper.findTotalDownLong(month);
                if (totalDownLong != null) {
                    mdcMttrInfo.setTotalDownLong(totalDownLong);
                }
                // 总故障次数
                Integer totalDownCount = this.baseMapper.findTotalDownCount(month);
                if (totalDownLong != null) {
                    mdcMttrInfo.setTotalDownCount(totalDownCount);
                }
                // MTBF
                if (mdcMttrInfo.getTotalDownCount() != 0) {
                    BigDecimal mtbf = (mdcMttrInfo.getCalendarLong().subtract(mdcMttrInfo.getTotalDownLong())).divide(new BigDecimal(mdcMttrInfo.getTotalDownCount()), 4, RoundingMode.HALF_UP);
                    mdcMttrInfo.setMtbf(mtbf);
                }
            }
            mdcMttrInfoService.updateBatchById(mdcMttrInfos);
        } else {
            List<MdcEquipment> equipmentList = mdcEquipmentService.list();
            if (equipmentList != null && !equipmentList.isEmpty()) {
                List<MdcMttrInfo> mmi = new ArrayList<>();
                for (MdcEquipment mdcEquipment : equipmentList) {
                    MdcMttrInfo mdcMttrInfo = new MdcMttrInfo();
                    mdcMttrInfo.setEquipmentId(mdcEquipment.getEquipmentId());
                    mdcMttrInfo.setTheDate(month);
                    // 日历时间
                    mdcMttrInfo.setCalendarLong(new BigDecimal(calendarLong));
                    // 总故障停机时间
                    BigDecimal totalDownLong = this.baseMapper.findTotalDownLong(month);
                    if (totalDownLong != null) {
                        mdcMttrInfo.setTotalDownLong(totalDownLong);
                    }
                    // 总故障次数
                    Integer totalDownCount = this.baseMapper.findTotalDownCount(month);
                    if (totalDownLong != null) {
                        mdcMttrInfo.setTotalDownCount(totalDownCount);
                    }
                    // MTBF
                    if (mdcMttrInfo.getTotalDownCount() != 0) {
                        BigDecimal mtbf = (mdcMttrInfo.getCalendarLong().subtract(mdcMttrInfo.getTotalDownLong())).divide(new BigDecimal(mdcMttrInfo.getTotalDownCount()), 4, RoundingMode.HALF_UP);
                        mdcMttrInfo.setMtbf(mtbf);
                    }
                    mmi.add(mdcMttrInfo);
                }
                mdcMttrInfoService.saveBatch(mmi);
            }
        }
 
    }
}