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; @Autowired private ICarService iCarService; @Scheduled(cron="0 0 5 * * ?")//每晚凌晨1点执行 public void execute(){ List all = userService.findAll(); for(User user:all){ Integer type2 = user.getType2(); if(type2!=null){ QueryWrapper 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{ if(max==min){ one.setSafeDriving(max);//安全驾驶里程 one.setDriving(max);//驾驶里程 }else{ one.setSafeDriving(max-min);//安全驾驶里程 one.setDriving(max-min);//驾驶里程 } } QueryWrapper wrapper2 = new QueryWrapper<>(); wrapper2.eq("user_id",user.getId()); int count = iDrivingRecordService.count(wrapper2); one.setOutCar(count);//出车次数 one.setLikes(iDrivingRecordService.sumLikeByUserId(user.getId()));//点赞数 QueryWrapper 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 wrapper2 = new QueryWrapper<>(); wrapper2.eq("user_id",user.getId()); wrapper2.ne("status",0); int count = iOrderTaskService.count(wrapper2); one.setSends(count);//配送次数 // QueryWrapper wrapper22 = new QueryWrapper<>(); // wrapper22.eq("user_id",user.getId()); // wrapper22.eq("status",1); // int count2 = iOrderTaskService.count(wrapper22); one.setSends2(count);//送达次数 // QueryWrapper wrapper3 = new QueryWrapper<>(); // wrapper3.eq("user_id",user.getId()); // Area area = iAreaService.getOne(wrapper3); Area area = getArea(user.getId()); if(area!=null){ QueryWrapper 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 wrapper4 = new QueryWrapper<>(); wrapper4.eq("follow_user_id",user.getId()); wrapper4.eq("type",5); one.setAbnormalOpen(iAlarmService.count(wrapper4));//异常开启 } iUserStatisticService.saveOrUpdate(one); } } } public Area getArea(String userId){ QueryWrapper carQueryWrapper = new QueryWrapper(); carQueryWrapper.eq("follow_user_id",userId); Car one = iCarService.getOne(carQueryWrapper); if(one==null){ return null; }else{ QueryWrapper wrapper = new QueryWrapper(); wrapper.eq("car_id",one.getId()); Area area = iAreaService.getOne(wrapper); return area; } } }