Junjie
2026-04-27 b8b8fd58c948692a7b6fb5f3dad715a2a9a3a083
src/main/java/com/zy/core/plugin/store/StoreInTaskGenerationService.java
@@ -6,7 +6,9 @@
import com.core.common.Cools;
import com.zy.asrs.domain.param.CreateInTaskParam;
import com.zy.asrs.entity.BasDevp;
import com.zy.asrs.entity.WrkLastno;
import com.zy.asrs.entity.WrkMast;
import com.zy.asrs.service.WrkLastnoService;
import com.zy.asrs.service.WrkMastService;
import com.zy.common.model.StartupDto;
import com.zy.common.service.CommonService;
@@ -31,6 +33,7 @@
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@@ -52,6 +55,8 @@
    private MainProcessTaskSubmitter mainProcessTaskSubmitter;
    @Autowired
    private StationCommandDispatcher stationCommandDispatcher;
    @Autowired
    private WrkLastnoService wrkLastnoService;
    /**
     * 保留当前按站点 lane 并发的能力,同时用一个简单计数避免并发生成把站点任务数顶穿上限。
@@ -61,6 +66,11 @@
    public void generate(StoreInTaskPolicy policy, BasDevp basDevp, StationObjModel stationObjModel) {
        try {
            if (!policy.isEnabled()) {
                return;
            }
            StoreInTaskContext earlyContext = buildContext(basDevp, stationObjModel);
            if (earlyContext == null || !handleErrorStationBack(policy, earlyContext)) {
                return;
            }
@@ -179,6 +189,86 @@
        }
    }
    private boolean handleErrorStationBack(StoreInTaskPolicy policy, StoreInTaskContext context) {
        StationProtocol stationProtocol = context.getStationProtocol();
        if (stationProtocol == null) {
            return false;
        }
        if (!stationProtocol.isAutoing()) {
            return false;
        }
        if (!stationProtocol.isLoading()) {
            return false;
        }
        if (stationProtocol.getError() <= 0) {
            return true;
        }
        if (!stationProtocol.isInBarcodeError()) {
            return true;
        }
        WrkLastno stationBackTaskRange = wrkLastnoService.getById(WrkIoType.STATION_BACK.id);
        Integer currentTaskNo = stationProtocol.getTaskNo();
        if (currentTaskNo != null
                && currentTaskNo > 0
                && stationBackTaskRange != null
                && stationBackTaskRange.getsNo() != null
                && stationBackTaskRange.geteNo() != null
                && currentTaskNo >= stationBackTaskRange.getsNo()
                && currentTaskNo <= stationBackTaskRange.geteNo()) {
            News.info("条码站已处于退回工作号范围,跳过重复生成退回命令。stationId={},taskNo={},range=[{}, {}]",
                    stationProtocol.getStationId(),
                    currentTaskNo,
                    stationBackTaskRange.getsNo(),
                    stationBackTaskRange.geteNo());
            return false;
        }
        StationObjModel backStation = context.getStationObjModel().getBackStation();
        if (backStation == null || backStation.getStationId() == null) {
            News.warn("条码站退回失败,退回站未配置。deviceNo={},stationId={}",
                    context.getBasDevp() == null ? null : context.getBasDevp().getDevpNo(),
                    stationProtocol.getStationId());
            return false;
        }
        if (stationProtocol.getTaskNo() != null
                && stationProtocol.getTaskNo() > 0
                && backStation.getStationId().equals(stationProtocol.getTargetStaNo())) {
            return false;
        }
        Object lock = redisUtil.get(RedisKeyType.GENERATE_STATION_BACK_LIMIT.key + stationProtocol.getStationId());
        if (lock != null) {
            return false;
        }
        StationCommand command = context.getStationThread().getCommand(StationCommandType.MOVE,
                commonService.getWorkNo(WrkIoType.STATION_BACK.id),
                context.getStationObjModel().getStationId(),
                backStation.getStationId(), 0);
        if (command == null) {
            News.taskInfo(stationProtocol.getTaskNo(), "{}工作,获取输送线命令失败", stationProtocol.getTaskNo());
            return false;
        }
        stationCommandDispatcher.dispatch(context.getBasDevp().getDevpNo(), command, "store-in-task", "station-back");
        String errorMsg = Cools.isEmpty(stationProtocol.getErrorMsg()) ? "未知异常" : stationProtocol.getErrorMsg();
        String warning = "条码站异常退回,报警信息:" + errorMsg;
        if (!Cools.isEmpty(stationProtocol.getSystemWarning())) {
            warning = stationProtocol.getSystemWarning() + ";" + warning;
        }
        policy.setSystemWarning(context, warning);
        News.info("{}扫码站异常,已退回至{},条码站状态:{}", stationProtocol.getTaskNo(),
                backStation.getStationId(), JSON.toJSONString(stationProtocol));
        redisUtil.set(RedisKeyType.GENERATE_STATION_BACK_LIMIT.key + stationProtocol.getStationId(),
                "lock", 60 * 60);
        return false;
    }
    private StoreInTaskContext buildContext(BasDevp basDevp, StationObjModel stationObjModel) {
        if (basDevp == null || stationObjModel == null || stationObjModel.getStationId() == null) {
            return null;
@@ -246,13 +336,13 @@
                request.getBarcode(), request.getSourceStaNo(), result.getResponse(), result.getMessage());
        redisUtil.set(policy.getGenerateLockKey(context), "lock", policy.getRetryLockSeconds(context));
        policy.onApplyFailed(context, result);
        triggerStationBackOnApplyFailed(context, request, result);
        triggerStationBackOnApplyFailed(policy, context, request, result);
    }
    /**
     * WMS 申请入库失败后,货物仍停留在扫码站,此时补发退回到入库站的输送命令,避免货物长期滞留。
     */
    private void triggerStationBackOnApplyFailed(StoreInTaskContext context,
    private void triggerStationBackOnApplyFailed(StoreInTaskPolicy policy, StoreInTaskContext context,
                                                 InTaskApplyRequest request,
                                                 InTaskApplyResult result) {
        if (context == null || context.getStationThread() == null || context.getStationObjModel() == null) {
@@ -323,6 +413,12 @@
        }
        redisUtil.set(lockKey, "lock", APPLY_FAIL_STATION_BACK_LOCK_SECONDS);
        String currentWarning = stationProtocol.getSystemWarning();
        String backWarning = "WMS入库失败,已退回入库站";
        if (!Cools.isEmpty(currentWarning)) {
            backWarning = currentWarning + ";" + backWarning;
        }
        policy.setSystemWarning(context, backWarning);
        News.warn("WMS入库失败,已触发货物退回入库站。barcode={},stationId={},backStationId={},warning={}",
                request == null ? null : request.getBarcode(),
                currentStationId,