自动化立体仓库 - WMS系统
skyouc
18 小时以前 7b72d43503aa5df4c152b777777085d3cd5e2132
src/main/java/com/zy/asrs/service/impl/MobileServiceImpl.java
@@ -8,17 +8,25 @@
import com.core.exception.CoolException;
import com.zy.asrs.entity.*;
import com.zy.asrs.entity.param.*;
import com.zy.asrs.enums.CommonEnum;
import com.zy.asrs.entity.result.ForwardAGVTaskDTO;
import com.zy.asrs.entity.result.HIKApiDTO;
import com.zy.asrs.entity.result.HIKResultDTO;
import com.zy.asrs.enums.*;
import com.zy.asrs.enums.OrderSettle;
import com.zy.asrs.enums.LocAreaType;
import com.zy.asrs.enums.LocStsType;
import com.zy.asrs.mapper.LocMastMapper;
import com.zy.asrs.mapper.ManLocDetlMapper;
import com.zy.asrs.service.*;
import com.zy.asrs.utils.MatUtils;
import com.zy.asrs.utils.OrderInAndOutUtil;
import com.zy.common.constant.HIKApiConstant;
import com.zy.common.constant.MesConstant;
import com.zy.common.entity.Parameter;
import com.zy.common.model.DetlDto;
import com.zy.common.model.LocDetlDto;
import com.zy.common.model.MesCombParam;
import com.zy.common.model.enums.IoWorkType;
import com.zy.common.model.enums.WorkNoType;
import com.zy.common.service.CommonService;
import com.zy.common.utils.HttpHandler;
@@ -114,6 +122,10 @@
    private BasStationService basStationService;
    @Autowired
    private BasContainerService basContainerService;
    @Autowired
    private BasAreasService basAreasService;
    @Autowired
    private StationRelaService stationRelaService;
    @Override
    @Transactional
@@ -616,6 +628,12 @@
                waitPakin.setIoStatus("N");
                waitPakin.setAnfme(detlDto.getAnfme());
                waitPakin.setStatus("Y");
                waitPakin.setStandby1(detlDto.getStandby1());
                waitPakin.setStandby2(detlDto.getStandby2());
                waitPakin.setStandby3(detlDto.getStandby3());
                waitPakin.setBoxType1(detlDto.getBoxType1());
                waitPakin.setBoxType2(detlDto.getBoxType2());
                waitPakin.setBoxType3(detlDto.getBoxType3());
                waitPakin.setAppeUser(userId);
                waitPakin.setAppeTime(now);
                waitPakin.setModiUser(userId);
@@ -646,7 +664,7 @@
                }
                if (elem.getAnfme() > detls.getEnableQty()) {
                    throw new CoolException(detls.getMatnr() + "入库数量不合法");
                    throw new CoolException(detls.getMatnr() + "入库数量不合规则");
                }
                OrderInAndOutUtil.increaseWorkQty(Boolean.TRUE, order.getId(), elem.getMatnr(), elem.getBatch(), elem.getBrand(), elem.getStandby1(), elem.getStandby2(), elem.getStandby3(),
                        elem.getBoxType1(), elem.getBoxType2(), elem.getBoxType3(), elem.getAnfme());
@@ -956,7 +974,6 @@
                        log.error("", e);
                    }
                }
            }
            // 生成入库单据
@@ -1184,13 +1201,12 @@
                .eq("frozen", 0)
                .ne("full_plt", "Y")
                .eq("loc_sts", LocStsType.LOC_STS_TYPE_O.type).orderAsc(Arrays.asList("row1"))));
    }
    /**
     * @author Ryan
     * @date 2025/9/22
     * @description: 呼叫AGV搬运
     * @description: 呼叫空车
     * @version 1.0
     */
    @Override
@@ -1252,6 +1268,7 @@
     */
    @Override
    public R OutCallAgv(AgvCallParams params, Long userId) {
        /**获取入库最早的一条数据**/
        LocCache locCaches = locCacheService.selectOne(new EntityWrapper<LocCache>()
                .eq("loc_sts", LocStsType.LOC_STS_TYPE_F.type)
                .eq("frozen", 0)
@@ -1267,9 +1284,249 @@
            throw new CoolException("站点正在执行任务!!");
        }
        generateOutTask(station, locCaches, userId);
        /**生成缓存区出库任务*/
        generateCacheOutTask(station, locCaches, userId);
//        /**生成立库出库任务*/
        generateCRNOutTask(station, locCaches, userId);
        return R.ok();
    }
    @Override
    public R callEmptyCar(AgvCallParams params, Long userId) {
        BasAreas basAreas = basAreasService.selectOne(new EntityWrapper<BasAreas>().eq("name", params.getOrgSite()));
        List<LocCache> locCaches = locCacheService.selectList(new EntityWrapper<LocCache>()
                .eq("loc_sts", LocStsType.LOC_STS_TYPE_D.type)
                .eq("area_id", basAreas.getId())
        );
        if (locCaches.isEmpty()){
            throw new CoolException("暂无空板库位");
        }
        BasStation station = basStationService.selectOne(new EntityWrapper<BasStation>()
                .eq("loc_sts", LocStsType.LOC_STS_TYPE_O.type)
                .eq("dev_no", params.getTarSite()));
        if (Objects.isNull(station)) {
            throw new CoolException("站点正在执行任务!!");
        }
        generateCacheOutTask(station, locCaches.get(0), userId);
//        HIKApiDTO hikApiDTO =new HIKApiDTO()
//                .setOrg(locCaches.get(0).getLocNo())
//                .setOrgType("05")
//                .setTar(params.getTarSite())
//                .setTarType("05")
//                .setTaskType("GT5")
//                .setPriority("1")
//                .setCtnrType("2")
//                ;
//        HIKResultDTO hikResultDTO = sendAgvTask(hikApiDTO, HIKApiConstant.AGV_CALL_IN_PATH);
//        if (!hikResultDTO.isSuccess()){
//            return R.error(hikResultDTO.getMessage());
//        }
        return R.ok();
    }
    @Override
//    @Transactional(rollbackFor = Exception.class)
    public R collectionInCall(AgvCallParams params, Long userId) {
        if (Objects.isNull(params.getOrgSite())) {
            throw new CoolException("源站点不能为空!!");
        }
        List<Task> tasks = taskService.selectList(new EntityWrapper<Task>().eq("barcode", params.getBarcode()));
        if (!tasks.isEmpty()){
            throw new CoolException("托盘码已生成任务");
        }
        BasAreas basAreas = basAreasService.selectOne(new EntityWrapper<BasAreas>().eq("name", params.getTarSite()));
        List<LocCache> locCaches = locCacheService.selectList(new EntityWrapper<LocCache>()
                .in("loc_sts", LocStsType.LOC_STS_TYPE_O.type, LocStsType.LOC_STS_TYPE_D.type)
                .eq("area_id", basAreas.getId())
        );
        if (locCaches.isEmpty()) {
            throw new CoolException("当前暂无空库位!!");
        }
        generateAgvTask("agv", locCaches.get(0), params.getOrgSite(), params.getBarcode(), userId);
        return R.ok();
    }
    /**
     * 生成立库出库任务
     * @author Ryan
     * @date 2025/12/3 8:06
     * @param locCaches
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void generateCRNOutTask(BasStation station, LocCache locCaches, Long userId) {
        if (Objects.isNull(locCaches)) {
            throw new CoolException("库位不能为空!!");
        }
        //获取缓存区信息
        BasAreas basAreas = basAreasService.selectOne(new EntityWrapper<BasAreas>().eq("whs_type_id", LocAreaType.LOC_AREA_TYPE_IN_CACHE.type));
        if (Objects.isNull(basAreas)) {
            throw new CoolException("库区不存在!!");
        }
        List<LocDetl> locDetls = locDetlService.selectList(new EntityWrapper<LocDetl>()
                .eq("area_id", basAreas.getId())
                .eq("loc_id", locCaches.getId()));
        if (Objects.isNull(locDetls)) {
            throw new CoolException("库存明细不存在!!");
        }
        //获取立库区信息
        BasAreas one = basAreasService.selectOne(new EntityWrapper<BasAreas>().eq("whs_type_id", LocAreaType.LOC_AREA_TYPE_CRN.type));
        if (Objects.isNull(one)) {
            throw new CoolException("数据错误:库区不存在!!");
        }
        List<StationRela> relas = stationRelaService.selectList(new EntityWrapper<StationRela>().eq("agv_sta", station.getDevNo()));
        if (Objects.isNull(relas) || relas.isEmpty()) {
            throw new CoolException("站点未关联堆垛机作业站点!!");
        }
        Set<String> crnStas = relas.stream().map(StationRela::getCrnSta).collect(Collectors.toSet());
        List<BasDevp> devps = basDevpService.selectList(new EntityWrapper<BasDevp>().in("dev_no", crnStas).eq("loading", CommonStation.COMMON_STATION_Y.type));
        if (Objects.isNull(devps) || devps.isEmpty()) {
            throw new CoolException("无站点可用!");
        }
        Collections.shuffle(devps);
        BasDevp basDevp = devps.stream().findFirst().get();
        List<LocMast> locMasts = new ArrayList<>();
        Map<String, List<LocDetl>> listMap = locDetls.stream().collect(Collectors.groupingBy(LocDetl::getMatnr));
        listMap.forEach((matnr, detls) -> {
            //根据supId(供应商)分类,得到出库总数
            Map<String, List<LocDetl>> supIds = detls.stream().collect(Collectors.groupingBy(LocDetl::getStandby1));
            supIds.forEach((supId, sups) -> {
                Double sum = sups.stream().mapToDouble(LocDetl::getAnfme).sum();
                //获取当前供应商+ 物料在库
                List<LocDetl> detlList = locDetlService.selectList(new EntityWrapper<LocDetl>()
                        .eq("matnr", matnr)
                        .eq("area_id", one.getId())
                        .eq("standby1", supId).orderAsc(Arrays.asList("appe_time")));
                //TODO  判断是否有新库位,没有新库位,再找有空格的位置放   1. 判断当前物料是否有库存 2. 没有余料查询新库位
                if (!Objects.isNull(detlList) && !detlList.isEmpty()) {
                    Map<String, List<LocDetl>> locMaps = detlList.stream().collect(Collectors.groupingBy(LocDetl::getLocNo));
                    locMaps.forEach((locNo, adetls) -> {
                        LocMast locMast = locMastService.selectById(locNo);
                        if (Objects.isNull(locMast)) {
                            throw new CoolException("数据错误,库位信息不存在!!");
                        }
                        BasContainer container = basContainerService.selectOne(new EntityWrapper<BasContainer>().eq("barcode", locMast.getBarcode()));
                        if (Objects.isNull(container)) {
                            throw new CoolException("数据错误,容器不存在!!");
                        }
                        Set<String> sets = adetls.stream().map(LocDetl::getMatnr).collect(Collectors.toSet());
                        //判断容器是否还可混放,及当前物料可放多少
                        if (container.getMixMax() > sets.size()) {
                            int suplus = container.getMixMax() - sets.size();
                            Mat mats = matService.selectOne(new EntityWrapper<Mat>().eq("matnr", matnr));
                            if (Objects.isNull(mats)) {
                                throw new CoolException("物料不存在!!");
                            }
                            Double v = mats.getUpQty() * suplus;
                            //小于零
                            if (sum.compareTo(v) <= 0) {
                                //可放下
                                locMasts.add(locMast);
                            }
                        }
                    });
                }
            });
        });
        if (!locMasts.isEmpty()) {
            //生成堆垛机出库任务
            generateOutTask(locMasts, TaskIOType.MERGE_OUT.type, basDevp, userId);
        }
    }
    /**
     * 生成堆垛机出库任务
     * @author Ryan
     * @date 2025/12/6 14:44
     * @param locMasts
     */
    @Transactional(rollbackFor = Exception.class)
    public void generateOutTask(List<LocMast> locMasts, Integer ioType, BasDevp devp, Long userId) {
        Date now = new Date();
        for (LocMast locMast : locMasts) {
            if (Objects.isNull(ioType)) {
                continue;
            }
            Integer outSta = devp.getDevNo();
            // 获取路径
            StaDesc staDesc = staDescService.queryCrnStn(ioType, locMast.getCrnNo(), outSta);
            // 生成工作号
            int workNo = commonService.getWorkNo(WorkNoType.getWorkNoType(ioType));
            // 生成工作档
            WrkMast wrkMast = new WrkMast();
            wrkMast.setWrkNo(workNo);
            wrkMast.setIoTime(now);
            wrkMast.setWrkSts(11L); // 工作状态:11.生成出库ID
            wrkMast.setIoType(ioType); // 入出库类型
            wrkMast.setIoPri(13D); // 优先级:13
            wrkMast.setCrnNo(locMast.getCrnNo());
            wrkMast.setSourceStaNo(staDesc.getCrnStn() + ""); // 源站
            wrkMast.setStaNo(staDesc.getStnNo() + ""); // 目标站
            wrkMast.setSourceLocNo(locMast.getLocNo()); // 源库位
            wrkMast.setFullPlt("Y"); // 满板:Y
            wrkMast.setPicking("N"); // 拣料
            wrkMast.setExitMk("N"); // 退出
            wrkMast.setEmptyMk("N"); // 空板
            wrkMast.setLinkMis("N");
            wrkMast.setBarcode(locMast.getBarcode());
            wrkMast.setAppeUser(userId); // 操作人员数据
            wrkMast.setAppeTime(now);
            wrkMast.setModiUser(userId);
            wrkMast.setModiTime(now);
            if (!wrkMastService.insert(wrkMast)) {
                throw new CoolException("保存工作档失败,出库库位号:" + locMast.getLocNo());
            }
            List<LocDetl> locDetls = locDetlService.selectList(new EntityWrapper<LocDetl>().eq("loc_no", locMast.getLocNo()));
            // 生成工作档明细
            for (LocDetl detlDto : locDetls) {
                WrkDetl wrkDetl = new WrkDetl();
                BeanUtils.copyProperties(detlDto, wrkDetl);
                wrkDetl.setOrderNo(""); // 手动出库不需要带出库存中的单据编号
                wrkDetl.setWrkNo(workNo);
                wrkDetl.setIoTime(now);
                wrkDetl.setAppeTime(now);
                wrkDetl.setAppeUser(userId);
                wrkDetl.setModiTime(now);
                wrkDetl.setModiUser(userId);
                if (!wrkDetlService.insert(wrkDetl)) {
                    throw new CoolException("保存工作档明细失败");
                }
            }
            // 修改库位状态:   F.在库 ====>>> R.出库预约/P.拣料/盘点/并板出库中
            locMast = locMastService.selectById(locMast.getLocNo());
            if (locMast.getLocSts().equals(LocStsType.LOC_STS_TYPE_F.type)) {
                locMast.setLocSts(ioType == 101 ? "R" : "P");
                locMast.setModiUser(userId);
                locMast.setModiTime(now);
                if (!locMastService.updateById(locMast)) {
                    throw new CoolException("预约库位状态失败,库位号:" + locMast.getLocNo());
                }
            } else {
                throw new CoolException(locMast.getLocNo() + "库位不是在库状态");
            }
        }
    }
    /**
@@ -1279,7 +1536,7 @@
     * @version 1.0
     */
    @Transactional(rollbackFor = Exception.class)
    public void generateOutTask(BasStation station, LocCache loc, Long userId) {
    public void generateCacheOutTask(BasStation station, LocCache loc, Long userId) {
        // 获取工作号
        int workNo = commonService.getWorkNo(WorkNoType.PICK.type);
        // 保存工作档
@@ -1287,10 +1544,10 @@
        task.setWrkNo(workNo)
                .setIoTime(new Date())
                .setWrkSts(11L) // 工作状态:11.生成出库ID
                .setIoType(101) // 入出库状态: 11.库格移载
                .setIoType(loc.getLocSts().equals("D") ? 110 : 101) // 入出库状态: 11.库格移载
                .setTaskType("agv")
                .setIoPri(10D)
                .setFullPlt("Y") // 满板:Y
                .setFullPlt(loc.getLocSts().equals("D") ? "N" : "Y") // 满板:Y
                .setPicking("N") // 拣料
                .setExitMk("N")// 退出
                .setStaNo(station.getDevNo())
@@ -1306,32 +1563,30 @@
            throw new CoolException("保存工作档失败");
        }
        List<LocDetl> detls = locDetlService.selectList(new EntityWrapper<LocDetl>().eq("loc_id", loc.getId()));
        if (Objects.isNull(detls) || detls.isEmpty()) {
            throw new CoolException("数据错误:库位明细为空!!");
        }
        List<TaskDetl> taskDetls = new ArrayList<>();
        detls.forEach(pakin -> {
            TaskDetl wrkDetl = new TaskDetl();
            BeanUtils.copyProperties(pakin, wrkDetl);
            wrkDetl.setWrkNo(workNo)
                    .setIoTime(new Date())
                    .setOrderNo(pakin.getOrderNo())
                    .setAnfme(pakin.getAnfme())
                    .setZpallet(pakin.getZpallet())
                    .setBatch(pakin.getBatch())
                    .setMatnr(pakin.getMatnr())
                    .setMaktx(pakin.getMaktx())
                    .setAppeUser(userId)
                    .setUnit(pakin.getUnit())
                    .setModel(pakin.getModel())
                    .setAppeTime(new Date())
                    .setModiUser(userId);
            taskDetls.add(wrkDetl);
        });
        //保存工作档明细
        if (!taskDetlService.insertBatch(taskDetls)) {
            throw new CoolException("保存工作档明细失败");
        if (!detls.isEmpty()) {
            List<TaskDetl> taskDetls = new ArrayList<>();
            detls.forEach(pakin -> {
                TaskDetl wrkDetl = new TaskDetl();
                BeanUtils.copyProperties(pakin, wrkDetl);
                wrkDetl.setWrkNo(workNo)
                        .setIoTime(new Date())
                        .setOrderNo(pakin.getOrderNo())
                        .setAnfme(pakin.getAnfme())
                        .setZpallet(pakin.getZpallet())
                        .setBatch(pakin.getBatch())
                        .setMatnr(pakin.getMatnr())
                        .setMaktx(pakin.getMaktx())
                        .setAppeUser(userId)
                        .setUnit(pakin.getUnit())
                        .setModel(pakin.getModel())
                        .setAppeTime(new Date())
                        .setModiUser(userId);
                taskDetls.add(wrkDetl);
            });
            //保存工作档明细
            if (!taskDetlService.insertBatch(taskDetls)) {
                throw new CoolException("保存工作档明细失败");
            }
        }
        loc.setLocSts(LocStsType.LOC_STS_TYPE_R.type);
@@ -1417,9 +1672,16 @@
        if (!taskDetlService.insertBatch(taskDetls)) {
            throw new CoolException("保存工作档明细失败");
        }
        pakins.forEach(pakin -> {pakin.setIoStatus("Y");});
        if (!waitPakinService.updateBatchById(pakins)) {
            throw new CoolException("更新组托信息失败");
        }
        // 修改目标库位状态
        if (loc.getLocSts().equals("O")) {
            loc.setLocSts("S"); // S.入库预约
        if (loc.getLocSts().equals(LocStsType.LOC_STS_TYPE_O.type)) {
            loc.setLocSts(LocStsType.LOC_STS_TYPE_S.type); // S.入库预约
            loc.setModiTime(new Date());
            loc.setModiUser(userId);
            if (!locCacheService.updateById(loc)) {
@@ -1428,5 +1690,73 @@
        } else {
            throw new CoolException("移转失败,目标库位状态:" + loc.getLocSts$());
        }
        // 修改目标站点信息
        BasStation station = basStationService.selectOne(new EntityWrapper<BasStation>().eq("dev_no", orgSite));
        if (station.getLocSts().equals("O")) {
            station.setLocSts("R"); // S.入库预约
            station.setBarcode(barcode);
            station.setModiTime(new Date());
            station.setModiUser(userId);
            if (!basStationService.updateById(station)) {
                throw new CoolException("更新目标库位状态失败");
            }
        } else {
            throw new CoolException("移转失败,目标库位状态:" + station.getLocSts());
        }
    }
    public HIKResultDTO sendAgvTask(HIKApiDTO haiKangApiDTO,String path){
        HIKResultDTO result = new HIKResultDTO();
        ForwardAGVTaskDTO forwardAGVTaskParam = new ForwardAGVTaskDTO();
        forwardAGVTaskParam.setReqCode(UUID.randomUUID().toString().replace("-", ""));
        forwardAGVTaskParam.setClientCode("IWMS");
        forwardAGVTaskParam.setTaskTyp(haiKangApiDTO.getTaskType());
        forwardAGVTaskParam.setCtnrTyp(haiKangApiDTO.getCtnrType());
        forwardAGVTaskParam.setPriority(haiKangApiDTO.getPriority());
        List<ForwardAGVTaskDTO.PositionCodePaths> positionCodePathsList = new ArrayList<>();
        positionCodePathsList.add(new ForwardAGVTaskDTO.PositionCodePaths(haiKangApiDTO.getOrg(), haiKangApiDTO.getOrgType()));
        positionCodePathsList.add(new ForwardAGVTaskDTO.PositionCodePaths(haiKangApiDTO.getTar(), haiKangApiDTO.getTarType()));
        forwardAGVTaskParam.setPositionCodePath(positionCodePathsList);
        String body = JSON.toJSONString(forwardAGVTaskParam);
        String response = "";
        try {
            response = new HttpHandler.Builder()
                    .setUri(HIKApiConstant.AGV_IP)
                    .setPath(path)
                    .setJson(body)
                    .build()
                    .doPost();
            JSONObject jsonObject = JSON.parseObject(response);
            if (jsonObject.getInteger("code").equals(0)) {
                result.setSuccess(true);
            } else {
                result.setMessage(jsonObject.getString("message"));
                log.error("发送agv任务失败!!!url:{};request:{};response:{}", HIKApiConstant.AGV_IP + path, body, response);
            }
//            {"code":"1","data":"","interrupt":false,"message":"重复提交","msgErrCode":"0x3a80D012","reqCode":"fa92b49481a44627ae4d80c1400f28f6"}
        } catch (Exception e) {
            result.setMessage(e.getMessage());
            log.error("发送agv任务异常", e);
        } finally {
            try {
                // 保存接口日志
                apiLogService.save(
                        "发送agv任务",
                        HIKApiConstant.AGV_IP + path,
                        null,
                        "127.0.0.1",
                        body,
                        response,
                        result.isSuccess()
                );
            } catch (Exception e) {
                log.error("", e);
            }
        }
        return result;
    }
}