bug
zhangzeli
2022-01-11 9c1840e4302d8c4f7af549e5de412488073490bf
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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.your.entity.*;
import cn.exrick.xboot.your.service.*;
import cn.exrick.xboot.your.vo.CarVo;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * @author zhangzeli
 */
@Slf4j
@RestController
@Api(tags = "车辆表管理接口")
@RequestMapping("/xboot/car")
@Transactional
public class CarController {
 
    @Autowired
    private ICarService iCarService;
    @Autowired
    private IAddOilService iAddOilService;
    @Autowired
    private IDrivingRecordService iDrivingRecordService;
    @Autowired
    private ICustomerService iCustomerService;
 
 
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<Car> get(@PathVariable String id) {
 
        Car car = iCarService.getById(id);
        return new ResultUtil<Car>().setData(car);
    }
 
    @RequestMapping(value = "/getAllCar", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<Car>> getAllCar() {
 
        List<Car> list = iCarService.list();
        return new ResultUtil<List<Car>>().setData(list);
    }
 
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<CarVo>> getAll(String beginTime,String endTime,PageVo page) {
        List<CarVo> carVos = new ArrayList<>();
 
        IPage<Car> list = iCarService.getAll2(PageUtil.initMpPage(page));
        for (Car car:list.getRecords()){
            CarVo carVo = new CarVo();
            //加油信息
            QueryWrapper<AddOil> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("sum(money) as money,sum(amount) as amount").eq("car_id", car.getId());
 
 
            //从发车时间行驶里程判断初始里程
            QueryWrapper<DrivingRecord> wrapper = new QueryWrapper<>();
            wrapper.select("min(mileage) as beginMileage,max(mileage) as endMileage").eq("car_id",car.getId());
 
            wrapper.ge(beginTime!=null && beginTime!="","out_time",beginTime);
            queryWrapper.ge(beginTime!=null && beginTime!="","add_date",beginTime);
 
            wrapper.le(endTime!=null && endTime!="","in_time",endTime);
            queryWrapper.le(endTime!=null && endTime!="","add_date",beginTime);
 
            QueryWrapper<OrderTask> wrapper1 = new QueryWrapper<>();
 
 
            wrapper1.eq("car_id",car.getId());
            wrapper1.ge(beginTime!=null && beginTime!="","send_date",beginTime);
            wrapper1.le(endTime!=null && endTime!="","send_date",endTime);
 
 
            DrivingRecord drivingRecord = iDrivingRecordService.getOne(wrapper);
 
            AddOil addOil = iAddOilService.getOne(queryWrapper);
 
            if (addOil!=null){
                carVo.setMoney(addOil.getMoney());
                carVo.setOilWear(addOil.getAmount());
            }
 
            if (drivingRecord!=null) {
                carVo.setBeginMileage(drivingRecord.getBeginMileage());
                carVo.setEndMileage(drivingRecord.getEndMileage());
            }
 
            carVo.setCar(car);
            carVos.add(carVo);
            carVo.setTotal(list.getTotal());
        }
        return new ResultUtil<List<CarVo>>().setData(carVos);
    }
    @RequestMapping(value = "/getInfo", method = RequestMethod.GET)
    @ApiOperation(value = "通过车辆id获取")
    public Result<List<Car>> getInfo(String beginTime,String endTime) {
        QueryWrapper<Car> wrapper = new QueryWrapper<>();
        wrapper.ge(beginTime!=null && beginTime!="","a.add_date",beginTime);
        wrapper.le(endTime!=null && endTime!="","a.add_date",endTime);
        wrapper.groupBy("car_no");
        List<Car> list2 = iCarService.getCarInfo2(wrapper);
        List<Car> list = iCarService.getCarInfo();
        for (Car car:list2){
            for (Car car1:list){
                if (car.getId().equals(car1.getId())){
                    car1.setAmount(car.getAmount());
                    car1.setMileage(car.getMileage());
                    car1.setMoney(car.getMoney());
                    continue;
                }
            }
        }
        return new ResultUtil<List<Car>>().setData(list);
    }
 
    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<Car>> getByPage(String carNo,PageVo page) {
        QueryWrapper<Car> wrapper = new QueryWrapper<>();
        if(!StrUtil.isEmpty(carNo)){
            wrapper.like("a.car_no","%"+carNo+"%");
        }
        IPage<Car> data = iCarService.page2(PageUtil.initMpPage(page),wrapper);
        return new ResultUtil<IPage<Car>>().setData(data);
    }
 
    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<Car> saveOrUpdate(Car car) {
 
        QueryWrapper<Car> wrapper = new QueryWrapper<>();
        QueryWrapper<Car> wrapper1 = new QueryWrapper<>();
        if (!car.getUserId().equals("")){
            wrapper.eq("user_id",car.getUserId());
            Car car1 = iCarService.getOne(wrapper);
            if (car1!=null && !car1.getId().equals(car.getId())){
                return new ResultUtil<Car>().setErrorMsg("该驾驶员已被其他车辆绑定,请先解绑");
            }
        }
 
        if (!car.getFollowUserId().equals("")){
            wrapper1.eq("follow_user_id",car.getFollowUserId());
            Car car1 = iCarService.getOne(wrapper1);
            if (car1!=null && !car1.getId().equals(car.getId())){
                return new ResultUtil<Car>().setErrorMsg("该配送员已被其他车辆绑定,请先解绑");
            }
        }
 
        if (iCarService.saveOrUpdate(car)) {
            return new ResultUtil<Car>().setData(car);
        }
 
        return new ResultUtil<Car>().setErrorMsg("操作失败");
    }
 
    @RequestMapping(value = "/bindCar", method = RequestMethod.POST)
    @ApiOperation(value = "绑定车辆人员")
    public Result<Car> bindCar(String carId,String userId) {
        Car car = iCarService.getById(carId);
        car.setUserId(userId);
        iCarService.saveOrUpdate(car);
        return ResultUtil.success("绑定成功");
    }
 
    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@RequestParam String[] ids) {
 
        for (String id : ids) {
            iCarService.removeById(id);
        }
        return ResultUtil.success("批量通过id删除数据成功");
    }
}