luodangjia
2024-12-10 31ce6be2d56798d9509e6d90335999064351f7f3
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
package com.stylefeng.guns.modular.system.service.impl;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.modular.system.dao.CarMapper;
import com.stylefeng.guns.modular.system.model.Car;
import com.stylefeng.guns.modular.system.model.Driver;
import com.stylefeng.guns.modular.system.service.ICarService;
import com.stylefeng.guns.modular.system.service.IDispatchService;
import com.stylefeng.guns.modular.system.service.IDriverService;
import com.stylefeng.guns.modular.system.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
 
 
@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements ICarService {
 
    @Resource
    private CarMapper carMapper;
 
    @Autowired
    private IDispatchService dispatchService;
 
    @Autowired
    private IDriverService driverService;
 
 
    /**
     * 获取车辆列表
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, Object>> queryCarList(Integer uid) throws Exception {
        Integer companyId = dispatchService.selectById(uid).getCompanyId();
        return carMapper.queryCarList(companyId);
    }
 
 
    /**
     * 获取车辆详情
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> queryCarInfo(Integer id) throws Exception {
        Map<String, Object> map = carMapper.queryCarInfo(id);
        List<Driver> drivers = driverService.selectList(new EntityWrapper<Driver>().eq("authState", 2)
                .ne("flag", 3).eq("carId", map.get("id")));
        if (drivers.isEmpty()){
            throw new Exception("该车辆还未绑定司机");
        }
        StringBuffer sb = new StringBuffer();
        for (Driver driver : drivers) {
            sb.append(driver.getName() + "-" + driver.getPhone() + "/");
        }
        map.put("driver", sb.substring(0, sb.length() - 1));
        return map;
    }
 
 
    /**
     * 重新绑定/解绑 车辆司机关系
     * @param id
     * @param driverId
     * @return
     * @throws Exception
     */
    @Override
    public ResultUtil rebindDriver(Integer id, Integer driverId) throws Exception {
        if(null == driverId){//解除绑定
            List<Driver> drivers = driverService.queryByCarId(id);
            if(drivers.size() == 0){
                return ResultUtil.error("车辆还未绑定司机");
            }
            for(Driver driver : drivers){
                driver.setCarId(null);
            }
            driverService.updateAllColumnBatchById(drivers);
            return ResultUtil.success();
        }
 
        Driver driver = driverService.selectById(driverId);
        driver.setCarId(id);
        driverService.updateById(driver);
        return ResultUtil.success();
    }
}