wang-hao-jie
2021-12-27 fb53a9997de491c3d0a30ed333a6bd3b24877b82
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
package cn.exrick.xboot.your.schedulings;
import cn.exrick.xboot.core.entity.User;
import cn.exrick.xboot.core.service.UserService;
import cn.exrick.xboot.your.entity.*;
import cn.exrick.xboot.your.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.elasticsearch.cluster.ClusterState;
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.List;
//客户端首页数据统计
@Component
public class StatisticScheduleImpl {
 
    @Autowired
    private IUserStatisticService iUserStatisticService;
 
    @Autowired
    private UserService userService;
 
    @Autowired
    private IDrivingRecordService iDrivingRecordService;
 
    @Autowired
    private IOrderTaskService iOrderTaskService;
 
    @Autowired
    private IAreaService iAreaService;
 
    @Autowired
    private ICustomerService iCustomerService;
 
    @Autowired
    private IAlarmService iAlarmService;
 
    @Scheduled(cron="0 0 1 * * ?")//每晚凌晨1点执行
    public void execute(){
        List<User> all = userService.findAll();
        for(User user:all){
            Integer type2 = user.getType2();
            if(type2!=null){
                QueryWrapper<UserStatistic> wrapper = new QueryWrapper<>();
                wrapper.eq("user_id",user.getId());
                UserStatistic one = iUserStatisticService.getOne(wrapper);
                if(one==null){
                    one = new UserStatistic();
                    one.setUserId(user.getId());
                }
                //司机
                if(type2==0){
                    Integer max = iDrivingRecordService.maxByUserId(user.getId());
                    Integer min = iDrivingRecordService.minByUserId(user.getId());
                    if(max==null||min==null){
                        one.setSafeDriving(0);//安全驾驶里程
                        one.setDriving(0);//驾驶里程
                    }else{
                        one.setSafeDriving(max-min);//安全驾驶里程
                        one.setDriving(max-min);//驾驶里程
                    }
 
                    QueryWrapper<DrivingRecord> wrapper2 = new QueryWrapper<>();
                    wrapper2.eq("user_id",user.getId());
                    int count = iDrivingRecordService.count(wrapper2);
                    one.setOutCar(count);//出车次数
 
                    one.setLikes(iDrivingRecordService.sumLikeByUserId(user.getId()));//点赞数
 
                    QueryWrapper<Alarm> wrapper3 = new QueryWrapper<>();
                    wrapper3.eq("car_user_id",user.getId());
                    wrapper3.eq("type",1);
                    one.setFatigueDriving(iAlarmService.count(wrapper3));//疲劳驾驶
                    wrapper3.eq("type",3);
                    one.setNoBelt(iAlarmService.count(wrapper3));//不系安全带
                    wrapper3.eq("type",4);
                    one.setSmoking(iAlarmService.count(wrapper3));//抽烟
                    wrapper3.eq("type",2);
                    one.setPhone(iAlarmService.count(wrapper3));//接打电话
                }
                //配送员
                if(type2==1){
                    QueryWrapper<OrderTask> wrapper2 = new QueryWrapper<>();
                    wrapper2.eq("user_id",user.getId());
                    int count = iOrderTaskService.count(wrapper2);
                    one.setSends(count);//配送次数
 
                    wrapper2.ne("status",0);
                    int count2 = iOrderTaskService.count(wrapper2);
                    one.setSends2(count2);//送达次数
 
                    QueryWrapper<Area> wrapper3 = new QueryWrapper<>();
                    wrapper3.eq("user_id",user.getId());
                    Area area = iAreaService.getOne(wrapper3);
                    if(area!=null){
                        QueryWrapper<Customer> wrapper4 = new QueryWrapper<>();
                        wrapper4.eq("area_id",area.getId());
                        int count3 = iCustomerService.count(wrapper4);
                        one.setService(count3);//服务商户数
                    }
 
                    int i = iOrderTaskService.countLike();
                    one.setLikes(i);//点赞次数
                    int likeCount = iOrderTaskService.count();
                    if(likeCount==0){
                        one.setLikesRate(0);//点赞率
                    }else{
                        one.setLikesRate((i*100)/likeCount);//点赞率
                    }
 
                    QueryWrapper<Alarm> wrapper4 = new QueryWrapper<>();
                    wrapper4.eq("follow_user_id",user.getId());
                    wrapper4.eq("type",5);
                    one.setAbnormalOpen(iAlarmService.count(wrapper4));//异常开启
                }
 
                iUserStatisticService.saveOrUpdate(one);
            }
 
        }
 
    }
}