| package com.zy.acs.manager.core.service; | 
|   | 
| import com.alibaba.fastjson.JSON; | 
| import com.zy.acs.framework.common.Cools; | 
| import com.zy.acs.framework.common.SnowflakeIdWorker; | 
| import com.zy.acs.manager.common.domain.TaskDto; | 
| import com.zy.acs.manager.common.exception.BusinessException; | 
| import com.zy.acs.manager.manager.entity.Code; | 
| import com.zy.acs.manager.manager.entity.Loc; | 
| import com.zy.acs.manager.manager.entity.Sta; | 
| import com.zy.acs.manager.manager.entity.Task; | 
| import com.zy.acs.manager.manager.enums.TaskTypeType; | 
| import com.zy.acs.manager.manager.service.CodeService; | 
| import com.zy.acs.manager.manager.service.LocService; | 
| import com.zy.acs.manager.manager.service.StaService; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.stereotype.Component; | 
|   | 
| import java.util.ArrayList; | 
| import java.util.List; | 
| import java.util.Objects; | 
|   | 
| /** | 
|  * Created by vincent on 2023/6/14 | 
|  */ | 
| @Component("validService") | 
| public class ValidService { | 
|   | 
|     @Autowired | 
|     private StaService staService; | 
|     @Autowired | 
|     private LocService locService; | 
|     @Autowired | 
|     private CodeService codeService; | 
|     @Autowired | 
|     private MapService mapService; | 
|     @Autowired | 
|     private SnowflakeIdWorker snowflakeIdWorker; | 
|   | 
|     public List<Task> validBusDto(List<TaskDto> taskDtoList) { | 
|         List<Task> taskList = new ArrayList<>(); | 
|         for (TaskDto taskDto : taskDtoList) { | 
|             if (Cools.isEmpty(taskDto.getSeqNum())) { | 
|                 throw new BusinessException("seqNum不能为空"); | 
|             } | 
|             Task task = new Task(); | 
|             taskList.add(task); | 
|             task.setUuid(String.valueOf(snowflakeIdWorker.nextId()).substring(3)); | 
|             task.setSeqNum(taskDto.getSeqNum()); | 
|             task.setPriority(taskDto.getPriority()); | 
|   | 
|             // ori -------------------------- | 
|             if (!Cools.isEmpty(taskDto.getOriLoc())) { | 
|                 Loc oriLoc = locService.selecatByLocNo(taskDto.getOriLoc()); | 
|                 if (null == oriLoc) { | 
|                     throw new BusinessException("oriLoc: " + taskDto.getOriLoc() + "不存在"); | 
|                 } | 
|                 task.setOriLoc(oriLoc.getId()); | 
|             } | 
|             if (!Cools.isEmpty(taskDto.getOriSta())) { | 
|                 Sta oriSta = staService.selectByStaNo(taskDto.getOriSta()); | 
|                 if (null == oriSta) { | 
|                     throw new BusinessException("oriSta: " + taskDto.getOriSta() + "不存在"); | 
|                 } | 
|                 task.setOriSta(oriSta.getId()); | 
|             } | 
|             // dest -------------------------- | 
|             if (!Cools.isEmpty(taskDto.getDestLoc())) { | 
|                 Loc destLoc = locService.selecatByLocNo(taskDto.getDestLoc()); | 
|                 if (null == destLoc) { | 
|                     throw new BusinessException("destLoc: " + taskDto.getDestLoc() + "不存在"); | 
|                 } | 
|                 task.setDestLoc(destLoc.getId()); | 
|                 task.setDestCode(destLoc.getCode()); | 
|             } | 
|             if (!Cools.isEmpty(taskDto.getDestSta())) { | 
|                 Sta destSta = staService.selectByStaNo(taskDto.getDestSta()); | 
|                 if (null == destSta) { | 
|                     throw new BusinessException("destSta: " + taskDto.getDestSta() + "不存在"); | 
|                 } | 
|                 task.setDestSta(destSta.getId()); | 
|                 task.setDestCode(destSta.getCode()); | 
|             } | 
|   | 
|             // task-type ---------------------------------- | 
|             if (null == task.getOriLoc() && null == task.getOriSta()) { | 
|                 throw new BusinessException(task.getSeqNum() + " 错误,oriLoc和oriSta必须存在一项"); | 
|             } | 
|             if (null != task.getOriLoc() && null != task.getOriSta()) { | 
|                 throw new BusinessException(task.getSeqNum() + " 错误,oriLoc和oriSta只能存在一项"); | 
|             } | 
|             if (null == task.getDestLoc() && null == task.getDestSta()) { | 
|                 throw new BusinessException(task.getSeqNum() + " 错误,destLoc和destSta必须存在一项"); | 
|             } | 
|             if (null != task.getDestLoc() && null != task.getDestSta()) { | 
|                 throw new BusinessException(task.getSeqNum() + " 错误,destLoc和destSta只能存在一项"); | 
|             } | 
|             if (null != task.getOriSta()) { | 
|                 if (null != task.getDestLoc()) { | 
|                     task.setTaskType(TaskTypeType.STA_TO_LOC.val()); | 
|                 } | 
|                 if (null != task.getDestSta()) { | 
|                     task.setTaskType(TaskTypeType.STA_TO_STA.val()); | 
|                 } | 
|             } | 
|             if (null != task.getOriLoc()) { | 
|                 if (null != task.getDestLoc()) { | 
|                     task.setTaskType(TaskTypeType.LOC_TO_LOC.val()); | 
|                 } | 
|                 if (null != task.getDestSta()) { | 
|                     task.setTaskType(TaskTypeType.LOC_TO_STA.val()); | 
|                 } | 
|             } | 
|         } | 
|   | 
|         // 校验路径合理性 | 
|         for (Task task : taskList) { | 
|             Code startCode = null; | 
|             Code endCode = null; | 
|             Loc oriLoc = null; Loc destLoc = null; | 
|             Sta oriSta = null; Sta destSta = null; | 
|             List<String> pathList = null; | 
|             switch (Objects.requireNonNull(TaskTypeType.get(task.getTaskTypeEl()))) { | 
|                 case LOC_TO_LOC: | 
|                     oriLoc = locService.getById(task.getOriLoc()); | 
|                     destLoc = locService.getById(task.getDestLoc()); | 
|   | 
|                     startCode = codeService.getById(oriLoc.getCode()); | 
|                     endCode = codeService.getById(destLoc.getCode()); | 
|                     if (null == startCode) { | 
|                         throw new BusinessException("oriLoc:" + oriLoc.getLocNo() + " 未绑定地面码"); | 
|                     } | 
|                     if (null == endCode) { | 
|                         throw new BusinessException("destLoc:" + destLoc.getLocNo() + " 未绑定地面码"); | 
|                     } | 
|                     pathList = mapService.validFeasibility(startCode, endCode); | 
|                     if (Cools.isEmpty(pathList)) { | 
|                         throw new BusinessException(task.getSeqNum() + "任务错误,oriLoc:" + task.getOriLoc() + "无法行走至 destLoc" + task.getDestLoc()); | 
|                     } | 
|                     task.setPhase(JSON.toJSONString(pathList)); | 
|                     break; | 
|                 case LOC_TO_STA: | 
|                     oriLoc = locService.getById(task.getOriLoc()); | 
|                     destSta = staService.getById(task.getDestSta()); | 
|   | 
|                     startCode = codeService.getById(oriLoc.getCode()); | 
|                     endCode = codeService.getById(destSta.getCode()); | 
|                     if (null == startCode) { | 
|                         throw new BusinessException("oriLoc:" + oriLoc.getLocNo() + " 未绑定地面码"); | 
|                     } | 
|                     if (null == endCode) { | 
|                         throw new BusinessException("destSta:" + destSta.getStaNo() + " 未绑定地面码"); | 
|                     } | 
|                     pathList = mapService.validFeasibility(startCode, endCode); | 
|                     if (Cools.isEmpty(pathList)) { | 
|                         throw new BusinessException(task.getSeqNum() + "任务错误,oriLoc:" + task.getOriLoc() + "无法行走至 destSta" + task.getDestSta()); | 
|                     } | 
|                     task.setPhase(JSON.toJSONString(pathList)); | 
|                     break; | 
|                 case STA_TO_LOC: | 
|                     oriSta = staService.getById(task.getOriSta()); | 
|                     destLoc = locService.getById(task.getDestLoc()); | 
|   | 
|                     startCode = codeService.getById(oriSta.getCode()); | 
|                     endCode = codeService.getById(destLoc.getCode()); | 
|                     if (null == startCode) { | 
|                         throw new BusinessException("oriSta:" + oriSta.getStaNo() + " 未绑定地面码"); | 
|                     } | 
|                     if (null == endCode) { | 
|                         throw new BusinessException("destLoc:" + destLoc.getLocNo() + " 未绑定地面码"); | 
|                     } | 
|                     pathList = mapService.validFeasibility(startCode, endCode); | 
|                     if (Cools.isEmpty(pathList)) { | 
|                         throw new BusinessException(task.getSeqNum() + "任务错误,oriSta:" + task.getOriSta() + "无法行走至 destLoc" + task.getDestLoc()); | 
|                     } | 
|                     task.setPhase(JSON.toJSONString(pathList)); | 
|                     break; | 
|                 case STA_TO_STA: | 
|                     oriSta = staService.getById(task.getOriSta()); | 
|                     destSta = staService.getById(task.getDestSta()); | 
|   | 
|                     startCode = codeService.getById(oriSta.getCode()); | 
|                     endCode = codeService.getById(destSta.getCode()); | 
|                     if (null == startCode) { | 
|                         throw new BusinessException("oriSta:" + oriSta.getStaNo() + " 未绑定地面码"); | 
|                     } | 
|                     if (null == endCode) { | 
|                         throw new BusinessException("destSta:" + destSta.getStaNo() + " 未绑定地面码"); | 
|                     } | 
|                     pathList = mapService.validFeasibility(startCode, endCode); | 
|                     if (Cools.isEmpty(pathList)) { | 
|                         throw new BusinessException(task.getSeqNum() + "任务错误,oriSta:" + task.getOriSta() + "无法行走至 destSta" + task.getDestSta()); | 
|                     } | 
|                     task.setPhase(JSON.toJSONString(pathList)); | 
|                     break; | 
|                 default: | 
|                     throw new BusinessException(task.getSeqNum() + "任务类型错误"); | 
|             } | 
|   | 
|         } | 
|   | 
|         return taskList; | 
|     } | 
|   | 
| } |