wjli
2024-04-08 fc96e4d7fc20dbec5d2d96c6e8c557c1cc3212d3
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
package cn.exrick.xboot.your.schedulings;
import cn.exrick.xboot.your.entity.Area;
import cn.exrick.xboot.your.entity.AreaSection;
import cn.exrick.xboot.your.entity.Customer;
import cn.exrick.xboot.your.entity.OrderTask;
import cn.exrick.xboot.your.service.IAreaSectionService;
import cn.exrick.xboot.your.service.IAreaService;
import cn.exrick.xboot.your.service.ICustomerService;
import cn.exrick.xboot.your.service.IOrderTaskService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
 
//大屏配送分析
@Component
public class StatisticPc2ScheduleImpl {
 
    @Autowired
    private IAreaService iAreaService;
 
    @Autowired
    private IAreaSectionService iAreaSectionService;
 
    @Autowired
    private ICustomerService iCustomerService;
 
    @Autowired
    private IOrderTaskService iOrderTaskService;
 
    @Scheduled(cron="0 0 20 * * ?")//每晚8点执行
    public void execute(){
        List<Area> list = iAreaService.list();
        for(Area area:list){
            QueryWrapper<Customer> wrapper = new QueryWrapper<>();
            wrapper.eq("area_id",area.getId());
            int count = iCustomerService.count(wrapper);
            area.setCustomerSum(count);
            iAreaService.saveOrUpdate(area);
        }
 
        List<AreaSection> list2 = iAreaSectionService.list();
        for(AreaSection areaSection:list2){
            String id = areaSection.getId();
            QueryWrapper<Customer> wrapper = new QueryWrapper<>();
            wrapper.eq("area_section_id",id);
            int count = iCustomerService.count(wrapper);
            areaSection.setCustomerNum(count);
 
            List<OrderTask> orderTasks = iOrderTaskService.sumTime(id);
            int sum = 0;
            List<Integer> listTime = new ArrayList<>();
            for(OrderTask orderTask:orderTasks){
                sum+=orderTask.getTime();
                listTime.add(orderTask.getTime());
            }
 
            Collections.sort(listTime);
 
            double v = (sum * 1.0) / (1000 * 60 * 60) / listTime.size();
            areaSection.setAvgTime((double) Math.round(v * 10) / 10);
            if(listTime.size()>0){
                double v1 = (listTime.get(0) * 1.0) / (1000 * 60 * 60);
                areaSection.setMinTime((double) Math.round(v1 * 10) / 10);
 
                double v2 = (listTime.get(listTime.size()-1) * 1.0) / (1000 * 60 * 60);
                areaSection.setMaxTime((double) Math.round(v2 * 10) / 10);
            }
 
            double variance = Variance(listTime);
            areaSection.setStability(variance);
            iAreaSectionService.saveOrUpdate(areaSection);
        }
 
    }
 
    public double Variance(List<Integer> x) {
        List<Double> y = new ArrayList<>();
        for(Integer s:x){
            y.add(trans((s*1.0)/(3600*1000)));
        }
 
        int m = x.size();
        double sum = 0;
        for (int i = 0; i < m; i++) {
            sum += y.get(i);
        }
        double dAve = sum / m;
        double dVar = 0;
        for (int i = 0; i < m; i++) {
            dVar += (y.get(i) - dAve) * (y.get(i) - dAve);
        }
        double v = dVar / m;
        return trans2(v);
    }
 
    public double trans(double v2){
        return (double) Math.round(v2 * 10) / 10;
    }
 
    public double trans2(double v2){
        return (double) Math.round(v2 * 100) / 100;
    }
}