wlzboy
2025-10-27 559b2e34c983f615b6d6747f52c801022c561803
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
package com.ruoyi.web.controller.system;
 
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.VehicleInfo;
import com.ruoyi.system.service.IVehicleInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
 
/**
 * 车辆信息Controller
 */
@RestController
@RequestMapping("/system/vehicle")
public class VehicleInfoController extends BaseController {
    @Autowired
    private IVehicleInfoService vehicleInfoService;
 
    /**
     * 查询车辆信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(VehicleInfo vehicleInfo) {
        startPage();
        List<VehicleInfo> list = vehicleInfoService.selectVehicleInfoList(vehicleInfo);
        return getDataTable(list);
    }
 
    /**
     * 导出车辆信息列表
     */
    @Log(title = "车辆信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(VehicleInfo vehicleInfo) {
        List<VehicleInfo> list = vehicleInfoService.selectVehicleInfoList(vehicleInfo);
        ExcelUtil<VehicleInfo> util = new ExcelUtil<VehicleInfo>(VehicleInfo.class);
        return util.exportExcel(list, "车辆信息数据");
    }
 
    /**
     * 获取车辆信息详细信息
     */
    @GetMapping(value = "/{vehicleId}")
    public AjaxResult getInfo(@PathVariable("vehicleId") Long vehicleId) {
        return success(vehicleInfoService.selectVehicleInfoById(vehicleId));
    }
 
    /**
     * 新增车辆信息
     */
    @Log(title = "车辆信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VehicleInfo vehicleInfo) {
        return toAjax(vehicleInfoService.insertVehicleInfo(vehicleInfo));
    }
 
    /**
     * 修改车辆信息
     */
    @Log(title = "车辆信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VehicleInfo vehicleInfo) {
        return toAjax(vehicleInfoService.updateVehicleInfo(vehicleInfo));
    }
 
    /**
     * 删除车辆信息
     */
    @Log(title = "车辆信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{vehicleIds}")
    public AjaxResult remove(@PathVariable Long[] vehicleIds) {
        return toAjax(vehicleInfoService.deleteVehicleInfoByIds(vehicleIds));
    }
    
    /**
     * 绑定车辆到用户
     */
    @Anonymous
    @Log(title = "用户绑定车辆", businessType = BusinessType.UPDATE)
    @PostMapping("/bind")
    public AjaxResult bindVehicle(@RequestBody Map<String, Object> params) {
        try {
            Long userId = Long.valueOf(params.get("userId").toString());
            Long vehicleId = Long.valueOf(params.get("vehicleId").toString());
            
            // 验证车辆是否存在
            VehicleInfo vehicle = vehicleInfoService.selectVehicleInfoById(vehicleId);
            if (vehicle == null) {
                return error("车辆不存在");
            }
            
            // 验证车辆状态是否正常
            if (!"0".equals(vehicle.getStatus())) {
                return error("车辆状态异常,无法绑定");
            }
            
            // 调用绑定服务
            int result = vehicleInfoService.bindVehicleToUser(userId, vehicleId);
            if (result > 0) {
                return success("车辆绑定成功");
            } else {
                return error("车辆绑定失败");
            }
        } catch (Exception e) {
            logger.error("绑定车辆失败", e);
            return error("绑定车辆失败:" + e.getMessage());
        }
    }
    
    /**
     * 解绑用户车辆
     */
    @Anonymous
    @Log(title = "用户解绑车辆", businessType = BusinessType.UPDATE)
    @PostMapping("/unbind")
    public AjaxResult unbindVehicle(@RequestBody Map<String, Object> params) {
        try {
            Long userId = Long.valueOf(params.get("userId").toString());
            Long vehicleId = Long.valueOf(params.get("vehicleId").toString());
            
            // 调用解绑服务
            int result = vehicleInfoService.unbindVehicleFromUser(userId, vehicleId);
            if (result > 0) {
                return success("车辆解绑成功");
            } else {
                return error("车辆解绑失败");
            }
        } catch (Exception e) {
            logger.error("解绑车辆失败", e);
            return error("解绑车辆失败:" + e.getMessage());
        }
    }
    
    /**
     * 获取用户当前绑定的车辆
     */
    @Anonymous
    @GetMapping("/user/bound/{userId}")
    public AjaxResult getUserBoundVehicle(@PathVariable("userId") Long userId) {
        try {
            VehicleInfo vehicle = vehicleInfoService.getUserBoundVehicle(userId);
            if (vehicle != null) {
                Map<String, Object> result = new HashMap<>();
                result.put("vehicleId", vehicle.getVehicleId());
                result.put("vehicleNumber", vehicle.getVehicleNo());
                result.put("vehicleType", vehicle.getVehicleType());
                result.put("vehicleBrand", vehicle.getVehicleBrand());
                result.put("vehicleModel", vehicle.getVehicleModel());
                return success(result);
            } else {
                return success(null);
            }
        } catch (Exception e) {
            logger.error("获取用户绑定车辆失败", e);
            return error("获取用户绑定车辆失败:" + e.getMessage());
        }
    }