wlzboy
2025-10-26 2c86a8bd60deed0dd0e044bad6fb83f75d19a332
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;
@@ -90,4 +94,88 @@
    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());
        }
    }