自动化立体仓库 - WMS系统
cl
2 天以前 5677fd88b56cd69e416b52144734f3997ef8f8f4
src/main/java/com/zy/api/service/impl/WcsApiServiceImpl.java
@@ -7,6 +7,7 @@
import com.core.common.Cools;
import com.core.common.R;
import com.core.exception.CoolException;
import com.zy.api.controller.params.ChangeLocParams;
import com.zy.api.controller.params.ReassignLocParams;
import com.zy.api.controller.params.ReceviceTaskParams;
import com.zy.api.controller.params.StopOutTaskParams;
@@ -17,24 +18,25 @@
import com.zy.api.service.WcsApiService;
import com.zy.asrs.entity.*;
import com.zy.asrs.service.*;
import com.zy.asrs.task.support.OutboundBatchSeqReleaseGuard;
import com.zy.asrs.utils.Utils;
import com.zy.common.constant.MesConstant;
import com.zy.common.model.LocTypeDto;
import com.zy.common.model.StartupDto;
import com.zy.common.model.CrnDepthRuleProfile;
import com.zy.common.model.enums.WorkNoType;
import com.zy.common.service.CommonService;
import com.zy.common.utils.HttpHandler;
import com.zy.common.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronizationAdapter;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@@ -48,6 +50,8 @@
    private static final long OUT_LOCK_REPORT_PENDING_WRK_STS = 13L;
    private static final long OUT_LOCK_REPORT_SUCCESS_WRK_STS = 21L;
    private static final long OUT_LOCK_REPORT_FAIL_WRK_STS = 22L;
    private static final long OUTBOUND_CRN_COMPLETE_WRK_STS = 25L;
    private static final long OUTBOUND_STATION_COMPLETE_WRK_STS = 26L;
    private static final String OUT_LOCK_REPORT_PENDING_FLAG = "P";
    /** 同一 WCS 路径、同一单号下一组下发的任务条数上限 */
@@ -55,8 +59,19 @@
    /** 三方接口统计:本系统调用 WCS 的 namespace 约定 */
    private static final String NS_WMS_TO_WCS = "本系统请求WCS";
    private static final String REASSIGN_CRN_LOCK_KEY_PREFIX = "wcs:reassign:inbound:crn:";
    private static final long REASSIGN_CRN_LOCK_SECONDS = 180L;
    private static final String EMPTY_PALLET_MATNR = "emptyPallet";
    private static final int EMPTY_PALLET_REASSIGN_TARGET_LEVEL = 8;
    private static final short EMPTY_PALLET_REASSIGN_LOC_TYPE1 = 3;
    private static final short EMPTY_PALLET_REASSIGN_LOC_TYPE2 = 0;
    private static final int CHANGE_LOC_IO_TYPE = 5;
    private static class ReassignCrnPool {
        private final List<Integer> crnNos;
        private ReassignCrnPool(List<Integer> crnNos) {
            this.crnNos = crnNos;
        }
    }
    @Autowired
    private LocMastService locMastService;
@@ -104,9 +119,11 @@
    @Autowired
    private ApiLogService apiLogService;
    @Autowired
    private RowLastnoService rowLastnoService;
    private OutboundBatchSeqReleaseGuard outboundBatchSeqReleaseGuard;
    @Autowired
    private RedisUtil redisUtil;
    private BasCrnDepthRuleService basCrnDepthRuleService;
    @Autowired
    private RowLastnoService rowLastnoService;
    /**
@@ -126,6 +143,10 @@
        String validateMsg = validatePubTask(params, wrkMast);
        if (!Cools.isEmpty(validateMsg)) {
            return R.error(validateMsg);
        }
        String batchBlockMsg = validateOutboundBatchSeqReady(params, wrkMast);
        if (!Cools.isEmpty(batchBlockMsg)) {
            return R.error(batchBlockMsg);
        }
        String url = resolveTaskPath(params);
        String requestJson = JSON.toJSONString(params);
@@ -203,6 +224,7 @@
        List<String> failMsgs = new ArrayList<>();
        List<WorkTaskParams> lastSentChunk = null;
        String skipGroupKey = null;
        Set<String> blockedOutboundUserKeys = new HashSet<>();
        for (List<WorkTaskParams> chunk : chunks) {
            if (chunk == null || chunk.isEmpty()) {
@@ -211,8 +233,22 @@
            WorkTaskParams head = chunk.get(0);
            WrkMast headMast = wrkMastMap.get(head.getTaskNo());
            String key = buildBatchGroupKey(head, headMast);
            String outboundUserKey = buildOutboundUserKey(head, headMast);
            if (skipGroupKey != null && skipGroupKey.equals(key)) {
                continue;
            }
            if (outboundUserKey != null && blockedOutboundUserKeys.contains(outboundUserKey)) {
                continue;
            }
            String batchBlockMsg = validateOutboundBatchSeqReady(chunk, wrkMastMap);
            if (!Cools.isEmpty(batchBlockMsg)) {
                skipMsgs.add(batchBlockMsg);
                skipGroupKey = key;
                if (outboundUserKey != null) {
                    blockedOutboundUserKeys.add(outboundUserKey);
                }
                continue;
            }
@@ -464,6 +500,26 @@
        return outboundPltSlotReleasedInWms(userNo, batchGroup, minPlt - 1);
    }
    private String validateOutboundBatchSeqReady(List<WorkTaskParams> chunk, Map<String, WrkMast> wrkMastMap) {
        if (chunk == null || chunk.isEmpty()) {
            return null;
        }
        WorkTaskParams head = chunk.get(0);
        return validateOutboundBatchSeqReady(head, wrkMastMap.get(head.getTaskNo()));
    }
    private String validateOutboundBatchSeqReady(WorkTaskParams params, WrkMast wrkMast) {
        if (params == null || !"out".equalsIgnoreCase(params.getType())) {
            return null;
        }
        if (wrkMast == null || !Objects.equals(wrkMast.getIoType(), 101)) {
            return null;
        }
        return outboundBatchSeqReleaseGuard.validateReady(
                sortUserNoForPub(params, wrkMast),
                sortBatchGroupForPub(params, wrkMast));
    }
    private boolean outboundPltSlotReleasedInWms(String userNo, String batchSeq, int pltType) {
        EntityWrapper<WrkMast> mastWrapper = new EntityWrapper<>();
        mastWrapper.eq("user_no", userNo);
@@ -521,7 +577,7 @@
        return Comparator
                .comparing((WorkTaskParams p) -> Optional.ofNullable(p.getType()).orElse(""), String.CASE_INSENSITIVE_ORDER)
                .thenComparing(p -> sortUserNoForPub(p, wrkMastMap.get(p.getTaskNo())), Comparator.nullsLast(String::compareTo))
                .thenComparing(p -> sortBatchGroupForPub(p, wrkMastMap.get(p.getTaskNo())), Comparator.nullsLast(String::compareTo))
                .thenComparing(p -> sortBatchGroupForPub(p, wrkMastMap.get(p.getTaskNo())), WcsApiServiceImpl::compareBatchSeqNatural)
                .thenComparing(p -> sortPltForPub(p, wrkMastMap.get(p.getTaskNo())), Comparator.nullsLast(Integer::compareTo));
    }
@@ -545,6 +601,64 @@
            return wrkMast.getBatchSeq();
        }
        return null;
    }
    private static String buildOutboundUserKey(WorkTaskParams params, WrkMast wrkMast) {
        if (params == null || !"out".equalsIgnoreCase(params.getType())) {
            return null;
        }
        String userNo = sortUserNoForPub(params, wrkMast);
        if (Cools.isEmpty(userNo)) {
            return null;
        }
        return resolveSafeKey(userNo);
    }
    private static int compareBatchSeqNatural(String left, String right) {
        String safeLeft = normalizeBatchSeq(left);
        String safeRight = normalizeBatchSeq(right);
        int leftIndex = 0;
        int rightIndex = 0;
        while (leftIndex < safeLeft.length() && rightIndex < safeRight.length()) {
            char leftChar = safeLeft.charAt(leftIndex);
            char rightChar = safeRight.charAt(rightIndex);
            if (Character.isDigit(leftChar) && Character.isDigit(rightChar)) {
                int leftStart = leftIndex;
                int rightStart = rightIndex;
                while (leftIndex < safeLeft.length() && Character.isDigit(safeLeft.charAt(leftIndex))) {
                    leftIndex++;
                }
                while (rightIndex < safeRight.length() && Character.isDigit(safeRight.charAt(rightIndex))) {
                    rightIndex++;
                }
                String leftNumber = safeLeft.substring(leftStart, leftIndex);
                String rightNumber = safeRight.substring(rightStart, rightIndex);
                int compare = new BigInteger(leftNumber).compareTo(new BigInteger(rightNumber));
                if (compare != 0) {
                    return compare;
                }
                compare = Integer.compare(leftNumber.length(), rightNumber.length());
                if (compare != 0) {
                    return compare;
                }
                continue;
            }
            int compare = Character.compare(leftChar, rightChar);
            if (compare != 0) {
                return compare;
            }
            leftIndex++;
            rightIndex++;
        }
        return Integer.compare(safeLeft.length(), safeRight.length());
    }
    private static String normalizeBatchSeq(String value) {
        return Cools.isEmpty(value) ? "" : value;
    }
    private static String resolveSafeKey(String value) {
        return Cools.isEmpty(value) ? "_EMPTY_" : value;
    }
    private static String buildOutboundBatchCacheKey(String userNo, String batchSeq) {
@@ -590,11 +704,29 @@
                    throw new CoolException("任务状态修改失败!!");
                }
            }
        } else if (params.getNotifyType().equals("task")) {
        } else if (isOutboundCrnTaskComplete(params)) {
            // WCS出库任务完成:堆垛机出库任务执行完成,工作状态 -> 25。
            if (isOutboundTask(mast) && canMarkOutboundCrnComplete(mast)) {
                mast.setWrkSts(OUTBOUND_CRN_COMPLETE_WRK_STS);
                mast.setModiTime(new Date());
                if (!wrkMastService.updateById(mast)) {
                    throw new CoolException("任务状态修改失败!!");
                }
            }
        } else if (isOutboundStationTaskRunComplete(params)) {
            // WCS输送站点出库任务运行完成:托盘已到目的地,工作状态 -> 26。
            if (isOutboundTask(mast) && canMarkOutboundStationComplete(mast)) {
                mast.setWrkSts(OUTBOUND_STATION_COMPLETE_WRK_STS);
                mast.setModiTime(new Date());
                if (!wrkMastService.updateById(mast)) {
                    throw new CoolException("任务状态修改失败!!");
                }
            }
        } else if ("task".equalsIgnoreCase(params.getNotifyType())) {
            //任务
            if (params.getMsgType().equals("task_complete")) {
            if ("task_complete".equalsIgnoreCase(params.getMsgType())) {
                if (mast.getIoType() == 1 || mast.getIoType() == 2 ||mast.getIoType() == 10) {
                if (mast.getIoType() == 1 || mast.getIoType() == 2 || mast.getIoType() == 10 || mast.getIoType() == CHANGE_LOC_IO_TYPE) {
                    mast.setWrkSts(4L);
                } else if (isOutboundTask(mast) && canMarkOutboundTaskComplete(mast)) {
                    mast.setWrkSts(14L);
@@ -606,25 +738,9 @@
                    throw new CoolException("任务状态修改失败!!");
                }
                //wcs任务取消接口
            } else if (params.getMsgType().equals("task_cancel")) {
            } else if ("task_cancel".equalsIgnoreCase(params.getMsgType())) {
                workService.cancelWrkMast(String.valueOf(mast.getWrkNo()), 9955L);
            } else if (params.getMsgType().equals("task_arrive")) {
                //到达目的地
                //如果出库任务是跨区则需要生成新的入库任务入库
                if(!Cools.isEmpty(mast.getLocNo())){
                    mast.setOnlineYn("N");//等待生成跨区入库任务
                }
                mast.setWrkSts(14L);
                if(Cools.isEmpty(mast.getStaNo())){
                    mast.setOveMk("Y");
                }
                mast.setModiTime(new Date());
                if (!wrkMastService.updateById(mast)) {
                    throw new CoolException("任务状态修改失败!!");
                }
            }
        } else if (params.getNotifyType().equals("weight")) {
        }
        return R.ok();
    }
@@ -635,8 +751,29 @@
                && "crn_out_task_run".equalsIgnoreCase(params.getMsgType());
    }
    private boolean isOutboundCrnTaskComplete(ReceviceTaskParams params) {
        return params != null
                && "Crn".equalsIgnoreCase(params.getNotifyType())
                && "crn_out_task_complete".equalsIgnoreCase(params.getMsgType());
    }
    private boolean isOutboundStationTaskRunComplete(ReceviceTaskParams params) {
        return params != null
                && "Devp".equalsIgnoreCase(params.getNotifyType())
                && "station_out_task_run_complete".equalsIgnoreCase(params.getMsgType());
    }
    private boolean isOutboundTask(WrkMast mast) {
        return mast != null && mast.getIoType() != null && (mast.getIoType() == 101 || mast.getIoType() == 110);
    }
    private boolean canMarkOutboundCrnComplete(WrkMast mast) {
        if (mast == null || mast.getWrkSts() == null) {
            return false;
        }
        return mast.getWrkSts() < 14
                || mast.getWrkSts().equals(OUT_LOCK_REPORT_SUCCESS_WRK_STS)
                || mast.getWrkSts().equals(OUT_LOCK_REPORT_FAIL_WRK_STS);
    }
    private boolean canMarkOutboundTaskComplete(WrkMast mast) {
@@ -645,7 +782,19 @@
        }
        return mast.getWrkSts() < 14
                || mast.getWrkSts().equals(OUT_LOCK_REPORT_SUCCESS_WRK_STS)
                || mast.getWrkSts().equals(OUT_LOCK_REPORT_FAIL_WRK_STS);
                || mast.getWrkSts().equals(OUT_LOCK_REPORT_FAIL_WRK_STS)
                || mast.getWrkSts().equals(OUTBOUND_CRN_COMPLETE_WRK_STS)
                || mast.getWrkSts().equals(OUTBOUND_STATION_COMPLETE_WRK_STS);
    }
    private boolean canMarkOutboundStationComplete(WrkMast mast) {
        if (mast == null || mast.getWrkSts() == null) {
            return false;
        }
        return mast.getWrkSts() < 14
                || mast.getWrkSts().equals(OUT_LOCK_REPORT_SUCCESS_WRK_STS)
                || mast.getWrkSts().equals(OUT_LOCK_REPORT_FAIL_WRK_STS)
                || mast.getWrkSts().equals(OUTBOUND_CRN_COMPLETE_WRK_STS);
    }
    @Override
@@ -706,28 +855,37 @@
            return R.error("当前目标库位不存在");
        }
        LocTypeDto locTypeDto = buildReassignLocTypeDto(currentLoc);
        List<Integer> areaOrder = buildReassignAreaOrder(wrkMast, currentLoc);
        if (Cools.isEmpty(areaOrder)) {
            return R.error("无法确定任务所属库区");
        boolean emptyPallet = isReassignEmptyPallet(wrkMast);
        LocTypeDto locTypeDto = buildReassignLocTypeDto(currentLoc, emptyPallet);
        BasDevp sourceStation = basDevpService.selectById(wrkMast.getSourceStaNo());
        if (Cools.isEmpty(sourceStation)) {
            return R.error("源站未配置入库优先池");
        }
        List<ReassignCrnPool> poolOrder;
        try {
            poolOrder = buildReassignCrnPoolOrder(sourceStation, wrkMast.getCrnNo());
        } catch (CoolException e) {
            return R.error(e.getMessage());
        }
        if (Cools.isEmpty(poolOrder)) {
            return R.error("源站未配置入库优先池");
        }
        StartupDto startupDto = null;
        Integer preferredArea = null;
        for (Integer area : areaOrder) {
            List<Integer> candidateCrnNos = buildReassignCandidateCrnNos(area, wrkMast.getCrnNo());
        Integer targetLevel = resolveReassignTargetLevel(emptyPallet);
        for (ReassignCrnPool pool : poolOrder) {
            List<Integer> candidateCrnNos = buildReassignCandidateCrnNos(pool.crnNos, wrkMast.getCrnNo());
            if (candidateCrnNos.isEmpty()) {
                continue;
            }
            startupDto = commonService.findRun2InboundLocByCandidateCrnNos(
                    wrkMast.getSourceStaNo(), wrkMast.getIoType(), area, candidateCrnNos, locTypeDto);
                    wrkMast.getSourceStaNo(), wrkMast.getIoType(), candidateCrnNos, locTypeDto, targetLevel);
            if (startupDto != null && !Cools.isEmpty(startupDto.getLocNo())) {
                preferredArea = area;
                break;
            }
        }
        if (startupDto == null || Cools.isEmpty(startupDto.getLocNo())) {
            return R.error("当前库区没有可重新分配的空库位");
            return R.error("当前优先池没有可重新分配的空库位");
        }
        LocMast targetLoc = locMastService.selectById(startupDto.getLocNo());
@@ -742,11 +900,219 @@
        updateReassignTargetLoc(targetLoc, wrkMast, currentLoc, now);
        updateReassignWorkMast(wrkMast, startupDto, now);
        releaseOldReservedLocIfNeeded(currentLoc, targetLoc.getLocNo(), now);
        lockReassignedCrnAfterCommit(preferredArea, targetLoc.getCrnNo(), wrkMast.getWrkNo());
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("locNo", Utils.WMSLocToWCSLoc(targetLoc.getLocNo()));
        return R.ok("操作成功").add(result);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R changeLoc(ChangeLocParams params) {
        if (params == null || Cools.isEmpty(params.getLocNo())) {
            return R.error("locNo不能为空");
        }
        LocMast sourceLoc = locMastService.selectById(params.getLocNo());
        if (sourceLoc == null) {
            return R.error("当前库位不存在");
        }
        CrnDepthRuleProfile profile = resolveChangeLocProfile(sourceLoc);
        String validateMsg = validateChangeLocSource(sourceLoc, profile);
        if (!Cools.isEmpty(validateMsg)) {
            return R.error(validateMsg);
        }
        List<Integer> candidateRows = resolveChangeLocRows(params.getRow(), profile);
        LocMast targetLoc = selectChangeLocTarget(sourceLoc, profile, candidateRows);
        if (targetLoc == null) {
            return R.error("当前堆垛机无可用移库目标位");
        }
        Integer workNo = commonService.getWorkNo(WorkNoType.PICK.type);
        Date now = new Date();
        createChangeLocTask(workNo, sourceLoc, targetLoc, now);
        reserveChangeLocTargetLoc(targetLoc, sourceLoc, now);
        reserveChangeLocSourceLoc(sourceLoc, now);
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("locNo", Utils.WMSLocToWCSLoc(targetLoc.getLocNo()));
        result.put("taskNo", String.valueOf(workNo));
        return R.ok("操作成功").add(result);
    }
    private CrnDepthRuleProfile resolveChangeLocProfile(LocMast sourceLoc) {
        RowLastno rowLastno = rowLastnoService.selectById(sourceLoc.getWhsType());
        return basCrnDepthRuleService.resolveProfile(rowLastno, sourceLoc.getCrnNo(), sourceLoc.getRow1());
    }
    private String validateChangeLocSource(LocMast sourceLoc, CrnDepthRuleProfile profile) {
        if (sourceLoc.getCrnNo() == null) {
            return "当前库位未绑定堆垛机";
        }
        if (profile == null || !profile.isDoubleExtension()) {
            return "当前堆垛机非双深库位,不支持移库";
        }
        if (sourceLoc.getRow1() == null || profile == null || !profile.isShallowRow(sourceLoc.getRow1())) {
            return "当前库位不是浅库位";
        }
        if (!isChangeLocInStockLocSts(sourceLoc.getLocSts())) {
            return "当前库位状态不允许移库";
        }
        return null;
    }
    private List<Integer> resolveChangeLocRows(List<Integer> requestRows, CrnDepthRuleProfile profile) {
        if (!Cools.isEmpty(requestRows)) {
            return requestRows.stream()
                    .filter(Objects::nonNull)
                    .distinct()
                    .collect(Collectors.toList());
        }
        return profile == null ? Collections.emptyList() : profile.getSearchRows();
    }
    private LocMast selectChangeLocTarget(LocMast sourceLoc, CrnDepthRuleProfile profile, List<Integer> candidateRows) {
        List<LocMast> locMasts = locMastService.selectList(new EntityWrapper<LocMast>()
                .eq("crn_no", sourceLoc.getCrnNo())
                .eq("loc_type1",2)
                .eq("loc_sts", "O")
                .orderBy("row1", true)
                .orderBy("lev1", true)
                .orderBy("bay1", true));
        if (Cools.isEmpty(locMasts)) {
            return null;
        }
//        for (LocMast locMast : locMasts) {
//            if (!isChangeLocRowAllowed(locMast, candidateRows)) {
//                continue;
//            }
//            if (locMast.getRow1() != null && Utils.isDeepLoc(slaveProperties, locMast.getRow1())) {
//                return locMast;
//            }
//        }
//        for (LocMast locMast : locMasts) {
//            if (!isChangeLocRowAllowed(locMast, candidateRows)) {
//                continue;
//            }
//            if (locMast.getRow1() == null || !Utils.isShallowLoc(slaveProperties, locMast.getRow1())) {
//                continue;
//            }
//            String deepLocNo = Utils.getDeepLoc(slaveProperties, locMast.getLocNo());
//            LocMast deepLoc = locMastService.selectById(deepLocNo);
//            if (deepLoc != null && ("F".equals(deepLoc.getLocSts()) || "D".equals(deepLoc.getLocSts()))) {
//                return locMast;
//            }
//        }
        LocMast shallowFallback = null;
        for (LocMast locMast : locMasts) {
            //排过滤
            if (!isChangeLocRowAllowed(locMast, candidateRows)) {
                continue;
            }
            //深位优先
            if (isChangeLocDeepCandidate(locMast, profile)) {
                return locMast;
            }
            //浅位兜底
            if (shallowFallback == null && isChangeLocShallowFallbackCandidate(locMast, profile)) {
                shallowFallback = locMast;
            }
        }
        return shallowFallback;
    }
    private boolean isChangeLocRowAllowed(LocMast locMast, List<Integer> candidateRows) {
        if (Cools.isEmpty(candidateRows)) {
            return true;
        }
        return locMast.getRow1() != null && candidateRows.contains(locMast.getRow1());
    }
    private boolean isChangeLocDeepCandidate(LocMast locMast, CrnDepthRuleProfile profile) {
        return locMast.getRow1() != null
                && profile != null
                && profile.isDeepRow(locMast.getRow1());
    }
    private boolean isChangeLocShallowFallbackCandidate(LocMast locMast, CrnDepthRuleProfile profile) {
        if (locMast.getRow1() == null || profile == null || !profile.isShallowRow(locMast.getRow1())) {
            return false;
        }
        Integer deepRow = profile.getPairedDeepRow(locMast.getRow1());
        if (deepRow == null) {
            return false;
        }
        LocMast deepLoc = locMastService.selectOne(new EntityWrapper<LocMast>()
                .eq("crn_no", locMast.getCrnNo())
                .eq("row1", deepRow)
                .eq("bay1", locMast.getBay1())
                .eq("lev1", locMast.getLev1()));
        return deepLoc != null && isChangeLocInStockLocSts(deepLoc.getLocSts());
    }
    private boolean isChangeLocInStockLocSts(String locSts) {
        return "F".equals(locSts) || "D".equals(locSts);
    }
    private void createChangeLocTask(Integer workNo, LocMast sourceLoc, LocMast targetLoc, Date now) {
        List<LocDetl> sourceDetls = locDetlService.selectList(new EntityWrapper<LocDetl>().eq("loc_no", sourceLoc.getLocNo()));
        WrkMast wrkMast = new WrkMast();
        wrkMast.setWrkNo(workNo);
        wrkMast.setIoTime(now);
        wrkMast.setWrkSts(11L);
        wrkMast.setIoType(CHANGE_LOC_IO_TYPE);
        wrkMast.setIoPri(10D);
        wrkMast.setCrnNo(sourceLoc.getCrnNo());
        wrkMast.setSourceLocNo(sourceLoc.getLocNo());
        wrkMast.setLocNo(targetLoc.getLocNo());
        wrkMast.setFullPlt(Cools.isEmpty(sourceDetls) ? "N" : "Y");
        wrkMast.setPicking("N");
        wrkMast.setExitMk("N");
        wrkMast.setEmptyMk("D".equals(sourceLoc.getLocSts()) ? "Y" : "N");
        wrkMast.setBarcode(sourceLoc.getBarcode());
        wrkMast.setLinkMis("N");
        wrkMast.setMemo("CHANGE_LOC");
        wrkMast.setAppeUser(WCS_SYNC_USER);
        wrkMast.setAppeTime(now);
        wrkMast.setModiUser(WCS_SYNC_USER);
        wrkMast.setModiTime(now);
        if (!wrkMastService.insert(wrkMast)) {
            throw new CoolException("保存工作档失败");
        }
        if (Cools.isEmpty(sourceDetls)) {
            return;
        }
        for (LocDetl sourceDetl : sourceDetls) {
            WrkDetl wrkDetl = new WrkDetl();
            wrkDetl.sync(sourceDetl);
            wrkDetl.setWrkNo(workNo);
            wrkDetl.setIoTime(now);
            wrkDetl.setAnfme(sourceDetl.getAnfme());
            wrkDetl.setAppeTime(now);
            wrkDetl.setAppeUser(WCS_SYNC_USER);
            wrkDetl.setModiTime(now);
            wrkDetl.setModiUser(WCS_SYNC_USER);
            if (!wrkDetlService.insert(wrkDetl)) {
                throw new CoolException("保存工作档明细失败");
            }
        }
    }
    private void reserveChangeLocTargetLoc(LocMast targetLoc, LocMast sourceLoc, Date now) {
        targetLoc.setLocSts("S");
        targetLoc.setBarcode(sourceLoc.getBarcode());
        targetLoc.setScWeight(sourceLoc.getScWeight() == null ? BigDecimal.ZERO : sourceLoc.getScWeight());
        targetLoc.setModiUser(WCS_SYNC_USER);
        targetLoc.setModiTime(now);
        if (!locMastService.updateById(targetLoc)) {
            throw new CoolException("更新目标库位状态失败");
        }
    }
    private void reserveChangeLocSourceLoc(LocMast sourceLoc, Date now) {
        sourceLoc.setLocSts("R");
        sourceLoc.setModiUser(WCS_SYNC_USER);
        sourceLoc.setModiTime(now);
        if (!locMastService.updateById(sourceLoc)) {
            throw new CoolException("更新源库位状态失败");
        }
    }
    private boolean requiresOutboundErpConfirm(WrkMast wrkMast) {
@@ -778,6 +1144,9 @@
            if ("Y".equalsIgnoreCase(wrkMast.getPauseMk())) {
                return "task paused";
            }
            if (Objects.equals(wrkMast.getIoType(), 101) && Cools.isEmpty(wrkMast.getBatchSeq())) {
                return "出库进仓编号(batchSeq)为空,跳过下发";
            }
            if (requiresOutboundErpConfirm(wrkMast) && !"Y".equalsIgnoreCase(wrkMast.getPdcType())) {
                return "task not confirmed by erp";
            }
@@ -807,115 +1176,102 @@
        return null;
    }
    private Integer resolveReassignArea(WrkMast wrkMast, LocMast currentLoc) {
        List<Integer> stationAreas = Utils.getStationStorageAreas(wrkMast.getSourceStaNo());
        if (!Cools.isEmpty(stationAreas)) {
            for (Integer area : stationAreas) {
                if (belongsToArea(area, wrkMast.getCrnNo(), currentLoc)) {
                    return area;
                }
            }
    private List<ReassignCrnPool> buildReassignCrnPoolOrder(BasDevp sourceStation, Integer currentCrnNo) {
        List<Integer> firstPoolCrnNos = Utils.distinctCrnNos(sourceStation.getInFirstCrnCsv());
        List<Integer> secondPoolCrnNos = excludeReassignPoolCrnNos(
                Utils.distinctCrnNos(sourceStation.getInSecondCrnCsv()), firstPoolCrnNos);
        if (Cools.isEmpty(firstPoolCrnNos) && Cools.isEmpty(secondPoolCrnNos)) {
            throw new CoolException("源站未配置入库优先池");
        }
        Integer fallbackArea = findAreaByCurrentTask(wrkMast.getCrnNo(), currentLoc);
        if (fallbackArea != null) {
            return fallbackArea;
        boolean inFirstPool = firstPoolCrnNos.contains(currentCrnNo);
        boolean inSecondPool = secondPoolCrnNos.contains(currentCrnNo);
        if (!inFirstPool && !inSecondPool) {
            throw new CoolException("当前任务堆垛机未配置在源站入库优先池");
        }
        return Utils.getStationStorageArea(wrkMast.getSourceStaNo());
        List<ReassignCrnPool> poolOrder = new ArrayList<>();
        if (inFirstPool) {
            poolOrder.add(new ReassignCrnPool(firstPoolCrnNos));
            poolOrder.add(new ReassignCrnPool(secondPoolCrnNos));
            return poolOrder;
        }
        poolOrder.add(new ReassignCrnPool(secondPoolCrnNos));
        poolOrder.add(new ReassignCrnPool(firstPoolCrnNos));
        return poolOrder;
    }
    private Integer findAreaByCurrentTask(Integer currentCrnNo, LocMast currentLoc) {
        for (int area = 1; area <= 3; area++) {
            if (belongsToArea(area, currentCrnNo, currentLoc)) {
                return area;
    private List<Integer> excludeReassignPoolCrnNos(List<Integer> crnNos, List<Integer> excludedCrnNos) {
        List<Integer> result = new ArrayList<>();
        if (Cools.isEmpty(crnNos)) {
            return result;
        }
        LinkedHashSet<Integer> excluded = new LinkedHashSet<>(Utils.distinctCrnNos(excludedCrnNos));
        for (Integer crnNo : Utils.distinctCrnNos(crnNos)) {
            if (crnNo == null || excluded.contains(crnNo)) {
                continue;
            }
            result.add(crnNo);
        }
        return result;
    }
    private List<Integer> buildReassignCandidateCrnNos(List<Integer> poolCrnNos, Integer currentCrnNo) {
        return buildReassignCrnSearchOrder(poolCrnNos, currentCrnNo);
    }
    private List<Integer> buildReassignCrnSearchOrder(List<Integer> poolCrnNos, Integer currentCrnNo) {
        List<Integer> orderedCrnNos = Utils.distinctCrnNos(poolCrnNos);
        Collections.sort(orderedCrnNos);
        if (Cools.isEmpty(orderedCrnNos) || currentCrnNo == null) {
            return orderedCrnNos;
        }
        List<Integer> searchOrder = new ArrayList<>();
        for (int index = orderedCrnNos.size() - 1; index >= 0; index--) {
            Integer crnNo = orderedCrnNos.get(index);
            if (crnNo == null || crnNo >= currentCrnNo) {
                continue;
            }
            searchOrder.add(crnNo);
        }
        for (int index = orderedCrnNos.size() - 1; index >= 0; index--) {
            Integer crnNo = orderedCrnNos.get(index);
            if (crnNo == null || crnNo <= 0 || crnNo.equals(currentCrnNo) || crnNo < currentCrnNo) {
                continue;
            }
            searchOrder.add(crnNo);
        }
        return searchOrder;
    }
    private Integer resolveReassignTargetLevel(boolean emptyPallet) {
        if (emptyPallet) {
            return EMPTY_PALLET_REASSIGN_TARGET_LEVEL;
        }
        return null;
    }
    private List<Integer> buildReassignAreaOrder(WrkMast wrkMast, LocMast currentLoc) {
        LinkedHashSet<Integer> areaOrder = new LinkedHashSet<>();
        List<Integer> stationAreas = Utils.getStationStorageAreas(wrkMast.getSourceStaNo());
        if (!Cools.isEmpty(stationAreas)) {
            areaOrder.addAll(stationAreas);
    private boolean isReassignEmptyPallet(WrkMast wrkMast) {
        if (wrkMast == null || wrkMast.getWrkNo() == null) {
            return false;
        }
        Integer currentArea = findAreaByCurrentTask(wrkMast.getCrnNo(), currentLoc);
        if (currentArea != null) {
            areaOrder.add(currentArea);
        List<WrkDetl> wrkDetls = wrkDetlService.selectByWrkNo(wrkMast.getWrkNo());
        if (Cools.isEmpty(wrkDetls)) {
            return false;
        }
        if (areaOrder.isEmpty()) {
            Integer resolvedArea = resolveReassignArea(wrkMast, currentLoc);
            if (resolvedArea != null) {
                areaOrder.add(resolvedArea);
        for (WrkDetl wrkDetl : wrkDetls) {
            if (wrkDetl != null && EMPTY_PALLET_MATNR.equalsIgnoreCase(String.valueOf(wrkDetl.getMatnr()).trim())) {
                return true;
            }
        }
        return new ArrayList<>(areaOrder);
        return false;
    }
    private boolean belongsToArea(Integer area, Integer currentCrnNo, LocMast currentLoc) {
        if (area == null || area <= 0) {
            return false;
        }
        RowLastno areaRowLastno = rowLastnoService.selectById(area);
        if (areaRowLastno == null) {
            return false;
        }
        Integer startCrnNo = resolveAreaStartCrnNo(areaRowLastno);
        Integer endCrnNo = resolveAreaEndCrnNo(areaRowLastno, startCrnNo);
        if (currentCrnNo != null && currentCrnNo >= startCrnNo && currentCrnNo <= endCrnNo) {
            return true;
        }
        Integer row = currentLoc == null ? null : currentLoc.getRow1();
        Integer startRow = areaRowLastno.getsRow();
        Integer endRow = areaRowLastno.geteRow();
        return row != null && startRow != null && endRow != null && row >= startRow && row <= endRow;
    }
    private List<Integer> buildReassignCandidateCrnNos(Integer area, Integer currentCrnNo) {
        RowLastno areaRowLastno = rowLastnoService.selectById(area);
        if (areaRowLastno == null) {
            throw new CoolException("未找到库区轮询规则");
        }
        int startCrnNo = resolveAreaStartCrnNo(areaRowLastno);
        int endCrnNo = resolveAreaEndCrnNo(areaRowLastno, startCrnNo);
        if (currentCrnNo == null || currentCrnNo < startCrnNo || currentCrnNo > endCrnNo) {
            throw new CoolException("当前任务堆垛机不在所属库区范围内");
        }
        List<Integer> candidateCrnNos = new ArrayList<>();
        for (int crnNo = currentCrnNo - 1; crnNo >= startCrnNo; crnNo--) {
            addUnlockedReassignCandidate(candidateCrnNos, area, crnNo);
        }
        for (int crnNo = endCrnNo; crnNo > currentCrnNo; crnNo--) {
            addUnlockedReassignCandidate(candidateCrnNos, area, crnNo);
        }
        return candidateCrnNos;
    }
    private void addUnlockedReassignCandidate(List<Integer> candidateCrnNos, Integer area, int crnNo) {
        if (isReassignCrnLocked(area, crnNo)) {
            log.info("skip locked reassign crane. area={}, crnNo={}, ttl={}s",
                    area, crnNo, redisUtil.getExpire(buildReassignCrnLockKey(area, crnNo)));
            return;
        }
        candidateCrnNos.add(crnNo);
    }
    private int resolveAreaStartCrnNo(RowLastno areaRowLastno) {
        if (areaRowLastno.getsCrnNo() != null && areaRowLastno.getsCrnNo() > 0) {
            return areaRowLastno.getsCrnNo();
        }
        return 1;
    }
    private int resolveAreaEndCrnNo(RowLastno areaRowLastno, int startCrnNo) {
        if (areaRowLastno.geteCrnNo() != null && areaRowLastno.geteCrnNo() >= startCrnNo) {
            return areaRowLastno.geteCrnNo();
        }
        int crnQty = areaRowLastno.getCrnQty() == null || areaRowLastno.getCrnQty() <= 0 ? 1 : areaRowLastno.getCrnQty();
        return startCrnNo + crnQty - 1;
    }
    private LocTypeDto buildReassignLocTypeDto(LocMast currentLoc) {
    private LocTypeDto buildReassignLocTypeDto(LocMast currentLoc, boolean emptyPallet) {
        LocTypeDto locTypeDto = new LocTypeDto();
        if (emptyPallet) {
            locTypeDto.setLocType1(EMPTY_PALLET_REASSIGN_LOC_TYPE1);
            locTypeDto.setLocType2(EMPTY_PALLET_REASSIGN_LOC_TYPE2);
            return locTypeDto;
        }
        if (currentLoc == null) {
            return locTypeDto;
        }
@@ -981,43 +1337,6 @@
        if (!locMastService.updateById(currentLoc)) {
            throw new CoolException("释放原目标库位失败");
        }
    }
    private boolean isReassignCrnLocked(Integer area, Integer crnNo) {
        if (area == null || crnNo == null) {
            return false;
        }
        return redisUtil.hasKey(buildReassignCrnLockKey(area, crnNo));
    }
    private String buildReassignCrnLockKey(Integer area, Integer crnNo) {
        return REASSIGN_CRN_LOCK_KEY_PREFIX + area + ":" + crnNo;
    }
    private void lockReassignedCrnAfterCommit(Integer area, Integer crnNo, Integer wrkNo) {
        if (area == null || crnNo == null) {
            return;
        }
        Runnable action = () -> {
            String key = buildReassignCrnLockKey(area, crnNo);
            boolean locked = redisUtil.set(key, String.valueOf(wrkNo), REASSIGN_CRN_LOCK_SECONDS);
            if (!locked) {
                log.warn("failed to lock reassigned crane in redis. area={}, crnNo={}, wrkNo={}", area, crnNo, wrkNo);
                return;
            }
            log.info("locked reassigned crane in redis. area={}, crnNo={}, wrkNo={}, ttl={}s",
                    area, crnNo, wrkNo, REASSIGN_CRN_LOCK_SECONDS);
        };
        if (TransactionSynchronizationManager.isActualTransactionActive()) {
            TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
                @Override
                public void afterCommit() {
                    action.run();
                }
            });
            return;
        }
        action.run();
    }
    /**