huminmin
9 天以前 4c6168cf60426dc1e9c95d575c5efc66bf8d9af9
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
package com.ruoyi.quality.service.impl;
 
import com.ruoyi.basic.service.IProductModelService;
import com.ruoyi.basic.service.IProductService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.production.service.ProductOrderService;
import com.ruoyi.quality.dto.*;
import com.ruoyi.quality.mapper.QualityInspectMapper;
import com.ruoyi.quality.service.QualityReportService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
 
@Service
public class QualityReportServiceImpl implements QualityReportService {
 
    @Autowired
    private QualityInspectMapper qualityInspectMapper;
 
    @Override
    public List<QualityInspectStatDto> getInspectStatistics() {
        return qualityInspectMapper.getInspectStatistics();
    }
 
    @Override
    public List<QualityPassRateDto> getPassRateStatistics() {
        return qualityInspectMapper.getPassRateStatistics();
    }
 
    @Override
    public List<QualityMonthlyPassRateWrapperDto> getMonthlyPassRateStatistics(String year) {
        if (StringUtils.isEmpty(year)) {
            return new ArrayList<>();
        }
        List<QualityMonthlyPassRateDto> flatData = qualityInspectMapper.getMonthlyPassRateStatistics(year);
 
        // 按月份分组,并保持顺序
        Map<String, List<QualityMonthlyPassRateDto>> groupedByMonth = flatData.stream()
                .collect(Collectors.groupingBy(QualityMonthlyPassRateDto::getMonth, LinkedHashMap::new, Collectors.toList()));
 
        List<QualityMonthlyPassRateWrapperDto> result = new ArrayList<>();
 
        groupedByMonth.forEach((month, dtos) -> {
            QualityMonthlyPassRateWrapperDto wrapper = new QualityMonthlyPassRateWrapperDto();
            wrapper.setMonth(month);
 
            for (QualityMonthlyPassRateDto dto : dtos) {
                QualityPassRateDto passRateDto = new QualityPassRateDto();
                BeanUtils.copyProperties(dto, passRateDto);
 
                if (dto.getModelType() == 0) {
                    wrapper.setRawMaterial(passRateDto);
                } else if (dto.getModelType() == 1) {
                    wrapper.setProcess(passRateDto);
                } else if (dto.getModelType() == 2) {
                    wrapper.setOutgoing(passRateDto);
                }
            }
            result.add(wrapper);
        });
 
        return result;
    }
 
    @Override
    public List<QualityPassRateDto> getYearlyPassRateStatistics(String year) {
        if (StringUtils.isEmpty(year)) {
            return new ArrayList<>();
        }
        return qualityInspectMapper.getYearlyPassRateStatistics(year);
    }
 
    @Override
    public List<QualityMonthlyDetailDto> getMonthlyCompletionDetails(String year) {
        if (StringUtils.isEmpty(year)) {
            return new ArrayList<>();
        }
        return qualityInspectMapper.getMonthlyCompletionDetails(year);
    }
 
    @Override
    public QualityTopParameterDto getTopParameters(Integer modelType) {
        if (modelType == null) {
            return new QualityTopParameterDto();
        }
        List<QualityParameterStatDto> list = qualityInspectMapper.getTopParameters(modelType);
 
        BigDecimal total = list.stream()
                .map(QualityParameterStatDto::getCount)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
 
        QualityTopParameterDto result = new QualityTopParameterDto();
        result.setTotalCount(total);
        result.setList(list);
 
        return result;
    }
 
    @Override
    public List<QualityPassRateDto> getMonthlyPassRateWithComparison(String year, Integer month) {
        if (StringUtils.isEmpty(year) || month == null || month < 1 || month > 12) {
            return new ArrayList<>();
        }
 
        // 获取当前年月的合格率数据
        List<QualityMonthlyPassRateDto> currentMonthData = qualityInspectMapper.getMonthlyPassRateStatistics(year);
 
        // 计算上月
        int lastMonth = month - 1;
        String lastMonthYear = year;
        if (lastMonth == 0) {
            lastMonth = 12;
            lastMonthYear = String.valueOf(Integer.parseInt(year) - 1);
        }
 
        // 计算去年同月
        String lastYear = String.valueOf(Integer.parseInt(year) - 1);
 
        // 获取上月数据
        List<QualityMonthlyPassRateDto> lastMonthData = qualityInspectMapper.getMonthlyPassRateStatistics(lastMonthYear);
 
        // 获取去年同月数据
        List<QualityMonthlyPassRateDto> lastYearData = qualityInspectMapper.getMonthlyPassRateStatistics(lastYear);
 
        // 月份名称映射
        String[] monthNames = {"一月", "二月", "三月", "四月", "五月", "六月",
                               "七月", "八月", "九月", "十月", "十一月", "十二月"};
        String currentMonthName = monthNames[month - 1];
        String lastMonthName = monthNames[lastMonth - 1];
 
        // 构建结果
        List<QualityPassRateDto> result = new ArrayList<>();
 
        for (int modelType = 0; modelType <= 2; modelType++) {
            final int type = modelType;
 
            QualityPassRateDto dto = new QualityPassRateDto();
            dto.setModelType(modelType);
 
            // 当前月数据
            QualityMonthlyPassRateDto currentDto = currentMonthData.stream()
                    .filter(d -> d.getMonth().equals(currentMonthName) && d.getModelType() == type)
                    .findFirst()
                    .orElse(null);
 
            if (currentDto != null) {
                dto.setTotalCount(currentDto.getTotalCount());
                dto.setCompletedCount(currentDto.getCompletedCount());
                dto.setQualifiedCount(currentDto.getQualifiedCount());
                dto.setUnqualifiedCount(currentDto.getUnqualifiedCount());
                dto.setCompletionRate(currentDto.getCompletionRate());
                dto.setPassRate(currentDto.getPassRate());
            } else {
                dto.setTotalCount(BigDecimal.ZERO);
                dto.setCompletedCount(BigDecimal.ZERO);
                dto.setQualifiedCount(BigDecimal.ZERO);
                dto.setUnqualifiedCount(BigDecimal.ZERO);
                dto.setCompletionRate(BigDecimal.ZERO);
                dto.setPassRate(BigDecimal.ZERO);
            }
 
            // 上月数据
            QualityMonthlyPassRateDto lastMonthDto = lastMonthData.stream()
                    .filter(d -> d.getMonth().equals(lastMonthName) && d.getModelType() == type)
                    .findFirst()
                    .orElse(null);
 
            BigDecimal lastMonthPassRate = lastMonthDto != null ? lastMonthDto.getPassRate() : BigDecimal.ZERO;
            dto.setLastMonthPassRate(lastMonthPassRate);
 
            // 去年同月数据
            QualityMonthlyPassRateDto lastYearDto = lastYearData.stream()
                    .filter(d -> d.getMonth().equals(currentMonthName) && d.getModelType() == type)
                    .findFirst()
                    .orElse(null);
 
            BigDecimal lastYearPassRate = lastYearDto != null ? lastYearDto.getPassRate() : BigDecimal.ZERO;
            dto.setLastYearPassRate(lastYearPassRate);
 
            // 计算环比变化
            BigDecimal currentPassRate = dto.getPassRate() != null ? dto.getPassRate() : BigDecimal.ZERO;
            if (lastMonthPassRate != null && lastMonthPassRate.compareTo(BigDecimal.ZERO) > 0) {
                BigDecimal momChange = currentPassRate.subtract(lastMonthPassRate);
                dto.setMomChange(momChange);
                dto.setMomTrend(momChange.compareTo(BigDecimal.ZERO));
            } else {
                dto.setMomChange(BigDecimal.ZERO);
                dto.setMomTrend(0);
            }
 
            // 计算同比变化
            if (lastYearPassRate != null && lastYearPassRate.compareTo(BigDecimal.ZERO) > 0) {
                BigDecimal yoyChange = currentPassRate.subtract(lastYearPassRate);
                dto.setYoyChange(yoyChange);
                dto.setYoyTrend(yoyChange.compareTo(BigDecimal.ZERO));
            } else {
                dto.setYoyChange(BigDecimal.ZERO);
                dto.setYoyTrend(0);
            }
 
            result.add(dto);
        }
 
        return result;
    }
}