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
package com.ruoyi.report.service.impl;
 
import cn.hutool.core.date.DateUtil;
import com.ruoyi.report.dto.WorkStatisticsDto;
import com.ruoyi.report.mapper.WorkStatisticsMapper;
import com.ruoyi.report.service.WorkStatisticsService;
import com.ruoyi.report.vo.WorkStatisticsVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
 
import java.util.*;
 
/**
 * 工作统计服务实现
 */
@Service
@AllArgsConstructor
public class WorkStatisticsServiceImpl implements WorkStatisticsService {
 
    private WorkStatisticsMapper workStatisticsMapper;
 
    @Override
    public List<WorkStatisticsVo> getByUser(WorkStatisticsDto dto) {
        // 处理时间类型
        processDateType(dto);
 
        List<WorkStatisticsVo> list = workStatisticsMapper.getByUser(dto);
 
        // 计算及时率
        for (WorkStatisticsVo vo : list) {
            int timely = vo.getTimelyCount() != null ? vo.getTimelyCount() : 0;
            int overdue = vo.getOverdueCount() != null ? vo.getOverdueCount() : 0;
            int total = timely + overdue;
            if (total > 0) {
                vo.setTimelyRate((timely * 100.0) / total);
            } else {
                vo.setTimelyRate(0.0);
            }
        }
 
        return list;
    }
 
    @Override
    public List<Map<String, Object>> getTimelyRate(WorkStatisticsDto dto) {
        processDateType(dto);
        return workStatisticsMapper.getTimelyRate(dto);
    }
 
    @Override
    public Map<String, Object> getTrend(WorkStatisticsDto dto) {
        processDateType(dto);
 
        List<Map<String, Object>> trendData = workStatisticsMapper.getTrend(dto);
 
        Map<String, Object> result = new HashMap<>();
        List<String> dates = new ArrayList<>();
        List<Integer> sampleCounts = new ArrayList<>();
        List<Integer> itemCounts = new ArrayList<>();
 
        for (Map<String, Object> item : trendData) {
            dates.add((String) item.get("date"));
            sampleCounts.add(((Number) item.get("sampleCount")).intValue());
            itemCounts.add(((Number) item.get("itemCount")).intValue());
        }
 
        result.put("dates", dates);
        result.put("sampleCounts", sampleCounts);
        result.put("itemCounts", itemCounts);
        return result;
    }
 
    /**
     * 处理时间类型
     */
    private void processDateType(WorkStatisticsDto 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;
        }
    }
 
}