wlzboy
2025-11-16 f67945d53b20f6a45ae50b27d74c966eb1355bb4
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/VehicleInfoController.java
@@ -1,6 +1,8 @@
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;
@@ -12,9 +14,11 @@
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;
@@ -32,7 +36,6 @@
    /**
     * 查询车辆信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:vehicle:list')")
    @GetMapping("/list")
    public TableDataInfo list(VehicleInfo vehicleInfo) {
        startPage();
@@ -43,7 +46,6 @@
    /**
     * 导出车辆信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:vehicle:export')")
    @Log(title = "车辆信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(VehicleInfo vehicleInfo) {
@@ -55,16 +57,14 @@
    /**
     * 获取车辆信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:vehicle:query')")
    @GetMapping(value = "/{vehicleId}")
    public AjaxResult getInfo(@PathVariable("vehicleId") Long vehicleId) {
        return success(vehicleInfoService.selectVehicleInfoById(vehicleId));
        return success(vehicleInfoService.selectVehicleInfoWithDeptsById(vehicleId));
    }
    /**
     * 新增车辆信息
     */
    @PreAuthorize("@ss.hasPermi('system:vehicle:add')")
    @Log(title = "车辆信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VehicleInfo vehicleInfo) {
@@ -74,7 +74,6 @@
    /**
     * 修改车辆信息
     */
    @PreAuthorize("@ss.hasPermi('system:vehicle:edit')")
    @Log(title = "车辆信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VehicleInfo vehicleInfo) {
@@ -84,10 +83,93 @@
    /**
     * 删除车辆信息
     */
    @PreAuthorize("@ss.hasPermi('system:vehicle:remove')")
    @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());
        }
    }