zhou zhou
4 天以前 7a215e51f9f4066f2024d8476e5b5db2c06358a1
rsf-server/src/main/java/com/vincent/rsf/server/manager/utils/LocManageUtil.java
@@ -12,14 +12,20 @@
import com.vincent.rsf.server.manager.enums.TaskType;
import com.vincent.rsf.server.manager.enums.WaveRuleType;
import com.vincent.rsf.server.manager.service.*;
import com.vincent.rsf.server.manager.service.impl.WmsRedisLuaService;
import com.vincent.rsf.server.manager.enums.LocStsType;
import org.apache.commons.lang3.StringUtils;
import java.math.BigDecimal;
import java.time.Duration;
import java.util.*;
import java.util.stream.Collectors;
public class LocManageUtil {
    private static final int TARGET_LOC_QUERY_LIMIT = 10;
    private static final String TARGET_LOC_LOCK_KEY_PREFIX = "wms:loc:claim:";
    private static final Duration TARGET_LOC_LOCK_TTL = Duration.ofMinutes(1);
    /**
     * @param
@@ -39,7 +45,7 @@
        Long locType = containerType;
        //TODO 库位策略后续排期
        LocService locService = SpringUtils.getBean(LocService.class);
        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>()
        List<Loc> locList = locService.list(new LambdaQueryWrapper<Loc>()
                .eq(!Objects.isNull(locType), Loc::getType, locType)
                .eq(Loc::getAreaId, areaId)
                .eq(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_O.type)
@@ -47,45 +53,48 @@
                .orderByAsc(Loc::getLev)
                .orderByAsc(Loc::getCol)
                .orderByAsc(Loc::getRow)
                .last("LIMIT 1")
                .last("LIMIT " + TARGET_LOC_QUERY_LIMIT)
        );
        return !Objects.isNull(loc) ? loc.getCode() : null;
        return claimTargetLoc(locList);
    }
    public static String getTargetLoc(Long areaId, Long containerType) {
        Long locType = containerType;
//        if (!Objects.isNull(containerType)) {
//            LocTypeService locService = SpringUtils.getBean(LocTypeService.class);
//            if (containerType.equals(ContainerType.CONTAINER_TYPE_NORMAL.val)) {
//                LocType low = locService.getOne(new LambdaQueryWrapper<LocType>()
//                        .eq(LocType::getCode, "L"));
//                if (Objects.isNull(low)) {
//                    throw new CoolException("庫位類型不存在!!");
//                }
//                locType = low.getId();
//            } else {
//                LocType low = locService.getOne(new LambdaQueryWrapper<LocType>()
//                        .eq(LocType::getCode, "H"));
//                if (Objects.isNull(low)) {
//                    throw new CoolException("庫位類型不存在!!");
//                }
//                locType = low.getId();
//            }
//        }
        //TODO 库位策略后续排期
        LocService locService = SpringUtils.getBean(LocService.class);
        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>()
        List<Loc> locList = locService.list(new LambdaQueryWrapper<Loc>()
                .eq(!Objects.isNull(locType), Loc::getType, locType)
                .eq(Loc::getAreaId, areaId)
                .eq(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_O.type)
                .orderByAsc(Loc::getLev)
                .orderByAsc(Loc::getCol)
                .orderByAsc(Loc::getRow)
                .last("LIMIT 1")
                .last("LIMIT " + TARGET_LOC_QUERY_LIMIT)
        );
        return !Objects.isNull(loc) ? loc.getCode() : null;
        return claimTargetLoc(locList);
    }
    private static String claimTargetLoc(List<Loc> locList) {
        if (Cools.isEmpty(locList)) {
            return null;
        }
        WmsRedisLuaService wmsRedisLuaService = SpringUtils.getBean(WmsRedisLuaService.class);
        for (Loc loc : locList) {
            if (Objects.isNull(loc) || Cools.isEmpty(loc.getCode())) {
                continue;
            }
            boolean claimed = wmsRedisLuaService.claimLocation(buildTargetLocLockKey(loc.getCode()), loc.getCode(), TARGET_LOC_LOCK_TTL);
            if (claimed) {
                return loc.getCode();
            }
        }
        return null;
    }
    private static String buildTargetLocLockKey(String locCode) {
        return TARGET_LOC_LOCK_KEY_PREFIX + locCode;
    }
    /**