huminmin
2026-06-05 24019a8c1d8e78656e85b29ee7be223e0dd253a0
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
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<SampleProgressVo> pageSampleProgress(Page page, SampleProgressDto dto) {
        Page<SampleProgressVo> result = sampleProgressMapper.pageSampleProgress(page, dto);
        // 处理状态名称和进度百分比
        List<SampleProgressVo> 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<String, Object> getStatistics(SampleProgressDto dto) {
        Map<String, Object> 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<SampleProgressVo> page = new Page<>();
            page.setSize(Long.MAX_VALUE);
            Page<SampleProgressVo> result = sampleProgressMapper.pageSampleProgress(page, dto);
 
            // 处理数据
            List<SampleProgressVo> 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<String, Object> getChartData(SampleProgressDto dto) {
        List<Map<String, Object>> chartData = sampleProgressMapper.getChartData(dto);
 
        Map<String, Object> result = new HashMap<>();
        List<String> dates = chartData.stream()
                .map(m -> (String) m.get("date"))
                .collect(Collectors.toList());
        List<Integer> totalCounts = chartData.stream()
                .map(m -> ((Number) m.get("totalCount")).intValue())
                .collect(Collectors.toList());
        List<Integer> 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<Integer, String> 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, "");
    }
 
}