1
2 天以前 8dfd55ef1c0eccf3adf105f0d4f5828bdbc3f86d
rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/TaskServiceImpl.java
@@ -125,7 +125,7 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized R generateAGVTasks(WaitPakin pakins, String locCode, String orgSta, Long loginUserId) {
    public synchronized R generateAGVTasks(WaitPakin pakins, String locCode, String orgSta,String targSite, Long loginUserId) {
        if (Cools.isEmpty(pakins) || Cools.isEmpty(locCode)) {
            throw new CoolException("参数不能为空!!");
        }
@@ -154,6 +154,7 @@
                    .setTaskType(TaskType.TASK_TYPE_IN.type)
                    .setWarehType(WarehType.WAREHOUSE_TYPE_AGV.val)
                    .setTargLoc(targetLoc)
                    .setTargSite(targSite)
                    .setOrgSite(orgSta)
                    .setBarcode(pakin.getBarcode())
                    .setCreateBy(loginUserId)
@@ -206,7 +207,6 @@
        }
        return R.ok("任务生成完毕!");
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
@@ -334,13 +334,16 @@
            throw new CoolException("请检查组拖状态是否完成!!");
        }
        waitPakins.forEach(pakin -> {
            BasContainer container = basContainerService.getOne(new LambdaUpdateWrapper<BasContainer>()
                    .eq(BasContainer::getCode, pakin.getBarcode()));
            if (Objects.isNull(container)) {
                throw new CoolException("容器未维护入库,请维护后再操作!!");
            }
//            BasContainer container = basContainerService.getOne(new LambdaUpdateWrapper<BasContainer>()
//                    .eq(BasContainer::getCode, pakin.getBarcode()));
//            if (Objects.isNull(container)) {
//                throw new CoolException("容器未维护入库,请维护后再操作!!");
//            }
            //验证基础信息
            checkStaStatus(pakin.getBarcode(), deviceSite.getSite(),warehouseArea.getId().toString());
            /**获取库位*/
            String targetLoc = LocManageUtil.getTargetLoc(warehouseArea.getId(), container.getContainerType());
            String targetLoc = LocManageUtil.getTargetLoc(warehouseArea.getId(), null);
            if (Cools.isEmpty(targetLoc)) {
                throw new CoolException("该站点对应库区未找到库位");
            }
@@ -446,123 +449,274 @@
        return R.ok("任务生成完毕!");
    }
    private BasStation checkStaStatus(String barcode, String sta,String area) {
        if (Cools.isEmpty(barcode)) {
            throw new CoolException("容器码不能为空");
        }
        if (Cools.isEmpty(sta)) {
            throw new CoolException("接驳位条码不能为空");
        }
        WaitPakin waitPakin = waitPakinService.getOne(new LambdaQueryWrapper<WaitPakin>()
                .eq(WaitPakin::getBarcode, barcode)
                .eq(WaitPakin::getIoStatus, PakinIOStatus.PAKIN_IO_STATUS_DONE.val)
        );
        if (Cools.isEmpty(waitPakin)) {
            throw new CoolException("容器码未找到组托信息,请检查组托状态");
        }
        BasStation isBarcodeSta = basStationService.getOne(new LambdaQueryWrapper<BasStation>()
                        .eq(BasStation::getBarcode, barcode)
                , false
        );
        if (!Cools.isEmpty(isBarcodeSta)) {
            throw new CoolException("该条码已被" + isBarcodeSta.getStationName() + "站绑定");
        }
        BasStation basStation = basStationService.getOne(new LambdaQueryWrapper<BasStation>()
                .eq(BasStation::getStationName, sta)
        );
        if (Cools.isEmpty(basStation)) {
            throw new CoolException("未找到站点信息");
        }
        if (!basStation.getUseStatus().equals("O")) {
            throw new CoolException("站点状态不为空闲");
        }
        List<String> areaList = JSONObject.parseArray(basStation.getCrossZoneArea(), String.class);
        if (!areaList.contains(area)) {
            throw new CoolException("当前站点不支持目标库区");
        }
        if (!Cools.isEmpty(basStation.getContainerType())) {
            List<Long> longs1 = JSONObject.parseArray(basStation.getContainerType(), Long.class);
            List<BasContainer> containers = basContainerService.list(
                    new LambdaQueryWrapper<BasContainer>()
                            .in(BasContainer::getContainerType, longs1)
            );
            boolean matches = false;
            boolean matches2 = true;
            for (BasContainer container : containers) {
                String codeType = container.getCodeType();  // 获取正则表达式
                if (barcode.matches(codeType)) {  // 判断条码是否符合这个正则
                    List<Integer> areaList2 = container.getAreas();
                    if (!areaList2.contains(Integer.parseInt(area))) {
                        matches2 = false;
                        continue;
                    }
                    matches = true;
                    break;  // 找到匹配的就退出循环
                }
            }
//            boolean matches = containers.stream()
//                    .map(BasContainer::getCodeType)
//                    .anyMatch(codeType -> barcode.matches(codeType));
            if (!matches2) {
                throw new CoolException("查询到的容器不支持目标库区");
            }
            if (!matches) {
                throw new CoolException("条码与站点不匹配");
            }
        } else {
            throw new CoolException("数据异常:验证基础信息");
        }
        return basStation;
    }
    private BasStation checkStaStatus(String barcode, String sta) {
        if (Cools.isEmpty(barcode)) {
            throw new CoolException("容器码不能为空");
        }
        if (Cools.isEmpty(sta)) {
            throw new CoolException("接驳位不能为空");
        }
        BasStation isBarcodeSta = basStationService.getOne(new LambdaQueryWrapper<BasStation>()
                        .eq(BasStation::getBarcode, barcode)
                , false
        );
        if (!Cools.isEmpty(isBarcodeSta)) {
            throw new CoolException("该条码已被" + isBarcodeSta.getStationName() + "站绑定");
        }
        BasStation basStation = basStationService.getOne(new LambdaQueryWrapper<BasStation>()
                .eq(BasStation::getStationName, sta)
        );
        if (Cools.isEmpty(basStation)) {
            throw new CoolException("未找到站点信息");
        }
        if (!basStation.getUseStatus().equals("O")) {
            throw new CoolException("站点状态不为空闲");
        }
        if (!Cools.isEmpty(basStation.getContainerType())) {
            List<Long> longs1 = JSONObject.parseArray(basStation.getContainerType(), Long.class);
            List<BasContainer> containers = basContainerService.list(
                    new LambdaQueryWrapper<BasContainer>()
                            .in(BasContainer::getContainerType, longs1)
            );
            boolean matches = false;
            for (BasContainer container : containers) {
                String codeType = container.getCodeType();  // 获取正则表达式
                if (barcode.matches(codeType)) {  // 判断条码是否符合这个正则
                    matches = true;
                    break;  // 找到匹配的就退出循环
                }
            }
//            boolean matches = containers.stream()
//                    .map(BasContainer::getCodeType)
//                    .anyMatch(codeType -> barcode.matches(codeType));
            if (!matches) {
                throw new CoolException("条码与站点不匹配");
            }
        } else {
            throw new CoolException("数据异常:验证基础信息");
        }
        return basStation;
    }
    /**
     * @param
     * @param loginUserId
     * @return
     * @author Munch D. Luffy
     * @date 2026/01/10
     * @description: WCS入库任务申请
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized R generateTasksWcs(GenerateTaskParams waitPakin, Long loginUserId) {
    public synchronized R generateTasksWcs(GenerateTaskParams waitPakin, Long loginUserId,List<Integer> rowList) {
        if (Objects.isNull(waitPakin) || waitPakin.getWaitPakins().isEmpty()) {
            throw new CoolException("参数不能为空!!");
        }
        DeviceSite deviceSite = deviceSiteService.getById(waitPakin.getSiteId());
        if (Objects.isNull(deviceSite)) {
        BasStation basStation = basStationService.getById(waitPakin.getSiteId());
        if (Objects.isNull(basStation)) {
            throw new CoolException("站点不存在!!");
        }
        DeviceBind deviceBind = deviceBindService.getById(LocUtils.getAreaType(deviceSite.getSite()));
        if (Cools.isEmpty(deviceBind)) {
            throw new CoolException("库位规则未知");
        List<WarehouseAreas> warehouseAreasList = new ArrayList<>();
        if (basStation.getIsWcs() == 1){
            DeviceBind deviceBind = deviceBindService.getById(LocUtils.getAreaType(basStation.getStationName()));
            if (Cools.isEmpty(deviceBind)) {
                throw new CoolException("库位规则未知");
            }
            WarehouseAreas warehouseArea = warehouseAreasService.getById(deviceBind.getTypeId());
            if (Cools.isEmpty(warehouseArea)) {
                throw new CoolException("未找到所属库区信息");
            }
            warehouseAreasList.add(warehouseArea);
        } else {
            List<String> areaList = JSONObject.parseArray(basStation.getCrossZoneArea(), String.class);
            if (areaList.isEmpty()) {
                throw new CoolException("当前站点库区未配置");
            }
            warehouseAreasList = warehouseAreasService.list(new LambdaQueryWrapper<WarehouseAreas>().in(WarehouseAreas::getId,areaList));
        }
        WarehouseAreas warehouseArea = warehouseAreasService.getById(deviceBind.getTypeId());
        if (Cools.isEmpty(warehouseArea)) {
        if (Cools.isEmpty(warehouseAreasList) || warehouseAreasList.size() == 0) {
            throw new CoolException("未找到所属库区信息");
        }
        /**获取组拖*/
        List<Long> ids = waitPakin.getWaitPakins().stream().map(WaitPakin::getId).collect(Collectors.toList());
        List<WaitPakin> waitPakins = waitPakinService.list(new LambdaQueryWrapper<WaitPakin>()
                .in(WaitPakin::getId, ids)
                .eq(WaitPakin::getIoStatus, PakinIOStatus.PAKIN_IO_STATUS_DONE.val));
        if (waitPakins.isEmpty()) {
            throw new CoolException("请检查组拖状态是否完成!!");
        }
        waitPakins.forEach(pakin -> {
            BasContainer container = basContainerService.getOne(new LambdaUpdateWrapper<BasContainer>()
                    .eq(BasContainer::getCode, pakin.getBarcode()));
            if (Objects.isNull(container)) {
                throw new CoolException("容器未维护入库,请维护后再操作!!");
        final String[] errMsg = {""};
        for (WarehouseAreas warehouseArea : warehouseAreasList) {
            /**获取组拖*/
            List<Long> ids = waitPakin.getWaitPakins().stream().map(WaitPakin::getId).collect(Collectors.toList());
            List<WaitPakin> waitPakins = waitPakinService.list(new LambdaQueryWrapper<WaitPakin>()
                    .in(WaitPakin::getId, ids)
                    .eq(WaitPakin::getIoStatus, PakinIOStatus.PAKIN_IO_STATUS_DONE.val));
            if (waitPakins.isEmpty()) {
                throw new CoolException("请检查组拖状态是否完成!!");
            }
            /**获取库位*/
            String targetLoc = LocManageUtil.getTargetLoc(warehouseArea.getId(), container.getContainerType());
            if (Cools.isEmpty(targetLoc)) {
                throw new CoolException("该站点对应库区未找到库位");
            }
            List<TaskItem> taskItems = new ArrayList<>();
            String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_TASK_CODE, null);
            if (StringUtils.isBlank(ruleCode)) {
                throw new CoolException("编码错误:请确认编码「SYS_TASK_CODE」是否已生成!!");
            }
            Task task = new Task();
            task.setTaskCode(ruleCode)
                    .setTaskStatus(TaskStsType.WCS_EXECUTE_IN.id)
                    .setTaskType(TaskType.TASK_TYPE_IN.type)
                    .setResource(TaskResouceType.TASK_RESOUCE_PAKIN_TYPE.val)
                    .setTargLoc(targetLoc)
                    .setBarcode(pakin.getBarcode())
                    .setOrgSite(deviceSite.getSite())
                    .setTargSite(deviceSite.getDeviceSite())
                    .setCreateBy(loginUserId)
                    .setUpdateBy(loginUserId);
            if (!this.save(task)) {
                throw new CoolException("任务保存失败!!");
            }
            if (!locService.update(new LambdaUpdateWrapper<Loc>().eq(Loc::getCode, task.getTargLoc())
                    .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_S.type).set(Loc::getBarcode, pakin.getBarcode()))) {
                throw new CoolException("库位预约失败!!");
            }
            /**获取组拖明细**/
            List<WaitPakinItem> waitPakinItems = waitPakinItemService.list(new LambdaQueryWrapper<WaitPakinItem>().eq(WaitPakinItem::getPakinId, pakin.getId()));
            if (waitPakinItems.isEmpty()) {
                throw new CoolException("数据错误:组拖明细不存在");
            }
            waitPakinItems.forEach(item -> {
                TaskItem taskItem = new TaskItem();
                BeanUtils.copyProperties(item, taskItem);
                taskItem.setTaskId(task.getId())
                        .setOrderType(OrderType.ORDER_IN.type)
                        .setSource(item.getId())
                        .setTrackCode(item.getTrackCode())
                        .setPlatItemId(item.getPlatItemId())
                        .setPlatOrderCode(item.getPlatOrderCode())
                        .setPlatWorkCode(item.getPlatWorkCode())
                        .setProjectCode(item.getProjectCode())
                        .setCreateBy(loginUserId)
                        .setUpdateBy(loginUserId)
                        .setExtendFields(item.getExtendFields())
                        .setOrderId(item.getAsnId())
                        .setOrderItemId(item.getAsnItemId());
                taskItems.add(taskItem);
            });
            if (!taskItemService.saveBatch(taskItems)) {
                throw new CoolException("任务明细保存失败!!");
            }
            waitPakinItems.forEach(item -> {
                if (!waitPakinItemService.update(new LambdaUpdateWrapper<WaitPakinItem>()
                        .set(WaitPakinItem::getWorkQty, item.getAnfme())
                        .eq(WaitPakinItem::getId, item.getId()))) {
                    throw new CoolException("组托明细修执行数量修改失败!!");
            boolean sign = true;
            for (WaitPakin pakin : waitPakins) {//验证基础信息
                try {
                    checkStaStatus(pakin.getBarcode(), basStation.getStationName(), warehouseArea.getId().toString());
                } catch (Exception e) {
                    errMsg[0] = errMsg[0] + e.getMessage();
                    sign = false;
                    break;
                }
            });
        });
//            BasContainer container = basContainerService.getOne(new LambdaUpdateWrapper<BasContainer>()
//                    .eq(BasContainer::getCode, pakin.getBarcode()));
//            if (Objects.isNull(container)) {
//                throw new CoolException("容器未维护入库,请维护后再操作!!");
//            }
                /**获取库位*/
                String targetLoc = LocManageUtil.getTargetLoc(warehouseArea.getId(), null, rowList);
                if (Cools.isEmpty(targetLoc)) {
                    throw new CoolException("该站点对应库区未找到库位");
                }
        if (!waitPakinService.update(new LambdaUpdateWrapper<WaitPakin>()
                .in(WaitPakin::getId, ids)
                .set(WaitPakin::getUpdateBy, loginUserId)
                .set(WaitPakin::getCreateBy, loginUserId)
                .set(WaitPakin::getIoStatus, PakinIOStatus.PAKIN_IO_STATUS_TASK_EXCE.val))) {
            throw new CoolException("组拖状态修改失败!!");
                List<TaskItem> taskItems = new ArrayList<>();
                String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_TASK_CODE, null);
                if (StringUtils.isBlank(ruleCode)) {
                    throw new CoolException("编码错误:请确认编码「SYS_TASK_CODE」是否已生成!!");
                }
                String staNoEnd = basStation.getStationName();
                if (basStation.getIsWcs() != 0) {
                    staNoEnd = "B102";
                }
                Task task = new Task();
                task.setTaskCode(ruleCode)
                        .setTaskStatus(TaskStsType.WCS_EXECUTE_IN.id)
                        .setTaskType(TaskType.TASK_TYPE_IN.type)
                        .setResource(TaskResouceType.TASK_RESOUCE_PAKIN_TYPE.val)
                        .setTargLoc(targetLoc)
                        .setBarcode(pakin.getBarcode())
                        .setOrgSite(basStation.getStationName())
                        .setTargSite(staNoEnd)
                        .setCreateBy(loginUserId)
                        .setUpdateBy(loginUserId);
                if (!this.save(task)) {
                    throw new CoolException("任务保存失败!!");
                }
                if (!locService.update(new LambdaUpdateWrapper<Loc>().eq(Loc::getCode, task.getTargLoc())
                        .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_S.type).set(Loc::getBarcode, pakin.getBarcode()))) {
                    throw new CoolException("库位预约失败!!");
                }
                /**获取组拖明细**/
                List<WaitPakinItem> waitPakinItems = waitPakinItemService.list(new LambdaQueryWrapper<WaitPakinItem>().eq(WaitPakinItem::getPakinId, pakin.getId()));
                if (waitPakinItems.isEmpty()) {
                    throw new CoolException("数据错误:组拖明细不存在");
                }
                waitPakinItems.forEach(item -> {
                    TaskItem taskItem = new TaskItem();
                    BeanUtils.copyProperties(item, taskItem);
                    taskItem.setTaskId(task.getId())
                            .setOrderType(OrderType.ORDER_IN.type)
                            .setSource(item.getId())
                            .setTrackCode(item.getTrackCode())
                            .setPlatItemId(item.getPlatItemId())
                            .setPlatOrderCode(item.getPlatOrderCode())
                            .setPlatWorkCode(item.getPlatWorkCode())
                            .setProjectCode(item.getProjectCode())
                            .setCreateBy(loginUserId)
                            .setUpdateBy(loginUserId)
                            .setExtendFields(item.getExtendFields())
                            .setOrderId(item.getAsnId())
                            .setOrderItemId(item.getAsnItemId());
                    taskItems.add(taskItem);
                });
                if (!taskItemService.saveBatch(taskItems)) {
                    throw new CoolException("任务明细保存失败!!");
                }
                waitPakinItems.forEach(item -> {
                    if (!waitPakinItemService.update(new LambdaUpdateWrapper<WaitPakinItem>()
                            .set(WaitPakinItem::getWorkQty, item.getAnfme())
                            .eq(WaitPakinItem::getId, item.getId()))) {
                        throw new CoolException("组托明细修执行数量修改失败!!");
                    }
                });
            }
            if (!sign){
                continue;
            }
            if (!waitPakinService.update(new LambdaUpdateWrapper<WaitPakin>()
                    .in(WaitPakin::getId, ids)
                    .set(WaitPakin::getUpdateBy, loginUserId)
                    .set(WaitPakin::getCreateBy, loginUserId)
                    .set(WaitPakin::getIoStatus, PakinIOStatus.PAKIN_IO_STATUS_TASK_EXCE.val))) {
                throw new CoolException("组拖状态修改失败!!");
            }
            return R.ok("任务生成完毕!");
        }
        return R.ok("任务生成完毕!");
        String msg = "未找库位" + Arrays.toString(errMsg);
        throw new CoolException(msg);
    }
    /**
@@ -643,7 +797,7 @@
//        if (task.getTaskType().equals(TaskType.TASK_TYPE_LOC_MOVE.type)) {
//            task.setTaskStatus(TaskStsType.COMPLETE_OUT.id);
//        } else {
        task.setTaskStatus(task.getTaskType() < 100 ? TaskStsType.COMPLETE_IN.id : TaskStsType.AWAIT.id);
        task.setTaskStatus(task.getTaskType() < 100 ? TaskStsType.COMPLETE_IN.id : task.getTaskType()==101? TaskStsType.COMPLETE_OUT.id:TaskStsType.AWAIT.id);
//        }
        if (!this.updateById(task)) {
            throw new CoolException("完成任务失败");
@@ -659,7 +813,7 @@
     */
    private Task modiftyTaskSort(Task task, Long loginUserId) {
        String curLoc;
        if (task.getTaskType().equals(TaskType.TASK_TYPE_IN.type)) {
        if (task.getTaskType().equals(TaskType.TASK_TYPE_IN.type) || task.getTaskType().equals(TaskType.TASK_TYPE_EMPITY_IN.type)) {
            curLoc = task.getTargLoc();
        } else {
            curLoc = task.getOrgLoc();
@@ -810,7 +964,12 @@
        for (Task task : tasks) {
            //出库任务
            try {
                complateOutStock(task, loginUserId);
                if (task.getTaskType().equals(TaskType.TASK_TYPE_EMPITY_OUT.type)) {
                    //110.空板出库
                    complateOutStockEmpty(task, loginUserId);
                } else {
                    complateOutStock(task, loginUserId);
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
//                throw new CoolException(e.getMessage());
@@ -1391,6 +1550,45 @@
    /**
     * @author Ryan
     * @date 2025/5/20
     * @description: 完成出库任务,更新出库库存信息
     * @version 1.0
     */
    @Synchronized
    @Transactional(rollbackFor = Exception.class)
    public void complateOutStockEmpty(Task task, Long loginUserId) throws Exception {
        if (Objects.isNull(task)) {
            throw new CoolException("参数不能为空!!");
        }
        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getCode, task.getOrgLoc()));
        if (Objects.isNull(loc)) {
            throw new CoolException("库位不存在!!");
        }
        if (!loc.getUseStatus().equals(LocStsType.LOC_STS_TYPE_R.type)) {
            throw new CoolException("库位状态不处理于R.出库预约!!");
        }
        /**修改为库位状态为O.空库*/
        if (!locService.update(new LambdaUpdateWrapper<Loc>()
                .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_O.type)
                .set(Loc::getBarcode, null)
                .set(Loc::getUpdateBy, loginUserId)
                .set(Loc::getUpdateTime, new Date())
                .eq(Loc::getId, loc.getId()))) {
            throw new CoolException("库位状态修改失败!!");
        }
        if (!this.update(new LambdaUpdateWrapper<Task>()
                .eq(Task::getId, task.getId())
                .set(Task::getUpdateBy, loginUserId)
                .set(Task::getUpdateTime, new Date())
                .set(Task::getTaskStatus, TaskStsType.WAVE_SEED.id))) {
            throw new CoolException("库存状态更新失败!!");
        }
    }
    /**
     * @author Ryan
     * @date 2025/5/20
     * @description: 出库信息保存至库存明细表
     * @version 1.0
     */