Lius
2025-06-17 d0f024586f38a11662787c42b84e037a1c1be6cd
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
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.MdcPassRate;
import org.jeecg.modules.mdc.mapper.MdcPassRateMapper;
import org.jeecg.modules.mdc.service.IMdcEquipmentService;
import org.jeecg.modules.mdc.service.IMdcPassRateService;
import org.jeecg.modules.mdc.util.DateUtils;
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.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
 
/**
 * @Description: 合格率表
 * @Author: lius
 * @Date: 2023-07-18
 * @Version: V1.0
 */
@Service
public class MdcPassRateServiceImpl extends ServiceImpl<MdcPassRateMapper, MdcPassRate> implements IMdcPassRateService {
 
    @Resource
    private IMdcEquipmentService mdcEquipmentService;
 
    @Override
    public IPage<MdcPassRate> pageList(String userId, Page<MdcPassRate> page, MdcPassRate mdcPassRate, HttpServletRequest req) {
        List<String> equipmentIds = new ArrayList<>();
        if (StringUtils.isNotEmpty(mdcPassRate.getParentId()) && StringUtils.isEmpty(mdcPassRate.getEquipmentId())) {
            if ("2".equals(mdcPassRate.getTypeTree())) {
                //部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, mdcPassRate.getParentId());
            } else {
                //产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, mdcPassRate.getParentId());
            }
        } else if (StringUtils.isNotEmpty(mdcPassRate.getEquipmentId())) {
            //单台设备信息
            mdcPassRate.setMdcSectionIds(Collections.singletonList(mdcPassRate.getEquipmentId()));
        } else {
            //查询用户拥有的所有设备信息
            if ("2".equals(mdcPassRate.getTypeTree())) {
                //部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, null);
            } else {
                //产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, null);
            }
        }
 
        if (mdcPassRate.getMdcSectionIds() == null || mdcPassRate.getMdcSectionIds().isEmpty()) {
            mdcPassRate.setMdcSectionIds(equipmentIds);
        }
 
        if (mdcPassRate.getMdcSectionIds() == null || mdcPassRate.getMdcSectionIds().isEmpty()) {
            return null;
        }
 
        return this.baseMapper.pageList(page, mdcPassRate);
    }
 
    /**
     * 添加数据
     *
     * @param mdcPassRate
     * @return
     */
    @Override
    public boolean addPassRate(MdcPassRate mdcPassRate) {
        boolean result = false;
        String[] equipmentIds = mdcPassRate.getEquipmentIds().split(",");
        for (String equipmentId : equipmentIds) {
            MdcEquipment mdcEquipment = mdcEquipmentService.findEquipmentNameByEquipmentId(equipmentId);
            MdcPassRate passRate = new MdcPassRate();
            BeanUtils.copyProperties(mdcPassRate, passRate);
            passRate.setEquipmentId(mdcEquipment.getEquipmentId());
            passRate.setEquipmentName(mdcEquipment.getEquipmentName());
            if (passRate.getProcessQuantity() == null || passRate.getProcessQuantity() == 0) {
                passRate.setPassRate(new BigDecimal("0"));
            } else {
                if (passRate.getUnqualifiedQuantity() == null || passRate.getUnqualifiedQuantity() == 0) {
                    passRate.setPassRate(new BigDecimal("1"));
                } else {
                    passRate.setPassRate(new BigDecimal("1").subtract(new BigDecimal(passRate.getUnqualifiedQuantity()).divide(new BigDecimal(passRate.getProcessQuantity()), 4, BigDecimal.ROUND_HALF_UP)));
                }
            }
            boolean b = super.save(passRate);
            if (b) {
                result = true;
            }
        }
        return result;
    }
 
    /**
     * 编辑数据
     *
     * @param mdcPassRate
     * @return
     */
    @Override
    public boolean updatePassRate(MdcPassRate mdcPassRate) {
        if (mdcPassRate.getProcessQuantity() == null || mdcPassRate.getProcessQuantity() == 0) {
            mdcPassRate.setPassRate(new BigDecimal("0"));
        } else {
            if (mdcPassRate.getUnqualifiedQuantity() == null || mdcPassRate.getUnqualifiedQuantity() == 0) {
                mdcPassRate.setPassRate(new BigDecimal("1"));
            } else {
                mdcPassRate.setPassRate(new BigDecimal("1").subtract(new BigDecimal(mdcPassRate.getUnqualifiedQuantity()).divide(new BigDecimal(mdcPassRate.getProcessQuantity()), 4, BigDecimal.ROUND_HALF_UP)));
            }
        }
        return super.updateById(mdcPassRate);
    }
 
    /**
     * 导出
     *
     * @param userId
     * @param mdcPassRate
     * @return
     */
    @Override
    public ModelAndView exportXls(String userId, MdcPassRate mdcPassRate) {
        LambdaQueryWrapper<MdcPassRate> queryWrapper = new LambdaQueryWrapper<>();
        List<String> equipmentIds = new ArrayList<>();
        if (StringUtils.isNotEmpty(mdcPassRate.getParentId()) && StringUtils.isEmpty(mdcPassRate.getEquipmentId())) {
            if ("2".equals(mdcPassRate.getTypeTree())) {
                //部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, mdcPassRate.getParentId());
            } else {
                //产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, mdcPassRate.getParentId());
            }
        } else if (StringUtils.isNotEmpty(mdcPassRate.getEquipmentId())) {
            //单台设备信息
            mdcPassRate.setMdcSectionIds(Collections.singletonList(mdcPassRate.getEquipmentId()));
        } else {
            //查询用户所拥有的所有设备信息
            if ("2".equals(mdcPassRate.getTypeTree())) {
                //部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, null);
            } else {
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, null);
            }
        }
        if (mdcPassRate.getMdcSectionIds() == null || mdcPassRate.getMdcSectionIds().isEmpty()) {
            mdcPassRate.setMdcSectionIds(equipmentIds);
        }
        if (mdcPassRate.getMdcSectionIds() == null || mdcPassRate.getMdcSectionIds().isEmpty()) {
            return null;
        } else {
            queryWrapper.in(MdcPassRate::getEquipmentId, mdcPassRate.getMdcSectionIds());
        }
        if (StringUtils.isNotEmpty(mdcPassRate.getEquipmentId())) {
            queryWrapper.like(MdcPassRate::getEquipmentId, mdcPassRate.getEquipmentId());
        }
        if (StringUtils.isNotEmpty(mdcPassRate.getEquipmentName())) {
            queryWrapper.like(MdcPassRate::getEquipmentName, mdcPassRate.getEquipmentName());
        }
        if (StringUtils.isNotEmpty(mdcPassRate.getStartTime()) && StringUtils.isNotEmpty(mdcPassRate.getEndTime())) {
            queryWrapper.between(MdcPassRate::getEfficientDate, mdcPassRate.getStartTime(), mdcPassRate.getEndTime());
        }
        queryWrapper.orderByAsc(MdcPassRate::getEquipmentName).orderByDesc(MdcPassRate::getEfficientDate);
        // Step.2 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        List<MdcPassRate> mdcPassRates = this.baseMapper.selectList(queryWrapper);
        // 导出文件名称
        mv.addObject(NormalExcelConstants.FILE_NAME, "合格率列表");
        mv.addObject(NormalExcelConstants.CLASS, MdcPassRate.class);
        //获取当前登录用户
        //update-begin---author:wangshuai ---date:20211227  for:[JTC-116]导出人写死了------------
        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, mdcPassRates);
        return mv;
    }
 
    @Override
    public BigDecimal findProcessQuantity(String equipmentId, String validDate) {
        String startTime = DateUtils.format(DateUtils.toDate(validDate + "-01 00:00:00", DateUtils.STR_DATE_TIME_SMALL), DateUtils.STR_DATE_TIME_SMALL);
        LocalDate localDate = LocalDate.of(Integer.parseInt(validDate.split("-")[0]), Integer.parseInt(validDate.split("-")[1]), Integer.parseInt("01")).plusMonths(1);
        String endTime = DateUtils.format(DateUtils.toDate(localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + " 00:00:00", DateUtils.STR_DATE_TIME_SMALL), DateUtils.STR_DATE_TIME_SMALL);
        Integer result = this.baseMapper.findProcessQuantity(equipmentId, startTime, endTime);
        return result == null ? new BigDecimal("0") : new BigDecimal(result);
    }
 
    @Override
    public BigDecimal findUnqualifiedQuantity(String equipmentId, String validDate) {
        String startTime = DateUtils.format(DateUtils.toDate(validDate + "-01 00:00:00", DateUtils.STR_DATE_TIME_SMALL), DateUtils.STR_DATE_TIME_SMALL);
        LocalDate localDate = LocalDate.of(Integer.parseInt(validDate.split("-")[0]), Integer.parseInt(validDate.split("-")[1]), Integer.parseInt("01")).plusMonths(1);
        String endTime = DateUtils.format(DateUtils.toDate(localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + " 00:00:00", DateUtils.STR_DATE_TIME_SMALL), DateUtils.STR_DATE_TIME_SMALL);
        Integer result = this.baseMapper.findUnqualifiedQuantity(equipmentId, startTime, endTime);
        return result == null ? new BigDecimal("0") : new BigDecimal(result);
    }
}