package com.ruoyi.report.service.impl; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.excel.EasyExcel; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.ruoyi.report.dto.SampleProgressDto; import com.ruoyi.report.mapper.SampleProgressMapper; import com.ruoyi.report.service.SampleProgressService; import com.ruoyi.report.vo.SampleProgressVo; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.*; import java.util.stream.Collectors; /** * 样品进度报表服务实现 */ @Service @AllArgsConstructor public class SampleProgressServiceImpl implements SampleProgressService { private SampleProgressMapper sampleProgressMapper; @Override public Page pageSampleProgress(Page page, SampleProgressDto dto) { Page result = sampleProgressMapper.pageSampleProgress(page, dto); // 处理状态名称和进度百分比 List records = result.getRecords(); for (SampleProgressVo vo : records) { vo.setInsStateName(formatInsState(vo.getInsState())); if (vo.getTotalItems() != null && vo.getTotalItems() > 0) { int finished = vo.getFinishedItems() != null ? vo.getFinishedItems() : 0; vo.setProgressPercent((finished * 100.0) / vo.getTotalItems()); } } return result; } @Override public Map getStatistics(SampleProgressDto dto) { Map statistics = sampleProgressMapper.getStatistics(dto); if (statistics == null) { statistics = new HashMap<>(); statistics.put("waitInspection", 0); statistics.put("inspecting", 0); statistics.put("waitAudit", 0); statistics.put("finished", 0); } return statistics; } @Override public void exportSampleProgress(SampleProgressDto dto, HttpServletResponse response) { try { // 查询全部数据 Page page = new Page<>(); page.setSize(Long.MAX_VALUE); Page result = sampleProgressMapper.pageSampleProgress(page, dto); // 处理数据 List records = result.getRecords(); for (SampleProgressVo vo : records) { vo.setInsStateName(formatInsState(vo.getInsState())); if (vo.getTotalItems() != null && vo.getTotalItems() > 0) { int finished = vo.getFinishedItems() != null ? vo.getFinishedItems() : 0; vo.setProgressPercent((finished * 100.0) / vo.getTotalItems()); } } // 设置响应头 response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("utf-8"); String fileName = "样品进度报表_" + DateUtil.format(new Date(), "yyyyMMddHHmmss"); response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx"); // 使用EasyExcel导出 EasyExcel.write(response.getOutputStream(), SampleProgressVo.class) .sheet("样品进度") .doWrite(records); } catch (IOException e) { e.printStackTrace(); } } @Override public Map getChartData(SampleProgressDto dto) { List> chartData = sampleProgressMapper.getChartData(dto); Map result = new HashMap<>(); List dates = chartData.stream() .map(m -> (String) m.get("date")) .collect(Collectors.toList()); List totalCounts = chartData.stream() .map(m -> ((Number) m.get("totalCount")).intValue()) .collect(Collectors.toList()); List finishedCounts = chartData.stream() .map(m -> ((Number) m.get("finishedCount")).intValue()) .collect(Collectors.toList()); result.put("dates", dates); result.put("totalCounts", totalCounts); result.put("finishedCounts", finishedCounts); return result; } /** * 格式化检测状态 */ private String formatInsState(Integer val) { if (val == null) return ""; Map map = new HashMap<>(); map.put(0, "待检"); map.put(1, "检验中"); map.put(2, "已检验"); map.put(3, "待审核"); map.put(4, "审核未通过"); map.put(5, "审核通过"); return map.getOrDefault(val, ""); } }