| | |
| | | int type = param.getType(); |
| | | String sourceSite = param.getSourceSite(); |
| | | String barcode = param.getBarcode(); |
| | | |
| | | // 检查托盘码和暂存位编码是否相同 |
| | | if (barcode != null && sourceSite != null && barcode.trim().equals(sourceSite.trim())) { |
| | | throw new CoolException("托盘码和暂存位编码不能相同"); |
| | | } |
| | | |
| | | int ioType; |
| | | // 查询源站点(库位)信息,但不检查是否存在,允许下单成功 |
| | | // 站点不存在的检查将在定时任务(AgvHandler.callAgv)中进行 |
| | |
| | | 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和库位编号前缀选择站点和机器人组 |
| | | // 如果库位不存在,使用默认逻辑(根据type判断),站点不存在的检查将在定时任务中进行 |
| | | // 根据whs_type确定机器人组(站点分配完全由定时任务处理) |
| | | // 如果库位不存在,使用默认逻辑(根据type判断) |
| | | Long whsType = locCache != null ? locCache.getWhsType() : null; |
| | | String locNo = locCache != null ? locCache.getLocNo() : sourceSite; |
| | | List<String> targetStations; |
| | | String robotGroup; |
| | | |
| | | // 判断库位编号前缀:CA开头只做入库,WA开头才会被出库分配缓存区(从配置读取) |
| | | String inboundOnlyPrefix = agvProperties.getLocationPrefix().getInboundOnly(); |
| | | String cacheAreaPrefix = agvProperties.getLocationPrefix().getCacheArea(); |
| | | boolean isCA = locNo != null && locNo.startsWith(inboundOnlyPrefix); |
| | | boolean isWA = locNo != null && locNo.startsWith(cacheAreaPrefix); |
| | | |
| | | 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={},库位编号={}({}),使用入库区配置({}站点和{})", |
| | | whsType, locNo, isCA ? "CA" : (isWA ? "WA" : "其他"), |
| | | agvProperties.getEastDisplayName(), robotGroup); |
| | | log.info("库位whs_type={},使用入库区配置({})", whsType, robotGroup); |
| | | } else if (whsType != null && whsType.equals(agvProperties.getWhsTypeMapping().getCacheArea())) { |
| | | // whs_type = 2: 缓存区,使用西侧站点和Group-002 |
| | | // 注意:如果有CA开头的入库,但是标记在西侧的(whs_type=2),也会分配到西侧的站点入库 |
| | | targetStations = agvProperties.getWestStations(); |
| | | // whs_type = 2: 缓存区,使用Group-002 |
| | | robotGroup = agvProperties.getRobotGroupWest(); |
| | | log.info("库位whs_type={},库位编号={}({}),使用缓存区配置({}站点和{})", |
| | | whsType, locNo, isCA ? "CA" : (isWA ? "WA" : "其他"), |
| | | agvProperties.getWestDisplayName(), robotGroup); |
| | | 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(); |
| | | } |
| | | if (locCache == null) { |
| | | log.warn("源站点(库位){}不存在,使用type={}的默认逻辑,站点检查将在定时任务中进行", sourceSite, type); |
| | | log.warn("源站点(库位){}不存在,使用type={}的默认逻辑,机器人组:{},站点分配将在定时任务中进行", sourceSite, type, robotGroup); |
| | | } else { |
| | | log.warn("库位whs_type={}未配置或不在映射范围内,使用type={}的默认逻辑", whsType, type); |
| | | log.warn("库位whs_type={}未配置或不在映射范围内,使用type={}的默认逻辑,机器人组:{}", whsType, type, robotGroup); |
| | | } |
| | | } |
| | | |
| | | // 将站点字符串列表转换为整数列表 |
| | | List<Integer> siteIntList = targetStations.stream() |
| | | .map(Integer::parseInt) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 判断能入站点(in_enable="Y"表示能入),排除dev_no=0的无效站点 |
| | | // 注意:不在此处检查站点是否存在或可用,允许下单成功 |
| | | // 站点检查和分配将在定时任务(AgvHandler.callAgv)中进行 |
| | | List<Integer> sites = basDevpMapper.selectList( |
| | | new EntityWrapper<BasDevp>() |
| | | .eq("in_enable", "Y") // in_enable是能入 |
| | | .in("dev_no", siteIntList) |
| | | .ne("dev_no", 0) // 排除dev_no=0的无效站点 |
| | | ).stream() |
| | | .map(BasDevp::getDevNo) |
| | | .filter(devNo -> devNo != null && devNo != 0) // 再次过滤,确保不为null或0 |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 获取没有出库任务的站点 |
| | | List<Integer> canInSites = sites.isEmpty() ? new ArrayList<>() : basDevpMapper.getCanInSites(sites); |
| | | |
| | | // 寻找入库任务最少的站点(且必须in_enable="Y"能入 和 canining="Y"可入),排除dev_no=0的无效站点 |
| | | // 注意:不在此处检查未完成的AGV任务,允许PDA持续申请下单 |
| | | // 未完成任务的检查将在发送AGV请求时进行(AgvHandler.callAgv) |
| | | List<BasDevp> devList = canInSites.isEmpty() ? new ArrayList<>() : basDevpMapper.selectList(new EntityWrapper<BasDevp>() |
| | | .in("dev_no", canInSites) |
| | | .eq("in_enable", "Y") // in_enable是能入 |
| | | .eq("canining", "Y") // canining是可入 |
| | | .ne("dev_no", 0) // 排除dev_no=0的无效站点 |
| | | ).stream() |
| | | .filter(dev -> dev.getDevNo() != null && dev.getDevNo() != 0) // 再次过滤,确保不为null或0 |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 选择站点(如果可入站点为空,则不在创建任务时分配站点,由定时任务分配) |
| | | Integer endSite = null; |
| | | if (!devList.isEmpty()) { |
| | | // 入库任务数排序 |
| | | devList.sort(Comparator.comparing(BasDevp::getInQty)); |
| | | |
| | | // 选择站点 |
| | | 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(); |
| | | |
| | | // 检查站点是否有效(不能为0或null) |
| | | if (endSite == null || endSite == 0) { |
| | | log.error("分配的站点无效(dev_no={}),不分配站点,将在定时任务中分配", endSite); |
| | | endSite = null; // 设置为null,由定时任务分配 |
| | | } else { |
| | | // 入库暂存+1 |
| | | basDevpMapper.incrementInQty(endSite); |
| | | } |
| | | } else { |
| | | // 没有可入站点,记录日志但不阻止下单,站点分配将在定时任务中处理 |
| | | String groupName = (whsType != null && whsType.equals(agvProperties.getWhsTypeMapping().getInboundArea())) |
| | | ? agvProperties.getEastDisplayName() : agvProperties.getWestDisplayName(); |
| | | // log.warn("{}可用站点({})中没有可入站点(in_enable='Y'且canining='Y'),暂不分配站点,将在定时任务中分配", groupName, canInSites); |
| | | } |
| | | // 站点分配完全由定时任务处理,此处不分配站点,只创建任务 |
| | | log.info("创建AGV任务,站点分配将在定时任务中处理,机器人组:{}", robotGroup); |
| | | |
| | | |
| | | // 获取工作号 |
| | |
| | | .setIoType(ioType) // 入出库状态: 1.入库 |
| | | .setTaskType("agv") |
| | | .setIoPri(10D) |
| | | .setStaNo(endSite != null ? String.valueOf(endSite) : null) // 如果分配了站点则设置,否则为null,由定时任务分配 |
| | | .setStaNo(null) // 站点分配完全由定时任务处理 |
| | | .setSourceStaNo(sourceSite) // 设置源站点 |
| | | .setInvWh(robotGroup) // 根据whs_type设置机器人组 |
| | | .setFullPlt(ioType == 10 ? "N" : "Y")// 空托入库(ioType=10)设置为N,其他入库设置为Y(满板) |