| | |
| | | import com.zy.common.model.DetlDto; |
| | | import com.zy.common.model.MesCombParam; |
| | | import com.zy.common.model.enums.WorkNoType; |
| | | import com.zy.common.properties.AgvProperties; |
| | | import com.zy.common.service.CommonService; |
| | | import com.zy.common.utils.HttpHandler; |
| | | import lombok.extern.slf4j.Slf4j; |
| | |
| | | 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; |
| | | |
| | | /** |
| | |
| | | @Resource |
| | | private BasDevpMapper basDevpMapper; |
| | | |
| | | @Resource |
| | | private AgvProperties agvProperties; |
| | | |
| | | /** |
| | | * 站点轮询计数器,用于平均分配站点 |
| | | * Key: 站点组标识(如 "east" 或 "west"),Value: 当前轮询索引 |
| | | */ |
| | | private final Map<String, AtomicInteger> siteRoundRobinCounters = new ConcurrentHashMap<>(); |
| | | |
| | | @Override |
| | | public R inLocCallAgv(CallAgvParam param,Long userId) { |
| | | 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)中进行 |
| | | LocCache locCache = locCacheService.selectOne(new EntityWrapper<LocCache>().eq("loc_no", sourceSite)); |
| | | switch (type) { |
| | | case 1: |
| | | // 判断有没有组托 |
| | |
| | | if (count == 0) { |
| | | throw new CoolException("条码未组托:" + barcode); |
| | | } |
| | | ioType = 1; |
| | | ioType = 1; // AGV容器入库(实托入库) |
| | | |
| | | // 如果库位存在,更新状态为入库预约;不存在则跳过,由定时任务处理 |
| | | if (locCache != null) { |
| | | locCache.setLocSts(LocStsType.LOC_STS_TYPE_S.type); // S.入库预约 |
| | | locCacheService.updateById(locCache); |
| | | } |
| | | break; |
| | | case 2: |
| | | // 判断是拣选回库托盘 |
| | |
| | | if (wrkMast.getIoType() != 103 && wrkMast.getIoType() != 107) { |
| | | throw new CoolException("条码不需要回库:" + barcode); |
| | | } |
| | | ioType = wrkMast.getIoType() - 50; |
| | | ioType = wrkMast.getIoType() - 50; // 103->53(拣料入库), 107->57(盘点入库) |
| | | |
| | | // 如果库位存在,更新状态为入库预约;不存在则跳过,由定时任务处理 |
| | | if (locCache != null) { |
| | | locCache.setLocSts(LocStsType.LOC_STS_TYPE_S.type); // S.入库预约(容器回库是入库操作) |
| | | locCacheService.updateById(locCache); |
| | | } |
| | | break; |
| | | case 3: |
| | | // 判断条码在wms不存在,是空托盘 |
| | | // 已组托 |
| | | // 判断是否为空托入库:检查条码在wms中不存在,确认为空托盘 |
| | | log.info("开始判断是否为空托入库,条码:{}", barcode); |
| | | |
| | | // 检查是否已组托 |
| | | int waitPakInCount = waitPakinService.selectCount(new EntityWrapper<WaitPakin>().eq("zpallet", barcode)); |
| | | if (waitPakInCount != 0) { |
| | | log.warn("条码组托档已存在,不是空托盘:{}", barcode); |
| | | throw new CoolException("条码组托档已存在:" + barcode); |
| | | } |
| | | // 有任务 |
| | | |
| | | // 检查是否有任务 |
| | | int wrkMastCount = wrkMastService.selectCount(new EntityWrapper<WrkMast>().eq("barcode", barcode)); |
| | | if (wrkMastCount != 0) { |
| | | log.warn("条码任务档已存在,不是空托盘:{}", barcode); |
| | | throw new CoolException("条码任务档已存在:" + barcode); |
| | | } |
| | | // 有库存 |
| | | |
| | | // 检查是否有库存 |
| | | int locDetlCount = locDetlService.selectCount(new EntityWrapper<LocDetl>().eq("zpallet", barcode)); |
| | | if (locDetlCount != 0) { |
| | | log.warn("条码库存已存在,不是空托盘:{}", barcode); |
| | | throw new CoolException("条码库存已存在:" + barcode); |
| | | } |
| | | |
| | | // 通过所有检查,确认为空托盘,设置为空托入库 |
| | | ioType = 10; |
| | | log.info("确认为空托盘,设置为空托入库,条码:{},ioType:{}", barcode, ioType); |
| | | break; |
| | | default: |
| | | 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搬运任务!"); |
| | | } |
| | | |
| | | // 判断能入站点 |
| | | List<Integer> sites = basDevpMapper.selectList( |
| | | new EntityWrapper<BasDevp>() |
| | | .eq("canining", "Y") |
| | | .in("dev_no", type == 1 ? Arrays.asList(1003, 1005, 1007) : Arrays.asList(1044, 1046, 1048)) |
| | | ).stream().map(BasDevp::getDevNo).collect(Collectors.toList()); |
| | | |
| | | if (sites.isEmpty()) { |
| | | throw new CoolException("没有能入站点,type:" + type); |
| | | // 根据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 |
| | | robotGroup = agvProperties.getRobotGroupEast(); |
| | | log.info("库位whs_type={},使用入库区配置({})", whsType, robotGroup); |
| | | } else if (whsType != null && whsType.equals(agvProperties.getWhsTypeMapping().getCacheArea())) { |
| | | // whs_type = 2: 缓存区,使用Group-002 |
| | | robotGroup = agvProperties.getRobotGroupWest(); |
| | | log.info("库位whs_type={},使用缓存区配置({})", whsType, robotGroup); |
| | | } else { |
| | | // whs_type为空或其他值,根据type判断(兼容旧逻辑) |
| | | // 如果库位不存在,也使用此逻辑 |
| | | if (type == 1) { |
| | | robotGroup = agvProperties.getRobotGroupEast(); |
| | | } else { |
| | | robotGroup = agvProperties.getRobotGroupWest(); |
| | | } |
| | | if (locCache == null) { |
| | | log.warn("源站点(库位){}不存在,使用type={}的默认逻辑,机器人组:{},站点分配将在定时任务中进行", sourceSite, type, robotGroup); |
| | | } else { |
| | | log.warn("库位whs_type={}未配置或不在映射范围内,使用type={}的默认逻辑,机器人组:{}", whsType, type, robotGroup); |
| | | } |
| | | } |
| | | |
| | | // 获取没有出库任务的站点 |
| | | List<Integer> canInSites = basDevpMapper.getCanInSites(sites); |
| | | if (canInSites.isEmpty()) { |
| | | throw new CoolException("请等待出库完成,type:" + type); |
| | | } |
| | | |
| | | // 寻找入库任务最少的站点 |
| | | List<BasDevp> devList = basDevpMapper.selectList(new EntityWrapper<BasDevp>().in("dev_no", canInSites)); |
| | | // 入库任务数排序 |
| | | devList.sort(Comparator.comparing(BasDevp::getInQty)); |
| | | |
| | | // 取入库任务最少站点 |
| | | BasDevp basDevp = devList.get(0); |
| | | int endSite = basDevp.getDevNo(); |
| | | |
| | | // 入库暂存+1 |
| | | basDevpMapper.incrementInQty(endSite); |
| | | |
| | | // 站点分配完全由定时任务处理,此处不分配站点,只创建任务 |
| | | log.info("创建AGV任务,站点分配将在定时任务中处理,机器人组:{}", robotGroup); |
| | | |
| | | |
| | | // 获取工作号 |
| | |
| | | .setIoType(ioType) // 入出库状态: 1.入库 |
| | | .setTaskType("agv") |
| | | .setIoPri(10D) |
| | | .setStaNo(String.valueOf(endSite)) |
| | | .setFullPlt(ioType != 10 ? "N" : "Y")// 满板:Y |
| | | .setStaNo(null) // 站点分配完全由定时任务处理 |
| | | .setSourceStaNo(sourceSite) // 设置源站点 |
| | | .setInvWh(robotGroup) // 根据whs_type设置机器人组 |
| | | .setFullPlt(ioType == 10 ? "N" : "Y")// 空托入库(ioType=10)设置为N,其他入库设置为Y(满板) |
| | | .setPicking("N") // 拣料 |
| | | .setExitMk("N")// 退出 |
| | | .setSourceLocNo(sourceSite) |
| | | .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) |
| | |
| | | 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任务生成成功!"); |
| | | |
| | | } |