package com.ruoyi.web.controller.task; import java.util.List; 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.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.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.ruoyi.common.annotation.Log; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.system.domain.SysTaskVehicle; import com.ruoyi.system.service.ISysTaskService; /** * 任务车辆关联Controller * * @author ruoyi * @date 2024-01-15 */ @RestController @RequestMapping("/task/vehicle") public class SysTaskVehicleController extends BaseController { @Autowired private ISysTaskService sysTaskService; /** * 查询任务关联的车辆列表 */ @PreAuthorize("@ss.hasPermi('task:general:query')") @GetMapping("/list/{taskId}") public AjaxResult list(@PathVariable("taskId") Long taskId) { List list = sysTaskService.getTaskVehicles(taskId); return success(list); } /** * 查询可用车辆列表 */ @PreAuthorize("@ss.hasPermi('task:general:query')") @GetMapping("/available") public AjaxResult getAvailableVehicles(@RequestParam Long deptId, @RequestParam(required = false) String taskType) { List list = sysTaskService.getAvailableVehicles(deptId, taskType); return success(list); } /** * 分配车辆给任务 */ @PreAuthorize("@ss.hasPermi('task:general:assign')") @Log(title = "任务车辆分配", businessType = BusinessType.INSERT) @PostMapping("/assign/{taskId}") public AjaxResult assignVehicle(@PathVariable("taskId") Long taskId, @RequestBody AssignVehicleRequest request) { try { int result = sysTaskService.assignVehicleToTask(taskId, request.getVehicleId(), request.getRemark()); if (result > 0) { return success("分配成功"); } else { return error("分配失败"); } } catch (Exception e) { return error("分配失败:" + e.getMessage()); } } /** * 批量分配车辆给任务 */ @PreAuthorize("@ss.hasPermi('task:general:assign')") @Log(title = "任务车辆批量分配", businessType = BusinessType.INSERT) @PostMapping("/assign-batch/{taskId}") public AjaxResult assignVehicles(@PathVariable("taskId") Long taskId, @RequestBody BatchAssignVehicleRequest request) { try { // 设置请求对象中的taskId,确保参数一致性 request.setTaskId(taskId); int result = sysTaskService.assignMultipleVehiclesToTask(request.getTaskId(), request.getVehicleIds(), request.getRemark()); if (result > 0) { return success("批量分配成功,共分配 " + result + " 辆车"); } else { return error("批量分配失败"); } } catch (Exception e) { return error("批量分配失败:" + e.getMessage()); } } /** * 取消任务车辆分配 */ @PreAuthorize("@ss.hasPermi('task:general:assign')") @Log(title = "取消任务车辆分配", businessType = BusinessType.DELETE) @DeleteMapping("/{taskId}/{vehicleId}") public AjaxResult unassignVehicle(@PathVariable("taskId") Long taskId, @PathVariable("vehicleId") Long vehicleId) { try { int result = sysTaskService.unassignVehicleFromTask(taskId, vehicleId); if (result > 0) { return success("取消分配成功"); } else { return error("取消分配失败"); } } catch (Exception e) { return error("取消分配失败:" + e.getMessage()); } } /** * 分配车辆请求对象 */ public static class AssignVehicleRequest { private Long vehicleId; private String remark; public Long getVehicleId() { return vehicleId; } public void setVehicleId(Long vehicleId) { this.vehicleId = vehicleId; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } } /** * 批量分配车辆请求对象 */ public static class BatchAssignVehicleRequest { private Long taskId; private List vehicleIds; private String remark; public Long getTaskId() { return taskId; } public void setTaskId(Long taskId) { this.taskId = taskId; } public List getVehicleIds() { return vehicleIds; } public void setVehicleIds(List vehicleIds) { this.vehicleIds = vehicleIds; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } } }