#
Administrator
2026-04-28 c83a822ffbf3f3421ad035ce59cf502f3860d0ca
src/main/java/com/zy/core/plugin/XiaosongProcess.java
@@ -15,10 +15,7 @@
import com.zy.core.News;
import com.zy.core.cache.MessageQueue;
import com.zy.core.cache.SlaveConnection;
import com.zy.core.enums.RedisKeyType;
import com.zy.core.enums.SlaveType;
import com.zy.core.enums.StationCommandType;
import com.zy.core.enums.WrkIoType;
import com.zy.core.enums.*;
import com.zy.core.model.StationObjModel;
import com.zy.core.model.Task;
import com.zy.core.model.command.StationCommand;
@@ -35,6 +32,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -97,130 +95,215 @@
    private void submitGenerateStoreWrkFileTask() {
        mainProcessTaskSubmitter.submitSerialTask(
                MainProcessLane.GENERATE_STORE,
                "generateStoreWrkFile",
                "submitGenerateStoreWrkFileTask",
                DISPATCH_INTERVAL_MS,
                this::generateStoreWrkFile
                this::submitGenerateStoreWrkFileTasksInternal
        );
    }
    /**
     * 请求生成入库任务
     * 入库站,根据条码扫描生成入库工作档
     */
    public synchronized void generateStoreWrkFile() {
        try {
            Object systemConfigMapObj = redisUtil.get(RedisKeyType.SYSTEM_CONFIG_MAP.key);
            if (systemConfigMapObj == null) {
                return;
            }
            HashMap<String, String> systemConfigMap = (HashMap<String, String>) systemConfigMapObj;
            int conveyorStationTaskLimit = 30;
            String conveyorStationTaskLimitStr = systemConfigMap.get("conveyorStationTaskLimit");
            if (conveyorStationTaskLimitStr != null) {
                conveyorStationTaskLimit = Integer.parseInt(conveyorStationTaskLimitStr);
            }
            int currentStationTaskCount = stationOperateProcessUtils.getCurrentStationTaskCount();
            if (currentStationTaskCount > conveyorStationTaskLimit) {
                News.error("输送站点任务已达到上限,上限值:{},站点任务数:{}", conveyorStationTaskLimit, currentStationTaskCount);
                return;
    private void submitGenerateStoreWrkFileTasksInternal() {
        List<BasDevp> basDevps = basDevpService.selectList(new EntityWrapper<>());
        for (final BasDevp basDevp : basDevps) {
            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
            if (stationThread == null) {
                continue;
            }
            List<BasDevp> basDevps = basDevpService.selectList(new EntityWrapper<>());
            for (BasDevp basDevp : basDevps) {
                StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
                if (stationThread == null) {
            Map<Integer, StationProtocol> stationMap = stationThread.getStatusMap();
            if (stationMap == null || stationMap.isEmpty()) {
                continue;
            }
            List<StationObjModel> list = basDevp.getBarcodeStationList$();
            for (final StationObjModel entity : list) {
                Integer stationId = entity == null ? null : entity.getStationId();
                if (stationId == null || !stationMap.containsKey(stationId)) {
                    continue;
                }
                Map<Integer, StationProtocol> stationMap = stationThread.getStatusMap();
                List<StationObjModel> list = basDevp.getBarcodeStationList$();
                for (StationObjModel entity : list) {
                    Integer stationId = entity.getStationId();
                    if (!stationMap.containsKey(stationId)) {
                        continue;
                    }
                    StationProtocol stationProtocol = stationMap.get(stationId);
                    if (stationProtocol == null) {
                        continue;
                    }
                    if (stationProtocol.isAutoing()
                            && stationProtocol.isLoading()
                            && stationProtocol.isInEnable()
                            && stationProtocol.getTaskNo() > 0) {
                        if (Cools.isEmpty(stationProtocol.getBarcode())) {
                            continue;
                        }
                        if (stationProtocol.getError() > 0) {
                            Object lock = redisUtil.get(RedisKeyType.GENERATE_STATION_BACK_LIMIT.key + stationProtocol.getTaskNo());
                            if (lock != null) {
                                continue;
                mainProcessTaskSubmitter.submitKeyedSerialTask(
                        MainProcessLane.GENERATE_STORE,
                        stationId,
                        "generateStoreWrkFile",
                        DISPATCH_INTERVAL_MS,
                        new Runnable() {
                            @Override
                            public void run() {
                                generateStoreWrkFile(basDevp, entity);
                            }
                            StationObjModel backStation = entity.getBackStation();
                            StationCommand command = stationThread.getCommand(StationCommandType.MOVE, commonService.getWorkNo(WrkIoType.STATION_BACK.id), stationId, backStation.getStationId(), 0);
                            if (command == null) {
                                News.taskInfo(stationProtocol.getTaskNo(), "{}工作,获取输送线命令失败", stationProtocol.getTaskNo());
                                continue;
                            }
                            MessageQueue.offer(SlaveType.Devp, basDevp.getDevpNo(), new Task(2, command));
                            News.taskInfo(stationProtocol.getTaskNo(), "{}扫码异常,已退回至{}", backStation.getStationId());
                            redisUtil.set(RedisKeyType.GENERATE_STATION_BACK_LIMIT.key + stationProtocol.getTaskNo(), "lock", 10);
                                continue;
                        }
                );
            }
        }
    }
                        // 检测任务是否生成
                        List<WrkMast> wrkMasts = wrkMastService
                                .selectList(new EntityWrapper<WrkMast>()
                                        .eq("barcode", stationProtocol.getBarcode()));
                        if (!wrkMasts.isEmpty()) {
                            continue;
                        }
    private void generateStoreWrkFile(BasDevp basDevp, StationObjModel entity) {
        if (basDevp == null || entity == null || entity.getStationId() == null) {
            return;
        }
                        Object lock = redisUtil.get(RedisKeyType.GENERATE_IN_TASK_LIMIT.key + stationId);
                        if (lock != null) {
                            continue;
                        }
        Object systemConfigMapObj = redisUtil.get(RedisKeyType.SYSTEM_CONFIG_MAP.key);
        if (systemConfigMapObj == null) {
            return;
        }
        HashMap<String, String> systemConfigMap = (HashMap<String, String>) systemConfigMapObj;
                        String barcode = stationProtocol.getBarcode();
                        Integer stationIdVal = stationProtocol.getStationId();
        int conveyorStationTaskLimit = 30;
        String conveyorStationTaskLimitStr = systemConfigMap.get("conveyorStationTaskLimit");
        if (conveyorStationTaskLimitStr != null) {
            conveyorStationTaskLimit = Integer.parseInt(conveyorStationTaskLimitStr);
        }
        int currentStationTaskCount = stationOperateProcessUtils.getCurrentStationTaskCount();
        if (currentStationTaskCount > conveyorStationTaskLimit) {
            News.error("输送站点任务已达到上限,上限值:{},站点任务数:{}", conveyorStationTaskLimit, currentStationTaskCount);
            return;
        }
                        String response = wmsOperateUtils.applyInTask(barcode, stationIdVal,
                                stationProtocol.getPalletHeight());
                        if (response == null) {
                            News.error("WMS入库同步请求失败,接口未响应,barcode={},stationId={}", barcode, stationIdVal);
                            redisUtil.set(RedisKeyType.GENERATE_IN_TASK_LIMIT.key + stationId, "lock", 2);
                            stationProtocol.setSystemWarning("请求入库失败,WMS无返回");
                            continue;
                        }
        Integer stationId = entity.getStationId();
        StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
        if (stationThread == null) {
            return;
        }
                        JSONObject jsonObject = JSON.parseObject(response);
                        Integer code = jsonObject.getInteger("code");
                        if (code != null && code.equals(200)) {
                            StartupDto dto = jsonObject.getObject("data", StartupDto.class);
        Map<Integer, StationProtocol> stationMap = stationThread.getStatusMap();
        if (stationMap == null || !stationMap.containsKey(stationId)) {
            return;
        }
                            CreateInTaskParam taskParam = new CreateInTaskParam();
                            taskParam.setTaskNo(dto.getTaskNo());
                            taskParam.setLocNo(dto.getLocNo());
                            taskParam.setTaskPri(dto.getTaskPri());
                            taskParam.setBarcode(barcode);
                            WrkMast wrkMast = commonService.createInTask(taskParam);
                            stationProtocol.setSystemWarning("");
                        } else {
                            News.error("WMS入库同步请求失败,接口返回Code异常,barcode={},stationId={},response={}",
                                    barcode, stationIdVal, response);
                            redisUtil.set(RedisKeyType.GENERATE_IN_TASK_LIMIT.key + stationId, "lock", 2);
                            stationProtocol.setSystemWarning("请求入库失败,WMS返回=" + response);
                        }
        StationProtocol stationProtocol = stationMap.get(stationId);
        if (stationProtocol == null) {
            return;
        }
        if (!(stationProtocol.isAutoing()
                && stationProtocol.isLoading()
                && stationProtocol.isInEnable()
                && stationProtocol.getTaskNo() > 0)) {
            logTraceLimited("generate_wait_condition_" + stationId, 5,
                    "[WCS Trace][条码站入库] 条码站暂未满足生成条件。stationId={},autoing={},loading={},inEnable={},enableIn={},taskNo={},targetStaNo={},barcode={},error={},warning={}",
                    stationId, stationProtocol.isAutoing(), stationProtocol.isLoading(),
                    stationProtocol.isInEnable(), stationProtocol.isEnableIn(), stationProtocol.getTaskNo(),
                    stationProtocol.getTargetStaNo(), stationProtocol.getBarcode(),
                    stationProtocol.getError(), stationProtocol.getSystemWarning());
            return;
        }
        if (Cools.isEmpty(stationProtocol.getBarcode())) {
            logTraceLimited("generate_wait_barcode_" + stationId, 5,
                    "[WCS Trace][条码站入库] 条码站已到位但还没有条码。stationId={},taskNo={},targetStaNo={},palletHeight={},weight={},error={},warning={}",
                    stationId, stationProtocol.getTaskNo(), stationProtocol.getTargetStaNo(),
                    stationProtocol.getPalletHeight(), stationProtocol.getWeight(),
                    stationProtocol.getError(), stationProtocol.getSystemWarning());
            return;
        }
        if (stationProtocol.getError() > 0) {
            Object backLock = redisUtil.get(RedisKeyType.GENERATE_STATION_BACK_LIMIT.key + stationProtocol.getTaskNo());
            if (backLock != null) {
                return;
            }
            StationObjModel backStation = entity.getBackStation();
            StationCommand command = stationThread.getCommand(StationCommandType.MOVE, commonService.getWorkNo(WrkIoType.STATION_BACK.id), stationId, backStation.getStationId(), 0);
            if (command == null) {
                News.taskInfo(stationProtocol.getTaskNo(), "{}工作,获取输送线命令失败", stationProtocol.getTaskNo());
                return;
            }
            MessageQueue.offer(SlaveType.Devp, basDevp.getDevpNo(), new Task(2, command));
            News.taskInfo(stationProtocol.getTaskNo(), "{}扫码异常,已退回至{}", backStation.getStationId());
            redisUtil.set(RedisKeyType.GENERATE_STATION_BACK_LIMIT.key + stationProtocol.getTaskNo(), "lock", 10);
            return;
        }
        List<WrkMast> wrkMasts = wrkMastService
                .selectList(new EntityWrapper<WrkMast>()
                        .eq("barcode", stationProtocol.getBarcode()));
        if (!wrkMasts.isEmpty()) {
            WrkMast firstWrkMast = wrkMasts.get(0);
            News.info("[WCS Debug][条码站入库] 条码已有工作档,跳过生成。stationId={},barcode={},当前站点taskNo={},已存在数量={},首个工作号={},状态={},locNo={}",
                    stationId, stationProtocol.getBarcode(), stationProtocol.getTaskNo(),
                    wrkMasts.size(), firstWrkMast.getWrkNo(), firstWrkMast.getWrkSts(),
                    firstWrkMast.getLocNo());
            for (WrkMast wrkMast : wrkMasts) {
                if (wrkMast.getIoType().equals(WrkIoType.OUT.id)) {
                    if (wrkMast.getWrkSts().equals(WrkStsType.STATION_RUN.sts) || wrkMast.getWrkSts().equals(WrkStsType.STATION_RUN_COMPLETE.sts)) {
                        wrkMast.setWrkSts(WrkStsType.COMPLETE_OUTBOUND.sts);
                        wrkMast.setModiTime(new Date());
                        wrkMastService.updateById(wrkMast);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        Object generateLock = redisUtil.get(RedisKeyType.GENERATE_IN_TASK_LIMIT.key + stationId);
        if (generateLock != null) {
            logTraceLimited("generate_wait_lock_" + stationId, 3,
                    "[WCS Trace][条码站入库] 条码站生成任务命中限流锁。stationId={},taskNo={},barcode={},warning={}",
                    stationId, stationProtocol.getTaskNo(), stationProtocol.getBarcode(), stationProtocol.getSystemWarning());
            return;
        }
        String barcode = stationProtocol.getBarcode();
        Integer stationIdVal = stationProtocol.getStationId();
        long applyStartMs = System.currentTimeMillis();
        News.info("[WCS Debug][条码站入库] 准备请求WMS生成入库任务。stationId={},barcode={},stationTaskNo={},palletHeight={},isInEnable={}",
                stationIdVal, barcode, stationProtocol.getTaskNo(),
                stationProtocol.getPalletHeight(), stationProtocol.isInEnable());
        String response = wmsOperateUtils.applyInTask(barcode, stationIdVal,
                stationProtocol.getPalletHeight());
        News.info("[WCS Debug][条码站入库] WMS生成入库任务返回。stationId={},barcode={},stationTaskNo={},costMs={},response={}",
                stationIdVal, barcode, stationProtocol.getTaskNo(),
                System.currentTimeMillis() - applyStartMs, response);
        if (response == null) {
            News.error("WMS入库同步请求失败,接口未响应,barcode={},stationId={}", barcode, stationIdVal);
            redisUtil.set(RedisKeyType.GENERATE_IN_TASK_LIMIT.key + stationId, "lock", 2);
            stationProtocol.setSystemWarning("请求入库失败,WMS无返回");
            return;
        }
        JSONObject jsonObject = JSON.parseObject(response);
        Integer code = jsonObject.getInteger("code");
        if (code != null && code.equals(200)) {
            StartupDto dto = jsonObject.getObject("data", StartupDto.class);
            if (dto == null || Cools.isEmpty(dto.getLocNo())) {
                News.warn("[WCS Debug][条码站入库] WMS返回成功但库位为空,等待下一轮生成。stationId={},barcode={},stationTaskNo={},response={}",
                        stationIdVal, barcode, stationProtocol.getTaskNo(), response);
                redisUtil.set(RedisKeyType.GENERATE_IN_TASK_LIMIT.key + stationId, "lock", 2);
                stationProtocol.setSystemWarning("WMS已返回任务但库位为空");
                return;
            }
            CreateInTaskParam taskParam = new CreateInTaskParam();
            taskParam.setTaskNo(dto.getTaskNo());
            taskParam.setLocNo(dto.getLocNo());
            taskParam.setTaskPri(dto.getTaskPri());
            taskParam.setBarcode(barcode);
            News.info("[WCS Debug][条码站入库] 准备创建入库工作档。stationId={},barcode={},wmsTaskNo={},locNo={},taskPri={}",
                    stationIdVal, barcode, dto.getTaskNo(), dto.getLocNo(), dto.getTaskPri());
            WrkMast wrkMast = commonService.createInTask(taskParam);
            News.info("[WCS Debug][条码站入库] 入库工作档创建完成。stationId={},barcode={},wmsTaskNo={},wrkNo={},locNo={}",
                    stationIdVal, barcode, dto.getTaskNo(),
                    wrkMast == null ? null : wrkMast.getWrkNo(), dto.getLocNo());
            stationProtocol.setSystemWarning("");
        } else {
            News.error("WMS入库同步请求失败,接口返回Code异常,barcode={},stationId={},response={}",
                    barcode, stationIdVal, response);
            redisUtil.set(RedisKeyType.GENERATE_IN_TASK_LIMIT.key + stationId, "lock", 2);
            stationProtocol.setSystemWarning("请求入库失败,WMS返回=" + response);
        }
    }
    private void logTraceLimited(String lockKey, int seconds, String format, Object... arguments) {
        String redisKey = RedisKeyType.LOG_LIMIT.key + "wcs_trace_" + lockKey;
        try {
            Object lock = redisUtil.get(redisKey);
            if (lock != null) {
                return;
            }
            redisUtil.set(redisKey, "lock", seconds);
        } catch (Exception e) {
            // 诊断日志不能影响主流程。
        }
        News.info(format, arguments);
    }
    //检测入库站是否有任务生成,并启动入库