From bc64b3b45cf00885696aea4cfa97230390717012 Mon Sep 17 00:00:00 2001 From: zhang <zc857179121@qq.com> Date: 星期二, 17 六月 2025 15:38:59 +0800 Subject: [PATCH] 1 --- zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/BaseMainService.java | 442 ------------------------------------------------------- 1 files changed, 2 insertions(+), 440 deletions(-) diff --git a/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/MainService.java b/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/BaseMainService.java similarity index 72% rename from zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/MainService.java rename to zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/BaseMainService.java index c18e9f1..d9c55cb 100644 --- a/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/MainService.java +++ b/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/BaseMainService.java @@ -19,14 +19,10 @@ import com.zy.acs.framework.common.Cools; import com.zy.acs.framework.common.SnowflakeIdWorker; import com.zy.acs.framework.exception.CoolException; -import com.zy.acs.manager.common.domain.TaskDto; -import com.zy.acs.manager.common.domain.param.HandlerPublishParam; import com.zy.acs.manager.common.exception.BusinessException; -import com.zy.acs.manager.core.domain.AgvBackpackDto; import com.zy.acs.manager.core.domain.Lane; import com.zy.acs.manager.core.domain.TaskPosDto; import com.zy.acs.manager.core.service.astart.MapDataDispatcher; -import com.zy.acs.manager.manager.controller.param.OpenBusSubmitParam; import com.zy.acs.manager.manager.entity.*; import com.zy.acs.manager.manager.enums.*; import com.zy.acs.manager.manager.service.*; @@ -41,14 +37,13 @@ import org.springframework.transaction.interceptor.TransactionAspectSupport; import java.util.*; -import java.util.stream.Collectors; /** * Created by vincent on 2023/6/14 */ @Slf4j -@Service("mainService") -public class MainService implements IMainService { +@Service("baseMainService") +public class BaseMainService implements IBaseMainService { @Autowired private BusService busService; @@ -96,439 +91,6 @@ private LaneService laneService; @Autowired private ActionSorter actionSorter; - - @SuppressWarnings("all") - @Transactional - public Bus generateBusAndTask(OpenBusSubmitParam busSubmitParam, String memo) { - String errorMsg = busService.checkoutValid(busSubmitParam); - if (!Cools.isEmpty(errorMsg)) { - throw new BusinessException(errorMsg); - } - String batch = busSubmitParam.getBatch(); - List<TaskDto> taskDtoList = busSubmitParam.getTaskList(); - if (Cools.isEmpty(taskDtoList)) { - throw new BusinessException("taskList can't be empty!"); - } - - // 浼樺厛绾ф帓搴� - taskDtoList.sort((o1, o2) -> o2.getPriority() - o1.getPriority()); - - // 鏍¢獙 - List<Task> taskList = validService.validTaskDtoList(taskDtoList); - - // 淇濆瓨鎬荤嚎 - Date now = new Date(); - Bus bus = new Bus(); - bus.setUuid(String.valueOf(snowflakeIdWorker.nextId()).substring(3)); - bus.setBusNo(batch); - bus.setStartTime(now); - bus.setBusSts(BusStsType.RECEIVE.val()); - bus.setMemo(memo); - - if (!busService.save(bus)) { - throw new BusinessException("Internal Server Error!"); - } - - // 淇濆瓨浠诲姟 - for (Task task : taskList) { - task.setBusId(bus.getId()); - task.setTaskSts(TaskStsType.INIT.val()); - if (!taskService.save(task)) { - throw new BusinessException("seqNum:" + task.getSeqNum() + " failed to save!"); - } - - // 淇敼搴撲綅鐘舵�� - Loc oriLoc = null; - Loc destLoc = null; - Sta oriSta = null; - Sta destSta = null; - switch (TaskTypeType.get(task.getTaskTypeEl())) { - case LOC_TO_LOC: - oriLoc = locService.getById(task.getOriLoc()); - if (!oriLoc.getLocSts().equals(LocStsType.STOCK.val())) { - throw new BusinessException("oriLoc锛�" + task.getOriLoc$() + " is not in STOCK status"); - } - oriLoc.setLocSts(LocStsType.PAKOUT.val()); - oriLoc.setUpdateTime(now); - if (!locService.updateById(oriLoc)) { - throw new BusinessException("oriLoc锛�" + task.getOriLoc$() + " failed to update"); - } - - destLoc = locService.getById(task.getDestLoc()); - if (!destLoc.getLocSts().equals(LocStsType.IDLE.val())) { - throw new BusinessException("destLoc锛�" + task.getDestLoc$() + " is not in IDLE status"); - } - destLoc.setLocSts(LocStsType.PAKIN.val()); - destLoc.setUpdateTime(now); - if (!locService.updateById(destLoc)) { - throw new BusinessException("destLoc锛�" + task.getDestLoc$() + " failed to update"); - } - break; - case LOC_TO_STA: - oriLoc = locService.getById(task.getOriLoc()); - if (!oriLoc.getLocSts().equals(LocStsType.STOCK.val())) { - throw new BusinessException("oriLoc锛�" + task.getOriLoc$() + " is not in STOCK status"); - } - oriLoc.setLocSts(LocStsType.PAKOUT.val()); - oriLoc.setUpdateTime(now); - if (!locService.updateById(oriLoc)) { - throw new BusinessException("oriLoc锛�" + task.getOriLoc$() + " failed to update"); - } - - destSta = staService.getById(task.getDestSta()); - if (!destSta.getStaSts().equals(StaStsType.IDLE.val())) { - throw new BusinessException("destSta锛�" + task.getDestSta$() + " is not in IDLE status"); - } - destSta.setStaSts(StaStsType.READY_RELEASE.val()); - destSta.setUpdateTime(now); - if (!staService.updateById(destSta)) { - throw new BusinessException("destSta锛�" + task.getDestSta$() + " failed to update"); - } - break; - case STA_TO_LOC: - oriSta = staService.getById(task.getOriSta()); - if (!oriSta.getStaSts().equals(StaStsType.STOCK.val())) { - throw new BusinessException("oriSta锛�" + task.getOriSta$() + " is not in STOCK status"); - } - oriSta.setStaSts(StaStsType.READY_TAKE.val()); - oriSta.setUpdateTime(now); - if (!staService.updateById(oriSta)) { - throw new BusinessException("oriSta锛�" + task.getOriSta$() + " failed to update"); - } - - destLoc = locService.getById(task.getDestLoc()); - if (!destLoc.getLocSts().equals(LocStsType.IDLE.val())) { - throw new BusinessException("destLoc锛�" + task.getDestLoc$() + " is not in IDLE status"); - } - destLoc.setLocSts(LocStsType.PAKIN.val()); - destLoc.setUpdateTime(now); - if (!locService.updateById(destLoc)) { - throw new BusinessException("destLoc锛�" + task.getDestLoc$() + " failed to update"); - } - break; - case STA_TO_STA: - oriSta = staService.getById(task.getOriSta()); - if (!oriSta.getStaSts().equals(StaStsType.STOCK.val())) { - throw new BusinessException("oriSta锛�" + task.getOriSta$() + " is not in STOCK status"); - } - oriSta.setStaSts(StaStsType.READY_TAKE.val()); - oriSta.setUpdateTime(now); - if (!staService.updateById(oriSta)) { - throw new BusinessException("oriSta锛�" + task.getOriSta$() + " failed to update"); - } - - destSta = staService.getById(task.getDestSta()); - if (!destSta.getStaSts().equals(StaStsType.IDLE.val())) { - throw new BusinessException("destSta锛�" + task.getDestSta$() + " is not in IDLE status"); - } - destSta.setStaSts(StaStsType.READY_RELEASE.val()); - destSta.setUpdateTime(now); - if (!staService.updateById(destSta)) { - throw new BusinessException("destSta锛�" + task.getDestSta$() + " failed to update"); - } - break; - default: - break; - } - - } - return bus; - } - - /** - * 浠诲姟鍒嗛厤缁欒溅杈� ( 杞﹁締姝ゆ椂鏄┖闂蹭笖闈欐鐨� ) - */ - @Transactional - public synchronized void allocateTask(Bus bus) { - try { - Date now = new Date(); - List<Task> taskList = taskService.list(new LambdaQueryWrapper<Task>() - .eq(Task::getBusId, bus.getId()) - .eq(Task::getTaskSts, TaskStsType.INIT.val()) - .orderByDesc(Task::getPriority) - ); - - if (Cools.isEmpty(taskList)) { - bus.setBusSts(BusStsType.PROGRESS.val()); - bus.setUpdateTime(now); - if (!busService.updateById(bus)) { - log.error("Bus [{}] failed to Update 锛侊紒锛�", bus.getUuid()); - } - return; - } - - List<Long> taskIds = taskList.stream().map(Task::getId).distinct().collect(Collectors.toList()); - for (Task task : taskList) { - Agv agv = allocateService.execute(task); - if (null == agv) { -// log.warn("Task[{}] has an issue锛� because it failed to check out agv which is idle...", task.getSeqNum()); - continue; - } - task.setAgvId(agv.getId()); - task.setTaskSts(TaskStsType.WAITING.val()); - task.setIoTime(now); - task.setUpdateTime(now); - if (!taskService.updateById(task)) { - throw new BusinessException("seqNum: " + task.getSeqNum() + " failed to update"); - } - } - } catch (Exception e) { - log.error("mainService.infuseAgvForTask", e); - TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); - } - } - - /** - * 瑙f瀽鍙栨斁璐ч泦鍚堜换鍔�,杩涜鏈�浼樼殑鎺掑垪缁勫悎椤哄簭 ( 杞﹁締姝ゆ椂鏄┖闂蹭笖闈欐鐨� ) - * todo: {@link com.zy.acs.manager.core.HandlerController#controlAgv(String, HandlerPublishParam)} - */ - @Transactional(propagation = Propagation.REQUIRES_NEW) - public void buildMajorTask(Long agvId, List<Task> taskList) { - if (Cools.isEmpty(agvId, taskList)) { - return; - } - try { - // valid ----------------------------------------------- - Agv agv = agvService.getById(agvId); - if (!agvService.judgeEnable(agv.getId(), false)) { - return; - } - if (!Cools.isEmpty(taskService.selectInSts(agvId, TaskStsType.ASSIGN, TaskStsType.PROGRESS))) { - throw new CoolException("AGV[" + agv.getUuid() + "]鍒嗛厤浠诲姟澶辫触锛屽凡瀛樺湪鎵ц浠诲姟..."); - } - if (!Cools.isEmpty(segmentService.getByAgvAndState(agv.getId(), SegmentStateType.WAITING.toString())) - || !Cools.isEmpty(segmentService.getByAgvAndState(agv.getId(), SegmentStateType.RUNNING.toString()))) { - throw new CoolException("AGV[" + agv.getUuid() + "] failed to assign锛宐ecause already has the segment in running..."); - } - - // execute ---------------------------------------------------- - Date now = new Date(); - // sort and sub - taskList.sort(new Comparator<Task>() { - @Override - public int compare(Task o1, Task o2) { - return o2.getPriority() - o1.getPriority(); - } - }); - Integer backpack = agvService.getBackpack(agv); - if (taskList.size() > backpack) { - taskList = taskList.subList(0, backpack); - } - - AgvDetail agvDetail = agvDetailService.selectByAgvId(agvId); - List<AgvBackpackDto> backpackDtoList = new ArrayList<>(); - - /** - * 鍚屽贩閬撳綊绫� - * same lane for every single agvId - * - * key: y(val) + TaskPosDto.PosType.ORI_LOC / ORI_STA / DEST_LOC / DEST_STA - * val: new TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType) - */ - Map<String, List<TaskPosDto>> groups = new HashMap<>(); - final String sameGroupXy = configService.getVal("sameGroupXy", String.class); - - int backpackLev = 0; - for (Task task : taskList) { - - backpackLev++; - Code startCode = null; - Code endCode = null; - Loc oriLoc = null; - Loc destLoc = null; - Sta oriSta = null; - Sta destSta = null; - switch (Objects.requireNonNull(TaskTypeType.get(task.getTaskTypeEl()))) { - case LOC_TO_LOC: - oriLoc = locService.getById(task.getOriLoc()); - destLoc = locService.getById(task.getDestLoc()); - - startCode = codeService.getCacheById(oriLoc.getCode()); - endCode = codeService.getCacheById(destLoc.getCode()); - - TaskPosDto.packagePosGroup(groups, task, startCode, TaskPosDto.PosType.ORI_LOC, sameGroupXy); - TaskPosDto.packagePosGroup(groups, task, endCode, TaskPosDto.PosType.DEST_LOC, sameGroupXy); - break; - case LOC_TO_STA: - oriLoc = locService.getById(task.getOriLoc()); - destSta = staService.getById(task.getDestSta()); - - startCode = codeService.getCacheById(oriLoc.getCode()); - endCode = codeService.getCacheById(destSta.getCode()); - - TaskPosDto.packagePosGroup(groups, task, startCode, TaskPosDto.PosType.ORI_LOC, sameGroupXy); - TaskPosDto.packagePosGroup(groups, task, endCode, TaskPosDto.PosType.DEST_STA, sameGroupXy); - break; - case STA_TO_LOC: - oriSta = staService.getById(task.getOriSta()); - destLoc = locService.getById(task.getDestLoc()); - - startCode = codeService.getCacheById(oriSta.getCode()); - endCode = codeService.getCacheById(destLoc.getCode()); - - TaskPosDto.packagePosGroup(groups, task, startCode, TaskPosDto.PosType.ORI_STA, sameGroupXy); - TaskPosDto.packagePosGroup(groups, task, endCode, TaskPosDto.PosType.DEST_LOC, sameGroupXy); - break; - case STA_TO_STA: - oriSta = staService.getById(task.getOriSta()); - destSta = staService.getById(task.getDestSta()); - - startCode = codeService.getCacheById(oriSta.getCode()); - endCode = codeService.getCacheById(destSta.getCode()); - - TaskPosDto.packagePosGroup(groups, task, startCode, TaskPosDto.PosType.ORI_STA, sameGroupXy); - TaskPosDto.packagePosGroup(groups, task, endCode, TaskPosDto.PosType.DEST_STA, sameGroupXy); - break; - default: - throw new BusinessException(task.getSeqNum() + "浠诲姟绫诲瀷閿欒"); - - } - - if (backpackLev > backpack) { - throw new BusinessException("瑙f瀽Task澶辫触,AGV鑳岀瘬宸叉弧......"); - } - - backpackDtoList.add(new AgvBackpackDto(backpackLev, task.getId())); - } - - /** - * 1.Map<String, List<TaskPosDto>> groups - * - * key: 1000 + ORIGIN - * val: [TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType), TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType),...] - * - * key: 3000 + ORIGIN - * val: [TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType), TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType),...] - * - * key: 2000 + ORIGIN - * val: [TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType), TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType),...] - * - * key: 1000 + DESTINATION - * val: [TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType), TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType),...] - * - * key: 2000 + DESTINATION - * val: [TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType), TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType),...] - * - * ...... - * - * 2.ArrayList<List<TaskPosDto>> list - * [ - * [TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType), TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType),...], - * [TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType), TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType),...], - * [TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType), TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType),...], - * ...... - * ] - */ - - /** - * 瀵规墍鏈夊贩閬撹繘琛屾湁搴忔帓搴忥紝鍏堟槸閽堝List涓轰竴涓崟浣嶏紝瀵逛粬浠繘琛孎irstWeight鎺掑簭锛岀浉褰撲簬琛�1鐨刱ey鐨勬暟鍊艰繘琛屾湁搴忔帓搴� - * List<TaskPosDto>: task list on the same lane - * ArrayList<List<TaskPosDto>>: all the task list by one agv - * - * tip: ORI 鍜� DEST 姘歌繙涓嶄細瀛樺湪鍚屼竴涓� List - */ - ArrayList<List<TaskPosDto>> list = new ArrayList<>(groups.values()); - list.sort((o1, o2) -> { - double o1CompVal = (o1.get(0).getFirstWeight(sameGroupXy) * 100) + o1.get(0).getPosType().compOffset; - double o2CompVal = (o2.get(0).getFirstWeight(sameGroupXy) * 100) + o2.get(0).getPosType().compOffset; - return (int) (o1CompVal - o2CompVal); - }); - // 閽堝Dto锛屾寜鐓econdWeight杩涜鎺掑簭 - for (List<TaskPosDto> taskPosDtoList : list) { - taskPosDtoList.sort((o1, o2) -> (int) (o1.getSecondWeight(sameGroupXy) * 100 - o2.getSecondWeight(sameGroupXy) * 100)); - } - - // re-order by agv current position - Code currCode = codeService.getCacheById(agvDetail.getRecentCode()); - Double[] currPosition = new Double[]{currCode.getX(), currCode.getY()}; - - List<List<TaskPosDto>> pickGroups = new ArrayList<>(); - List<List<TaskPosDto>> dropGroups = new ArrayList<>(); - - for (List<TaskPosDto> group : list) { - // Assume 涓�涓换鍔$粍涓墍鏈塗askPosDto鐨凱osType.brief鐩稿悓 - TaskPosDto.PosType posType = group.get(0).getPosType(); - if (posType == TaskPosDto.PosType.ORI_LOC || posType == TaskPosDto.PosType.ORI_STA) { - pickGroups.add(group); - } else if (posType == TaskPosDto.PosType.DEST_LOC || posType == TaskPosDto.PosType.DEST_STA) { - dropGroups.add(group); - } else { - // import tip: the list must only contain ORIGIN and DESTINATION - log.error("the list must only contain ORIGIN and DESTINATION"); - } - } - - currPosition = allocateService.pac(currPosition, pickGroups); - currPosition = allocateService.pac(currPosition, dropGroups); - - List<List<TaskPosDto>> reorderedList = new ArrayList<>(); - reorderedList.addAll(pickGroups); - reorderedList.addAll(dropGroups); - - list.clear(); - list.addAll(reorderedList); - - - // generate travel - Travel travel = new Travel(); - travel.setUuid(String.valueOf(snowflakeIdWorker.nextId()).substring(3)); - travel.setTravelId(String.valueOf(snowflakeIdWorker.nextId()).substring(3)); - travel.setAgvId(agvId); - travel.setTaskContent(JSON.toJSONString(list)); - travel.setTaskIds(JSON.toJSONString(taskList.stream().map(Task::getId).collect(Collectors.toList()))); - travel.setState(TravelStateType.RUNNING.toString()); - if (!travelService.save(travel)) { - throw new BusinessException("浠诲姟缁勪繚瀛樺け璐�"); - } - - // generate segment - int segSerial = 0; - List<Segment> segmentList = new ArrayList<>(); - for (List<TaskPosDto> dtoList : list) { - for (TaskPosDto taskPosDto : dtoList) { - segSerial++; - AgvBackpackType backpackType = AgvBackpackDto.find(backpackDtoList, taskPosDto.getTaskId()); - assert null != backpackType; - - Segment segment = new Segment(); - segment.setUuid(String.valueOf(snowflakeIdWorker.nextId()).substring(3)); - segment.setTravelId(travel.getId()); - segment.setAgvId(agvId); - segment.setTaskId(taskPosDto.getTaskId()); - segment.setSerial(segSerial); - segment.setEndNode(taskPosDto.getCodeId()); - segment.setPosType(taskPosDto.getPosType().toString()); - segment.setBackpack(backpackType.lev); - segment.setState(SegmentStateType.INIT.toString()); - segmentList.add(segment); - } - } - for (int i = 0; i < segmentList.size(); i++) { - Segment segment = segmentList.get(i); - if (i == 0) { - segment.setState(SegmentStateType.WAITING.toString()); - } - if (!segmentService.save(segment)) { - throw new BusinessException("浠诲姟缁勪繚瀛樺け璐�"); - } - } - - // task - for (Task task : taskList) { - task.setTaskSts(TaskStsType.ASSIGN.val()); - task.setStartTime(now); - task.setUpdateTime(now); - if (!taskService.updateById(task)) { - throw new BusinessException(task.getUuid() + "浠诲姟鏇存柊澶辫触"); - } - } - - } catch (Exception e) { - - log.error("mainService.buildMajorTask[task]", e); - TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); - } - } /** -- Gitblit v1.9.1