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 list = vehicleInfoService.selectVehicleInfoList(vehicleInfo); return getDataTable(list); } /** * 导出车辆信息列表 */ @Log(title = "车辆信息", businessType = BusinessType.EXPORT) @GetMapping("/export") public AjaxResult export(VehicleInfo vehicleInfo) { List list = vehicleInfoService.selectVehicleInfoList(vehicleInfo); ExcelUtil util = new ExcelUtil(VehicleInfo.class); return util.exportExcel(list, "车辆信息数据"); } /** * 获取车辆信息详细信息 */ @GetMapping(value = "/{vehicleId}") public AjaxResult getInfo(@PathVariable("vehicleId") Long vehicleId) { return success(vehicleInfoService.selectVehicleInfoWithDeptsById(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 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 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 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()); } } }