自动化立体仓库 - WMS系统
chen.llin
3 天以前 73e42333948a8143c54218cd26435c2233daf279
src/main/java/com/zy/asrs/service/impl/MobileServiceImpl.java
@@ -9,18 +9,25 @@
import com.zy.asrs.entity.*;
import com.zy.asrs.entity.param.*;
import com.zy.asrs.enums.LocStsType;
import com.zy.asrs.enums.TaskIOType;
import com.zy.asrs.mapper.BasDevpMapper;
import com.zy.asrs.mapper.BasStationMapper;
import com.zy.asrs.mapper.LocMastMapper;
import com.zy.asrs.mapper.ManLocDetlMapper;
import com.zy.asrs.service.*;
import com.zy.asrs.task.core.ReturnT;
import com.zy.asrs.utils.MatUtils;
import com.zy.common.constant.ApiInterfaceConstant;
import com.zy.common.constant.MesConstant;
import com.zy.common.entity.Parameter;
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 org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -30,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;
/**
@@ -109,6 +118,241 @@
    private BasStationService basStationService;
    @Autowired
    private BasContainerService basContainerService;
    @Resource
    private BasStationMapper basStationMapper;
    @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();
        int ioType;
        LocCache locCache = locCacheService.selectOne(new EntityWrapper<LocCache>().eq("loc_no", sourceSite));
        if (null == locCache) {
            throw new CoolException("站点不存在:" + sourceSite);
        }
        switch (type) {
            case 1:
                // 判断有没有组托
                int count = waitPakinService.selectCount(new EntityWrapper<WaitPakin>().eq("zpallet", barcode));
                if (count == 0) {
                    throw new CoolException("条码未组托:" + barcode);
                }
                ioType = 101;
                locCache.setLocSts(LocStsType.LOC_STS_TYPE_R.type);
                locCacheService.updateById(locCache);
                break;
            case 2:
                // 判断是拣选回库托盘
                WrkMast wrkMast = wrkMastService.selectOne(new EntityWrapper<WrkMast>().eq("barcode", barcode));
                if (wrkMast == null) {
                    throw new CoolException("条码不存在:" + barcode);
                }
                if (wrkMast.getIoType() != 103 && wrkMast.getIoType() != 107) {
                    throw new CoolException("条码不需要回库:" + barcode);
                }
                ioType = wrkMast.getIoType() - 50;
                locCache.setLocSts(LocStsType.LOC_STS_TYPE_R.type);
                locCacheService.updateById(locCache);
                break;
            case 3:
                // 判断是否为空托入库:检查条码在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));
        if (count > 0) {
            throw new CoolException(barcode+ ":条码存在agv搬运任务!");
        }
        // 根据whs_type选择站点和机器人组
        Long whsType = locCache.getWhsType();
        List<String> targetStations;
        String robotGroup;
        if (whsType != null && whsType.equals(agvProperties.getWhsTypeMapping().getInboundArea())) {
            // whs_type = 1: 入库区,使用东侧站点和Group-001
            targetStations = agvProperties.getEastStations();
            robotGroup = agvProperties.getRobotGroupEast();
            log.info("库位whs_type={},使用入库区配置(东侧站点和Group-001)", whsType);
        } else if (whsType != null && whsType.equals(agvProperties.getWhsTypeMapping().getCacheArea())) {
            // whs_type = 2: 缓存区,使用西侧站点和Group-002
            targetStations = agvProperties.getWestStations();
            robotGroup = agvProperties.getRobotGroupWest();
            log.info("库位whs_type={},使用缓存区配置(西侧站点和Group-002)", whsType);
        } 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);
        }
        // 寻找入库任务最少的站点(且必须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;
            // 获取最少任务数
            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);
        } else {
            // 没有可入站点,记录日志但不阻止下单,站点分配将在定时任务中处理
            String groupName = (whsType != null && whsType.equals(agvProperties.getWhsTypeMapping().getInboundArea()))
                    ? "东侧" : "西侧";
            log.warn("{}可用站点({})中没有可入站点(in_enable='Y'且canining='Y'),暂不分配站点,将在定时任务中分配", groupName, canInSites);
        }
        // 获取工作号
        int workNo = commonService.getWorkNo(WorkNoType.PICK.type);
        // 保存工作档
        Task task = new Task();
        Date now = new Date();
        task.setWrkNo(workNo)
                .setIoTime(now)
                .setWrkSts(7L) // 工作状态:11.生成出库ID
                .setIoType(ioType) // 入出库状态: 1.入库
                .setTaskType("agv")
                .setIoPri(10D)
                .setStaNo(endSite != null ? String.valueOf(endSite) : null) // 如果分配了站点则设置,否则为null,由定时任务分配
                .setSourceStaNo(sourceSite) // 设置源站点
                .setInvWh(robotGroup) // 根据whs_type设置机器人组
                .setFullPlt(ioType != 10 ? "N" : "Y")// 满板:Y
                .setPicking("N") // 拣料
                .setExitMk("N")// 退出
                .setSourceLocNo(locCache.getLocNo()) // 设置源库位编号,用于AGV fromBin
                .setEmptyMk(ioType == 10 ? "Y" : "N")// 空板
                .setBarcode(barcode)// 托盘码
                .setLinkMis("N")
                .setAppeTime(now)
                .setModiTime(now);
        if (!taskService.insert(task)) {
            throw new CoolException("保存工作档失败");
        }
        // 更新暂存位状态为 R.出库预约
        basStationMapper.updateLocStsBatch( Collections.singletonList(String.valueOf(sourceSite)), "R");
        return R.ok("agv任务生成成功!");
    }
    @Override
    @Transactional
@@ -601,9 +845,9 @@
                if (Cools.isEmpty(mat)) {
                    throw new CoolException(detlDto.getMatnr() + "商品档案不存在");
                }
                if (mat.getUpQty().compareTo(detlDto.getAnfme()) < 0) {
                    throw new CoolException("物料:" + detlDto.getMatnr() + "单次最大组托上限为:" + mat.getUpQty());
                }
//                if (mat.getUpQty().compareTo(detlDto.getAnfme()) < 0) {
//                    throw new CoolException("物料:" + detlDto.getMatnr() + "单次最大组托上限为:" + mat.getUpQty());
//                }
                WaitPakin waitPakin = new WaitPakin();
                BeanUtils.copyProperties(mat, waitPakin);
                waitPakin.setBatch(detlDto.getBatch());