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 getInspectStatistics() { return qualityInspectMapper.getInspectStatistics(); } @Override public List getPassRateStatistics() { return qualityInspectMapper.getPassRateStatistics(); } @Override public List getMonthlyPassRateStatistics(String year) { if (StringUtils.isEmpty(year)) { return new ArrayList<>(); } List flatData = qualityInspectMapper.getMonthlyPassRateStatistics(year); // 按月份分组,并保持顺序 Map> groupedByMonth = flatData.stream() .collect(Collectors.groupingBy(QualityMonthlyPassRateDto::getMonth, LinkedHashMap::new, Collectors.toList())); List 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 getYearlyPassRateStatistics(String year) { if (StringUtils.isEmpty(year)) { return new ArrayList<>(); } return qualityInspectMapper.getYearlyPassRateStatistics(year); } @Override public List 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 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 getMonthlyPassRateWithComparison(String year, Integer month) { if (StringUtils.isEmpty(year) || month == null || month < 1 || month > 12) { return new ArrayList<>(); } // 获取当前年月的合格率数据 List 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 lastMonthData = qualityInspectMapper.getMonthlyPassRateStatistics(lastMonthYear); // 获取去年同月数据 List lastYearData = qualityInspectMapper.getMonthlyPassRateStatistics(lastYear); // 月份名称映射 String[] monthNames = {"一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"}; String currentMonthName = monthNames[month - 1]; String lastMonthName = monthNames[lastMonth - 1]; // 构建结果 List 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; } }