Junjie
昨天 9079c4f09f4c267576638b5f41c8aa695742a66a
src/main/java/com/zy/common/service/CommonService.java
@@ -20,6 +20,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Comparator;
@@ -31,7 +32,7 @@
@Service
public class CommonService {
    private static final long OUT_STATION_ROUTE_CACHE_SECONDS = 300L;
    private static final long OUT_STATION_ROUTE_CACHE_SECONDS = 60 * 60 * 24 * 7;
    @Autowired
    private WrkMastService wrkMastService;
@@ -124,6 +125,7 @@
        return true;
    }
    @Transactional
    public boolean cancelTask(CancelTaskParam param) {
        WrkMast wrkMast = null;
        Integer wrkNo = param.getWrkNo();
@@ -141,24 +143,59 @@
            throw new CoolException("任务不存在");
        }
        boolean cancelSuccess = false;
        if (wrkMast.getIoType().equals(WrkIoType.IN.id) && !wrkMast.getWrkSts().equals(WrkStsType.NEW_INBOUND.sts)) {
            cancelSuccess = true;
        } else if (wrkMast.getIoType().equals(WrkIoType.OUT.id) && !wrkMast.getWrkSts().equals(WrkStsType.NEW_OUTBOUND.sts)) {
            cancelSuccess = true;
        } else if (wrkMast.getIoType().equals(WrkIoType.LOC_MOVE.id) && !wrkMast.getWrkSts().equals(WrkStsType.NEW_LOC_MOVE.sts)) {
            cancelSuccess = true;
        } else if (wrkMast.getIoType().equals(WrkIoType.CRN_MOVE.id) && !wrkMast.getWrkSts().equals(WrkStsType.NEW_CRN_MOVE.sts)) {
            cancelSuccess = true;
        Long expectedWrkSts;
        if (wrkMast.getIoType().equals(WrkIoType.IN.id)) {
            expectedWrkSts = WrkStsType.NEW_INBOUND.sts;
        } else if (wrkMast.getIoType().equals(WrkIoType.OUT.id)) {
            expectedWrkSts = WrkStsType.NEW_OUTBOUND.sts;
        } else if (wrkMast.getIoType().equals(WrkIoType.LOC_MOVE.id)) {
            expectedWrkSts = WrkStsType.NEW_LOC_MOVE.sts;
        } else if (wrkMast.getIoType().equals(WrkIoType.CRN_MOVE.id)) {
            expectedWrkSts = WrkStsType.NEW_CRN_MOVE.sts;
        } else {
            throw new CoolException("任务类型不支持取消");
        }
        if (cancelSuccess) {
        if (!expectedWrkSts.equals(wrkMast.getWrkSts())) {
            throw new CoolException("任务已执行,取消失败");
        }
        wrkMast.setMk("taskCancel");
        wrkMast.setModiTime(new Date());
        wrkMastService.updateById(wrkMast);
        boolean updated = wrkMastService.update(null, new UpdateWrapper<WrkMast>()
                .eq("wrk_no", wrkMast.getWrkNo())
                .eq("wrk_sts", expectedWrkSts)
                .set("mk", "taskCancel")
                .set("modi_time", new Date()));
        if (!updated) {
            throw new CoolException("任务状态已变化,取消失败");
        }
        return true;
    }
    @Transactional
    public boolean forceCancelTask(CancelTaskParam param) {
        WrkMast wrkMast = null;
        Integer wrkNo = param.getWrkNo();
        String taskNo = param.getTaskNo();//wms任务号
        if (wrkNo == null) {
            if (!Cools.isEmpty(taskNo)) {
                wrkMast = wrkMastService.getOne(new QueryWrapper<WrkMast>().eq("wms_wrk_no", taskNo));
            }
        } else {
            wrkMast = wrkMastService.selectByWorkNo(wrkNo);
        }
        if (wrkMast == null) {
            throw new CoolException("任务不存在");
        }
        boolean updated = wrkMastService.update(null, new UpdateWrapper<WrkMast>()
                .eq("wrk_no", wrkMast.getWrkNo())
                .set("mk", "taskForceCancel")
                .set("modi_time", new Date()));
        if (!updated) {
            throw new CoolException("任务强制取消失败");
        }
        return true;
    }
@@ -656,12 +693,18 @@
            stationList = basDualCrnp.getInStationList$();
        }
        Integer cachedTargetStationId = resolveCachedInStationId(findCrnNoResult, sourceStationId, stationList);
        if (cachedTargetStationId != null) {
            return cachedTargetStationId;
        }
        Integer targetStationId = null;
        for (StationObjModel stationObjModel : stationList) {
            try {
                List<NavigateNode> navigateNodes = navigateUtils.calcReachablePathByStationId(sourceStationId, stationObjModel.getStationId());
                if(!navigateNodes.isEmpty()) {
                    targetStationId = stationObjModel.getStationId();
                    cacheInStationId(findCrnNoResult, sourceStationId, targetStationId);
                    break;
                }
            } catch (Exception e) {
@@ -710,6 +753,58 @@
    }
    /**
     * 入库路径搜索同样代价较高,只缓存已确认可达的目标站点结果,并通过 TTL 限制陈旧风险。
     */
    private Integer resolveCachedInStationId(FindCrnNoResult findCrnNoResult,
                                             Integer sourceStationId,
                                             List<StationObjModel> stationList) {
        if (findCrnNoResult == null || findCrnNoResult.getCrnType() == null
                || findCrnNoResult.getCrnNo() == null || sourceStationId == null
                || stationList == null || stationList.isEmpty()) {
            return null;
        }
        Object cacheValue = redisUtil.get(buildInStationRouteCacheKey(findCrnNoResult, sourceStationId));
        if (cacheValue == null) {
            return null;
        }
        Integer cachedStationId = parseInteger(cacheValue);
        if (cachedStationId == null) {
            return null;
        }
        for (StationObjModel stationObjModel : stationList) {
            if (stationObjModel != null && cachedStationId.equals(stationObjModel.getStationId())) {
                return cachedStationId;
            }
        }
        return null;
    }
    private void cacheInStationId(FindCrnNoResult findCrnNoResult,
                                  Integer sourceStationId,
                                  Integer targetStationId) {
        if (findCrnNoResult == null || findCrnNoResult.getCrnType() == null
                || findCrnNoResult.getCrnNo() == null || sourceStationId == null
                || targetStationId == null) {
            return;
        }
        redisUtil.set(buildInStationRouteCacheKey(findCrnNoResult, sourceStationId),
                targetStationId,
                OUT_STATION_ROUTE_CACHE_SECONDS);
    }
    private String buildInStationRouteCacheKey(FindCrnNoResult findCrnNoResult, Integer sourceStationId) {
        return RedisKeyType.IN_STATION_ROUTE_CACHE.key
                + findCrnNoResult.getCrnType().name()
                + "_"
                + findCrnNoResult.getCrnNo()
                + "_"
                + sourceStationId;
    }
    /**
     * 出库路径搜索代价较高,只缓存已确认可达的站点结果,并通过 TTL 控制陈旧风险。
     */
    private Integer resolveCachedOutStationId(FindCrnNoResult findCrnNoResult,