package com.zy.asrs.task; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.core.common.Cools; import com.zy.asrs.entity.BasCrnp; import com.zy.asrs.entity.BasStation; import com.zy.asrs.entity.WrkMast; import com.zy.asrs.service.BasCrnpService; import com.zy.asrs.service.BasStationService; import com.zy.asrs.service.WrkMastService; import com.zy.asrs.utils.Utils; import com.zy.common.entity.FindCrnNoResult; import com.zy.common.service.CommonService; import com.zy.core.News; import com.zy.core.cache.SlaveConnection; import com.zy.core.enums.SlaveType; import com.zy.core.enums.WrkIoType; import com.zy.core.enums.WrkStsType; import com.zy.core.model.StationObjModel; import com.zy.core.model.protocol.StationProtocol; import com.zy.core.move.StationMoveCoordinator; import com.zy.core.move.StationMoveSession; import com.zy.core.thread.StationThread; import com.zy.core.utils.CrnOperateProcessUtils; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.List; import java.util.Map; import java.util.Objects; @Component public class InboundCrnMoveDispatchScheduler { private final WrkMastService wrkMastService; private final BasStationService basStationService; private final CommonService commonService; private final BasCrnpService basCrnpService; private final CrnOperateProcessUtils crnOperateProcessUtils; private final StationMoveCoordinator stationMoveCoordinator; public InboundCrnMoveDispatchScheduler(WrkMastService wrkMastService, BasStationService basStationService, CommonService commonService, BasCrnpService basCrnpService, CrnOperateProcessUtils crnOperateProcessUtils, StationMoveCoordinator stationMoveCoordinator) { this.wrkMastService = wrkMastService; this.basStationService = basStationService; this.commonService = commonService; this.basCrnpService = basCrnpService; this.crnOperateProcessUtils = crnOperateProcessUtils; this.stationMoveCoordinator = stationMoveCoordinator; } @Scheduled(fixedDelay = 1000L) public void dispatchInboundCrnMove() { List wrkMasts = wrkMastService.list(new QueryWrapper() .eq("io_type", WrkIoType.IN.id) .eq("wrk_sts", WrkStsType.INBOUND_STATION_RUN.sts) .isNotNull("sta_no")); for (WrkMast wrkMast : wrkMasts) { if (wrkMast == null || wrkMast.getWrkNo() == null || wrkMast.getStaNo() == null || Cools.isEmpty(wrkMast.getLocNo())) { continue; } BasStation basStation = basStationService.getOne(new QueryWrapper() .eq("station_id", wrkMast.getStaNo()) .last("limit 1")); if (basStation == null || basStation.getDeviceNo() == null) { continue; } StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basStation.getDeviceNo()); if (stationThread == null) { continue; } Map statusMap = stationThread.getStatusMap(); StationProtocol stationProtocol = statusMap == null ? null : statusMap.get(basStation.getStationId()); tryDispatchInboundCrnMove(wrkMast, stationProtocol); } } private void tryDispatchInboundCrnMove(WrkMast wrkMast, StationProtocol targetStationProtocol) { if (targetStationProtocol != null && targetStationProtocol.isLoading() && targetStationProtocol.getTaskNo() > 0 && targetStationProtocol.isInEnable()) { return; } FindCrnNoResult findCrnNoResult = commonService.findCrnNoByLocNo(wrkMast.getLocNo()); if (findCrnNoResult == null || !Objects.equals(findCrnNoResult.getCrnType(), SlaveType.Crn) || findCrnNoResult.getCrnNo() == null) { return; } Integer crnNo = findCrnNoResult.getCrnNo(); BasCrnp basCrnp = basCrnpService.getOne(new QueryWrapper() .eq("crn_no", crnNo) .last("limit 1")); if (basCrnp == null) { return; } String inboundPickupLocNo = resolveInboundPickupLocNo(basCrnp, wrkMast.getStaNo()); if (Cools.isEmpty(inboundPickupLocNo)) { return; } // 同堆垛机没有未完成出库任务时,优先直接到当前入库任务取货位等待。 if (!hasPendingOutboundTask(crnNo)) { boolean dispatched = crnOperateProcessUtils.dispatchCrnMove(crnNo, inboundPickupLocNo); if (dispatched) { News.info("检测到仅有入库任务,已触发堆垛机直接移动到入库任务取货位等待,工作号={},堆垛机号={},取货位={}", wrkMast.getWrkNo(), crnNo, inboundPickupLocNo); } return; } StationMoveSession session = stationMoveCoordinator == null ? null : stationMoveCoordinator.loadSession(wrkMast.getWrkNo()); if (!isInboundCrnMoveDispatchWindow(wrkMast, session)) { return; } boolean dispatched = crnOperateProcessUtils.dispatchCrnMove(crnNo, inboundPickupLocNo); if (dispatched) { News.info("入库任务即将到达取货位,已触发堆垛机预移车,工作号={},堆垛机号={},取货位={}", wrkMast.getWrkNo(), crnNo, inboundPickupLocNo); } } private String resolveInboundPickupLocNo(BasCrnp basCrnp, Integer targetStationId) { if (basCrnp == null || targetStationId == null) { return null; } for (StationObjModel stationObjModel : basCrnp.getInStationList$()) { if (stationObjModel == null || !Objects.equals(stationObjModel.getStationId(), targetStationId)) { continue; } if (stationObjModel.getDeviceRow() == null || stationObjModel.getDeviceBay() == null || stationObjModel.getDeviceLev() == null) { continue; } return Utils.getLocNo(stationObjModel.getDeviceRow(), stationObjModel.getDeviceBay(), stationObjModel.getDeviceLev()); } return null; } private boolean hasPendingOutboundTask(Integer crnNo) { if (crnNo == null) { return false; } return wrkMastService.count(new QueryWrapper() .eq("crn_no", crnNo) .eq("io_type", WrkIoType.OUT.id) .in("wrk_sts", WrkStsType.NEW_OUTBOUND.sts, WrkStsType.OUTBOUND_RUN.sts, WrkStsType.OUTBOUND_RUN_COMPLETE.sts, WrkStsType.STATION_RUN.sts, WrkStsType.STATION_RUN_COMPLETE.sts, WrkStsType.OUTBOUND_MANUAL.sts)) > 0; } private boolean isInboundCrnMoveDispatchWindow(WrkMast wrkMast, StationMoveSession session) { if (wrkMast == null || session == null || !session.isActive() || wrkMast.getStaNo() == null) { return false; } List fullPathStationIds = session.getFullPathStationIds(); Integer currentStationId = session.getCurrentStationId(); if (fullPathStationIds == null || fullPathStationIds.isEmpty() || currentStationId == null) { return false; } int currentIndex = fullPathStationIds.lastIndexOf(currentStationId); if (currentIndex < 0 || currentIndex >= fullPathStationIds.size() - 1) { return false; } int remainingStationCount = fullPathStationIds.size() - currentIndex - 1; if (remainingStationCount != 1) { return false; } Integer nextStationId = fullPathStationIds.get(currentIndex + 1); return Objects.equals(nextStationId, wrkMast.getStaNo()); } }