自动化立体仓库 - WMS系统
chen.llin
4 天以前 cea1758e1f540e3f5f807951f128b7385b32afe6
src/main/java/com/zy/asrs/service/impl/MobileServiceImpl.java
@@ -139,11 +139,16 @@
        int type = param.getType();
        String sourceSite = param.getSourceSite();
        String barcode = param.getBarcode();
        int ioType;
        LocCache locCache = locCacheService.selectOne(new EntityWrapper<LocCache>().eq("loc_no", sourceSite));
        if (null == locCache) {
            throw new CoolException("站点不存在:" + sourceSite);
        // 检查托盘码和暂存位编码是否相同
        if (barcode != null && sourceSite != null && barcode.trim().equals(sourceSite.trim())) {
            throw new CoolException("托盘码和暂存位编码不能相同");
        }
        int ioType;
        // 查询源站点(库位)信息,但不检查是否存在,允许下单成功
        // 站点不存在的检查将在定时任务(AgvHandler.callAgv)中进行
        LocCache locCache = locCacheService.selectOne(new EntityWrapper<LocCache>().eq("loc_no", sourceSite));
        switch (type) {
            case 1:
                // 判断有没有组托
@@ -151,10 +156,13 @@
                if (count == 0) {
                    throw new CoolException("条码未组托:" + barcode);
                }
                ioType = 101;
                ioType = 1; // AGV容器入库(实托入库)
                locCache.setLocSts(LocStsType.LOC_STS_TYPE_R.type);
                locCacheService.updateById(locCache);
                // 如果库位存在,更新状态为入库预约;不存在则跳过,由定时任务处理
                if (locCache != null) {
                    locCache.setLocSts(LocStsType.LOC_STS_TYPE_S.type); // S.入库预约
                    locCacheService.updateById(locCache);
                }
                break;
            case 2:
                // 判断是拣选回库托盘
@@ -165,10 +173,13 @@
                if (wrkMast.getIoType() != 103 && wrkMast.getIoType() != 107) {
                    throw new CoolException("条码不需要回库:" + barcode);
                }
                ioType = wrkMast.getIoType() - 50;
                ioType = wrkMast.getIoType() - 50; // 103->53(拣料入库), 107->57(盘点入库)
                locCache.setLocSts(LocStsType.LOC_STS_TYPE_R.type);
                locCacheService.updateById(locCache);
                // 如果库位存在,更新状态为入库预约;不存在则跳过,由定时任务处理
                if (locCache != null) {
                    locCache.setLocSts(LocStsType.LOC_STS_TYPE_S.type); // S.入库预约(容器回库是入库操作)
                    locCacheService.updateById(locCache);
                }
                break;
            case 3:
                // 判断是否为空托入库:检查条码在wms中不存在,确认为空托盘
@@ -203,160 +214,41 @@
                throw new CoolException("入库类型错误,type:" + type);
        }
        // 条码存在agv任务
        int count = taskService.selectCount(new EntityWrapper<Task>().eq("barcode", barcode));
        int count = taskService.selectCount(new EntityWrapper<Task>().eq("barcode", barcode).eq("is_deleted", 0));
        if (count > 0) {
            throw new CoolException(barcode+ ":条码存在agv搬运任务!");
        }
        // 根据whs_type选择站点和机器人组
        Long whsType = locCache.getWhsType();
        List<String> targetStations;
        // 根据whs_type确定机器人组(站点分配完全由定时任务处理)
        // 如果库位不存在,使用默认逻辑(根据type判断)
        Long whsType = locCache != null ? locCache.getWhsType() : null;
        String robotGroup;
        
        if (whsType != null && whsType.equals(agvProperties.getWhsTypeMapping().getInboundArea())) {
            // whs_type = 1: 入库区,使用东侧站点和Group-001
            targetStations = agvProperties.getEastStations();
            // whs_type = 1: 入库区,使用Group-001
            robotGroup = agvProperties.getRobotGroupEast();
            log.info("库位whs_type={},使用入库区配置(东侧站点和Group-001)", whsType);
            log.info("库位whs_type={},使用入库区配置({})", whsType, robotGroup);
        } else if (whsType != null && whsType.equals(agvProperties.getWhsTypeMapping().getCacheArea())) {
            // whs_type = 2: 缓存区,使用西侧站点和Group-002
            targetStations = agvProperties.getWestStations();
            // whs_type = 2: 缓存区,使用Group-002
            robotGroup = agvProperties.getRobotGroupWest();
            log.info("库位whs_type={},使用缓存区配置(西侧站点和Group-002)", whsType);
            log.info("库位whs_type={},使用缓存区配置({})", whsType, robotGroup);
        } else {
            // whs_type为空或其他值,根据type判断(兼容旧逻辑)
            // 如果库位不存在,也使用此逻辑
            if (type == 1) {
                targetStations = agvProperties.getEastStations();
                robotGroup = agvProperties.getRobotGroupEast();
            } else {
                targetStations = agvProperties.getWestStations();
                robotGroup = agvProperties.getRobotGroupWest();
            }
            log.warn("库位whs_type={}未配置或不在映射范围内,使用type={}的默认逻辑", whsType, type);
        }
        // 将站点字符串列表转换为整数列表
        List<Integer> siteIntList = targetStations.stream()
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        // 判断能入站点(in_enable="Y"表示能入)
        List<Integer> sites = basDevpMapper.selectList(
                new EntityWrapper<BasDevp>()
                        .eq("in_enable", "Y") // in_enable是能入
                        .in("dev_no", siteIntList)
        ).stream().map(BasDevp::getDevNo).collect(Collectors.toList());
        if (sites.isEmpty()) {
            throw new CoolException("没有能入站点,whs_type:" + whsType + ",type:" + type);
        }
        // 获取没有出库任务的站点
        List<Integer> canInSites = basDevpMapper.getCanInSites(sites);
        if (canInSites.isEmpty()) {
            throw new CoolException("请等待出库完成,type:" + type);
        }
        // 检查站点是否有未完成的AGV任务
        // 规则:当某个站点有未完成的AGV任务时,不分配该站点;只从没有未完成任务的站点中选择
        // 将站点列表转换为字符串列表(Task表的sta_no是String类型)
        List<String> siteStrList = canInSites.stream()
                .map(String::valueOf)
                .collect(Collectors.toList());
        // 查询这些站点中有未完成AGV任务的站点(wrk_sts不在5和15之间表示未完成)
        List<Task> unfinishedTasks = taskService.selectList(new EntityWrapper<Task>()
                .in("sta_no", siteStrList)
                .eq("task_type", "agv") // 只查询AGV任务
                .last("AND wrk_sts NOT IN (5, 15)") // 排除已完成状态(5和15表示已完成)
        );
        // 获取有未完成任务的站点集合(这些站点将被排除,不参与分配)
        Set<String> sitesWithUnfinishedTasks = unfinishedTasks.stream()
                .map(Task::getStaNo)
                .collect(Collectors.toSet());
        // 从可用站点中排除有未完成任务的站点,只保留没有未完成任务的站点
        List<Integer> availableSites = canInSites.stream()
                .filter(site -> !sitesWithUnfinishedTasks.contains(String.valueOf(site)))
                .collect(Collectors.toList());
        // 如果所有站点都有未完成任务,则没有可用站点,不分配
        if (availableSites.isEmpty()) {
            String groupName = (whsType != null && whsType.equals(agvProperties.getWhsTypeMapping().getInboundArea()))
                    ? "东侧" : "西侧";
            log.warn("{}所有站点({})都有未完成的AGV任务,无法分配站点,请等待任务完成", groupName, canInSites);
            throw new CoolException(groupName + "所有站点都有未完成的AGV任务,请等待任务完成后再试");
        }
        // 记录站点分配信息
        if (!sitesWithUnfinishedTasks.isEmpty()) {
            log.info("站点分配检查:总站点数={},有未完成任务的站点={}(已排除),可用站点数={},可用站点={}",
                    canInSites.size(), sitesWithUnfinishedTasks, availableSites.size(), availableSites);
        } else {
            log.info("站点分配检查:所有站点({})都没有未完成任务,全部可用", canInSites);
        }
        // 寻找入库任务最少的站点(只从可用站点中选择,且必须in_enable="Y"能入 和 canining="Y"可入)
        List<BasDevp> devList = basDevpMapper.selectList(new EntityWrapper<BasDevp>()
                .in("dev_no", availableSites)
                .eq("in_enable", "Y") // in_enable是能入
                .eq("canining", "Y") // canining是可入
        );
        // 如果查询结果为空,说明没有可入的站点
        if (devList.isEmpty()) {
            String groupName = (whsType != null && whsType.equals(agvProperties.getWhsTypeMapping().getInboundArea()))
                    ? "东侧" : "西侧";
            log.warn("{}可用站点({})中没有可入站点(in_enable='Y'且canining='Y'),无法分配", groupName, availableSites);
            throw new CoolException(groupName + "可用站点中没有可入站点,请检查站点配置");
        }
        // 入库任务数排序
        devList.sort(Comparator.comparing(BasDevp::getInQty));
        // 选择站点
        BasDevp basDevp;
        int endSite;
        // 获取最少任务数
        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());
            if (locCache == null) {
                log.warn("源站点(库位){}不存在,使用type={}的默认逻辑,机器人组:{},站点分配将在定时任务中进行", sourceSite, type, robotGroup);
            } else {
                log.warn("库位whs_type={}未配置或不在映射范围内,使用type={}的默认逻辑,机器人组:{}", whsType, type, robotGroup);
            }
        }
        
        endSite = basDevp.getDevNo();
        // 入库暂存+1
        basDevpMapper.incrementInQty(endSite);
        // 站点分配完全由定时任务处理,此处不分配站点,只创建任务
        log.info("创建AGV任务,站点分配将在定时任务中处理,机器人组:{}", robotGroup);
        // 获取工作号
@@ -370,14 +262,14 @@
                .setIoType(ioType) // 入出库状态: 1.入库
                .setTaskType("agv")
                .setIoPri(10D)
                .setStaNo(String.valueOf(endSite))
                .setStaNo(null) // 站点分配完全由定时任务处理
                .setSourceStaNo(sourceSite) // 设置源站点
                .setInvWh(robotGroup) // 根据whs_type设置机器人组
                .setFullPlt(ioType != 10 ? "N" : "Y")// 满板:Y
                .setFullPlt(ioType == 10 ? "N" : "Y")// 空托入库(ioType=10)设置为N,其他入库设置为Y(满板)
                .setPicking("N") // 拣料
                .setExitMk("N")// 退出
                .setSourceLocNo(locCache.getLocNo()) // 设置源库位编号,用于AGV fromBin
                .setEmptyMk(ioType == 10 ? "Y" : "N")// 空板
                .setSourceLocNo(locCache != null ? locCache.getLocNo() : sourceSite) // 设置源库位编号,用于AGV fromBin,如果库位不存在则使用sourceSite
                .setEmptyMk(ioType == 10 ? "Y" : "N")// 空托入库(ioType=10)设置为Y,其他设置为N
                .setBarcode(barcode)// 托盘码
                .setLinkMis("N")
                .setAppeTime(now)
@@ -386,8 +278,11 @@
            throw new CoolException("保存工作档失败");
        }
        // 更新暂存位状态为 R.出库预约
        basStationMapper.updateLocStsBatch( Collections.singletonList(String.valueOf(sourceSite)), "R");
        // 如果库位存在,根据ioType更新暂存位状态:入库任务设置为S(入库预约),出库任务设置为R(出库预约)
        if (locCache != null) {
            String locSts = (ioType < 100) ? "S" : "R"; // 入库任务(ioType < 100)设置为S,出库任务设置为R
            basStationMapper.updateLocStsBatch( Collections.singletonList(String.valueOf(sourceSite)), locSts);
        }
        return R.ok("agv任务生成成功!");
    }