zss
2024-04-17 ad5fa78540fdd7b5470072e0ead2b650afdca2fd
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.yuanchu.mom.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuanchu.mom.dto.CostStatisticsDto;
import com.yuanchu.mom.mapper.InsOrderMapper;
import com.yuanchu.mom.mapper.InsProductMapper;
import com.yuanchu.mom.mapper.InsProductUserMapper;
import com.yuanchu.mom.pojo.InsOrder;
import com.yuanchu.mom.pojo.InsProduct;
import com.yuanchu.mom.pojo.InsProductUser;
import com.yuanchu.mom.service.ReportService;
import com.yuanchu.mom.utils.QueryWrappers;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Service
@AllArgsConstructor
public class ReportServiceImpl implements ReportService {
 
    private InsOrderMapper insOrderMapper;
    private InsProductUserMapper insProductUserMapper;
    private InsProductMapper insProductMapper;
 
    //每日业务统计
    @Override
    public Map<String, Object> businessStatisticsByDay() {
        Map<String, Object> map = new HashMap<>();
        /*任务接收*/
        //今日任务接收
        Long receive = insOrderMapper.selectCount(Wrappers.<InsOrder>lambdaQuery().eq(InsOrder::getState, 1).apply("DATE(create_time) = CURDATE()"));
        map.put("RECEIVE", receive);
        //昨日任务接收
        Long received = insOrderMapper.selectCount(Wrappers.<InsOrder>lambdaQuery().eq(InsOrder::getState, 1).apply("DATE(create_time) = CURDATE() - INTERVAL 1 DAY"));
        received = received == 0 ? 1 : received;
        //比例=(今天-昨天)/昨天
        BigDecimal ratio = new BigDecimal(receive - received).divide(new BigDecimal(received), 2, BigDecimal.ROUND_HALF_UP);
        map.put("RECEIVE_RATIO", ratio);
 
        /*任务已完成*/
        //今日任务完成
        Long finishe = insOrderMapper.selectCount(Wrappers.<InsOrder>lambdaQuery().eq(InsOrder::getState, 4).apply("DATE(create_time) = CURDATE()"));
        map.put("FINISHE", finishe);
        //昨日任务完成
        Long finished = insOrderMapper.selectCount(Wrappers.<InsOrder>lambdaQuery().eq(InsOrder::getState, 4).apply("DATE(create_time) = CURDATE() - INTERVAL 1 DAY"));
        finished = finished == 0 ? 1 : finished;
        //任务完成比例=(今天-昨天)/昨天
        BigDecimal finishedRatio = new BigDecimal(finishe - finished).divide(new BigDecimal(finished), 2, BigDecimal.ROUND_HALF_UP);
        map.put("FINISHE_RATIO", finishedRatio);
 
        /*任务剩余*/
        //今日任务剩余
        long surplus = receive - finishe;
        map.put("SURPLUS", surplus);
        //昨日任务剩余
        long surplused = received - finished;
        surplused = surplused == 0 ? 1 : surplused;
        //剩余比例=(今天-昨天)/昨天
        BigDecimal surplusRatio = new BigDecimal(surplus - surplused).divide(new BigDecimal(surplused), 2, BigDecimal.ROUND_HALF_UP);
        map.put("SURPLUS_RATIO", surplusRatio);
 
        /*检测费用*/
        //今日检测费用
        QueryWrapper<CostStatisticsDto> costStatisticsDtoQueryWrappers = new QueryWrapper<>();
        costStatisticsDtoQueryWrappers.eq("create_time", LocalDateTime.now());
        IPage<CostStatisticsDto> page = new Page<>();
        page.setSize(-1);
        page.setCurrent(-1);
        IPage<CostStatisticsDto> iPage = insOrderMapper.selectCostStatistics(page, costStatisticsDtoQueryWrappers);
        BigDecimal price = BigDecimal.ZERO;
        for (CostStatisticsDto record : iPage.getRecords()) {
            price.add(record.getPrice());
        }
        map.put("PRICE", price);
        //昨日检测费用
        QueryWrapper<CostStatisticsDto> costWrappers = new QueryWrapper<>();
        costWrappers.eq("create_time", LocalDateTime.now().minusDays(1));
        IPage<CostStatisticsDto> dtoIPage = insOrderMapper.selectCostStatistics(page, costWrappers);
        BigDecimal priced = BigDecimal.ZERO;
        for (CostStatisticsDto record : dtoIPage.getRecords()) {
            priced.add(record.getPrice());
        }
        priced = priced.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ONE : priced;
        //比例=(今日-昨日)/昨日
        BigDecimal priceRatio = (price.subtract(priced)).divide(priced, 2, BigDecimal.ROUND_HALF_UP);
        map.put("PRICE_RATIO", priceRatio);
 
        /*检测工时*/
        //今日检测工时
        Double cost = 0.0;
        for (CostStatisticsDto record : iPage.getRecords()) {
            cost += record.getCost();
        }
        map.put("COST", cost);
        //昨日检测工时
        Double costed = 0.0;
        for (CostStatisticsDto record : dtoIPage.getRecords()) {
            costed += record.getCost();
        }
        costed = costed == 0 ? 1 : costed;
        //比例=(今日-昨日)/昨日
        BigDecimal costRatio = new BigDecimal(cost - costed).divide(new BigDecimal(costed), 2, BigDecimal.ROUND_HALF_UP);
        map.put("COST_RATIO", costRatio);
 
 
        /*检测人员*/
        //今日检测人员
        Long person = insProductUserMapper.selectCount(Wrappers.<InsProductUser>lambdaQuery().apply("DATE(create_time) = CURDATE()"));
        map.put("PERSON", person);
        //昨日检测人员
        Long persons = insProductUserMapper.selectCount(Wrappers.<InsProductUser>lambdaQuery().apply("DATE(create_time) = CURDATE() - INTERVAL 1 DAY"));
        persons = persons == 0 ? 1 : persons;
        //比例=(今天-昨天)/昨天
        BigDecimal personRatio = new BigDecimal(person - persons).divide(new BigDecimal(persons), 2, BigDecimal.ROUND_HALF_UP);
        map.put("PERSON_RATIO", personRatio);
 
        /*近十日任务接收量与完成量*/
        //获取近十日的横坐标
        LocalDate currentDate = LocalDate.now();
        List<LocalDate> lastTenDays = new ArrayList<>();
        List<Long> receTenDays = new ArrayList<>();
        List<Long> finTenDays = new ArrayList<>();
        for (int i = 9; i > -1; i--) {
            lastTenDays.add(currentDate.minusDays(i));
            receTenDays.add(insOrderMapper.selectCount(Wrappers.<InsOrder>lambdaQuery().eq(InsOrder::getState, 1).apply("DATE(create_time) = CURDATE() - INTERVAL " + i + " DAY")));
            finTenDays.add(insOrderMapper.selectCount(Wrappers.<InsOrder>lambdaQuery().eq(InsOrder::getState, 4).apply("DATE(create_time) = CURDATE() - INTERVAL " + i + " DAY")));
        }
        map.put("DAYS", lastTenDays);
        map.put("RECETENDAYS", receTenDays);
        map.put("FINISHTENDAYS", finTenDays);
        return map;
    }
 
    //检测项目统计
    @Override
    public Map<String, Object> testProductByDay() {
        Map<String, Object> map = new HashMap<>();
        /*项目接收*/
        //今日项目接收量
        Long receive = insProductMapper.selectCount(Wrappers.<InsProduct>lambdaQuery().eq(InsProduct::getState, 1).apply("DATE(create_time) = CURDATE()"));
        /*项目完成*/
        /*项目剩余*/
        /*今日项目合格率*/
        /*今日项目完成率*/
        /*今日项目延期率*/
        /*近十日的项目接收量与完成量*/
        return map;
    }
}