Junjie
8 小时以前 7fdc76dde7cd00510347e3231c4f60a88b012107
src/main/java/com/zy/core/utils/CrnOperateProcessUtils.java
@@ -15,6 +15,7 @@
import com.zy.asrs.service.BasCrnpService;
import com.zy.asrs.service.BasStationService;
import com.zy.asrs.service.LocMastService;
import com.zy.asrs.service.WrkAnalysisService;
import com.zy.asrs.service.WrkMastService;
import com.zy.asrs.utils.NotifyUtils;
import com.zy.asrs.utils.Utils;
@@ -30,6 +31,8 @@
import com.zy.core.model.command.CrnCommand;
import com.zy.core.model.protocol.CrnProtocol;
import com.zy.core.model.protocol.StationProtocol;
import com.zy.core.task.MainProcessLane;
import com.zy.core.task.MainProcessTaskSubmitter;
import com.zy.core.thread.CrnThread;
import com.zy.core.thread.StationThread;
import org.springframework.beans.factory.annotation.Autowired;
@@ -39,8 +42,11 @@
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
@Component
public class CrnOperateProcessUtils {
@@ -63,95 +69,97 @@
    private NotifyUtils notifyUtils;
    @Autowired
    private StationOperateProcessUtils stationOperateProcessUtils;
    @Autowired
    private WrkAnalysisService wrkAnalysisService;
    @Autowired
    private MainProcessTaskSubmitter mainProcessTaskSubmitter;
    private static final String CRN_OUT_REQUIRE_STATION_OUT_ENABLE_CONFIG = "crnOutRequireStationOutEnable";
    public synchronized void crnIoExecute() {
        Set<Integer> crnMoveBlockedCrnNos = executeCrnMoveTask();
        List<BasCrnp> basCrnps = basCrnpService.list(new QueryWrapper<>());
        for (BasCrnp basCrnp : basCrnps) {
            crnIoExecute(basCrnp, crnMoveBlockedCrnNos);
        }
    }
    public void crnIoExecute(BasCrnp basCrnp, Set<Integer> crnMoveBlockedCrnNos) {
        if (basCrnp == null || basCrnp.getCrnNo() == null) {
            return;
        }
        Object systemConfigMapObj = redisUtil.get(RedisKeyType.SYSTEM_CONFIG_MAP.key);
        if (systemConfigMapObj != null) {
            HashMap<String, String> systemConfigMap = (HashMap<String, String>) systemConfigMapObj;
            if (systemConfigMap.get("crnRunMethod").equals("solver")) {
                plannerExecute();
                plannerExecute(basCrnp, crnMoveBlockedCrnNos);
            }else {
                crnIoExecuteNormal();
                crnIoExecuteNormal(basCrnp, crnMoveBlockedCrnNos);
            }
        }
    }
    //入出库  ===>>  堆垛机入出库作业下发
    public synchronized void crnIoExecuteNormal() {
        List<BasCrnp> basCrnps = basCrnpService.list(new QueryWrapper<>());
        Map<Integer, BasCrnp> dispatchCrnMap = new HashMap<>();
        Map<Integer, CrnThread> dispatchThreadMap = new HashMap<>();
        Map<Integer, CrnProtocol> dispatchProtocolMap = new HashMap<>();
        for (BasCrnp basCrnp : basCrnps) {
            CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, basCrnp.getCrnNo());
            if (crnThread == null) {
                continue;
            }
            CrnProtocol crnProtocol = crnThread.getStatus();
            if (crnProtocol == null) {
                continue;
            }
            long runningCount = wrkMastService.count(new QueryWrapper<WrkMast>()
                    .eq("crn_no", basCrnp.getCrnNo())
                    .in("wrk_sts", WrkStsType.INBOUND_RUN.sts, WrkStsType.OUTBOUND_RUN.sts, WrkStsType.LOC_MOVE_RUN.sts));
            if (runningCount > 0) {
                continue;
            }
            // 只有当堆垛机空闲 并且 无任务时才继续执行
            if (crnProtocol.getMode() == CrnModeType.AUTO.id
                    && crnProtocol.getTaskNo() == 0
                    && crnProtocol.getStatus() == CrnStatusType.IDLE.id
                    && crnProtocol.getLoaded() == 0
                    && crnProtocol.getForkPos() == 0
                    && crnProtocol.getAlarm() == 0
            ) {
                Object clearLock = redisUtil.get(RedisKeyType.CLEAR_CRN_TASK_LIMIT.key + basCrnp.getCrnNo());
                if (clearLock != null) {
                    continue;
                }
                dispatchCrnMap.put(basCrnp.getCrnNo(), basCrnp);
                dispatchThreadMap.put(basCrnp.getCrnNo(), crnThread);
                dispatchProtocolMap.put(basCrnp.getCrnNo(), crnProtocol);
            }
        }
        if (dispatchCrnMap.isEmpty()) {
    private void crnIoExecuteNormal(BasCrnp currentCrn, Set<Integer> crnMoveBlockedCrnNos) {
        if (currentCrn == null || currentCrn.getCrnNo() == null || crnMoveBlockedCrnNos.contains(currentCrn.getCrnNo())) {
            return;
        }
        Integer crnNo = currentCrn.getCrnNo();
        CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, crnNo);
        if (crnThread == null) {
            return;
        }
        CrnProtocol crnProtocol = crnThread.getStatus();
        if (crnProtocol == null) {
            return;
        }
        long runningCount = wrkMastService.count(new QueryWrapper<WrkMast>()
                .eq("crn_no", crnNo)
                .in("wrk_sts", WrkStsType.INBOUND_RUN.sts, WrkStsType.OUTBOUND_RUN.sts, WrkStsType.LOC_MOVE_RUN.sts, WrkStsType.CRN_MOVE_RUN.sts));
        if (runningCount > 0) {
            return;
        }
        // 只有当堆垛机空闲 并且 无任务时才继续执行
        if (crnProtocol.getMode() != CrnModeType.AUTO.id
                || crnProtocol.getTaskNo() != 0
                || crnProtocol.getStatus() != CrnStatusType.IDLE.id
                || crnProtocol.getLoaded() != 0
                || crnProtocol.getForkPos() != 0
                || crnProtocol.getAlarm() != 0) {
            return;
        }
        Object clearLock = redisUtil.get(RedisKeyType.CLEAR_CRN_TASK_LIMIT.key + crnNo);
        if (clearLock != null) {
            return;
        }
        String lastIo = resolveCrnLastIo(crnProtocol);
        List<WrkMast> taskQueue = wrkMastService.list(new QueryWrapper<WrkMast>()
                .in("crn_no", new ArrayList<>(dispatchCrnMap.keySet()))
                .eq("crn_no", crnNo)
                .in("wrk_sts",
                        WrkStsType.INBOUND_DEVICE_RUN.sts,
                        WrkStsType.INBOUND_STATION_RUN_COMPLETE.sts,
                        WrkStsType.NEW_OUTBOUND.sts,
                        WrkStsType.NEW_LOC_MOVE.sts));
        taskQueue.sort(Comparator
                .comparingInt(this::resolveBatchOutboundRank)
        taskQueue.sort(Comparator.comparingInt((WrkMast wrkMast) -> resolveTaskTypeRank(wrkMast, lastIo))
                .thenComparingInt(this::resolveBatchOutboundRank)
                .thenComparingInt(this::resolveBatchSeqOrder)
                .thenComparingDouble(this::resolveTaskIoPri)
                .thenComparingLong(this::resolveTaskQueueTime)
                .thenComparingInt(this::resolveTaskQueueNo));
        for (WrkMast wrkMast : taskQueue) {
            if (wrkMast == null || wrkMast.getCrnNo() == null) {
            if (wrkMast == null || wrkMast.getCrnNo() == null || !Objects.equals(wrkMast.getCrnNo(), crnNo)) {
                continue;
            }
            Integer crnNo = wrkMast.getCrnNo();
            BasCrnp basCrnp = dispatchCrnMap.get(crnNo);
            CrnThread crnThread = dispatchThreadMap.get(crnNo);
            CrnProtocol crnProtocol = dispatchProtocolMap.get(crnNo);
            if (basCrnp == null || crnThread == null || crnProtocol == null) {
                continue;
            }
            if (wrkMast.getWrkSts() != null && wrkMast.getWrkSts() == WrkStsType.INBOUND_DEVICE_RUN.sts) {
                boolean result = this.crnExecuteInPlanner(basCrnp, crnThread, wrkMast);
            if (wrkMast.getWrkSts() != null && wrkMast.getWrkSts() == WrkStsType.INBOUND_STATION_RUN_COMPLETE.sts) {
                boolean result = this.crnExecuteInPlanner(currentCrn, crnThread, wrkMast);
                if (result) {
                    crnProtocol.setLastIo("O");
                    return;
@@ -160,7 +168,7 @@
            }
            if (wrkMast.getWrkSts() != null && wrkMast.getWrkSts() == WrkStsType.NEW_OUTBOUND.sts) {
                boolean result = this.crnExecuteOutPlanner(basCrnp, crnThread, wrkMast);
                boolean result = this.crnExecuteOutPlanner(currentCrn, crnThread, wrkMast);
                if (result) {
                    crnProtocol.setLastIo("I");
                    return;
@@ -169,7 +177,7 @@
            }
            if (wrkMast.getWrkSts() != null && wrkMast.getWrkSts() == WrkStsType.NEW_LOC_MOVE.sts) {
                boolean transfer = this.crnExecuteMovePlanner(basCrnp, crnThread, wrkMast);
                boolean transfer = this.crnExecuteMovePlanner(currentCrn, crnThread, wrkMast);
                if (transfer) {
                    return;
                }
@@ -179,6 +187,44 @@
    private int resolveBatchOutboundRank(WrkMast wrkMast) {
        return isBatchOutboundTask(wrkMast) ? 0 : 1;
    }
    private String resolveCrnLastIo(CrnProtocol crnProtocol) {
        if (crnProtocol == null) {
            return "I";
        }
        if (Cools.isEmpty(crnProtocol.getLastIo())) {
            return "I";
        }
        return crnProtocol.getLastIo();
    }
    private int resolveTaskTypeRank(WrkMast wrkMast, String lastIo) {
        if (wrkMast == null || wrkMast.getWrkSts() == null) {
            return Integer.MAX_VALUE;
        }
        if (Objects.equals(wrkMast.getWrkSts(), WrkStsType.NEW_LOC_MOVE.sts)) {
            return 0;
        }
        if (Objects.equals("O", lastIo)) {
            if (Objects.equals(wrkMast.getWrkSts(), WrkStsType.INBOUND_STATION_RUN_COMPLETE.sts)) {
                return 1;
            }
            if (Objects.equals(wrkMast.getWrkSts(), WrkStsType.NEW_OUTBOUND.sts)) {
                return 2;
            }
            return 3;
        }
        if (Objects.equals(wrkMast.getWrkSts(), WrkStsType.NEW_OUTBOUND.sts)) {
            return 1;
        }
        if (Objects.equals(wrkMast.getWrkSts(), WrkStsType.INBOUND_STATION_RUN_COMPLETE.sts)) {
            return 2;
        }
        return 3;
    }
    private int resolveBatchSeqOrder(WrkMast wrkMast) {
@@ -214,209 +260,6 @@
        return wrkMast != null
                && Integer.valueOf(WrkIoType.OUT.id).equals(wrkMast.getIoType())
                && !Cools.isEmpty(wrkMast.getBatch());
    }
    private synchronized boolean crnExecuteIn(BasCrnp basCrnp, CrnThread crnThread) {
        CrnProtocol crnProtocol = crnThread.getStatus();
        if(crnProtocol == null){
            return false;
        }
        if(!basCrnp.getInEnable().equals("Y")){
            News.info("堆垛机:{} 可入信号不满足", basCrnp.getCrnNo());
            return false;
        }
        List<StationObjModel> inStationList = basCrnp.getInStationList$();
        if(inStationList.isEmpty()){
            News.info("堆垛机:{} 入库站点未设置", basCrnp.getCrnNo());
            return false;
        }
        Integer crnNo = basCrnp.getCrnNo();
        for (StationObjModel stationObjModel : inStationList) {
            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, stationObjModel.getDeviceNo());
            if (stationThread == null) {
                continue;
            }
            Map<Integer, StationProtocol> stationProtocolMap = stationThread.getStatusMap();
            StationProtocol stationProtocol = stationProtocolMap.get(stationObjModel.getStationId());
            if (stationProtocol == null) {
                continue;
            }
            if (!stationProtocol.isAutoing()) {
                continue;
            }
            if (!stationProtocol.isLoading()) {
                continue;
            }
            if (stationProtocol.getTaskNo() <= 0) {
                continue;
            }
            if (!stationProtocol.isInEnable()) {
                News.taskInfo(stationProtocol.getTaskNo(), "取货站点:{} 没有可入信号", stationObjModel.getStationId());
                continue;
            }
            // 获取任务
            WrkMast wrkMast = wrkMastService.selectByWorkNo(stationProtocol.getTaskNo());
            if (null == wrkMast) {
                News.taskInfo(stationProtocol.getTaskNo(), "工作号:{} 任务信息不存在", stationProtocol.getTaskNo());
                continue;
            }
            if (!wrkMast.getCrnNo().equals(basCrnp.getCrnNo())) {
                continue;
            }
            if(wrkMast.getWrkSts() != WrkStsType.INBOUND_DEVICE_RUN.sts){
                continue;
            }
            // 获取库位信息
            LocMast locMast = locMastService.getById(wrkMast.getLocNo());
            if (locMast == null) {
                News.taskInfo(wrkMast.getWrkNo(), "目标库位:{} 信息不存在", wrkMast.getLocNo());
                continue;
            }
            if (!locMast.getLocSts().equals("S")) {
                News.taskInfo(wrkMast.getWrkNo(), "目标库位:{} 状态异常", wrkMast.getLocNo());
                continue;
            }
            //检测浅库位状态
            boolean checkStatus = checkShallowLocStatus(locMast.getLocNo(), wrkMast.getWrkNo());
            if (!checkStatus) {
                News.taskInfo(wrkMast.getWrkNo(), "因浅库位堵塞无法执行");
                continue;
            }
            String sourceLocNo = Utils.getLocNo(stationObjModel.getDeviceRow(), stationObjModel.getDeviceBay(), stationObjModel.getDeviceLev());
            CrnCommand command = crnThread.getPickAndPutCommand(sourceLocNo, wrkMast.getLocNo(), wrkMast.getWrkNo(), crnNo);
            wrkMast.setWrkSts(WrkStsType.INBOUND_RUN.sts);
            wrkMast.setCrnNo(crnNo);
            wrkMast.setSystemMsg("");
            wrkMast.setIoTime(new Date());
            if (wrkMastService.updateById(wrkMast)) {
                MessageQueue.offer(SlaveType.Crn, crnNo, new Task(2, command));
                notifyUtils.notify(String.valueOf(SlaveType.Crn), crnNo, String.valueOf(wrkMast.getWrkNo()), wrkMast.getWmsWrkNo(), NotifyMsgType.CRN_IN_TASK_RUN, null);
                News.info("堆垛机命令下发成功,堆垛机号={},任务数据={}", crnNo, JSON.toJSON(command));
                return true;
            }
        }
        return false;
    }
    private synchronized boolean crnExecuteOut(BasCrnp basCrnp, CrnThread crnThread) {
        CrnProtocol crnProtocol = crnThread.getStatus();
        if(crnProtocol == null){
            return false;
        }
        if(!basCrnp.getOutEnable().equals("Y")){
            News.info("堆垛机:{} 可出信号不满足", basCrnp.getCrnNo());
            return false;
        }
        List<StationObjModel> outStationList = basCrnp.getOutStationList$();
        if(outStationList.isEmpty()){
            News.info("堆垛机:{} 出库站点未设置", basCrnp.getCrnNo());
            return false;
        }
        if (isOutboundStationTaskLimitReached()) {
            return false;
        }
        Integer crnNo = basCrnp.getCrnNo();
        List<WrkMast> wrkMasts = wrkMastService.list(new QueryWrapper<WrkMast>()
                .eq("crn_no", crnNo)
                .eq("wrk_sts", WrkStsType.NEW_OUTBOUND.sts)
                .orderBy(true, true, "batch_seq")
        );
        for (WrkMast wrkMast : wrkMasts) {
            if (isOutboundTargetStationTaskLimitReached(wrkMast)) {
                continue;
            }
            if (!allowBatchOutboundExecute(wrkMast)) {
                continue;
            }
            for (StationObjModel stationObjModel : outStationList) {
                StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, stationObjModel.getDeviceNo());
                if (stationThread == null) {
                    continue;
                }
                Map<Integer, StationProtocol> stationProtocolMap = stationThread.getStatusMap();
                StationProtocol stationProtocol = stationProtocolMap.get(stationObjModel.getStationId());
                if (stationProtocol == null) {
                    continue;
                }
                if (!stationProtocol.isAutoing()) {
                    continue;
                }
                if (stationProtocol.isLoading()) {
                    continue;
                }
                if (stationProtocol.getTaskNo() != 0) {
                    continue;
                }
                if (isRequireOutboundStationOutEnable() && !stationProtocol.isOutEnable()) {
                    News.taskInfo(wrkMast.getWrkNo(), "放货站点:{} 没有可出信号", stationObjModel.getStationId());
                    continue;
                }
                // 获取库位信息
                LocMast locMast = locMastService.getById(wrkMast.getSourceLocNo());
                if (locMast == null) {
                    News.taskInfo(wrkMast.getWrkNo(), "源库位:{} 信息不存在", wrkMast.getSourceLocNo());
                    continue;
                }
                if (!locMast.getLocSts().equals("R")) {
                    News.taskInfo(wrkMast.getWrkNo(), "源库位:{} 状态异常", wrkMast.getSourceLocNo());
                    continue;
                }
                //检测浅库位状态
                boolean checkStatus = checkShallowLocStatus(locMast.getLocNo(), wrkMast.getWrkNo());
                if (!checkStatus) {
                    News.taskInfo(wrkMast.getWrkNo(), "因浅库位堵塞无法执行");
                    continue;
                }
                String targetLocNo = Utils.getLocNo(stationObjModel.getDeviceRow(), stationObjModel.getDeviceBay(), stationObjModel.getDeviceLev());
                CrnCommand command = crnThread.getPickAndPutCommand(wrkMast.getSourceLocNo(), targetLocNo, wrkMast.getWrkNo(), crnNo);
                wrkMast.setWrkSts(WrkStsType.OUTBOUND_RUN.sts);
                wrkMast.setCrnNo(crnNo);
                wrkMast.setSystemMsg("");
                wrkMast.setIoTime(new Date());
                if (wrkMastService.updateById(wrkMast)) {
                    MessageQueue.offer(SlaveType.Crn, crnNo, new Task(2, command));
                    notifyUtils.notify(String.valueOf(SlaveType.Crn), crnNo, String.valueOf(wrkMast.getWrkNo()), wrkMast.getWmsWrkNo(), NotifyMsgType.CRN_OUT_TASK_RUN, null);
                    News.info("堆垛机命令下发成功,堆垛机号={},任务数据={}", crnNo, JSON.toJSON(command));
                    return true;
                }
            }
        }
        return false;
    }
    private synchronized boolean crnExecuteInPlanner(BasCrnp basCrnp, CrnThread crnThread, WrkMast wrkMast) {
@@ -471,7 +314,7 @@
                continue;
            }
            if (wrkMast.getWrkSts() != WrkStsType.INBOUND_DEVICE_RUN.sts) {
            if (wrkMast.getWrkSts() != WrkStsType.INBOUND_STATION_RUN_COMPLETE.sts) {
                continue;
            }
@@ -498,11 +341,14 @@
            CrnCommand command = crnThread.getPickAndPutCommand(sourceLocNo, wrkMast.getLocNo(), wrkMast.getWrkNo(), crnNo);
            Date now = new Date();
            wrkMast.setWrkSts(WrkStsType.INBOUND_RUN.sts);
            wrkMast.setCrnNo(crnNo);
            wrkMast.setSystemMsg("");
            wrkMast.setIoTime(new Date());
            wrkMast.setIoTime(now);
            wrkMast.setModiTime(now);
            if (wrkMastService.updateById(wrkMast)) {
                wrkAnalysisService.markCraneStart(wrkMast, now);
                MessageQueue.offer(SlaveType.Crn, crnNo, new Task(2, command));
                notifyUtils.notify(String.valueOf(SlaveType.Crn), crnNo, String.valueOf(wrkMast.getWrkNo()), wrkMast.getWmsWrkNo(), NotifyMsgType.CRN_IN_TASK_RUN, null);
                News.info("堆垛机命令下发成功,堆垛机号={},任务数据={}", crnNo, JSON.toJSON(command));
@@ -539,7 +385,7 @@
            return false;
        }
        if (!allowBatchOutboundExecute(wrkMast)) {
        if (!allowBatchOutboundExecute(wrkMast, true)) {
            return false;
        }
@@ -595,11 +441,14 @@
            CrnCommand command = crnThread.getPickAndPutCommand(wrkMast.getSourceLocNo(), targetLocNo, wrkMast.getWrkNo(), crnNo);
            Date now = new Date();
            wrkMast.setWrkSts(WrkStsType.OUTBOUND_RUN.sts);
            wrkMast.setCrnNo(crnNo);
            wrkMast.setSystemMsg("");
            wrkMast.setIoTime(new Date());
            wrkMast.setIoTime(now);
            wrkMast.setModiTime(now);
            if (wrkMastService.updateById(wrkMast)) {
                wrkAnalysisService.markCraneStart(wrkMast, now);
                MessageQueue.offer(SlaveType.Crn, crnNo, new Task(2, command));
                notifyUtils.notify(String.valueOf(SlaveType.Crn), crnNo, String.valueOf(wrkMast.getWrkNo()), wrkMast.getWmsWrkNo(), NotifyMsgType.CRN_OUT_TASK_RUN, null);
                News.info("堆垛机命令下发成功,堆垛机号={},任务数据={}", crnNo, JSON.toJSON(command));
@@ -609,13 +458,19 @@
        return false;
    }
    private boolean allowBatchOutboundExecute(WrkMast wrkMast) {
    public boolean canOutboundTaskExecuteInCurrentBatchWindow(WrkMast wrkMast) {
        return allowBatchOutboundExecute(wrkMast, false);
    }
    private boolean allowBatchOutboundExecute(WrkMast wrkMast, boolean logBlockedReason) {
        if (wrkMast == null || Cools.isEmpty(wrkMast.getBatch())) {
            return true;
        }
        if (Cools.isEmpty(wrkMast.getBatchSeq())) {
            News.taskInfo(wrkMast.getWrkNo(), "批次:{} 缺少批次序号,暂不允许堆垛机出库", wrkMast.getBatch());
            if (logBlockedReason) {
                News.taskInfo(wrkMast.getWrkNo(), "批次:{} 缺少批次序号,暂不允许堆垛机出库", wrkMast.getBatch());
            }
            return false;
        }
@@ -630,7 +485,9 @@
        }
        if (Cools.isEmpty(firstBatchWrkMast.getBatchSeq())) {
            News.taskInfo(wrkMast.getWrkNo(), "批次:{} 存在未配置批次序号的任务,暂不允许堆垛机出库", wrkMast.getBatch());
            if (logBlockedReason) {
                News.taskInfo(wrkMast.getWrkNo(), "批次:{} 存在未配置批次序号的任务,暂不允许堆垛机出库", wrkMast.getBatch());
            }
            return false;
        }
@@ -638,7 +495,9 @@
        boolean firstBatchTaskExecuted = firstBatchWrkMast.getWrkSts() != null
                && !firstBatchWrkMast.getWrkSts().equals(WrkStsType.NEW_OUTBOUND.sts);
        if (!currentIsFirstBatchTask && !firstBatchTaskExecuted) {
            News.taskInfo(wrkMast.getWrkNo(), "批次:{} 首个序号任务:{} 尚未执行,当前任务暂不允许出库", wrkMast.getBatch(), firstBatchWrkMast.getWrkNo());
            if (logBlockedReason) {
                News.taskInfo(wrkMast.getWrkNo(), "批次:{} 首个序号任务:{} 尚未执行,当前任务暂不允许出库", wrkMast.getBatch(), firstBatchWrkMast.getWrkNo());
            }
            return false;
        }
@@ -649,19 +508,24 @@
        List<WrkMast> unfinishedBatchWrkMasts = listUnfinishedBatchWrkMasts(wrkMast.getBatch());
        if (hasMissingBatchSeq(unfinishedBatchWrkMasts)) {
            News.taskInfo(wrkMast.getWrkNo(), "批次:{} 存在未配置批次序号的未完成任务,暂不允许堆垛机出库", wrkMast.getBatch());
            if (logBlockedReason) {
                News.taskInfo(wrkMast.getWrkNo(), "批次:{} 存在未配置批次序号的未完成任务,暂不允许堆垛机出库", wrkMast.getBatch());
            }
            return false;
        }
        if (!isWithinBatchExecuteWindow(wrkMast, unfinishedBatchWrkMasts, batchRunningLimit)) {
            Integer windowStartSeq = unfinishedBatchWrkMasts.get(0).getBatchSeq();
            Integer windowEndSeq = unfinishedBatchWrkMasts.get(Math.min(batchRunningLimit, unfinishedBatchWrkMasts.size()) - 1).getBatchSeq();
            News.taskInfo(wrkMast.getWrkNo(),
                    "批次:{} 当前严格执行窗口序号为[{}-{}],当前序号={},暂不允许堆垛机出库",
                    wrkMast.getBatch(),
                    windowStartSeq,
                    windowEndSeq,
                    wrkMast.getBatchSeq());
            if (logBlockedReason) {
                List<Integer> windowBatchSeqList = unfinishedBatchWrkMasts.stream()
                        .limit(Math.min(batchRunningLimit, unfinishedBatchWrkMasts.size()))
                        .map(WrkMast::getBatchSeq)
                        .collect(java.util.stream.Collectors.toList());
                News.taskInfo(wrkMast.getWrkNo(),
                        "批次:{} 当前严格执行窗口序号列表为{},当前序号={},暂不允许堆垛机出库",
                        wrkMast.getBatch(),
                        windowBatchSeqList,
                        wrkMast.getBatchSeq());
            }
            return false;
        }
@@ -673,7 +537,9 @@
                        WrkStsType.COMPLETE_OUTBOUND.sts,
                        WrkStsType.SETTLE_OUTBOUND.sts));
        if (batchRunningCount >= batchRunningLimit) {
            News.taskInfo(wrkMast.getWrkNo(), "批次:{} 执行中任务数达到上限,当前={},上限={}", wrkMast.getBatch(), batchRunningCount, batchRunningLimit);
            if (logBlockedReason) {
                News.taskInfo(wrkMast.getWrkNo(), "批次:{} 执行中任务数达到上限,当前={},上限={}", wrkMast.getBatch(), batchRunningCount, batchRunningLimit);
            }
            return false;
        }
@@ -793,158 +659,124 @@
        return defaultValue;
    }
    private synchronized boolean crnExecuteLocTransfer(BasCrnp basCrnp, CrnThread crnThread) {
        CrnProtocol crnProtocol = crnThread.getStatus();
        if(crnProtocol == null){
            return false;
        }
        Integer crnNo = basCrnp.getCrnNo();
        List<WrkMast> wrkMasts = wrkMastService.list(new QueryWrapper<WrkMast>()
                .eq("crn_no", crnNo)
                .eq("wrk_sts", WrkStsType.NEW_LOC_MOVE.sts)
        );
        for (WrkMast wrkMast : wrkMasts) {
            // 获取源库位信息
            LocMast sourceLocMast = locMastService.getById(wrkMast.getSourceLocNo());
            if (sourceLocMast == null) {
                News.taskInfo(wrkMast.getWrkNo(), "源库位:{} 信息不存在", wrkMast.getSourceLocNo());
                continue;
            }
            if(!sourceLocMast.getLocSts().equals("R")){
                News.taskInfo(wrkMast.getWrkNo(), "源库位:{} 状态异常,不属于出库预约状态", wrkMast.getSourceLocNo());
                continue;
            }
            // 获取库位信息
            LocMast locMast = locMastService.getById(wrkMast.getLocNo());
            if (locMast == null) {
                News.taskInfo(wrkMast.getWrkNo(), "库位:{} 信息不存在", wrkMast.getLocNo());
                continue;
            }
            if (!locMast.getLocSts().equals("S")) {
                News.taskInfo(wrkMast.getWrkNo(), "库位:{} 状态异常,不属于入库预约状态", wrkMast.getLocNo());
                continue;
            }
            CrnCommand command = crnThread.getPickAndPutCommand(wrkMast.getSourceLocNo(), wrkMast.getLocNo(), wrkMast.getWrkNo(), crnNo);
            wrkMast.setWrkSts(WrkStsType.LOC_MOVE_RUN.sts);
            wrkMast.setCrnNo(crnNo);
            wrkMast.setSystemMsg("");
            wrkMast.setIoTime(new Date());
            if (wrkMastService.updateById(wrkMast)) {
                MessageQueue.offer(SlaveType.Crn, crnNo, new Task(2, command));
                notifyUtils.notify(String.valueOf(SlaveType.Crn), crnNo, String.valueOf(wrkMast.getWrkNo()), wrkMast.getWmsWrkNo(), NotifyMsgType.CRN_TRANSFER_TASK_RUN, null);
                News.info("堆垛机命令下发成功,堆垛机号={},任务数据={}", crnNo, JSON.toJSON(command));
                return true;
            }
        }
        return false;
    }
    //堆垛机任务执行完成
    public synchronized void crnIoExecuteFinish() {
        List<BasCrnp> basCrnps = basCrnpService.list(new QueryWrapper<>());
        for (BasCrnp basCrnp : basCrnps) {
            CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, basCrnp.getCrnNo());
            if(crnThread == null){
                continue;
            crnIoExecuteFinish(basCrnp);
        }
    }
    public void crnIoExecuteFinish(BasCrnp basCrnp) {
        if (basCrnp == null || basCrnp.getCrnNo() == null) {
            return;
        }
        CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, basCrnp.getCrnNo());
        if(crnThread == null){
            return;
        }
        CrnProtocol crnProtocol = crnThread.getStatus();
        if(crnProtocol == null){
            return;
        }
        if (crnProtocol.getMode() == CrnModeType.AUTO.id
                && crnProtocol.getTaskNo() > 0
                && crnProtocol.getStatus() == CrnStatusType.WAITING.id
        ) {
            Object lock = redisUtil.get(RedisKeyType.CRN_IO_EXECUTE_FINISH_LIMIT.key + basCrnp.getCrnNo());
            if(lock != null){
                return;
            }
            CrnProtocol crnProtocol = crnThread.getStatus();
            if(crnProtocol == null){
                continue;
            WrkMast wrkMast = wrkMastService.selectByWorkNo(crnProtocol.getTaskNo());
            if (wrkMast == null) {
                News.error("堆垛机处于等待确认且任务完成状态,但未找到工作档。堆垛机号={},工作号={}", basCrnp.getCrnNo(), crnProtocol.getTaskNo());
                return;
            }
            if (crnProtocol.getMode() == CrnModeType.AUTO.id
                    && crnProtocol.getTaskNo() > 0
                    && crnProtocol.getStatus() == CrnStatusType.WAITING.id
            ) {
                Object lock = redisUtil.get(RedisKeyType.CRN_IO_EXECUTE_FINISH_LIMIT.key + basCrnp.getCrnNo());
                if(lock != null){
                    continue;
            Long updateWrkSts = null;
            Date now = new Date();
            if(wrkMast.getWrkSts() == WrkStsType.INBOUND_RUN.sts){
                updateWrkSts = WrkStsType.COMPLETE_INBOUND.sts;
                notifyUtils.notify(String.valueOf(SlaveType.Crn), crnProtocol.getCrnNo(), String.valueOf(wrkMast.getWrkNo()), wrkMast.getWmsWrkNo(), NotifyMsgType.CRN_IN_TASK_COMPLETE, null);
            }else if(wrkMast.getWrkSts() == WrkStsType.OUTBOUND_RUN.sts){
                updateWrkSts = WrkStsType.OUTBOUND_RUN_COMPLETE.sts;
                notifyUtils.notify(String.valueOf(SlaveType.Crn), crnProtocol.getCrnNo(), String.valueOf(wrkMast.getWrkNo()), wrkMast.getWmsWrkNo(), NotifyMsgType.CRN_OUT_TASK_COMPLETE, null);
                List<StationObjModel> outStationList = basCrnp.getOutStationList$();
                if(outStationList.isEmpty()){
                    News.info("堆垛机:{} 出库站点未设置", basCrnp.getCrnNo());
                    return;
                }
                // 获取待确认工作档
                WrkMast wrkMast = wrkMastService.selectByWorkNo(crnProtocol.getTaskNo());
                if (wrkMast == null) {
                    News.error("堆垛机处于等待确认且任务完成状态,但未找到工作档。堆垛机号={},工作号={}", basCrnp.getCrnNo(), crnProtocol.getTaskNo());
                    continue;
                }
                Long updateWrkSts = null;
                if(wrkMast.getWrkSts() == WrkStsType.INBOUND_RUN.sts){
                    updateWrkSts = WrkStsType.COMPLETE_INBOUND.sts;
                    notifyUtils.notify(String.valueOf(SlaveType.Crn), crnProtocol.getCrnNo(), String.valueOf(wrkMast.getWrkNo()), wrkMast.getWmsWrkNo(), NotifyMsgType.CRN_IN_TASK_COMPLETE, null);
                }else if(wrkMast.getWrkSts() == WrkStsType.OUTBOUND_RUN.sts){
                    updateWrkSts = WrkStsType.OUTBOUND_RUN_COMPLETE.sts;
                    notifyUtils.notify(String.valueOf(SlaveType.Crn), crnProtocol.getCrnNo(), String.valueOf(wrkMast.getWrkNo()), wrkMast.getWmsWrkNo(), NotifyMsgType.CRN_OUT_TASK_COMPLETE, null);
                    List<StationObjModel> outStationList = basCrnp.getOutStationList$();
                    if(outStationList.isEmpty()){
                        News.info("堆垛机:{} 出库站点未设置", basCrnp.getCrnNo());
                        return;
                StationObjModel outStationObjModel = null;
                for (StationObjModel stationObjModel : outStationList) {
                    if (stationObjModel.getStationId().equals(wrkMast.getSourceStaNo())) {
                        outStationObjModel = stationObjModel;
                        break;
                    }
                    StationObjModel outStationObjModel = null;
                    for (StationObjModel stationObjModel : outStationList) {
                        if (stationObjModel.getStationId().equals(wrkMast.getSourceStaNo())) {
                            outStationObjModel = stationObjModel;
                            break;
                        }
                    }
                    redisUtil.set(RedisKeyType.CRN_OUT_TASK_COMPLETE_STATION_INFO.key + wrkMast.getWrkNo(), JSON.toJSONString(outStationObjModel, SerializerFeature.DisableCircularReferenceDetect), 60 * 60 * 24);
                }else if(wrkMast.getWrkSts() == WrkStsType.LOC_MOVE_RUN.sts){
                    updateWrkSts = WrkStsType.COMPLETE_LOC_MOVE.sts;
                    notifyUtils.notify(String.valueOf(SlaveType.Crn), crnProtocol.getCrnNo(), String.valueOf(wrkMast.getWrkNo()), wrkMast.getWmsWrkNo(), NotifyMsgType.CRN_TRANSFER_TASK_COMPLETE, null);
                }else{
                    News.error("堆垛机处于等待确认且任务完成状态,但工作状态异常。堆垛机号={},工作号={}", basCrnp.getCrnNo(), crnProtocol.getTaskNo());
                    continue;
                }
                wrkMast.setWrkSts(updateWrkSts);
                wrkMast.setSystemMsg("");
                wrkMast.setIoTime(new Date());
                if (wrkMastService.updateById(wrkMast)) {
                    CrnCommand resetCommand = crnThread.getResetCommand(crnProtocol.getTaskNo(), crnProtocol.getCrnNo());
                    MessageQueue.offer(SlaveType.Crn, crnProtocol.getCrnNo(), new Task(2, resetCommand));
                    News.info("堆垛机任务状态更新成功,堆垛机号={},工作号={}", basCrnp.getCrnNo(), crnProtocol.getTaskNo());
                }
                redisUtil.set(RedisKeyType.CRN_IO_EXECUTE_FINISH_LIMIT.key + basCrnp.getCrnNo(), "lock",10);
                redisUtil.set(RedisKeyType.CRN_OUT_TASK_COMPLETE_STATION_INFO.key + wrkMast.getWrkNo(), JSON.toJSONString(outStationObjModel, SerializerFeature.DisableCircularReferenceDetect), 60 * 60 * 24);
            }else if(wrkMast.getWrkSts() == WrkStsType.LOC_MOVE_RUN.sts){
                updateWrkSts = WrkStsType.COMPLETE_LOC_MOVE.sts;
                notifyUtils.notify(String.valueOf(SlaveType.Crn), crnProtocol.getCrnNo(), String.valueOf(wrkMast.getWrkNo()), wrkMast.getWmsWrkNo(), NotifyMsgType.CRN_TRANSFER_TASK_COMPLETE, null);
            }else if(wrkMast.getWrkSts() == WrkStsType.CRN_MOVE_RUN.sts){
                updateWrkSts = WrkStsType.COMPLETE_CRN_MOVE.sts;
            }else{
                News.error("堆垛机处于等待确认且任务完成状态,但工作状态异常。堆垛机号={},工作号={}", basCrnp.getCrnNo(), crnProtocol.getTaskNo());
                return;
            }
            wrkMast.setWrkSts(updateWrkSts);
            wrkMast.setSystemMsg("");
            wrkMast.setIoTime(now);
            wrkMast.setModiTime(now);
            if (wrkMastService.updateById(wrkMast)) {
                wrkAnalysisService.markCraneComplete(wrkMast, now, updateWrkSts);
                CrnCommand resetCommand = crnThread.getResetCommand(crnProtocol.getTaskNo(), crnProtocol.getCrnNo());
                MessageQueue.offer(SlaveType.Crn, crnProtocol.getCrnNo(), new Task(2, resetCommand));
                News.info("堆垛机任务状态更新成功,堆垛机号={},工作号={}", basCrnp.getCrnNo(), crnProtocol.getTaskNo());
            }
            redisUtil.set(RedisKeyType.CRN_IO_EXECUTE_FINISH_LIMIT.key + basCrnp.getCrnNo(), "lock",10);
        }
    }
    public synchronized void plannerExecute() {
        int nowSec = (int) (System.currentTimeMillis() / 1000);
        Set<Integer> crnMoveBlockedCrnNos = executeCrnMoveTask();
        List<BasCrnp> basCrnps = basCrnpService.list(new QueryWrapper<>());
        for (BasCrnp basCrnp : basCrnps) {
            plannerExecute(basCrnp, crnMoveBlockedCrnNos);
        }
    }
    private void plannerExecute(BasCrnp basCrnp, Set<Integer> crnMoveBlockedCrnNos) {
        int nowSec = (int) (System.currentTimeMillis() / 1000);
        if (basCrnp == null || basCrnp.getCrnNo() == null || crnMoveBlockedCrnNos.contains(basCrnp.getCrnNo())) {
            return;
        }
            String key = RedisKeyType.PLANNER_SCHEDULE.key + "CRN-" + basCrnp.getCrnNo();
            List<Object> items = redisUtil.lGet(key, 0, -1);
            if (items == null || items.isEmpty()) {
                continue;
                return;
            }
            CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, basCrnp.getCrnNo());
            if (crnThread == null) {
                continue;
                return;
            }
            CrnProtocol crnProtocol = crnThread.getStatus();
            if (crnProtocol == null) {
                continue;
                return;
            }
            List<WrkMast> running = wrkMastService.list(new QueryWrapper<WrkMast>()
                    .eq("crn_no", basCrnp.getCrnNo())
                    .in("wrk_sts", WrkStsType.INBOUND_RUN.sts, WrkStsType.OUTBOUND_RUN.sts, WrkStsType.LOC_MOVE_RUN.sts)
                    .in("wrk_sts", WrkStsType.INBOUND_RUN.sts, WrkStsType.OUTBOUND_RUN.sts, WrkStsType.LOC_MOVE_RUN.sts, WrkStsType.CRN_MOVE_RUN.sts)
            );
            if (!running.isEmpty()) {
                continue;
                return;
            }
            if (!(crnProtocol.getMode() == CrnModeType.AUTO.id
                    && crnProtocol.getTaskNo() == 0
@@ -952,7 +784,7 @@
                    && crnProtocol.getLoaded() == 0
                    && crnProtocol.getForkPos() == 0
                    && crnProtocol.getAlarm() == 0)) {
                continue;
                return;
            }
            for (Object v : items) {
@@ -1015,7 +847,6 @@
                    }
                }
            }
        }
    }
    private synchronized boolean crnExecuteMovePlanner(BasCrnp basCrnp, CrnThread crnThread, WrkMast wrkMast) {
@@ -1056,17 +887,175 @@
        CrnCommand command = crnThread.getPickAndPutCommand(wrkMast.getSourceLocNo(), wrkMast.getLocNo(), wrkMast.getWrkNo(), crnNo);
        Date now = new Date();
        wrkMast.setWrkSts(WrkStsType.LOC_MOVE_RUN.sts);
        wrkMast.setCrnNo(crnNo);
        wrkMast.setSystemMsg("");
        wrkMast.setIoTime(new Date());
        wrkMast.setIoTime(now);
        wrkMast.setModiTime(now);
        if (wrkMastService.updateById(wrkMast)) {
            wrkAnalysisService.markCraneStart(wrkMast, now);
            MessageQueue.offer(SlaveType.Crn, crnNo, new Task(2, command));
            notifyUtils.notify(String.valueOf(SlaveType.Crn), crnNo, String.valueOf(wrkMast.getWrkNo()), wrkMast.getWmsWrkNo(), NotifyMsgType.CRN_TRANSFER_TASK_RUN, null);
            News.info("堆垛机命令下发成功,堆垛机号={},任务数据={}", crnNo, JSON.toJSON(command));
            return true;
        }
        return false;
    }
    private Set<Integer> executeCrnMoveTask() {
        List<WrkMast> pendingTaskQueue = wrkMastService.list(new QueryWrapper<WrkMast>()
                .eq("io_type", WrkIoType.CRN_MOVE.id)
                .eq("wrk_sts", WrkStsType.NEW_CRN_MOVE.sts)
                .orderByAsc("appe_time")
                .orderByAsc("wrk_no"));
        Set<Integer> blockedCrnNoSet = new HashSet<>();
        for (WrkMast wrkMast : pendingTaskQueue) {
            if (wrkMast != null && wrkMast.getCrnNo() != null) {
                blockedCrnNoSet.add(wrkMast.getCrnNo());
            }
        }
        if (blockedCrnNoSet.isEmpty()) {
            return blockedCrnNoSet;
        }
        List<BasCrnp> basCrnps = basCrnpService.list(new QueryWrapper<>());
        Map<Integer, CrnThread> dispatchThreadMap = new HashMap<>();
        Map<Integer, CrnProtocol> dispatchProtocolMap = new HashMap<>();
        for (BasCrnp basCrnp : basCrnps) {
            if (basCrnp == null || basCrnp.getCrnNo() == null) {
                continue;
            }
            Integer crnNo = basCrnp.getCrnNo();
            CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, crnNo);
            if (crnThread == null) {
                continue;
            }
            CrnProtocol crnProtocol = crnThread.getStatus();
            if (crnProtocol == null) {
                continue;
            }
            long runningCount = wrkMastService.count(new QueryWrapper<WrkMast>()
                    .eq("crn_no", crnNo)
                    .in("wrk_sts",
                            WrkStsType.INBOUND_RUN.sts,
                            WrkStsType.OUTBOUND_RUN.sts,
                            WrkStsType.LOC_MOVE_RUN.sts,
                            WrkStsType.CRN_MOVE_RUN.sts));
            if (runningCount > 0) {
                continue;
            }
            if (!Objects.equals(crnProtocol.getMode(), CrnModeType.AUTO.id)
                    || !Objects.equals(crnProtocol.getTaskNo(), 0)
                    || !Objects.equals(crnProtocol.getStatus(), CrnStatusType.IDLE.id)
                    || !Objects.equals(crnProtocol.getLoaded(), 0)
                    || !Objects.equals(crnProtocol.getForkPos(), 0)
                    || !Objects.equals(crnProtocol.getAlarm(), 0)) {
                continue;
            }
            Object clearLock = redisUtil.get(RedisKeyType.CLEAR_CRN_TASK_LIMIT.key + crnNo);
            if (clearLock != null) {
                continue;
            }
            dispatchThreadMap.put(crnNo, crnThread);
            dispatchProtocolMap.put(crnNo, crnProtocol);
        }
        if (dispatchThreadMap.isEmpty()) {
            return blockedCrnNoSet;
        }
        List<WrkMast> taskQueue = wrkMastService.list(new QueryWrapper<WrkMast>()
                .in("crn_no", new ArrayList<>(dispatchThreadMap.keySet()))
                .eq("io_type", WrkIoType.CRN_MOVE.id)
                .eq("wrk_sts", WrkStsType.NEW_CRN_MOVE.sts)
                .orderByAsc("appe_time")
                .orderByAsc("wrk_no"));
        for (WrkMast wrkMast : taskQueue) {
            if (wrkMast == null || wrkMast.getCrnNo() == null || Cools.isEmpty(wrkMast.getLocNo())) {
                continue;
            }
            Integer crnNo = wrkMast.getCrnNo();
            CrnThread crnThread = dispatchThreadMap.get(crnNo);
            if (crnThread == null || dispatchProtocolMap.get(crnNo) == null) {
                continue;
            }
            CrnCommand moveCommand = crnThread.getMoveCommand(wrkMast.getLocNo(), wrkMast.getWrkNo(), crnNo);
            if (moveCommand == null) {
                continue;
            }
            Date now = new Date();
            wrkMast.setWrkSts(WrkStsType.CRN_MOVE_RUN.sts);
            wrkMast.setCrnNo(crnNo);
            wrkMast.setSystemMsg("");
            wrkMast.setIoTime(now);
            wrkMast.setModiTime(now);
            if (wrkMastService.updateById(wrkMast)) {
                wrkAnalysisService.markCraneStart(wrkMast, now);
                MessageQueue.offer(SlaveType.Crn, crnNo, new Task(2, moveCommand));
                News.info("堆垛机移动命令下发成功,堆垛机号={},工作号={},目标位={},任务数据={}",
                        crnNo, wrkMast.getWrkNo(), wrkMast.getLocNo(), JSON.toJSON(moveCommand));
                return blockedCrnNoSet;
            }
        }
        return blockedCrnNoSet;
    }
    public Set<Integer> executeCrnMoveTaskAndGetBlockedCrnNos() {
        return executeCrnMoveTask();
    }
    public void submitCrnIoTasks(long minIntervalMs) {
        submitCrnIoTasks(MainProcessLane.CRN_IO, minIntervalMs);
    }
    public void submitCrnIoTasks(MainProcessLane lane, long minIntervalMs) {
        Set<Integer> blockedCrnNos = executeCrnMoveTaskAndGetBlockedCrnNos();
        List<BasCrnp> basCrnps = basCrnpService.list(new QueryWrapper<>());
        for (BasCrnp basCrnp : basCrnps) {
            Integer crnNo = basCrnp == null ? null : basCrnp.getCrnNo();
            if (crnNo == null) {
                continue;
            }
            mainProcessTaskSubmitter.submitKeyedSerialTask(
                    lane,
                    crnNo,
                    "crnIoExecute",
                    minIntervalMs,
                    () -> crnIoExecute(basCrnp, blockedCrnNos)
            );
        }
    }
    public void submitCrnIoExecuteFinishTasks(long minIntervalMs) {
        submitCrnIoExecuteFinishTasks(MainProcessLane.CRN_IO_FINISH, minIntervalMs);
    }
    public void submitCrnIoExecuteFinishTasks(MainProcessLane lane, long minIntervalMs) {
        List<BasCrnp> basCrnps = basCrnpService.list(new QueryWrapper<>());
        for (BasCrnp basCrnp : basCrnps) {
            Integer crnNo = basCrnp == null ? null : basCrnp.getCrnNo();
            if (crnNo == null) {
                continue;
            }
            mainProcessTaskSubmitter.submitKeyedSerialTask(
                    lane,
                    crnNo,
                    "crnIoExecuteFinish",
                    minIntervalMs,
                    () -> crnIoExecuteFinish(basCrnp)
            );
        }
    }
    //检测浅库位状态
@@ -1131,4 +1120,83 @@
        return false;
    }
    //调度堆垛机移动
    public synchronized boolean dispatchCrnMove(Integer crnNo, String targetLocNo) {
        if (crnNo == null || Cools.isEmpty(targetLocNo)) {
            return false;
        }
        int targetRow = Utils.getRow(targetLocNo);
        int targetBay = Utils.getBay(targetLocNo);
        int targetLev = Utils.getLev(targetLocNo);
        try {
            targetRow = Utils.getRow(targetLocNo);
            targetBay = Utils.getBay(targetLocNo);
            targetLev = Utils.getLev(targetLocNo);
        } catch (Exception e) {
            News.error("生成堆垛机移动任务失败,目标位:{} 解析异常", targetLocNo);
            return false;
        }
        CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, crnNo);
        if (crnThread == null) {
            return false;
        }
        CrnProtocol crnProtocol = crnThread.getStatus();
        if (crnProtocol == null) {
            return false;
        }
        if (crnProtocol.getBay() == targetBay) {
            return false;
        }
        if (crnProtocol.getLevel() == targetLev) {
            return false;
        }
        long runningCount = wrkMastService.count(new QueryWrapper<WrkMast>()
                .eq("crn_no", crnNo)
                .in("wrk_sts",
                        WrkStsType.INBOUND_RUN.sts,
                        WrkStsType.OUTBOUND_RUN.sts,
                        WrkStsType.LOC_MOVE_RUN.sts,
                        WrkStsType.CRN_MOVE_RUN.sts));
        if (runningCount > 0) {
            News.info("堆垛机:{} 存在执行中的任务,暂不生成移动任务", crnNo);
            return false;
        }
        WrkMast activeTask = wrkMastService.getOne(new QueryWrapper<WrkMast>()
                .eq("crn_no", crnNo)
                .eq("io_type", WrkIoType.CRN_MOVE.id)
                .in("wrk_sts", WrkStsType.NEW_CRN_MOVE.sts, WrkStsType.CRN_MOVE_RUN.sts, WrkStsType.CRN_MOVE_MANUAL.sts)
                .orderByDesc("appe_time")
                .last("limit 1"));
        if (activeTask != null) {
            News.info("堆垛机:{} 已存在未完成移动任务,工作号={}", crnNo, activeTask.getWrkNo());
            return false;
        }
        Date now = new Date();
        WrkMast wrkMast = new WrkMast();
        wrkMast.setWrkNo(commonService.getWorkNo(WrkIoType.CRN_MOVE.id));
        wrkMast.setIoTime(now);
        wrkMast.setWrkSts(WrkStsType.NEW_CRN_MOVE.sts);
        wrkMast.setIoType(WrkIoType.CRN_MOVE.id);
        wrkMast.setIoPri(0D);
        wrkMast.setLocNo(targetLocNo);
        wrkMast.setCrnNo(crnNo);
        wrkMast.setAppeTime(now);
        wrkMast.setModiTime(now);
        wrkMast.setMemo("dispatchCrnMove");
        if (!wrkMastService.save(wrkMast)) {
            News.info("生成堆垛机移动任务失败,工作号={},任务数据={}", wrkMast.getWrkNo(), JSON.toJSON(wrkMast));
            return false;
        }
        wrkAnalysisService.initForTask(wrkMast);
        return true;
    }
}