wjli
2024-04-10 81af4cff627b7ec1e125b90f4fd57392c6d70588
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
package cn.exrick.xboot.your.controller;
 
import cn.exrick.xboot.core.common.utils.PageUtil;
import cn.exrick.xboot.core.common.utils.ResultUtil;
import cn.exrick.xboot.core.common.vo.PageVo;
import cn.exrick.xboot.core.common.vo.Result;
import cn.exrick.xboot.core.entity.User;
import cn.exrick.xboot.your.entity.Car;
import cn.exrick.xboot.your.entity.UserStatistic;
import cn.exrick.xboot.your.service.ICarService;
import cn.exrick.xboot.your.service.IUserStatisticService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
 
/**
 * @author whj
 */
@Slf4j
@RestController
@Api(tags = "用户统计表管理接口")
@RequestMapping("/xboot/userStatistic")
@Transactional
public class UserStatisticController {
 
    @Autowired
    private IUserStatisticService iUserStatisticService;
 
    @Autowired
    private ICarService iCarService;
 
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<UserStatistic> get(@PathVariable String id) {
 
        UserStatistic userStatistic = iUserStatisticService.getById(id);
        return new ResultUtil<UserStatistic>().setData(userStatistic);
    }
 
    @RequestMapping(value = "/getByUserId", method = RequestMethod.GET)
    @ApiOperation(value = "通过用户id获取统计信息")
    public Result<UserStatistic> getByUserId(String userId) {
        QueryWrapper<UserStatistic> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        UserStatistic userStatistic = iUserStatisticService.getOne(wrapper);
        if(userStatistic==null){
            return new ResultUtil<UserStatistic>().setData(new UserStatistic());
        }
        return new ResultUtil<UserStatistic>().setData(userStatistic);
    }
 
    @RequestMapping(value = "/getAllByUserId", method = RequestMethod.GET)
    @ApiOperation(value = "通过用户id获取统计信息")
    public Result<UserStatistic> getStatisticByUserId(String userId,int type) {
        UserStatistic userStatistic0 = getById(userId);
        String otherId = getOtherIdFromCar(userId,type);
        UserStatistic userStatistic1 = getById(otherId);
        if(type==0){//驾驶员
            userStatistic0.setSends(userStatistic1.getSends());
            userStatistic0.setService(userStatistic1.getService());
            userStatistic0.setSends2(userStatistic1.getSends2());
            userStatistic0.setAbnormalOpen(userStatistic1.getAbnormalOpen());
            userStatistic0.setLikesRate(userStatistic1.getLikesRate());
        }else{//配送员
            userStatistic0.setSafeDriving(userStatistic1.getSafeDriving());
            userStatistic0.setOutCar(userStatistic1.getOutCar());
            userStatistic0.setDriving(userStatistic1.getDriving());
            userStatistic0.setFatigueDriving(userStatistic1.getFatigueDriving());
            userStatistic0.setSmoking(userStatistic1.getSmoking());
            userStatistic0.setNoBelt(userStatistic1.getNoBelt());
            userStatistic0.setLikes(userStatistic1.getLikes());
        }
        return new ResultUtil<UserStatistic>().setData(userStatistic0);
    }
 
    public UserStatistic getById(String userId){
        QueryWrapper<UserStatistic> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        UserStatistic userStatistic = iUserStatisticService.getOne(wrapper);
        if(userStatistic==null){
            userStatistic = new UserStatistic();
        }
        return userStatistic;
    }
 
    public String getOtherIdFromCar(String userId,int type){
        QueryWrapper<Car> wrapper = new QueryWrapper<>();
        if(type==0){
            wrapper.eq("user_id",userId);
            Car myCar = iCarService.getOne(wrapper);
            if(myCar!=null){
                return myCar.getFollowUserId();
            }
        }else if(type==1){
            wrapper.eq("follow_user_id",userId);
            Car myCar = iCarService.getOne(wrapper);
            if(myCar!=null){
                return myCar.getUserId();
            }
        }
        return null;
    }
 
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<UserStatistic>> getAll() {
 
        List<UserStatistic> list = iUserStatisticService.list();
        return new ResultUtil<List<UserStatistic>>().setData(list);
    }
 
    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<UserStatistic>> getByPage(PageVo page) {
 
        IPage<UserStatistic> data = iUserStatisticService.page(PageUtil.initMpPage(page));
        return new ResultUtil<IPage<UserStatistic>>().setData(data);
    }
 
    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<UserStatistic> saveOrUpdate(UserStatistic userStatistic) {
 
        if (iUserStatisticService.saveOrUpdate(userStatistic)) {
            return new ResultUtil<UserStatistic>().setData(userStatistic);
        }
        return new ResultUtil<UserStatistic>().setErrorMsg("操作失败");
    }
 
    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@RequestParam String[] ids) {
 
        for (String id : ids) {
            iUserStatisticService.removeById(id);
        }
        return ResultUtil.success("批量通过id删除数据成功");
    }
}