package com.ruoyi.buss.controller; import com.ruoyi.buss.common.DateUtils; import com.ruoyi.buss.common.RfidUtil; import com.ruoyi.buss.domain.*; import com.ruoyi.buss.domain.dto.AllocBerthReqDTO; import com.ruoyi.buss.domain.dto.AllocBerthTimeReqDTO; import com.ruoyi.buss.domain.dto.DsTaskQueryParam; import com.ruoyi.buss.domain.dto.TaskQueryParam; import com.ruoyi.buss.domain.vo.RfIdVo; import com.ruoyi.buss.service.*; 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.common.utils.StringUtils; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import java.util.*; import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Collectors; @Tag(name = "支队任务接口") @RestController @RequestMapping("/buss/tasklist/ex") public class DMTaskTimeController extends BaseController { @Autowired private IDsTaskService iDsTaskService; @Autowired private IDsTaskList2Service iDsTaskListService; @Autowired private IDmBerth2Service dmBerthService; @Autowired private IDmHarbor2Service dmHarborService; @Autowired private IDmDdConfigService iDmDdConfigService; @Autowired private IDsTaskDetailService iDsTaskDetailService; @Value("${ruoyi.buss.rfid.url:http://192.168.0.2:10800/open/goods/task}") private String rfidUrl; private static String WATER_FLAG = "water"; private static String MATER_FLAG = "mater"; private static String OIL_FLAG = "oil"; private static String AMMO_FLAG = "ammo"; private static String AMMO_O_FLAG = "ammo_o"; @Operation(summary = "获取本支队所有任务列表") @PostMapping("/histask/list") @ResponseBody public AjaxResult getTaskList(@RequestBody DsTaskQueryParam param) { if(null != param.getEtime()){ param.setEtime(DateUtils.setToNextDayZero(param.getEtime())); } List dsTaskList = iDsTaskService.selectDsTaskListByParam(param); return success(dsTaskList); } @Operation(summary = "支队任务下发") @Log(title = "支队任务下发", businessType = BusinessType.UPDATE) @PostMapping("/apply/{taskid}/{deptid}") @ResponseBody public AjaxResult apply(@Parameter(name = "taskid", required = true, description = "任务编号") @PathVariable("taskid") Long taskid, @Parameter(name = "deptid", required = true, description = "部门ID") @PathVariable("deptid") Long deptid, @Parameter(name = "ids", required = false, description = "任务Ids") @RequestParam(value = "ids", required = false) String ids) { TaskQueryParam queryParam = new TaskQueryParam(); if(null != taskid){ queryParam.setTaskId(taskid); } queryParam.setDeptId(deptid); queryParam.setStatus("1"); if(StringUtils.isNotBlank(ids)){ List idList = Arrays.stream(ids.split(",")) .map(Long::valueOf) .collect(Collectors.toList()); if(null != idList && idList.size() > 0){ queryParam.setIds(idList); } } List dsTaskList = iDsTaskListService.selectDsTaskListByParam(queryParam); if(null == dsTaskList || dsTaskList.size() == 0){ return error("未获取到对应的任务"); } // 获取所有的泊位信息 List berth2List = dmBerthService.selectDmBerthList(new DmBerth2()); Map berth2Map = berth2List.stream().collect(Collectors.toMap(DmBerth2::getPKID, berth -> berth)); List taskToUpdate = dsTaskList.stream().map(tmp -> { tmp.setStatus("2"); DmBerth2 berth2 = berth2Map.get(tmp.getPKID()); if(null != berth2){ tmp.setPath(berth2.getPath()); } return tmp; }).collect(Collectors.toList()); if(!taskToUpdate.isEmpty()){ iDsTaskListService.batchUpdateDsTaskList(taskToUpdate); } List rfIdVoList = new ArrayList<>(); //rfIdVoList.add(new RfIdVo(Integer.valueOf(task.getShipNo()), task.getShipNo()+"舰艇补给任务", task.getPath())); // 推送数据到 RFID // try{ // RfidUtil.asyncSendJsonPost(rfidUrl, rfIdVoList); // }catch (Exception e){e.printStackTrace();} // 更新Detail 路径规划 this.updateDetailPath(taskid, deptid, berth2List); DsTaskList2 tmpTaskList = new DsTaskList2(); tmpTaskList.setStatus("1"); tmpTaskList.setTaskId(taskid); List taskList2List = iDsTaskListService.selectDsTaskListList(tmpTaskList); if(null != taskList2List && taskList2List.size() > 0){ DsTask dsTask = iDsTaskService.selectDsTaskByPKID(taskid); dsTask.setStatus("2"); iDsTaskService.updateDsTask(dsTask); return success("下发成功"); } return error("下发失败,请重试"); } @Operation(summary = "支队任务下发") @Log(title = "支队任务下发", businessType = BusinessType.UPDATE) @PostMapping("/apply/{deptid}") @ResponseBody public AjaxResult applyV2(@Parameter(name = "taskid", required = true, description = "任务编号") @RequestParam("taskid") Long taskid, @Parameter(name = "deptid", required = true, description = "部门ID") @PathVariable("deptid") Long deptid, @Parameter(name = "ids", required = true, description = "任务Ids") @RequestParam("ids") String ids) { TaskQueryParam queryParam = new TaskQueryParam(); if(null != taskid){ queryParam.setTaskId(taskid); } queryParam.setDeptId(deptid); queryParam.setStatus("1"); if(StringUtils.isNotBlank(ids)){ List idList = Arrays.stream(ids.split(",")) .map(Long::valueOf) .collect(Collectors.toList()); if(null != idList && idList.size() > 0){ queryParam.setIds(idList); } } List dsTaskList = iDsTaskListService.selectDsTaskListByParam(queryParam); if(null == dsTaskList || dsTaskList.size() == 0){ return error("未获取到对应的任务"); } int flag = 1; for (DsTaskList2 task : dsTaskList) { task.setStatus("2"); int updateResult = iDsTaskListService.updateDsTaskList(task); if (updateResult != 1) { flag = 0; break; } } DsTaskList2 tmpTaskList = new DsTaskList2(); tmpTaskList.setStatus("1"); tmpTaskList.setTaskId(taskid); List taskList2List = iDsTaskListService.selectDsTaskListList(tmpTaskList); if(null != taskList2List && taskList2List.size() == 0){ DsTask dsTask = iDsTaskService.selectDsTaskByPKID(taskid); dsTask.setStatus("2"); iDsTaskService.updateDsTask(dsTask); } if(flag == 1){ return success("下发成功"); } return error("下发失败,请重试"); } @Operation(summary = "获取最新下发的任务列表") @GetMapping("/task") @ResponseBody public AjaxResult getByTaskId(@Parameter(name = "deptId", description = "部门编码") Long deptId) { if(null == deptId)deptId = getDeptId(); // 获取支队正在进行的任务 // DsTask taskEntity = new DsTask(); // taskEntity.setStatus("1"); // 1 状态为 支队 2 状态为 完成状态。 // List tasks = iDsTaskService.selectDsTaskList(taskEntity); // if(null != tasks && tasks.size() > 0){ // DsTask curTask = tasks.get(0); DsTaskList2 taskListEntity = new DsTaskList2(); // taskListEntity.setTaskId(curTask.getPKID()); taskListEntity.setDeptId(deptId); taskListEntity.setStatus("1"); List dsTaskList2s = iDsTaskListService.selectDsTaskListList(taskListEntity); return success(dsTaskList2s); // } // return success(); } /** * 查询泊位信息列表 */ @Operation(summary = "查询泊位信息列表") @GetMapping("/berth/list") @ResponseBody public AjaxResult dmBerthList(@Parameter(name = "deptId", description = "部门编码") Long deptId) { if(null == deptId)deptId = getDeptId(); if(null == deptId){ return error("获取当前用户信息失败,请重新登录后尝试。"); } DmHarbor2 harbor = new DmHarbor2(); harbor.setDeptId(deptId); List harborList = dmHarborService.selectDmHarborList(harbor); List tmpHarborIds = new ArrayList<>(); harborList.stream().forEach(item -> { tmpHarborIds.add(item.getPKID()); }); List list = dmBerthService.selectDmBerthByHarborIdsAndStatus(tmpHarborIds); list = list.stream() .filter(task -> task.getDEPTH() != null) .sorted(Comparator.comparing(DmBerth2::getHarborId).thenComparing(DmBerth2::getOrderNum)) .collect(Collectors.toList()); return success(list); } @Operation(summary = "查询码头信息详情") @GetMapping("/harbor/id/{harborid}") @ResponseBody public AjaxResult dmBerthById(@Parameter(name = "harborid", description = "码头编码") @PathVariable("harborid") Long harborid) { return success(dmHarborService.selectDmHarborByPKID(harborid)); } /** * 码头调度分配接口 * */ @Operation(summary = "码头调度分配") @PostMapping("/allocation") @ResponseBody public AjaxResult allocation(@RequestBody AllocBerthReqDTO reqDTO) { Long deptId = getDeptId(); if(null != reqDTO.getDeptId()){ deptId =reqDTO.getDeptId(); } if(null == reqDTO.getTaskId()){ return error("任务编码不能为空"); } if(StringUtils.isBlank(reqDTO.getBerthids())){ return error("泊位编号不能为空"); } DsTaskList2 dsTaskList2Entity = new DsTaskList2(); dsTaskList2Entity.setTaskId(reqDTO.getTaskId()); dsTaskList2Entity.setDeptId(deptId); List taskLists = iDsTaskListService.selectDsTaskListList(dsTaskList2Entity); if(taskLists.size() == 0){ return error("所选舰艇为空,请重新检查参数"); } return success(allocationBerth(taskLists, reqDTO.getRule(), reqDTO.getBerthids())); } /** * 新增保存任务列表信息 */ @Operation(summary = "泊位调配信息更新接口") @Log(title = "泊位调配信息更新接口", businessType = BusinessType.UPDATE) @PostMapping("/tasklist/update") @ResponseBody public AjaxResult addTaskListSave(@RequestBody List taskList) { // 缺少数据非空以及合法性校验 List pkidList = taskList.stream() .map(DsTaskList2::getPKID) .collect(Collectors.toList()); List existingTaskLists = iDsTaskListService.selectDsTaskListByPkids(pkidList); for (DsTaskList2 existingTask : existingTaskLists) { for (DsTaskList2 newTask : taskList) { if (existingTask.getPKID().equals(newTask.getPKID())) { existingTask.setBerthId(newTask.getBerthId()); existingTask.setBerthName(newTask.getBerthName()); existingTask.setUpdateBy(getUsername()); iDsTaskListService.updateDsTaskList(existingTask); } } } return success("更新成功"); } /** * 码头调度分配接口 * */ @Operation(summary = "泊位补给计划分配") @PostMapping("/berth/alloc") @ResponseBody public AjaxResult berthAllocation(@RequestBody AllocBerthTimeReqDTO reqDTO) { //获取当前用户部门编号 Long deptId = getDeptId(); if(null != reqDTO.getDeptId()){ deptId = reqDTO.getDeptId(); } if(null == reqDTO.getTaskId()){ return error("任务编号不能为空"); } DsTaskList2 dsTaskList2Entity = new DsTaskList2(); dsTaskList2Entity.setTaskId(reqDTO.getTaskId()); dsTaskList2Entity.setDeptId(deptId); List taskLists = iDsTaskListService.selectDsTaskListList(dsTaskList2Entity); if(taskLists.size() == 0){ return error("当前无任务,请重新检查参数"); } // 重新组织数据结构,然后发送 return success(allocationBerthTime(taskLists, reqDTO.getRule())); } /** * 舰艇分组 * @param list * @param rule * @param berthids * @return */ private List allocationBerth(List list, String rule, String berthids){ List resultTask = new ArrayList<>(); String[] berthArr = berthids.split(","); if(null != list && list.size() > 0){ switch (rule){ case "JTXH": //按舰艇型号 case "DYYX": //按弹药优先 case "YXSX": //按优先顺序 case "RGFZ": //按人工分组 default: if(null != berthArr && berthArr.length > 0){ Map> harborBerthMap = new HashMap<>(); List berthIdList = Arrays.stream(berthArr) .map(Long::valueOf) .collect(Collectors.toList()); List berthList = dmBerthService.selectDmBerthByHarborIds(berthIdList); for (DmBerth2 dmBerth2 : berthList) { if (dmBerth2 != null) { harborBerthMap.computeIfAbsent(dmBerth2.getHarborId(), k -> new ArrayList<>()).add(dmBerth2); } } for (DsTaskList2 task : list) { Long harborId = task.getHarborId(); List availableBerths = harborBerthMap.get(harborId); if (availableBerths != null && !availableBerths.isEmpty()) { DmBerth2 assignedBerth = availableBerths.remove(0); task.setBerthId(assignedBerth.getPKID()); task.setBerthName(assignedBerth.getNAME()); resultTask.add(task); } else { // 如果没有可用的泊位则继续按照原来的逻辑重复分配 for (Map.Entry> entry : harborBerthMap.entrySet()) { if (!entry.getValue().isEmpty()) { DmBerth2 assignedBerth = entry.getValue().remove(0); task.setBerthId(assignedBerth.getPKID()); task.setBerthName(assignedBerth.getNAME()); resultTask.add(task); break; } } } } } } } return resultTask; } /** * 舰艇时间分配 * @param list * @param rule * @return */ private List allocationBerthTime(List list, String rule){ List result = new ArrayList<>(); // 获取时间计算配置信息 List configList = iDmDdConfigService.selectDmDdConfigList(new DmDdConfig()); // 遍历所有需要计算的船信息 for (DsTaskList2 task : list) { Long berthId = task.getBerthId(); List taskDetails = new ArrayList<>(); // 获取当前泊位的所有船只 List berthTasks = list.stream() .filter(t -> t.getBerthId().equals(berthId)) .sorted(Comparator.comparing(DsTaskList2::getLEVEL)) .collect(Collectors.toList()); // 遍历泊位的所有船只,计算每艘船的工作时间 for (DsTaskList2 berthTask : berthTasks) { DsTaskDetail taskDetail = new DsTaskDetail(); taskDetail.setTaskId(berthTask.getTaskId()); taskDetail.setBerthId(berthTask.getBerthId()); taskDetail.setShipNo(berthTask.getShipNo()); taskDetail.setBerthName(berthTask.getBerthName()); taskDetail.setHarborId(berthTask.getHarborId()); taskDetail.setHarborName(berthTask.getHarborName()); taskDetail.setParkingType(berthTask.getParkingType()); taskDetail.setShipType(berthTask.getShipType()); // 根据configList计算每一个类型物资工作需要的时间 double totalOil = 0.0; double totalWater = 0.0; double totalMater = 0.0; double totalAmmo = 0.0; // 油 if (berthTask.getOilA() != null && berthTask.getOilA() > 0) { totalOil += berthTask.getOilA(); } if (berthTask.getOilB() != null && berthTask.getOilB() > 0) { totalOil += berthTask.getOilB(); } if (berthTask.getOilG() != null && berthTask.getOilG() > 0) { totalOil += berthTask.getOilG(); } // 水 if (berthTask.getWATER() != null && berthTask.getWATER() > 0) { totalWater += berthTask.getWATER(); } if (berthTask.getWaterP() != null && berthTask.getWaterP() > 0) { totalWater += berthTask.getWaterP(); } // 物资 if (berthTask.getFOOD() != null && berthTask.getFOOD() > 0) { totalMater += berthTask.getFOOD(); } if (berthTask.getFoodO() != null && berthTask.getFoodO() > 0) { totalMater += berthTask.getFoodO(); } if (berthTask.getFoodW() != null && berthTask.getFoodW() > 0) { totalMater += berthTask.getFoodW(); } // 炮弹 if (berthTask.getAmmoD() != null && berthTask.getAmmoD() > 0) { totalAmmo += berthTask.getAmmoD(); } if (berthTask.getAmmoP() != null && berthTask.getAmmoP() > 0) { totalAmmo += berthTask.getAmmoP(); } if (berthTask.getAmmoS() != null && berthTask.getAmmoS() > 0) { totalAmmo += berthTask.getAmmoS(); } // taskDetail.setWaterTime(getTimeByType(configList, totalWater, WATER_FLAG)); // taskDetail.setOilTime(getTimeByType(configList, totalOil, OIL_FLAG)); // taskDetail.setMatTime(getTimeByType(configList, totalMater, MATER_FLAG)); // 弹药时间需要根据弹药种类进行分别计算 Double totalAmmoTime = getTimeByType(configList, null == berthTask.getAmmoO() ? 0.0 : (double)(berthTask.getAmmoO()), AMMO_O_FLAG); totalAmmoTime += getTimeByType(configList, totalAmmo, AMMO_FLAG); // taskDetail.setAmmoTime(totalAmmoTime); double totalTime = 0.0; if ("TS".equals(rule)) { totalTime = Math.max(taskDetail.getWaterTime(), Math.max(taskDetail.getOilTime(), Math.max(taskDetail.getMatTime(), taskDetail.getAmmoTime()))); } else if ("XYHD".equals(rule)) { totalTime = Math.max(taskDetail.getWaterTime(), Math.max(taskDetail.getMatTime(), taskDetail.getAmmoTime())) + taskDetail.getOilTime(); } // taskDetail.setTotalTime(totalTime); // 将计算结果添加到结果列表中 taskDetails.add(taskDetail); } // 将当前泊位的所有船只的计算结果添加到最终结果中 result.addAll(taskDetails); } return result; } /** * 根据类型获取消耗时间 * @param list * @param val * @param type * @return */ private Double getTimeByType(List list, Double val, String type){ AtomicReference totalTime = new AtomicReference<>(0.0); // 参数为空 则返回0 if(null != val && StringUtils.isNotBlank(type)){ list.stream().forEach(item -> { if(item.getTYPE().equals(type) && null != item.getTIME()){ totalTime.set(item.getTIME() * val); } }); } return totalTime.get(); } // 根据ID获取泊位路径信息详情 private String getBerthPathById(List list, Long id){ for(DmBerth2 berth2 : list){ if(berth2.getPKID() == id){ return berth2.getPath(); } } return ""; } private void updateDetailPath(Long taskId, Long deptId, List berth2List){ // 获取任务详情列表 List taskDetails = iDsTaskDetailService.selectDsTaskDetailByTaskId(taskId, null); // 创建泊位ID到路径的映射 Map berth2Map = berth2List.stream().collect(Collectors.toMap(DmBerth2::getPKID, berth -> berth)); // 批量更新任务详情 List updatedDetails = taskDetails.stream() .filter(detail -> detail.getBerthId() != null) .peek(detail -> detail.setPath(berth2Map.get(detail.getBerthId()).getPath())) .collect(Collectors.toList()); // 批量保存更新 if (!updatedDetails.isEmpty()) { iDsTaskDetailService.batchUpdateDsTaskDetail(updatedDetails); } } }