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;
|
}
|
}
|
|
}
|