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
package com.ruoyi.report.service.impl;
 
import cn.hutool.core.date.DateUtil;
import com.ruoyi.report.dto.PassRateDto;
import com.ruoyi.report.mapper.PassRateMapper;
import com.ruoyi.report.service.PassRateService;
import com.ruoyi.report.vo.ParetoVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
 
/**
 * 合格率统计服务实现
 */
@Service
@AllArgsConstructor
public class PassRateServiceImpl implements PassRateService {
 
    private PassRateMapper passRateMapper;
 
    @Override
    public List<Map<String, Object>> getRawMaterialPassRate(PassRateDto dto) {
        processDateType(dto);
        return passRateMapper.getRawMaterialPassRate(dto);
    }
 
    @Override
    public List<Map<String, Object>> getSupplierUnqualified(PassRateDto dto) {
        processDateType(dto);
        return passRateMapper.getSupplierUnqualified(dto);
    }
 
    @Override
    public ParetoVo getPareto(PassRateDto dto) {
        processDateType(dto);
 
        List<Map<String, Object>> stats = passRateMapper.getUnqualifiedItemStats(dto);
 
        ParetoVo vo = new ParetoVo();
        List<String> categories = new ArrayList<>();
        List<Integer> values = new ArrayList<>();
        List<Double> cumulativePercent = new ArrayList<>();
 
        if (stats.isEmpty()) {
            vo.setCategories(categories);
            vo.setValues(values);
            vo.setCumulativePercent(cumulativePercent);
            return vo;
        }
 
        // 计算总数
        int total = stats.stream()
                .mapToInt(m -> ((Number) m.get("unqualifiedCount")).intValue())
                .sum();
 
        // 计算累计百分比
        BigDecimal cumulative = BigDecimal.ZERO;
        for (Map<String, Object> stat : stats) {
            categories.add((String) stat.get("itemName"));
            int count = ((Number) stat.get("unqualifiedCount")).intValue();
            values.add(count);
 
            cumulative = cumulative.add(new BigDecimal(count));
            double percent = cumulative.divide(new BigDecimal(total), 4, RoundingMode.HALF_UP)
                    .multiply(new BigDecimal(100))
                    .setScale(2, RoundingMode.HALF_UP)
                    .doubleValue();
            cumulativePercent.add(percent);
        }
 
        vo.setCategories(categories);
        vo.setValues(values);
        vo.setCumulativePercent(cumulativePercent);
        return vo;
    }
 
    @Override
    public List<Map<String, Object>> getProcessPassRate(PassRateDto dto) {
        processDateType(dto);
        return passRateMapper.getProcessPassRate(dto);
    }
 
    @Override
    public List<Map<String, Object>> getMachineUnqualified(PassRateDto dto) {
        processDateType(dto);
        return passRateMapper.getMachineUnqualified(dto);
    }
 
    /**
     * 处理时间类型
     */
    private void processDateType(PassRateDto dto) {
        if (dto.getStartTime() != null && dto.getEndTime() != null) {
            return;
        }
 
        String dateType = dto.getDateType();
        if (dateType == null || dateType.isEmpty()) {
            dateType = "2"; // 默认本月
        }
 
        Date now = new Date();
        switch (dateType) {
            case "1": // 本周
                dto.setStartTime(DateUtil.format(DateUtil.beginOfWeek(now), "yyyy-MM-dd HH:mm:ss"));
                dto.setEndTime(DateUtil.format(DateUtil.endOfWeek(now), "yyyy-MM-dd HH:mm:ss"));
                break;
            case "2": // 本月
                dto.setStartTime(DateUtil.format(DateUtil.beginOfMonth(now), "yyyy-MM-dd HH:mm:ss"));
                dto.setEndTime(DateUtil.format(DateUtil.endOfMonth(now), "yyyy-MM-dd HH:mm:ss"));
                break;
            case "3": // 本年
                dto.setStartTime(DateUtil.format(DateUtil.beginOfYear(now), "yyyy-MM-dd HH:mm:ss"));
                dto.setEndTime(DateUtil.format(DateUtil.endOfYear(now), "yyyy-MM-dd HH:mm:ss"));
                break;
        }
    }
 
}