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
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.MdcEquipment;
import org.jeecg.modules.mdc.entity.MdcMttrInfo;
import org.jeecg.modules.mdc.entity.MdcRepairInfo;
import org.jeecg.modules.mdc.mapper.MdcRepairInfoMapper;
import org.jeecg.modules.mdc.service.IMdcEquipmentService;
import org.jeecg.modules.mdc.service.IMdcMttrInfoService;
import org.jeecg.modules.mdc.service.IMdcRepairInfoService;
import org.jeecg.modules.mdc.vo.MdcRepairInfoVo;
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.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
/**
 * @Description: 设备维修时长表
 * @Author: Lius
 * @Date: 2025-01-22
 */
@Service
public class MdcRepairInfoServiceImpl extends ServiceImpl<MdcRepairInfoMapper, MdcRepairInfo> implements IMdcRepairInfoService {
 
    @Resource
    private IMdcEquipmentService mdcEquipmentService;
 
    @Resource
    private IMdcMttrInfoService mdcMttrInfoService;
 
    /**
     * 分页列表
     *
     * @param userId
     * @param page
     * @param mdcRepairInfoVo
     * @param req
     * @return
     */
    @Override
    public IPage<MdcRepairInfo> pageList(String userId, Page<MdcRepairInfo> page, MdcRepairInfoVo mdcRepairInfoVo, HttpServletRequest req) {
        List<String> equipmentIds = new ArrayList<>();
        if (StringUtils.isNotEmpty(mdcRepairInfoVo.getParentId()) && StringUtils.isEmpty(mdcRepairInfoVo.getEquipmentId())) {
            if ("2".equals(mdcRepairInfoVo.getTypeTree())) {
                //部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, mdcRepairInfoVo.getParentId());
            } else {
                //产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, mdcRepairInfoVo.getParentId());
            }
        } else if (StringUtils.isNotEmpty(mdcRepairInfoVo.getEquipmentId())) {
            //单台设备信息
            mdcRepairInfoVo.setEquipmentIdList(Collections.singletonList(mdcRepairInfoVo.getEquipmentId()));
        } else {
            //查询用户拥有的所有设备信息
            if ("2".equals(mdcRepairInfoVo.getTypeTree())) {
                //部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, null);
            } else {
                //产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, null);
            }
        }
 
        if (mdcRepairInfoVo.getEquipmentIdList() == null || mdcRepairInfoVo.getEquipmentIdList().isEmpty()) {
            mdcRepairInfoVo.setEquipmentIdList(equipmentIds);
        }
 
        if (mdcRepairInfoVo.getEquipmentIdList() == null || mdcRepairInfoVo.getEquipmentIdList().isEmpty()) {
            return null;
        }
        return this.baseMapper.pageList(page, mdcRepairInfoVo);
    }
 
    /**
     * 添加
     *
     * @param mdcRepairInfoVo
     * @return
     */
    @Override
    public boolean addRepair(MdcRepairInfoVo mdcRepairInfoVo) {
        String[] equipmentIdList = mdcRepairInfoVo.getEquipmentIds().split(",");
        List<MdcRepairInfo> repairInfoList = new ArrayList<>();
        for (String equipmentId : equipmentIdList) {
            MdcRepairInfo mdcRepairInfo = new MdcRepairInfo();
            BeanUtils.copyProperties(mdcRepairInfoVo, mdcRepairInfo);
            mdcRepairInfo.setEquipmentId(equipmentId);
            repairInfoList.add(mdcRepairInfo);
        }
        this.saveBatch(repairInfoList);
        return true;
    }
 
    /**
     * 导出
     *
     * @param userId
     * @param mdcRepairInfoVo
     * @return
     */
    @Override
    public ModelAndView exportXls(String userId, MdcRepairInfoVo mdcRepairInfoVo) {
        LambdaQueryWrapper<MdcRepairInfo> queryWrapper = new LambdaQueryWrapper<>();
        List<String> equipmentIds = new ArrayList<>();
        if (StringUtils.isNotEmpty(mdcRepairInfoVo.getParentId()) && StringUtils.isEmpty(mdcRepairInfoVo.getEquipmentId())) {
            if ("2".equals(mdcRepairInfoVo.getTypeTree())) {
                //部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, mdcRepairInfoVo.getParentId());
            } else {
                //产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, mdcRepairInfoVo.getParentId());
            }
        } else if (StringUtils.isNotEmpty(mdcRepairInfoVo.getEquipmentId())) {
            //单台设备信息
            mdcRepairInfoVo.setEquipmentIdList(Collections.singletonList(mdcRepairInfoVo.getEquipmentId()));
        } else {
            //查询用户拥有的所有设备信息
            if ("2".equals(mdcRepairInfoVo.getTypeTree())) {
                //部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, null);
            } else {
                //产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, null);
            }
        }
 
        if (mdcRepairInfoVo.getEquipmentIdList() == null || mdcRepairInfoVo.getEquipmentIdList().isEmpty()) {
            mdcRepairInfoVo.setEquipmentIdList(equipmentIds);
        }
 
        if (mdcRepairInfoVo.getEquipmentIdList() == null || mdcRepairInfoVo.getEquipmentIdList().isEmpty()) {
            return null;
        } else {
            queryWrapper.in(MdcRepairInfo::getEquipmentId, mdcRepairInfoVo.getEquipmentIdList());
        }
        if (StringUtils.isNotEmpty(mdcRepairInfoVo.getEquipmentId())) {
            queryWrapper.eq(MdcRepairInfo::getEquipmentId, mdcRepairInfoVo.getEquipmentId());
        }
        if (StringUtils.isNotEmpty(mdcRepairInfoVo.getStartTime()) && StringUtils.isNotEmpty(mdcRepairInfoVo.getEndTime())) {
            queryWrapper.between(MdcRepairInfo::getTheDate, mdcRepairInfoVo.getStartTime(), mdcRepairInfoVo.getEndTime());
        }
        queryWrapper.orderByDesc(MdcRepairInfo::getTheDate).orderByDesc(MdcRepairInfo::getEquipmentId);
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        List<MdcRepairInfo> repairInfos = this.baseMapper.selectList(queryWrapper);
        // 导出文件名称
        mv.addObject(NormalExcelConstants.FILE_NAME, "设备维修时长列表");
        mv.addObject(NormalExcelConstants.CLASS, MdcRepairInfo.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, repairInfos);
        return mv;
    }
 
    /**
     * 计算MTTR
     *
     * @param month
     */
    @Override
    public void computeMttr(String month) {
        /*
        设备平均修理时间(MTTR) = 总维修时间 / 总维修次数
         */
        List<MdcMttrInfo> mdcMttrInfos = mdcMttrInfoService.list(new LambdaQueryWrapper<MdcMttrInfo>().eq(MdcMttrInfo::getTheDate, month));
        if (mdcMttrInfos != null && !mdcMttrInfos.isEmpty()) {
            for (MdcMttrInfo mdcMttrInfo : mdcMttrInfos) {
                // 总维修时间
                BigDecimal totalRepairLong = this.baseMapper.findTotalRepairLong(month);
                mdcMttrInfo.setTotalRepairLong(totalRepairLong);
                // 总维修次数
                Integer totalRepairCount = this.baseMapper.findTotalRepairCount(month);
                mdcMttrInfo.setTotalRepairCount(totalRepairCount);
                // MTTR
                if (mdcMttrInfo.getTotalRepairCount() != 0) {
                    BigDecimal mttr = mdcMttrInfo.getTotalRepairLong().divide(new BigDecimal(mdcMttrInfo.getTotalDownCount()), 4, RoundingMode.HALF_UP);
                    mdcMttrInfo.setMttr(mttr);
                }
            }
            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);
                    // 总维修时间
                    BigDecimal totalRepairLong = this.baseMapper.findTotalRepairLong(month);
                    mdcMttrInfo.setTotalRepairLong(totalRepairLong);
                    // 总维修次数
                    Integer totalRepairCount = this.baseMapper.findTotalRepairCount(month);
                    mdcMttrInfo.setTotalRepairCount(totalRepairCount);
                    // MTTR
                    if (mdcMttrInfo.getTotalRepairCount() != 0) {
                        BigDecimal mttr = mdcMttrInfo.getTotalRepairLong().divide(new BigDecimal(mdcMttrInfo.getTotalDownCount()), 4, RoundingMode.HALF_UP);
                        mdcMttrInfo.setMttr(mttr);
                    }
                    mmi.add(mdcMttrInfo);
                }
                mdcMttrInfoService.saveBatch(mmi);
            }
        }
    }
}