自动化立体仓库 - WMS系统
chen.llin
5 天以前 46168fbb7c925b0ec04def176095e967720e684a
src/main/java/com/zy/asrs/service/impl/MobileServiceImpl.java
@@ -37,6 +37,8 @@
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
/**
@@ -125,6 +127,12 @@
    @Resource
    private AgvProperties agvProperties;
    /**
     * 站点轮询计数器,用于平均分配站点
     * Key: 站点组标识(如 "east" 或 "west"),Value: 当前轮询索引
     */
    private final Map<String, AtomicInteger> siteRoundRobinCounters = new ConcurrentHashMap<>();
    @Override
    public R inLocCallAgv(CallAgvParam param,Long userId) {
@@ -232,10 +240,10 @@
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        
        // 判断能入站点
        // 判断能入站点(in_enable="Y"表示能入)
        List<Integer> sites = basDevpMapper.selectList(
                new EntityWrapper<BasDevp>()
                        .eq("canining", "Y")
                        .eq("in_enable", "Y") // in_enable是能入
                        .in("dev_no", siteIntList)
        ).stream().map(BasDevp::getDevNo).collect(Collectors.toList());
@@ -249,17 +257,68 @@
            throw new CoolException("请等待出库完成,type:" + type);
        }
        // 寻找入库任务最少的站点
        List<BasDevp> devList = basDevpMapper.selectList(new EntityWrapper<BasDevp>().in("dev_no", canInSites));
        // 入库任务数排序
        devList.sort(Comparator.comparing(BasDevp::getInQty));
        // 寻找入库任务最少的站点(且必须in_enable="Y"能入 和 canining="Y"可入)
        // 注意:不在此处检查未完成的AGV任务,允许PDA持续申请下单
        // 未完成任务的检查将在发送AGV请求时进行(AgvHandler.callAgv)
        List<BasDevp> devList = basDevpMapper.selectList(new EntityWrapper<BasDevp>()
                .in("dev_no", canInSites)
                .eq("in_enable", "Y") // in_enable是能入
                .eq("canining", "Y") // canining是可入
        );
        // 选择站点(如果可入站点为空,则不在创建任务时分配站点,由定时任务分配)
        Integer endSite = null;
        if (!devList.isEmpty()) {
            // 入库任务数排序
            devList.sort(Comparator.comparing(BasDevp::getInQty));
        // 取入库任务最少站点
        BasDevp basDevp = devList.get(0);
        int endSite = basDevp.getDevNo();
            // 选择站点
            BasDevp basDevp;
            // 获取最少任务数
            int minInQty = devList.get(0).getInQty();
            // 筛选出任务数最少的站点列表
            List<BasDevp> minTaskSites = devList.stream()
                    .filter(dev -> dev.getInQty() == minInQty)
                    .collect(Collectors.toList());
            // 根据配置选择分配策略
            String strategy = agvProperties.getSiteAllocation().getStrategy();
            boolean enableRoundRobin = agvProperties.getSiteAllocation().isEnableRoundRobin();
            if (minTaskSites.size() > 1 && enableRoundRobin && "round-robin".equals(strategy)) {
                // 轮询分配:当多个站点任务数相同时,使用轮询
                String groupKey = (whsType != null && whsType.equals(agvProperties.getWhsTypeMapping().getInboundArea()))
                        ? "east" : "west";
                AtomicInteger counter = siteRoundRobinCounters.computeIfAbsent(groupKey, k -> new AtomicInteger(0));
                int index = counter.getAndIncrement() % minTaskSites.size();
                basDevp = minTaskSites.get(index);
                log.info("使用轮询分配策略,站点组:{},轮询索引:{},选中站点:{}", groupKey, index, basDevp.getDevNo());
            } else if (minTaskSites.size() > 1 && enableRoundRobin && "random".equals(strategy)) {
                // 随机分配
                Random random = new Random();
                int index = random.nextInt(minTaskSites.size());
                basDevp = minTaskSites.get(index);
                log.info("使用随机分配策略,选中站点:{}", basDevp.getDevNo());
            } else {
                // 默认:选择第一个(任务最少的)
                basDevp = devList.get(0);
                if (minTaskSites.size() > 1) {
                    log.info("多个站点任务数相同({}),但未启用轮询,选择第一个站点:{}", minInQty, basDevp.getDevNo());
                }
            }
            endSite = basDevp.getDevNo();
        // 入库暂存+1
        basDevpMapper.incrementInQty(endSite);
            // 入库暂存+1
            basDevpMapper.incrementInQty(endSite);
        } else {
            // 没有可入站点,记录日志但不阻止下单,站点分配将在定时任务中处理
            String groupName = (whsType != null && whsType.equals(agvProperties.getWhsTypeMapping().getInboundArea()))
                    ? "东侧" : "西侧";
            log.warn("{}可用站点({})中没有可入站点(in_enable='Y'且canining='Y'),暂不分配站点,将在定时任务中分配", groupName, canInSites);
        }
        // 获取工作号
@@ -273,7 +332,7 @@
                .setIoType(ioType) // 入出库状态: 1.入库
                .setTaskType("agv")
                .setIoPri(10D)
                .setStaNo(String.valueOf(endSite))
                .setStaNo(endSite != null ? String.valueOf(endSite) : null) // 如果分配了站点则设置,否则为null,由定时任务分配
                .setSourceStaNo(sourceSite) // 设置源站点
                .setInvWh(robotGroup) // 根据whs_type设置机器人组
                .setFullPlt(ioType != 10 ? "N" : "Y")// 满板:Y