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.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.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; import java.util.stream.Collectors; @Component public class InboundCrnMoveDispatchScheduler { private final WrkMastService wrkMastService; private final BasStationService basStationService; private final BasCrnpService basCrnpService; private final CrnOperateProcessUtils crnOperateProcessUtils; public InboundCrnMoveDispatchScheduler(WrkMastService wrkMastService, BasStationService basStationService, BasCrnpService basCrnpService, CrnOperateProcessUtils crnOperateProcessUtils) { this.wrkMastService = wrkMastService; this.basStationService = basStationService; this.basCrnpService = basCrnpService; this.crnOperateProcessUtils = crnOperateProcessUtils; } @Scheduled(fixedDelay = 1000L) public void dispatchInboundCrnMove() { List basCrnps = basCrnpService.list(new QueryWrapper() .orderByAsc("crn_no")); if (basCrnps == null || basCrnps.isEmpty()) { return; } for (BasCrnp basCrnp : basCrnps) { if (basCrnp == null || basCrnp.getCrnNo() == null) { continue; } Integer crnNo = basCrnp.getCrnNo(); if (hasBlockingOutboundTask(crnNo)) { continue; } WrkMast inboundWrkMast = wrkMastService.getOne(new QueryWrapper() .eq("io_type", WrkIoType.IN.id) .eq("wrk_sts", WrkStsType.INBOUND_STATION_RUN.sts) .last("limit 1")); if (inboundWrkMast == null) { continue; } StationProtocol targetStationProtocol = resolveTargetStationProtocol(inboundWrkMast.getStaNo()); if (targetStationProtocol != null && targetStationProtocol.isLoading() && targetStationProtocol.getTaskNo() > 0 && targetStationProtocol.isInEnable()) { continue; } String inboundPickupLocNo = resolveInboundPickupLocNo(basCrnp, inboundWrkMast.getStaNo()); if (Cools.isEmpty(inboundPickupLocNo)) { continue; } boolean dispatched = crnOperateProcessUtils.dispatchCrnMove(crnNo, inboundPickupLocNo, true); if (dispatched) { News.info("已触发堆垛机直接移动到入库任务取货位等待,工作号={},堆垛机号={},取货位={}", inboundWrkMast.getWrkNo(), crnNo, inboundPickupLocNo); } } } private StationProtocol resolveTargetStationProtocol(Integer stationId) { if (stationId == null) { return null; } BasStation basStation = basStationService.getOne(new QueryWrapper() .eq("station_id", stationId) .last("limit 1")); if (basStation == null || basStation.getDeviceNo() == null) { return null; } StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basStation.getDeviceNo()); if (stationThread == null) { return null; } Map statusMap = stationThread.getStatusMap(); return statusMap == null ? null : statusMap.get(stationId); } 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 hasBlockingOutboundTask(Integer crnNo) { if (crnNo == null) { return false; } List pendingOutboundTasks = wrkMastService.list(new QueryWrapper() .eq("crn_no", crnNo) .eq("io_type", WrkIoType.OUT.id) .in("wrk_sts", WrkStsType.NEW_OUTBOUND.sts, WrkStsType.OUTBOUND_RUN.sts) .orderByAsc("wrk_no")); if (pendingOutboundTasks == null || pendingOutboundTasks.isEmpty()) { return false; } // 非批次任务或缺少批次序号的任务仍按原逻辑处理,避免放宽到无法确认执行顺序的出库任务。 boolean hasNonBatchTask = pendingOutboundTasks.stream().anyMatch(task -> !isBatchTaskWithSeq(task)); if (hasNonBatchTask) { return true; } return pendingOutboundTasks.stream() .filter(this::isCrnMoveBlockingOutboundTask) .filter(this::isBatchTaskWithSeq) .filter(crnOperateProcessUtils::canOutboundTaskExecuteInCurrentBatchWindow) .findAny() .isPresent(); } private boolean isBatchTaskWithSeq(WrkMast wrkMast) { return wrkMast != null && Objects.equals(wrkMast.getIoType(), WrkIoType.OUT.id) && !Cools.isEmpty(wrkMast.getBatch()) && wrkMast.getBatchSeq() != null; } private boolean isCrnMoveBlockingOutboundTask(WrkMast wrkMast) { if (wrkMast == null || wrkMast.getWrkSts() == null) { return false; } return Objects.equals(wrkMast.getWrkSts(), WrkStsType.NEW_OUTBOUND.sts) || Objects.equals(wrkMast.getWrkSts(), WrkStsType.OUTBOUND_RUN.sts) || Objects.equals(wrkMast.getWrkSts(), WrkStsType.OUTBOUND_MANUAL.sts); } }