rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/TaskServiceImpl.java
@@ -4,12 +4,14 @@
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.cfg.CoercionAction;
import com.fasterxml.jackson.databind.cfg.CoercionInputShape;
import com.vincent.rsf.framework.common.Cools;
import com.vincent.rsf.framework.common.DateUtils;
import com.vincent.rsf.server.api.config.RemotesInfoProperties;
import com.vincent.rsf.server.api.controller.erp.params.InOutResultReportParam;
import com.vincent.rsf.server.api.controller.erp.params.TaskInParam;
import com.vincent.rsf.server.api.entity.CommonResponse;
import com.vincent.rsf.server.api.entity.constant.RcsConstant;
@@ -18,6 +20,7 @@
import com.vincent.rsf.server.api.entity.params.WcsTaskParams;
import com.vincent.rsf.server.api.service.WcsService;
import com.vincent.rsf.server.common.constant.Constants;
import com.vincent.rsf.server.common.utils.QuantityUtils;
import com.vincent.rsf.server.manager.controller.params.LocToTaskParams;
import com.vincent.rsf.server.manager.controller.params.PakinItem;
import com.vincent.rsf.server.manager.enums.*;
@@ -25,6 +28,7 @@
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.server.api.utils.LocUtils;
import com.vincent.rsf.server.manager.controller.params.GenerateTaskParams;
import com.vincent.rsf.server.manager.entity.CloudWmsNotifyLog;
import com.vincent.rsf.server.manager.entity.*;
import com.vincent.rsf.server.manager.mapper.TaskMapper;
import com.vincent.rsf.server.manager.service.*;
@@ -123,6 +127,10 @@
    private RestTemplate restTemplate;
    @Autowired
    private RemotesInfoProperties.RcsApi rcsApi;
    @Autowired
    private CloudWmsNotifyLogService cloudWmsNotifyLogService;
    @Autowired
    private WarehouseService warehouseService;
    @Override
    @Transactional(rollbackFor = Exception.class)
@@ -459,16 +467,18 @@
        if (success.compareAndSet(false, true)) {
            Long loginUserId = SystemAuthUtils.getLoginUserId();
            for (Task task : tasks) {
                if (task.getTaskType().equals(TaskType.TASK_TYPE_IN.type)) {
                    //1.入库
                if (task.getTaskType().equals(TaskType.TASK_TYPE_IN.type) || task.getTaskType().equals(TaskType.TASK_TYPE_MERGE_IN.type)) {
                    //1.入库、54.并板再入库
                    complateInstock(task, loginUserId);
                } else if (task.getTaskType().equals(TaskType.TASK_TYPE_PICK_IN.type) || task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_IN.type)) {
                    //53.拣料再入库
                    //57.盘点再入库
                    //53.拣料再入库、57.盘点再入库
                    pickComplateInStock(task, loginUserId);
                } else if (task.getTaskType().equals(TaskType.TASK_TYPE_LOC_MOVE.type)) {
                    //移库
                    //11.库格移载
                    moveInStock(task, loginUserId);
                } else if (task.getTaskType().equals(TaskType.TASK_TYPE_EMPITY_IN.type)) {
                    //10.空板入库:与普通入库共用一个完成逻辑(若有组托则更新组托/库位并上报云仓)
                    complateInstock(task, loginUserId);
                }
            }
        }
@@ -508,25 +518,157 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Task operateComplete(Long id, Long loginUserId) {
        List<Integer> longs = Arrays.asList(TaskStsType.GENERATE_IN.id, TaskStsType.GENERATE_OUT.id);
        // 先查询任务,不限制状态
        Task task = taskService.getOne(new LambdaQueryWrapper<Task>()
                .eq(Task::getId, id)
                .in(Task::getTaskStatus, longs));
                .eq(Task::getId, id));
        if (Objects.isNull(task)) {
            throw new CoolException("数据错误:当前任务不可执行完结操作!!");
            throw new CoolException("任务不存在!!");
        }
        // 根据前端逻辑判断是否允许完成:
        // 前端条件:((taskStatus < 98) || (taskType >= 101 && taskStatus < 198)) || (taskType == 11 && taskStatus == 101)
        // 1. 任何任务状态 < 98 都可以完成
        // 2. 出库任务(taskType >= 101)且状态 < 198 可以完成
        // 3. 库格移载(taskType == 11)且状态 == 101 可以完成
        boolean canComplete = false;
        if (task.getTaskStatus() < TaskStsType.COMPLETE_IN.id) {
            // 任何任务状态 < 98 都可以完成
            canComplete = true;
        } else if (task.getTaskType() >= 101 && task.getTaskStatus() < TaskStsType.COMPLETE_OUT.id) {
            // 出库任务且状态 < 198
            canComplete = true;
        } else if (task.getTaskType().equals(TaskType.TASK_TYPE_LOC_MOVE.type)
                && task.getTaskStatus().equals(TaskStsType.GENERATE_OUT.id)) {
            // 库格移载且状态 == 101
            canComplete = true;
        }
        if (!canComplete) {
            throw new CoolException("数据错误:当前任务不可执行完结操作!!任务类型:" + task.getTaskType() + ",任务状态:" + task.getTaskStatus());
        }
        modiftyTaskSort(task, loginUserId);
        //
//        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);
//        }
        // 如果任务状态已经是AWAIT (196),再次点击完结时,直接完成
        if (task.getTaskStatus().equals(TaskStsType.AWAIT.id)) {
            // AWAIT状态的任务再次完结,直接设置为出库完成
            task.setTaskStatus(TaskStsType.COMPLETE_OUT.id);
            // 更新出库站点状态(与RCS通知完结保持一致)
            if (task.getTaskType() >= TaskType.TASK_TYPE_OUT.type && StringUtils.isNotBlank(task.getTargSite())) {
                BasStation station = basStationService.getOne(new LambdaQueryWrapper<BasStation>()
                        .eq(BasStation::getStationName, task.getTargSite()));
                if (Objects.nonNull(station) && station.getType().equals(StationTypeEnum.STATION_TYPE_NORMAL.type)) {
                    station.setUseStatus(LocStsType.LOC_STS_TYPE_F.type);
                    if (!basStationService.updateById(station)) {
                        throw new CoolException("出库站点状态修改失败!!");
                    }
                }
            }
        } else {
            // 其他情况按原有逻辑处理
            // 入库任务(taskType < 100):设置为入库完成
            // 出库任务(taskType >= 100):设置为等待确认
            Integer newStatus = task.getTaskType() < 100 ? TaskStsType.COMPLETE_IN.id : TaskStsType.AWAIT.id;
            task.setTaskStatus(newStatus);
            // 如果是入库任务完成,更新入库站点状态(与RCS通知完结保持一致)
            if (newStatus.equals(TaskStsType.COMPLETE_IN.id) && StringUtils.isNotBlank(task.getOrgSite())) {
                BasStation station = basStationService.getOne(new LambdaQueryWrapper<BasStation>()
                        .eq(BasStation::getStationName, task.getOrgSite()));
                if (Objects.nonNull(station) && station.getType().equals(StationTypeEnum.STATION_TYPE_NORMAL.type)) {
                    station.setUseStatus(LocStsType.LOC_STS_TYPE_O.type);
                    if (!basStationService.updateById(station)) {
                        throw new CoolException("入库站点状态修改失败!!");
                    }
                }
            }
        }
        if (!this.updateById(task)) {
            throw new CoolException("完成任务失败");
        }
        return task;
    }
    /**
     * 全版出库完结:扣除库位数量,将库位状态设为空
     *
     * @param id 任务ID
     * @param loginUserId 登录用户ID
     * @return 任务对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Task completeFullOutStock(Long id, Long loginUserId) {
        // 查询任务
        Task task = taskService.getOne(new LambdaQueryWrapper<Task>()
                .eq(Task::getId, id));
        if (Objects.isNull(task)) {
            throw new CoolException("任务不存在!!");
        }
        // 检查任务类型是否为全版出库
        if (!task.getTaskType().equals(TaskType.TASK_TYPE_OUT.type)) {
            throw new CoolException("当前任务不是全版出库任务,无法执行此操作!!");
        }
        // 检查任务状态:必须是199(WAVE_SEED)状态才能手动完结
        if (!task.getTaskStatus().equals(TaskStsType.WAVE_SEED.id)) {
            throw new CoolException("任务状态不是等待确认状态(199),无法执行此操作!!当前状态:" + task.getTaskStatus());
        }
        // 查询库位
        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getCode, task.getOrgLoc()));
        if (Objects.isNull(loc)) {
            throw new CoolException("库位不存在!!");
        }
        // 删除库位明细(扣除数量)
        try {
            subtractLocItem(loc);
        } catch (Exception e) {
            logger.error("删除库位明细失败", e);
            throw new CoolException("删除库位明细失败:" + e.getMessage());
        }
        // 删除作业中库存记录(LocItemWorking)
        locItemWorkingService.remove(new LambdaQueryWrapper<LocItemWorking>()
                .eq(LocItemWorking::getTaskId, task.getId()));
        // 将库位状态设为空(O状态)
        if (!locService.update(new LambdaUpdateWrapper<Loc>()
                .set(Loc::getBarcode, null)
                .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_O.type)
                .set(Loc::getUpdateBy, loginUserId)
                .set(Loc::getUpdateTime, new Date())
                .eq(Loc::getCode, task.getOrgLoc()))) {
            throw new CoolException("库位状态修改失败!!");
        }
        // 更新出库站点状态(如果有目标站点)
        if (StringUtils.isNotBlank(task.getTargSite())) {
            BasStation station = basStationService.getOne(new LambdaQueryWrapper<BasStation>()
                    .eq(BasStation::getStationName, task.getTargSite()));
            if (Objects.nonNull(station) && station.getType().equals(StationTypeEnum.STATION_TYPE_NORMAL.type)) {
                station.setUseStatus(LocStsType.LOC_STS_TYPE_F.type);
                if (!basStationService.updateById(station)) {
                    throw new CoolException("出库站点状态修改失败!!");
                }
            }
        }
        // 更新任务状态为库存更新完成(200)
        task.setTaskStatus(TaskStsType.UPDATED_OUT.id)
                .setUpdateBy(loginUserId)
                .setUpdateTime(new Date());
        if (!this.updateById(task)) {
            throw new CoolException("任务状态更新失败!!");
        }
        return task;
    }
@@ -589,7 +731,7 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void moveToDeep(Long loginUserId, String curLoc) throws Exception {
        //浅库位,判断深对应深库位是否为空,如果为空生成一个移库任务,将托盘送入深库位
        //浅库位,判断深对应深库位是否为空,如果为空生成一个移库任务,将料箱送入深库位
        String deepLoc = LocUtils.getDeepLoc(curLoc);
        if (StringUtils.isBlank(deepLoc)) {
            throw new CoolException("数据异常,请联系管理员!");
@@ -672,7 +814,11 @@
                .set(Task::getTaskStatus, TaskStsType.UPDATED_IN.id))) {
            throw new CoolException("任务状态修改失败!!");
        }
        // 9.1 入/出库结果上报:库格移载完成后通知云仓(与定时任务闭环一致)
        List<TaskItem> moveTaskItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>().eq(TaskItem::getTaskId, task.getId()));
        if (!moveTaskItems.isEmpty()) {
            reportInOutResultToCloud(task, loc, moveTaskItems, null, true);
        }
    }
@@ -728,27 +874,37 @@
            throw new CoolException("任务明细不存在!!");
        }
        List<LocItem> items = new ArrayList<>();
        for (TaskItem taskItem : taskItems) {
            LocItem locItem = new LocItem();
            LocItemWorking locWorking = locItemWorkingService.getOne(new LambdaQueryWrapper<LocItemWorking>()
                    .eq(LocItemWorking::getTaskId, taskItem.getTaskId())
                    .eq(LocItemWorking::getFieldsIndex, taskItem.getFieldsIndex())
                    .eq(StringUtils.isNotEmpty(taskItem.getBatch()), LocItemWorking::getBatch, taskItem.getBatch())
                    .eq(LocItemWorking::getMatnrId, taskItem.getMatnrId()));
            if (Objects.isNull(locWorking)) {
               continue;
        if (TaskType.TASK_TYPE_PICK_IN.type.equals(task.getTaskType())) {
            // 拣料再入库:出库时已在 pickOrCheckTask 中扣减原库位(1100 -> 1089.899),
            // 入库完成时不再回写/累加库位明细,保持 1 条 1089.899,避免出现两条 1100
            // 仅更新库位状态、清理 LocItemWorking、任务状态及流水
        } else {
            // 盘点入库等:沿用原逻辑,从 LocItemWorking 回写并 saveBatch
            List<LocItem> items = new ArrayList<>();
            for (TaskItem taskItem : taskItems) {
                LocItem locItem = new LocItem();
                LocItemWorking locWorking = locItemWorkingService.getOne(new LambdaQueryWrapper<LocItemWorking>()
                        .eq(LocItemWorking::getTaskId, taskItem.getTaskId())
                        .eq(StringUtils.isNotBlank(taskItem.getFieldsIndex()), LocItemWorking::getFieldsIndex, taskItem.getFieldsIndex())
                        .eq(StringUtils.isNotEmpty(taskItem.getBatch()), LocItemWorking::getBatch, taskItem.getBatch())
                        .eq(LocItemWorking::getMatnrId, taskItem.getMatnrId()));
                if (Objects.isNull(locWorking)) {
                   continue;
                }
                if (task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_IN.type)) {
                    locWorking.setAnfme(taskItem.getAnfme());
                }
                BeanUtils.copyProperties(locWorking, locItem);
                locItem.setWorkQty(0.0).setQty(0.0).setLocCode(loc.getCode()).setLocId(loc.getId()).setId(null).setUpdateBy(loginUserId).setUpdateTime(new Date());
                //数量为零的不入库
                if (locItem.getAnfme().compareTo(0.0) > 0) {
                    items.add(locItem);
                }
            }
            if (task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_IN.type)) {
                locWorking.setAnfme(taskItem.getAnfme());
            }
            BeanUtils.copyProperties(locWorking, locItem);
            locItem.setWorkQty(0.0).setQty(0.0).setLocCode(loc.getCode()).setLocId(loc.getId()).setId(null).setUpdateBy(loginUserId).setUpdateTime(new Date());
            items.add(locItem);
        }
        if (!locItemService.saveBatch(items)) {
            throw new CoolException("作业库存回写失败!!");
            if (!items.isEmpty() && !locItemService.saveBatch(items)) {
//            throw new CoolException("作业库存回写失败!!");
            }
        }
        TaskItem taskItem = taskItems.stream().findFirst().get();
@@ -761,7 +917,8 @@
        if (!taskService.updateById(task)) {
            throw new CoolException("任务状态修改失败!!");
        }
        // 9.1 入/出库结果上报:拣料再入库/盘点再入库完成后通知云仓(与定时任务闭环一致)
        reportInOutResultToCloud(task, loc, taskItems, null, true);
    }
    /**
@@ -774,16 +931,141 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R removeTask(Long[] ids, Long loginUserId) {
        List<Integer> longs = Arrays.asList(TaskStsType.GENERATE_IN.id, TaskStsType.GENERATE_OUT.id);
        // 先查询所有任务(不限制状态),用于检查RCS任务
        List<Integer> list = Arrays.asList(TaskType.TASK_TYPE_IN.type, TaskType.TASK_TYPE_OUT.type, TaskType.TASK_TYPE_PICK_AGAIN_OUT.type,
                TaskType.TASK_TYPE_CHECK_OUT.type, TaskType.TASK_TYPE_EMPITY_IN.type, TaskType.TASK_TYPE_LOC_MOVE.type,
                TaskType.TASK_TYPE_EMPITY_OUT.type, TaskType.TASK_TYPE_MERGE_OUT.type);
        List<Task> allTasks = this.list(new LambdaQueryWrapper<Task>()
                .in(Task::getTaskType, list)
                .in(Task::getId, (Object[]) ids));
        if (allTasks.isEmpty()) {
            throw new CoolException("任务不存在!!");
        }
        // 收集需要取消的RCS任务编号和批次编号(不限制状态,只要已下发到RCS就需要取消)
        List<String> rcsTaskCodes = new ArrayList<>();
        String batchNo = null;
        for (Task task : allTasks) {
            // 判断任务是否已下发到RCS
            // 入库任务:状态 >= WCS_EXECUTE_IN(2) 表示已下发
            // 出库任务:状态 >= WCS_EXECUTE_OUT(102) 表示已下发
            boolean isRcsTask = false;
            if (task.getTaskType() < 100) {
                // 入库任务
                if (task.getTaskStatus() >= TaskStsType.WCS_EXECUTE_IN.id) {
                    isRcsTask = true;
                }
            } else {
                // 出库任务
                if (task.getTaskStatus() >= TaskStsType.WCS_EXECUTE_OUT.id) {
                    isRcsTask = true;
                }
            }
            if (isRcsTask && StringUtils.isNotBlank(task.getTaskCode())) {
                rcsTaskCodes.add(task.getTaskCode());
                // 获取批次编号,优先使用任务的barcode,如果没有则使用任务编号
                if (StringUtils.isBlank(batchNo)) {
                    if (StringUtils.isNotBlank(task.getBarcode())) {
                        batchNo = task.getBarcode();
                    } else if (StringUtils.isNotBlank(task.getTaskCode())) {
                        // 如果任务没有barcode,使用任务编号作为批次编号
                        batchNo = task.getTaskCode();
                    }
                }
                log.info("任务已下发到RCS,需要取消RCS任务 - 任务ID:{},任务编号:{},任务状态:{},料箱码:{}",
                        task.getId(), task.getTaskCode(), task.getTaskStatus(), task.getBarcode());
            }
        }
        // 如果有任务已下发到RCS,先调用RCS取消接口
        boolean rcsCancelSuccess = false;
        if (!rcsTaskCodes.isEmpty()) {
            // 检查 RCS API 配置是否有效
            if (rcsApi == null || StringUtils.isBlank(rcsApi.getHost()) || StringUtils.isBlank(rcsApi.getPort())) {
                log.error("========== RCS任务取消失败 ==========");
                log.error("RCS API 配置无效!host: {}, port: {}",
                        rcsApi != null ? rcsApi.getHost() : "null",
                        rcsApi != null ? rcsApi.getPort() : "null");
                // 即使配置无效,也继续执行任务删除操作
            } else {
                try {
                    log.info("========== 开始取消RCS任务 ==========");
                    log.info("需要取消的RCS任务编号:{}", rcsTaskCodes);
                    String rcsUrl = rcsApi.getHost() + ":" + rcsApi.getPort() + RcsConstant.cancelTask;
                    log.info("RCS取消任务请求地址:{}", rcsUrl);
                // 如果没有批次编号,使用第一个任务编号作为批次编号
                if (StringUtils.isBlank(batchNo) && !rcsTaskCodes.isEmpty()) {
                    batchNo = rcsTaskCodes.get(0);
                }
                Map<String, Object> cancelParams = new HashMap<>();
                cancelParams.put("tasks", rcsTaskCodes);
                if (StringUtils.isNotBlank(batchNo)) {
                    cancelParams.put("batchNo", batchNo);
                }
                log.info("RCS取消任务请求参数:{}", JSONObject.toJSONString(cancelParams));
                HttpHeaders headers = new HttpHeaders();
                headers.add("Content-Type", "application/json");
                headers.add("api-version", "v2.0");
                HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(cancelParams, headers);
                long startTime = System.currentTimeMillis();
                ResponseEntity<String> exchange = restTemplate.exchange(rcsUrl, HttpMethod.POST, httpEntity, String.class);
                long endTime = System.currentTimeMillis();
                log.info("RCS取消任务响应耗时:{}ms", (endTime - startTime));
                log.info("RCS取消任务响应状态码:{}", exchange.getStatusCode());
                log.info("RCS取消任务响应体:{}", exchange.getBody());
                if (Objects.isNull(exchange.getBody())) {
                    log.error("RCS取消任务失败:响应体为空");
                    throw new CoolException("RCS取消任务失败:响应体为空");
                }
                ObjectMapper objectMapper = new ObjectMapper();
                objectMapper.coercionConfigDefaults()
                        .setCoercion(CoercionInputShape.EmptyString, CoercionAction.AsEmpty);
                CommonResponse result = objectMapper.readValue(exchange.getBody(), CommonResponse.class);
                if (result.getCode() == 200) {
                    log.info("========== RCS任务取消成功 ==========");
                    log.info("成功取消的RCS任务编号:{}", rcsTaskCodes);
                    rcsCancelSuccess = true;
                } else {
                    log.error("RCS取消任务失败:{}", result.getMsg());
                    throw new CoolException("RCS取消任务失败:" + result.getMsg());
                }
                } catch (JsonProcessingException e) {
                    log.error("RCS取消任务响应解析失败:{}", e.getMessage(), e);
                    throw new CoolException("RCS取消任务响应解析失败:" + e.getMessage());
                } catch (Exception e) {
                    log.error("RCS取消任务异常:{}", e.getMessage(), e);
                    throw new CoolException("RCS取消任务异常:" + e.getMessage());
                }
            }
        }
        // 查询符合取消条件的任务(状态为1、101、199)
        List<Integer> allowedStatuses = Arrays.asList(TaskStsType.GENERATE_IN.id, TaskStsType.GENERATE_OUT.id, TaskStsType.WAVE_SEED.id);
        List<Task> tasks = this.list(new LambdaQueryWrapper<Task>()
                .in(Task::getTaskType, list)
                .in(Task::getId, ids)
                .in(Task::getTaskStatus, longs));
        if (tasks.isEmpty()) {
                .in(Task::getId, (Object[]) ids)
                .in(Task::getTaskStatus, allowedStatuses));
        // 如果符合取消条件的任务为空,但RCS取消成功,允许继续(可能是任务状态已变更)
        if (tasks.isEmpty() && !rcsCancelSuccess) {
            throw new CoolException("任务已处执行状态不可取消!!");
        }
        // 如果符合取消条件的任务为空,但RCS取消成功,记录日志并返回
        if (tasks.isEmpty() && rcsCancelSuccess) {
            log.warn("WMS内部任务状态不符合取消条件,但RCS任务已成功取消 - 任务ID:{}", Arrays.toString(ids));
            return R.ok("RCS任务已取消,WMS内部任务状态已变更");
        }
        for (Task task : tasks) {
            //取消移库任务
@@ -1027,7 +1309,7 @@
//        if (Objects.isNull(locInfo)) {
//            throw new CoolException("获取库位失败!!");
//        }
        //希日上报物有情况,不需要获取新库位
        //上报物有情况,不需要获取新库位
        task.setTargLoc(task.getOrgLoc())
                .setOrgSite(task.getTargSite());
@@ -1036,27 +1318,105 @@
        }
        //获取因当前任务出库的所有物料信息
        List<LocItemWorking> tempLocs = locItemWorkingService.list(new LambdaQueryWrapper<LocItemWorking>().eq(LocItemWorking::getTaskId, task.getId()));
        if (tempLocs.isEmpty()) {
            throw new CoolException("数据错误,作业中库存数据丢失!!");
        }
        List<TaskItem> taskItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>().eq(TaskItem::getTaskId, task.getId()));
        if (taskItems.isEmpty()) {
            throw new CoolException("数据错误:任务明细为空!!");
        }
        // 与查询一致:若无作业中库存但存在任务明细,用任务明细在内存中兜底,避免“能查到却无法确认”
        if (tempLocs.isEmpty()) {
            tempLocs = taskItems.stream().map(ti -> {
                LocItemWorking w = new LocItemWorking();
                w.setTaskId(task.getId());
                w.setFieldsIndex(ti.getFieldsIndex());
                w.setAnfme(ti.getAnfme());
                w.setMatnrId(ti.getMatnrId());
                w.setMaktx(ti.getMaktx());
                w.setMatnrCode(ti.getMatnrCode());
                w.setSpec(ti.getSpec());
                w.setBatch(ti.getBatch());
                w.setUnit(ti.getUnit());
                w.setModel(ti.getModel());
                return w;
            }).collect(Collectors.toList());
        }
        // 拣料入库:先算剩余数量并更新 taskItem.anfme,已拣数量 taskItem.qty = 原库位 - 剩余(保证 数量=100、已拣数量=1、库存明细=100)
        if (TaskType.TASK_TYPE_PICK_IN.type.equals(type)) {
            log.debug("[拣料入库] 开始处理 taskId={}, taskCode={}, orgLoc={}, tempLocs.size={}, taskItems.size={}",
                    task.getId(), task.getTaskCode(), task.getOrgLoc(), tempLocs.size(), taskItems.size());
            tempLocs.forEach(working -> {
                taskItems.forEach(taskItem -> {
                    if (Objects.equals(taskItem.getFieldsIndex(), working.getFieldsIndex())) {
                        // 已拣数量:优先用 taskItem.qty;为 0 时从出库单明细取 执行数(workQty) 或 订单数量(anfme),避免手动完结未填 qty 导致不扣减
                        Double pickedQty = taskItem.getQty() != null && QuantityUtils.isPositive(taskItem.getQty())
                                ? taskItem.getQty()
                                : 0.0;
                        log.debug("[拣料入库] taskItemId={}, fieldsIndex={}, working.anfme={}, taskItem.qty={}, taskItem.orderItemId={}, pickedQty(初)={}",
                                taskItem.getId(), taskItem.getFieldsIndex(), working.getAnfme(), taskItem.getQty(), taskItem.getOrderItemId(), pickedQty);
                        if (pickedQty <= 0 && taskItem.getOrderItemId() != null) {
                            WkOrderItem orderItem = asnOrderItemService.getById(taskItem.getOrderItemId());
                            log.debug("[拣料入库] 查出库单明细 orderItemId={}, orderItem={}, workQty={}, anfme={}",
                                    taskItem.getOrderItemId(), orderItem != null ? "存在" : "null",
                                    orderItem != null ? orderItem.getWorkQty() : null, orderItem != null ? orderItem.getAnfme() : null);
                            if (orderItem != null) {
                                if (orderItem.getWorkQty() != null && QuantityUtils.isPositive(orderItem.getWorkQty())) {
                                    pickedQty = orderItem.getWorkQty();
                                } else if (orderItem.getAnfme() != null && QuantityUtils.isPositive(orderItem.getAnfme())) {
                                    pickedQty = orderItem.getAnfme();
                                }
                            }
                        }
                        Double minQty = QuantityUtils.subtract(working.getAnfme(), pickedQty);
                        log.debug("[拣料入库] 计算后 pickedQty={}, minQty(剩余)={}, 将更新 taskItem.anfme={}, taskItem.qty={}",
                                pickedQty, minQty, minQty, pickedQty);
                        if (QuantityUtils.isNonNegative(minQty)) {
                            taskItem.setAnfme(minQty);
                            taskItem.setQty(pickedQty);
                            if (!taskItemService.updateById(taskItem)) {
                                throw new CoolException("任务明细修改失败!!");
                            }
                        } else {
                            log.warn("[拣料入库] minQty<0 未更新 taskItem, taskItemId={}", taskItem.getId());
                        }
                    }
                });
            });
            log.debug("[拣料入库] 即将扣减库位 locId={}, locCode={}", loc.getId(), loc.getCode());
            subtractLocItemByTaskItems(loc, taskItems, SystemAuthUtils.getLoginUserId());
            // 料箱已全部用完则不生成拣货入库单,仅完结出库
            double totalRemaining = taskItems.stream().mapToDouble(ti -> ti.getAnfme() != null && ti.getAnfme().compareTo(0.0) > 0 ? ti.getAnfme() : 0.0).sum();
            if (totalRemaining <= 0.0) {
                task.setTaskType(TaskType.TASK_TYPE_PICK_AGAIN_OUT.type);
                task.setTaskStatus(TaskStsType.UPDATED_OUT.id);
                this.updateById(task);
                locItemWorkingService.remove(new LambdaQueryWrapper<LocItemWorking>().eq(LocItemWorking::getTaskId, task.getId()));
                return task;
            }
        }
        tempLocs.forEach(working -> {
            taskItems.forEach(taskItem -> {
                if (taskItem.getFieldsIndex().equals(working.getFieldsIndex())) {
                if (Objects.equals(taskItem.getFieldsIndex(), working.getFieldsIndex())) {
                    Double minQty = taskItem.getAnfme();
                    if (!task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_IN.type)) {
                        minQty = Math.round((working.getAnfme() - taskItem.getQty()) * 1000000) / 1000000.0;
                        // 计算剩余数量
                        minQty = QuantityUtils.subtract(working.getAnfme(), taskItem.getQty());
                    }
                    if (minQty.compareTo(0.0) >= 0) {
                    if (QuantityUtils.isNonNegative(minQty)) {
                        // 更新TaskItem的剩余数量
                        taskItem.setAnfme(minQty);
                        if (!taskItemService.updateById(taskItem)) {
                            throw new CoolException("任务明细修改失败!!");
                        }
                        // 更新LocItemWorking的剩余数量(非盘点入库时需要更新);仅持久化记录才写库
                        if (!task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_IN.type)) {
                            working.setAnfme(minQty);
                            if (working.getId() != null && !locItemWorkingService.updateById(working)) {
                                throw new CoolException("作业库存数量更新失败!!");
                            }
                        }
                    } else {
                        // 剩余数量小于0,删除任务明细
                        if (!taskItemService.removeById(taskItem)) {
                            log.error("任务明细修改失败!!");
                        }
@@ -1064,10 +1424,13 @@
                }
            });
        });
        List<String> matnrIds = taskItems.stream().map(TaskItem::getFieldsIndex).collect(Collectors.toList());
        List<String> matnrIds = taskItems.stream()
                .map(TaskItem::getFieldsIndex)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
        //删除与任务明细重复的库存信息,以任务明细为准
        List<LocItemWorking> itemWorkings = tempLocs.stream()
                .filter(working -> !matnrIds.contains(working.getFieldsIndex()))
                .filter(working -> working.getFieldsIndex() != null && !matnrIds.contains(working.getFieldsIndex()))
                .collect(Collectors.toList());
        itemWorkings.forEach(working -> {
            TaskItem taskItem = taskItems.stream().findFirst().get();
@@ -1103,6 +1466,10 @@
                    .setQty(0.0)
                    .setLocId(loc1.getId())
                    .setLocCode(loc1.getCode());
            // 拣料再入库:目标库位数量应为回库的剩余数量(taskItem.anfme),即原库位减掉本次拣出后的数量(如 1000 出库 10 → 990)
            if (TaskType.TASK_TYPE_PICK_IN.type.equals(task.getTaskType()) && taskItem.getAnfme() != null && taskItem.getAnfme().compareTo(0.0) > 0) {
                itemWorking.setAnfme(taskItem.getAnfme());
            }
            workings.add(itemWorking);
        });
@@ -1117,6 +1484,137 @@
//            throw new CoolException("库位预约入库失败!!");
//        }
        return task;
    }
    /**
     * 同箱码下多条 200 拣料出库一次性处理:按相同物料合计扣减库位、更新出库单/库存明细、生成一张拣料入库单(有余量时)、更新库位状态
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void processPickOutBarcodeAll200(List<Task> all200Tasks) {
        if (all200Tasks == null || all200Tasks.isEmpty()) {
            return;
        }
        Task first = all200Tasks.get(0);
        if (!TaskType.TASK_TYPE_PICK_AGAIN_OUT.type.equals(first.getTaskType()) || !TaskStsType.UPDATED_OUT.id.equals(first.getTaskStatus())) {
            throw new CoolException("非拣料出库200任务,不可批量处理");
        }
        List<Long> taskIds = all200Tasks.stream().map(Task::getId).collect(Collectors.toList());
        List<TaskItem> allItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>().in(TaskItem::getTaskId, taskIds));
        if (allItems.isEmpty()) {
            throw new CoolException("任务明细为空");
        }
        Long loginUserId = SystemAuthUtils.getLoginUserId();
        if (loginUserId == null) {
            loginUserId = 1L;
        }
        String orgLoc = first.getOrgLoc();
        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getCode, orgLoc));
        if (loc == null) {
            throw new CoolException("库位不存在:" + orgLoc);
        }
        // 按物料+批次+票号汇总已拣数量
        Map<String, List<TaskItem>> byKey = allItems.stream().collect(Collectors.groupingBy(ti ->
                (ti.getMatnrId() != null ? ti.getMatnrId() : "") + "_" + (ti.getBatch() != null ? ti.getBatch() : "") + "_" + (ti.getFieldsIndex() != null ? ti.getFieldsIndex() : "")));
        List<TaskItem> aggregatedForDeduct = new ArrayList<>();
        Map<String, Double> remainderByKey = new LinkedHashMap<>();
        for (Map.Entry<String, List<TaskItem>> e : byKey.entrySet()) {
            List<TaskItem> group = e.getValue();
            double totalQty = group.stream().mapToDouble(ti -> ti.getQty() != null && ti.getQty() > 0 ? ti.getQty() : (ti.getAnfme() != null ? ti.getAnfme() : 0)).sum();
            TaskItem rep = group.get(0);
            TaskItem forDeduct = new TaskItem();
            forDeduct.setMatnrId(rep.getMatnrId()).setBatch(rep.getBatch()).setFieldsIndex(rep.getFieldsIndex()).setQty(totalQty);
            aggregatedForDeduct.add(forDeduct);
            LambdaQueryWrapper<LocItem> qw = new LambdaQueryWrapper<LocItem>().eq(LocItem::getLocId, loc.getId()).eq(LocItem::getMatnrId, rep.getMatnrId());
            if (StringUtils.isNotBlank(rep.getBatch())) qw.eq(LocItem::getBatch, rep.getBatch());
            else qw.and(w -> w.isNull(LocItem::getBatch).or().eq(LocItem::getBatch, ""));
            if (StringUtils.isNotBlank(rep.getFieldsIndex())) qw.eq(LocItem::getFieldsIndex, rep.getFieldsIndex());
            else qw.and(w -> w.isNull(LocItem::getFieldsIndex).or().eq(LocItem::getFieldsIndex, ""));
            LocItem li = locItemService.getOne(qw);
            double remainder = (li != null && li.getAnfme() != null ? li.getAnfme() : 0) - totalQty;
            if (remainder > 0) {
                remainderByKey.put(e.getKey(), remainder);
            }
        }
        subtractLocItemByTaskItems(loc, aggregatedForDeduct, loginUserId);
        // 按 source 分组更新出库单并写库存流水
        Map<Long, List<TaskItem>> bySource = allItems.stream().collect(Collectors.groupingBy(TaskItem::getSource));
        for (Map.Entry<Long, List<TaskItem>> e : bySource.entrySet()) {
            Long key = e.getKey();
            List<TaskItem> items = e.getValue();
            if (first.getResource() != null && first.getResource().equals(TaskResouceType.TASK_RESOUCE_WAVE_TYPE.val)) {
                WaveItem waveItem = waveItemService.getById(key);
                if (waveItem != null) {
                    try {
                        saveOutStockItem(items, null, waveItem, null, loginUserId);
                    } catch (Exception ex) {
                        throw new CoolException(ex.getMessage());
                    }
                }
            } else if (first.getResource() != null && first.getResource().equals(TaskResouceType.TASK_RESOUCE_ORDER_TYPE.val)) {
                WkOrderItem orderItem = asnOrderItemService.getById(key);
                if (orderItem != null) {
                    try {
                        saveOutStockItem(items, orderItem, null, null, loginUserId);
                    } catch (Exception ex) {
                        throw new CoolException(ex.getMessage());
                    }
                }
            }
        }
        // 有余量则生成一张拣料入库单
        if (!remainderByKey.isEmpty()) {
            Task pickInTask = new Task();
            pickInTask.setTaskCode(SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_TASK_CODE, first));
            pickInTask.setTaskType(TaskType.TASK_TYPE_PICK_IN.type);
            pickInTask.setTaskStatus(TaskStsType.GENERATE_IN.id);
            pickInTask.setBarcode(first.getBarcode());
            pickInTask.setOrgLoc(orgLoc);
            pickInTask.setTargLoc(orgLoc);
            pickInTask.setOrgSite(first.getTargSite());
            pickInTask.setTargSite(first.getTargSite());
            pickInTask.setResource(first.getResource());
            if (!this.save(pickInTask)) {
                throw new CoolException("拣料入库任务创建失败");
            }
            List<LocItemWorking> workings = new ArrayList<>();
            for (Map.Entry<String, Double> re : remainderByKey.entrySet()) {
                String k = re.getKey();
                Double rem = re.getValue();
                if (rem == null || rem <= 0) continue;
                List<TaskItem> group = byKey.get(k);
                if (group == null || group.isEmpty()) continue;
                TaskItem rep = group.get(0);
                TaskItem ti = new TaskItem();
                ti.setTaskId(pickInTask.getId());
                ti.setMatnrId(rep.getMatnrId()).setMaktx(rep.getMaktx()).setMatnrCode(rep.getMatnrCode());
                ti.setBatch(rep.getBatch()).setFieldsIndex(rep.getFieldsIndex()).setUnit(rep.getUnit()).setSpec(rep.getSpec()).setModel(rep.getModel());
                ti.setAnfme(rem).setQty(0.0);
                taskItemService.save(ti);
                LocItemWorking w = new LocItemWorking();
                w.setTaskId(pickInTask.getId());
                w.setLocId(loc.getId());
                w.setLocCode(loc.getCode());
                w.setMatnrId(rep.getMatnrId()).setMaktx(rep.getMaktx()).setMatnrCode(rep.getMatnrCode());
                w.setBatch(rep.getBatch()).setFieldsIndex(rep.getFieldsIndex()).setUnit(rep.getUnit());
                w.setAnfme(rem);
                workings.add(w);
            }
            if (!workings.isEmpty()) {
                locItemWorkingService.saveBatch(workings);
            }
            loc.setUseStatus(LocStsType.LOC_STS_TYPE_S.type);
            locService.updateById(loc);
        } else {
            loc.setUseStatus(LocStsType.LOC_STS_TYPE_O.type);
            loc.setBarcode(null);
            loc.setUpdateBy(loginUserId);
            loc.setUpdateTime(new Date());
            locService.updateById(loc);
        }
        for (Long tid : taskIds) {
            locItemWorkingService.remove(new LambdaQueryWrapper<LocItemWorking>().eq(LocItemWorking::getTaskId, tid));
        }
    }
    /**
@@ -1135,8 +1633,33 @@
        if (Objects.isNull(loc)) {
            throw new CoolException("库位不存在!!");
        }
        if (!loc.getUseStatus().equals(LocStsType.LOC_STS_TYPE_R.type)) {
            throw new CoolException("库位状态不处理于R.出库预约!!");
        // 空板出库:无任务明细,不需要 PDA 拣货确认,RCS 回调后直接完成库位更新并置为 UPDATED_OUT
        if (task.getTaskType().equals(TaskType.TASK_TYPE_EMPITY_OUT.type)) {
            List<TaskItem> emptyItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>().eq(TaskItem::getTaskId, task.getId()));
            if (emptyItems.isEmpty()) {
                if (!LocStsType.LOC_STS_TYPE_R.type.equals(loc.getUseStatus())) {
                    log.warn("空板出库任务{}的库位{}状态不是R.出库预约,跳过", task.getId(), loc.getCode());
                    return;
                }
                if (!locService.update(new LambdaUpdateWrapper<Loc>()
                        .eq(Loc::getId, loc.getId())
                        .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_O.type)
                        .set(Loc::getBarcode, null)
                        .set(Loc::getUpdateBy, loginUserId)
                        .set(Loc::getUpdateTime, new Date()))) {
                    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.UPDATED_OUT.id))) {
                    throw new CoolException("空板出库任务状态更新失败!!");
                }
                log.info("[空板出库] 任务{} RCS回调后已直接完成库位更新,无需PDA确认", task.getTaskCode());
                return;
            }
        }
        List<TaskItem> taskItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>().eq(TaskItem::getTaskId, task.getId()));
@@ -1145,32 +1668,60 @@
        }
        List<LocItem> locItems = locItemService.list(new LambdaQueryWrapper<LocItem>().eq(LocItem::getLocId, loc.getId()));
        if (locItems.isEmpty()) {
            throw new CoolException("库位明细不存在!!");
        // 如果库位状态不是R,检查是否已经处理过
        if (!loc.getUseStatus().equals(LocStsType.LOC_STS_TYPE_R.type)) {
            // 如果库位明细为空,说明已经处理过了,直接更新任务状态为199
            if (locItems.isEmpty()) {
                logger.warn("任务{}的库位{}状态为{},但库位明细为空,可能已经处理过,直接更新任务状态为199",
                        task.getId(), loc.getCode(), loc.getUseStatus());
                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("任务状态更新失败!!");
                }
                return; // 跳过后续处理
            } else {
                // 库位明细不为空但状态不是R,跳过处理
                logger.error("任务{}的库位{}状态为{},不是R.出库预约状态,但库位明细不为空,跳过处理。任务编码:{},库位编码:{}",
                        task.getId(), loc.getCode(), loc.getUseStatus(), task.getTaskCode(), loc.getCode());
                return;
            }
        }
        // 如果库位明细为空,可能是已经被处理过了,允许继续执行
        if (!locItems.isEmpty()) {
            List<LocItemWorking> workings = new ArrayList<>();
            for (LocItem item : locItems) {
                LocItemWorking working = new LocItemWorking();
                BeanUtils.copyProperties(item, working);
                working.setId(null)
                        .setTaskId(task.getId())
                        .setLocItemId(item.getId())
                        .setUpdateBy(loginUserId)
                        .setUpdateTime(new Date());
                workings.add(working);
            }
        List<LocItemWorking> workings = new ArrayList<>();
        for (LocItem item : locItems) {
            LocItemWorking working = new LocItemWorking();
            BeanUtils.copyProperties(item, working);
            working.setId(null)
                    .setTaskId(task.getId())
                    .setLocItemId(item.getId())
                    .setUpdateBy(loginUserId)
                    .setUpdateTime(new Date());
            workings.add(working);
        }
            if (!workings.isEmpty() && !locItemWorkingService.saveBatch(workings)) {
                throw new CoolException("临时库存保存失败!!");
            }
        if (!locItemWorkingService.saveBatch(workings)) {
            throw new CoolException("临时库存保存失败!!");
        }
        try {
            //更新库位明细
            subtractLocItem(loc);
        } catch (Exception e) {
            logger.error("<UNK>", e);
            throw new CoolException(e.getMessage());
            try {
                // 根据任务类型更新库位明细
                if (task.getTaskType().equals(TaskType.TASK_TYPE_OUT.type)) {
                    // 全版出库:不删除库位明细,等待PDA快速拣货确认时再删除
                    // subtractLocItem(loc); // 已移除,改为在completeFullOutStock中删除
                } else if (!TaskType.TASK_TYPE_PICK_AGAIN_OUT.type.equals(task.getTaskType())) {
                    // 部分出库(如盘点出库):根据TaskItem数量扣减库位明细;拣料出库在生成拣料入库单时扣减
                    subtractLocItemByTaskItems(loc, taskItems, loginUserId);
                }
            } catch (Exception e) {
                logger.error("<UNK>", e);
                throw new CoolException(e.getMessage());
            }
        }
        //添加出入库记录信息
@@ -1188,11 +1739,11 @@
                    if (Objects.isNull(waveItem)) {
                        throw new CoolException("波次明细不存在!!");
                    }
//                    try {
//                        saveOutStockItem(maps.get(key), null, waveItem, null, loginUserId);
//                    } catch (Exception e) {
//                        throw new CoolException(e.getMessage());
//                    }
                    try {
                        saveOutStockItem(maps.get(key), null, waveItem, null, loginUserId);
                    } catch (Exception e) {
                        throw new CoolException(e.getMessage());
                    }
                } else if (task.getResource().equals(TaskResouceType.TASK_RESOUCE_ORDER_TYPE.val)) {
                    WkOrderItem orderItem = asnOrderItemService.getById(key);
                    if (Objects.isNull(orderItem)) {
@@ -1224,16 +1775,14 @@
                throw new CoolException(e.getMessage());
            }
        }
        // 根据任务类型更新库位状态
        if (task.getTaskType().equals(TaskType.TASK_TYPE_PICK_AGAIN_OUT.type) || task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_OUT.type)) {
            /**修改为库位状态为S.预约入库,保留原有库位*/
            if (!locService.update(new LambdaUpdateWrapper<Loc>()
                    .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_S.type)
                    .set(Loc::getBarcode, null)
                    .set(Loc::getUpdateBy, loginUserId)
                    .set(Loc::getUpdateTime, new Date())
                    .eq(Loc::getId, loc.getId()))) {
                throw new CoolException("库位状态修改失败!!");
            }
            // 拣料出库/盘点出库:在未生成拣料入库单之前保持 R.预约出库,否则下发任务时查不到该库位(只查 F+R)导致“库存不足”
            // 等 PDA 确认并生成拣料入库任务时,再在 pickOrCheckTask 中将目标库位改为 S.预约入库
        } else if (task.getTaskType().equals(TaskType.TASK_TYPE_OUT.type)) {
            // 全版出库:不更新库位状态为O,等待PDA快速拣货确认时再更新
            // 库位状态保持原样(R.出库预约状态)
        } else {
            /**修改为库位状态为O.空库*/
            if (!locService.update(new LambdaUpdateWrapper<Loc>()
@@ -1253,6 +1802,8 @@
                .set(Task::getTaskStatus, TaskStsType.WAVE_SEED.id))) {
            throw new CoolException("库存状态更新失败!!");
        }
        // 9.1 入/出库结果上报:出库完成后通知云仓
        reportInOutResultToCloud(task, loc, taskItems, null, false);
//        if (task.getTaskType().equals(TaskType.TASK_TYPE_PICK_AGAIN_OUT.type) || task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_OUT.type)) {
//            if (!this.update(new LambdaUpdateWrapper<Task>()
@@ -1313,8 +1864,35 @@
                .setUpdateTime(new Date())
                .setAnfme(sum);
        if (!Objects.isNull(waveItem)) {
            //TODO 生成波次时需要将波次号写入单据,通过物料,批次,动态字段等唯一值反查单据信息
            // 波次出库:更新出库单完成数量及出库单明细执行数量
            stock.setSourceId(waveItem.getId()).setType(OrderType.ORDER_OUT.type);
            Long orderId = waveItem.getOrderId();
            if (orderId != null) {
                WkOrder wkOrder = asnOrderService.getById(orderId);
                if (wkOrder != null) {
                    Double curQty = wkOrder.getQty() != null ? wkOrder.getQty() : 0.0;
                    Double newQty = QuantityUtils.roundToScale(curQty + sum);
                    wkOrder.setExceStatus(AsnExceStatus.OUT_STOCK_STATUS_TASK_WORKING.val).setQty(newQty);
                    if (!asnOrderService.updateById(wkOrder)) {
                        throw new CoolException("出库单完成数量更新失败!!");
                    }
                }
                // 按出库单明细汇总本次任务数量,更新各明细 workQty
                Map<Long, Double> sumByOrderItemId = taskItems.stream()
                        .filter(ti -> ti.getOrderItemId() != null)
                        .collect(Collectors.groupingBy(TaskItem::getOrderItemId,
                                Collectors.summingDouble(ti -> ti.getAnfme() != null ? ti.getAnfme() : 0.0)));
                for (Map.Entry<Long, Double> e : sumByOrderItemId.entrySet()) {
                    WkOrderItem oi = outStockItemService.getById(e.getKey());
                    if (oi != null) {
                        Double wq = oi.getWorkQty() != null ? oi.getWorkQty() : 0.0;
                        oi.setWorkQty(QuantityUtils.roundToScale(wq + e.getValue()));
                        if (!outStockItemService.updateById(oi)) {
                            throw new CoolException("出库单明细执行数量更新失败!!");
                        }
                    }
                }
            }
        } else if (!Objects.isNull(orderItem) && StringUtils.isNotBlank(orderItem.getId() + "")) {
            WkOrder wkOrder = asnOrderService.getById(orderItem.getOrderId());
            Double qty = Math.round((wkOrder.getQty() + sum) * 1000000) / 1000000.0;
@@ -1397,20 +1975,64 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void pubTaskToWcs(List<Task> tasks) {
        WcsTaskParams taskParams = new WcsTaskParams();
        List<TaskItemParam> items = new ArrayList<>();
        tasks.forEach(task -> {
        /**任务下发接口*/
        // 检查 RCS API 配置是否有效
        if (rcsApi == null || StringUtils.isBlank(rcsApi.getHost()) || StringUtils.isBlank(rcsApi.getPort())) {
            log.error("========== RCS任务下发失败 ==========");
            log.error("RCS API 配置无效!host: {}, port: {}",
                    rcsApi != null ? rcsApi.getHost() : "null",
                    rcsApi != null ? rcsApi.getPort() : "null");
            return;
        }
        // 同料箱号规则:101 任务所在料箱号下若已存在 196/198/199/200 任务,则不向 RCS 发送该 101 任务(/api/open/bus/submit)
        List<Integer> higherStatuses = Arrays.asList(TaskStsType.AWAIT.id, TaskStsType.COMPLETE_OUT.id, TaskStsType.WAVE_SEED.id, TaskStsType.UPDATED_OUT.id);
        List<Task> higherTasks = this.list(new LambdaQueryWrapper<Task>()
                .in(Task::getTaskStatus, higherStatuses)
                .isNotNull(Task::getBarcode)
                .ne(Task::getBarcode, ""));
        Set<String> barcodesWithHigher = higherTasks.stream().map(Task::getBarcode).filter(StringUtils::isNotBlank).collect(Collectors.toSet());
        List<Task> toSend = tasks.stream()
                .filter(t -> {
                    if (TaskStsType.GENERATE_OUT.id.equals(t.getTaskStatus()) && StringUtils.isNotBlank(t.getBarcode()) && barcodesWithHigher.contains(t.getBarcode())) {
                        log.debug("同料箱号{}下已存在196/198/199/200任务,跳过101任务下发:taskId={}", t.getBarcode(), t.getId());
                        return false;
                    }
                    return true;
                })
                .collect(Collectors.toList());
        if (toSend.isEmpty()) {
            log.debug("过滤后无待下发任务");
            return;
        }
        tasks = toSend;
        String pubTakUrl = rcsApi.getHost() + ":" + rcsApi.getPort() + RcsConstant.pubTask;
        for (Task task : tasks) {
            WcsTaskParams taskParams = new WcsTaskParams();
            List<TaskItemParam> items = new ArrayList<>();
            // 设置批次编号(使用当前任务的任务编码)
            String batchNo = task.getTaskCode();
            if (StringUtils.isBlank(batchNo)) {
                // 如果任务编号为空,生成一个默认值
                batchNo = "BATCH_" + System.currentTimeMillis();
            }
            taskParams.setBatchNo(batchNo);
            log.info("任务批次编号:{}", batchNo);
            // 构建当前任务的参数
            TaskItemParam itemParam = new TaskItemParam();
            //任务类型,任务编码
            itemParam.setTaskType(RcsTaskType.getTypeDesc(task.getTaskType()))
                    .setSeqNum(task.getTaskCode());
            //主参数
            taskParams.setBatch(task.getBarcode());
            //任务编码(对应seqNum)
            itemParam.setTaskNo(task.getTaskCode());
            itemParam.setPriority(1);
            BasStation station = null;
            if (!task.getTaskType().equals(TaskType.TASK_TYPE_LOC_MOVE.type)) {
                station = basStationService.getOne(new LambdaQueryWrapper<BasStation>().eq(BasStation::getStationName, task.getTargSite()));
                if (Objects.isNull(station)) {
                    throw new CoolException("站点不存在!!");
                    log.error("========== RCS任务下发失败 ==========");
                    log.error("站点不存在!!任务编码:{},目标站点:{}", task.getTaskCode(), task.getTargSite());
                    continue;
                }
            }
@@ -1419,12 +2041,16 @@
                if (task.getTaskType() <= TaskType.TASK_TYPE_CHECK_IN.type && !task.getTaskType().equals(TaskType.TASK_TYPE_LOC_MOVE.type)) {
                    station.setUseStatus(LocStsType.LOC_STS_TYPE_R.type);
                    if (!basStationService.updateById(station)) {
                        throw new CoolException("站点状态更新失败!!");
                        log.error("========== RCS任务下发失败 ==========");
                        log.error("站点状态更新失败!!任务编码:{},站点:{}", task.getTaskCode(), station.getStationName());
                        continue;
                    }
                } else if (task.getTaskType() >= TaskType.TASK_TYPE_OUT.type) {
                    station.setUseStatus(LocStsType.LOC_STS_TYPE_S.type);
                    if (!basStationService.updateById(station)) {
                        throw new CoolException("站点状态更新失败!!");
                        log.error("========== RCS任务下发失败 ==========");
                        log.error("站点状态更新失败!!任务编码:{},站点:{}", task.getTaskCode(), station.getStationName());
                        continue;
                    }
                }
            }
@@ -1447,52 +2073,172 @@
                    || task.getTaskType().equals(TaskType.TASK_TYPE_EMPITY_OUT.type)
                    || task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_OUT.type)
                    || task.getTaskType().equals(TaskType.TASK_TYPE_MERGE_OUT.type)) {
                /**出库参数*/
                itemParam.setOriLoc(task.getOrgLoc())
                        .setDestSta(task.getTargSite());
                /**出库参数 - 需要根据任务明细中的多个库位进行组装*/
                // 查询任务明细
                List<TaskItem> taskItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>().eq(TaskItem::getTaskId, task.getId()));
                if (!taskItems.isEmpty()) {
                    // 通过TaskItem的source字段查询LocItem,获取库位信息
                    Set<String> locCodes = new HashSet<>();
                    Map<String, LocItem> locItemMap = new HashMap<>();
                    for (TaskItem taskItem : taskItems) {
                        if (taskItem.getSource() != null) {
                            // source字段对应LocItem的id
                            LocItem locItem = locItemService.getById(taskItem.getSource());
                            if (locItem != null && StringUtils.isNotBlank(locItem.getLocCode())) {
                                locCodes.add(locItem.getLocCode());
                                locItemMap.put(locItem.getLocCode(), locItem);
                            }
                        }
                    }
                    // 如果通过source没有找到库位,使用Task的orgLoc作为默认值
                    if (locCodes.isEmpty() && StringUtils.isNotBlank(task.getOrgLoc())) {
                        locCodes.add(task.getOrgLoc());
                    }
                    // 为每个不同的库位创建一个TaskItemParam
                    for (String locCode : locCodes) {
                        TaskItemParam outItemParam = new TaskItemParam();
                        String taskNo = locCodes.size() > 1
                                ? task.getTaskCode() + "_" + locCode
                                : task.getTaskCode();
                        outItemParam.setTaskNo(taskNo);
                        outItemParam.setPriority(1);
                        outItemParam.setOriLoc(locCode);
                        outItemParam.setDestSta(task.getTargSite());
                        if (task.getBarcode() != null) {
                            outItemParam.setZpallet(task.getBarcode());
                        }
                        items.add(outItemParam);
                    }
                    log.info("出库任务包含{}个库位:{}", locCodes.size(), locCodes);
                } else {
                    // 如果没有任务明细,使用Task的orgLoc
                    itemParam.setOriLoc(task.getOrgLoc())
                            .setDestSta(task.getTargSite());
                    items.add(itemParam);
                }
            } else {
                /**站点间移库参数*/
                itemParam.setOriSta(task.getOrgSite()).setDestSta(task.getTargSite());
                BasStation curSta = basStationService.getOne(new LambdaQueryWrapper<BasStation>().eq(BasStation::getStationName, task.getOrgSite()));
                if (Objects.isNull(curSta)) {
                    throw new CoolException("站点不存在!!");
                    log.error("========== RCS任务下发失败 ==========");
                    log.error("站点不存在!!任务编码:{},源站点:{}", task.getTaskCode(), task.getOrgSite());
                    continue;
                }
                if (curSta.getType().equals(StationTypeEnum.STATION_TYPE_NORMAL.type)) {
                    if (!curSta.getUseStatus().equals(LocStsType.LOC_STS_TYPE_F.type)) {
                        throw new CoolException("当前站点不是F.在库状态!!");
                        log.error("========== RCS任务下发失败 ==========");
                        log.error("当前站点不是F.在库状态!!任务编码:{},站点:{},当前状态:{}", task.getTaskCode(), curSta.getStationName(), curSta.getUseStatus());
                        continue;
                    }
                }
                if (station.getType().equals(StationTypeEnum.STATION_TYPE_NORMAL.type)) {
                    if (!station.getUseStatus().equals(LocStsType.LOC_STS_TYPE_O.type)) {
                        throw new CoolException("目标站点不是O.空闲状态!!");
                // 站点间移库需要获取目标站点
                BasStation targetStation = basStationService.getOne(new LambdaQueryWrapper<BasStation>().eq(BasStation::getStationName, task.getTargSite()));
                if (Objects.isNull(targetStation)) {
                    log.error("========== RCS任务下发失败 ==========");
                    log.error("目标站点不存在!!任务编码:{},目标站点:{}", task.getTaskCode(), task.getTargSite());
                    continue;
                }
                if (targetStation.getType().equals(StationTypeEnum.STATION_TYPE_NORMAL.type)) {
                    if (!targetStation.getUseStatus().equals(LocStsType.LOC_STS_TYPE_O.type)) {
                        log.error("========== RCS任务下发失败 ==========");
                        log.error("目标站点不是O.空闲状态!!任务编码:{},站点:{},当前状态:{}", task.getTaskCode(), targetStation.getStationName(), targetStation.getUseStatus());
                        continue;
                    }
                }
            }
            items.add(itemParam);
        });
        taskParams.setTaskList(items);
        /**任务下发接口*/
        String pubTakUrl = rcsApi.getHost() + ":" + rcsApi.getPort() + RcsConstant.pubTask;
        /**RCS基础配置链接*/
        log.info("任务下发,请求地址: {}, 请求参数: {}", pubTakUrl, JSONObject.toJSONString(taskParams));
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
        headers.add("api-version", "v2.0");
        HttpEntity httpEntity = new HttpEntity(taskParams, headers);
        ResponseEntity<String> exchange = restTemplate.exchange(pubTakUrl, HttpMethod.POST, httpEntity, String.class);
        log.info("任务下发后,响应结果: {}", exchange);
        if (Objects.isNull(exchange.getBody())) {
            throw new CoolException("任务下发失败!!");
        } else {
            try {
                ObjectMapper objectMapper = new ObjectMapper();
                objectMapper.coercionConfigDefaults()
                        .setCoercion(CoercionInputShape.EmptyString, CoercionAction.AsEmpty);
                CommonResponse result = objectMapper.readValue(exchange.getBody(), CommonResponse.class);
                if (result.getCode() == 200) {
                    tasks.forEach(task -> {
            // 对于非出库任务,添加单个itemParam
            if (!(task.getTaskType().equals(TaskType.TASK_TYPE_OUT.type)
                    || task.getTaskType().equals(TaskType.TASK_TYPE_PICK_AGAIN_OUT.type)
                    || task.getTaskType().equals(TaskType.TASK_TYPE_EMPITY_OUT.type)
                    || task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_OUT.type)
                    || task.getTaskType().equals(TaskType.TASK_TYPE_MERGE_OUT.type))) {
                items.add(itemParam);
            }
            taskParams.setTasks(items);
            // 记录当前任务信息
            log.info("  RCS- 任务编码:{},任务类型:{},源库位:{},目标库位:{},源站点:{},目标站点:{}",
                    task.getTaskCode(), task.getTaskType(), task.getOrgLoc(),
                    task.getTargLoc(), task.getOrgSite(), task.getTargSite());
            log.info("RCS-请求参数:{}", JSONObject.toJSONString(taskParams));
            // 发送当前任务
            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Type", "application/json");
            headers.add("api-version", "v2.0");
            HttpEntity httpEntity = new HttpEntity(taskParams, headers);
            /**RCS基础配置链接*/
            log.info("========== 开始下发任务到RCS ==========");
            log.info("请求RCS-地址:{}", pubTakUrl);
            log.info("请求RCS-参数:{}", JSONObject.toJSONString(taskParams));
            long startTime = System.currentTimeMillis();
            ResponseEntity<String> exchange = null;
            try {
                exchange = restTemplate.exchange(pubTakUrl, HttpMethod.POST, httpEntity, String.class);
                long endTime = System.currentTimeMillis();
                log.info("RCS响应耗时:{}ms", (endTime - startTime));
                log.info("RCS响应状态码:{}", exchange.getStatusCode());
                log.info("RCS响应头:{}", exchange.getHeaders());
                log.info("RCS响应体:{}", exchange.getBody());
            } catch (org.springframework.web.client.ResourceAccessException e) {
                long endTime = System.currentTimeMillis();
                log.error("========== RCS任务下发资源访问异常 ==========");
                log.error("请求RCS-资源访问异常,耗时:{}ms,任务编码:{}", (endTime - startTime), task.getTaskCode(), e);
                log.error("请求RCS-请求地址:{}", pubTakUrl);
                log.error("请求RCS-请求参数:{}", JSONObject.toJSONString(taskParams));
                // 检查是否是连接超时异常
                Throwable cause = e.getCause();
                String errorMsg = e.getMessage();
                if (cause instanceof java.net.SocketTimeoutException ||
                    (cause instanceof java.net.ConnectException && cause.getMessage() != null && cause.getMessage().contains("timed out")) ||
                    (errorMsg != null && (errorMsg.contains("Connection timed out") || errorMsg.contains("timed out") || errorMsg.contains("timeout")))) {
                    log.error("RCS连接超时,任务下发失败!任务编码:{},错误信息:{}", task.getTaskCode(), errorMsg);
                } else {
                    log.error("RCS资源访问异常,任务下发失败!任务编码:{},错误信息:{}", task.getTaskCode(), errorMsg);
                }
                continue;
            } catch (Exception e) {
                long endTime = System.currentTimeMillis();
                log.error("========== RCS任务下发异常 ==========");
                String errorMsg = e.getMessage();
                // 检查是否是连接超时相关的异常
                if (errorMsg != null && (errorMsg.contains("Connection timed out") || errorMsg.contains("timed out") || errorMsg.contains("timeout"))) {
                log.error("请求RCS-连接超时异常,耗时:{}ms,任务编码:{},错误信息:{}-{}", (endTime - startTime), task.getTaskCode(), e, errorMsg);
                } else {
                log.error("请求RCS-异常,耗时:{}ms,任务编码:{},错误信息:{}-{}", (endTime - startTime), task.getTaskCode(), e, errorMsg);
                }
                log.error("请求RCS-地址:{}", pubTakUrl);
                log.error("请求RCS-参数:{}", JSONObject.toJSONString(taskParams));
                continue;
            }
            if (Objects.isNull(exchange) || Objects.isNull(exchange.getBody())) {
                log.error("========== RCS任务下发失败 ==========");
                log.error("请求RCS-RCS响应体为空,无法解析响应结果");
                log.error("请求RCS-地址:{}", pubTakUrl);
                log.error("请求RCS-参数:{}", JSONObject.toJSONString(taskParams));
                log.error("请求RCS-失败的任务编码:{}", task.getTaskCode());
                log.error("任务下发失败,RCS响应体为空!!任务编码:{}", task.getTaskCode());
                continue;
            } else {
                try {
                    ObjectMapper objectMapper = new ObjectMapper();
                    objectMapper.coercionConfigDefaults()
                            .setCoercion(CoercionInputShape.EmptyString, CoercionAction.AsEmpty);
                    CommonResponse result = objectMapper.readValue(exchange.getBody(), CommonResponse.class);
                    log.info("RCS响应解析结果 - code:{},msg:{},data:{}",
                            result.getCode(), result.getMsg(), result.getData());
                    if (result.getCode() == 200) {
                        log.info("RCS任务下发成功,开始更新任务状态 - 任务编码:{}", task.getTaskCode());
                        if (task.getTaskType().equals(TaskType.TASK_TYPE_IN.type)
                                || task.getTaskType().equals(TaskType.TASK_TYPE_PICK_IN.type)
                                || task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_IN.type)
@@ -1502,20 +2248,28 @@
                            BasStation curSta = basStationService.getOne(new LambdaQueryWrapper<BasStation>().eq(BasStation::getStationName, task.getOrgSite()));
                            if (Objects.isNull(curSta)) {
                                throw new CoolException("站点不存在!!");
                                log.error("========== RCS任务下发失败 ==========");
                                log.error("站点不存在!!任务编码:{},源站点:{}", task.getTaskCode(), task.getOrgSite());
                                continue;
                            }
                            log.info("更新入库任务状态 - 任务编码:{},新状态:{}", task.getTaskCode(), TaskStsType.WCS_EXECUTE_IN.id);
                            if (!taskService.update(new LambdaUpdateWrapper<Task>().eq(Task::getTaskCode, task.getTaskCode())
                                    .set(Task::getTaskStatus, TaskStsType.WCS_EXECUTE_IN.id))) {
                                throw new CoolException("任务状态修改失败!!");
                                log.error("========== RCS任务下发失败 ==========");
                                log.error("任务状态修改失败!!任务编码:{}", task.getTaskCode());
                                continue;
                            }
                            log.info("入库任务状态更新成功 - 任务编码:{}", task.getTaskCode());
                            /**排除移库功能*/
                            if (!task.getTaskType().equals(TaskType.TASK_TYPE_LOC_MOVE.type)) {
                                /**如果是普通站点,修改站点状态为出库预约*/
                                if (curSta.getType().equals(StationTypeEnum.STATION_TYPE_NORMAL.type)) {
                                    curSta.setUseStatus(LocStsType.LOC_STS_TYPE_R.type);
                                    if (!basStationService.updateById(curSta)) {
                                        throw new CoolException("站点预约失败!!");
                                        log.error("========== RCS任务下发失败 ==========");
                                        log.error("站点预约失败!!任务编码:{},站点:{}", task.getTaskCode(), curSta.getStationName());
                                        continue;
                                    }
                                }
                            }
@@ -1526,44 +2280,122 @@
                                || task.getTaskType().equals(TaskType.TASK_TYPE_PICK_AGAIN_OUT.type)) {
                            BasStation curSta = basStationService.getOne(new LambdaQueryWrapper<BasStation>().eq(BasStation::getStationName, task.getTargSite()));
                            if (Objects.isNull(curSta)) {
                                throw new CoolException("站点不存在!!");
                                log.error("========== RCS任务下发失败 ==========");
                                log.error("站点不存在!!任务编码:{},目标站点:{}", task.getTaskCode(), task.getTargSite());
                                continue;
                            }
                            log.info("更新出库任务状态 - 任务编码:{},新状态:{}", task.getTaskCode(), TaskStsType.WCS_EXECUTE_OUT.id);
                            if (!taskService.update(new LambdaUpdateWrapper<Task>().eq(Task::getTaskCode, task.getTaskCode())
                                    .set(Task::getTaskStatus, TaskStsType.WCS_EXECUTE_OUT.id))) {
                                throw new CoolException("任务状态修改失败!!");
                                log.error("========== RCS任务下发失败 ==========");
                                log.error("任务状态修改失败!!任务编码:{}", task.getTaskCode());
                                continue;
                            }
                            log.info("出库任务状态更新成功 - 任务编码:{}", task.getTaskCode());
                            /**如果是普通站点,修改站点状态为入库预约*/
                            if (curSta.getType().equals(StationTypeEnum.STATION_TYPE_NORMAL.type)) {
                                curSta.setUseStatus(LocStsType.LOC_STS_TYPE_S.type);
                                if (!basStationService.updateById(curSta)) {
                                    throw new CoolException("站点预约失败!!");
                                    log.error("========== RCS任务下发失败 ==========");
                                    log.error("站点预约失败!!任务编码:{},站点:{}", task.getTaskCode(), curSta.getStationName());
                                    continue;
                                }
                            }
                        }
                    });
                } else {
                    log.error(JSONObject.toJSONString(result));
//                    throw new CoolException("任务下发失败!!");
                    } else {
                        log.error("========== RCS任务下发失败 ==========");
                        log.error("RCS返回错误 - code:{},msg:{},data:{}",
                                result.getCode(), result.getMsg(), result.getData());
                        log.error("失败的任务编码:{},任务类型:{}", task.getTaskCode(), task.getTaskType());
                        log.error("任务下发失败!!任务编码:{}", task.getTaskCode());
                        continue;
                    }
                } catch (JsonProcessingException e) {
                    log.error("========== RCS任务下发异常 ==========");
                    log.error("解析RCS响应失败,响应体:{},任务编码:{}", exchange.getBody(), task.getTaskCode(), e);
                    log.error("解析RCS响应失败:{},任务编码:{}", e.getMessage(), task.getTaskCode());
                    continue;
                } catch (Exception e) {
                    log.error("========== RCS任务下发异常 ==========");
                    log.error("任务下发过程中发生异常,任务编码:{}", task.getTaskCode(), e);
                    log.error("任务下发异常:{},任务编码:{}", e.getMessage(), task.getTaskCode());
                    continue;
                }
            } catch (JsonProcessingException e) {
                throw new CoolException(e.getMessage());
            }
        }
        log.info("========== RCS任务下发完成,共{}个任务已处理 ==========", tasks.size());
    }/**
    /**
     * @author Ryan
     * @date 2025/5/20
     * @description: 扣减库存明细
     * @description: 扣减库存明细(全版出库:删除所有库位明细)
     * @version 1.0
     */
    @Transactional(rollbackFor = Exception.class)
    public void subtractLocItem(Loc loc) throws Exception {
        if (!locItemService.remove(new LambdaQueryWrapper<LocItem>().eq(LocItem::getLocId, loc.getId()))) {
            throw new CoolException("库存明细删除失败!!");
        // 删除库位明细,如果没有记录则忽略(可能已经被删除过了)
        locItemService.remove(new LambdaQueryWrapper<LocItem>().eq(LocItem::getLocId, loc.getId()));
    }
    /**
     * 根据任务明细扣减库位明细数量(部分出库)
     *
     * @param loc 库位
     * @param taskItems 任务明细列表
     * @param loginUserId 登录用户ID
     */
    @Transactional(rollbackFor = Exception.class)
    public void subtractLocItemByTaskItems(Loc loc, List<TaskItem> taskItems, Long loginUserId) {
        for (TaskItem taskItem : taskItems) {
            LambdaQueryWrapper<LocItem> locItemWrapper = new LambdaQueryWrapper<LocItem>()
                    .eq(LocItem::getLocId, loc.getId())
                    .eq(LocItem::getMatnrId, taskItem.getMatnrId());
            if (StringUtils.isNotBlank(taskItem.getBatch())) {
                locItemWrapper.eq(LocItem::getBatch, taskItem.getBatch());
            } else {
                locItemWrapper.and(w -> w.isNull(LocItem::getBatch).or().eq(LocItem::getBatch, ""));
            }
            if (StringUtils.isNotBlank(taskItem.getFieldsIndex())) {
                locItemWrapper.eq(LocItem::getFieldsIndex, taskItem.getFieldsIndex());
            } else {
                locItemWrapper.and(w -> w.isNull(LocItem::getFieldsIndex).or().eq(LocItem::getFieldsIndex, ""));
            }
            LocItem locItem = locItemService.getOne(locItemWrapper);
            log.info("[拣料入库-扣减库位] taskItemId={}, locId={}, matnrId={}, batch={}, fieldsIndex={}, locItem={}, locItem.anfme={}, taskItem.qty={}, taskItem.anfme={}",
                    taskItem.getId(), loc.getId(), taskItem.getMatnrId(), taskItem.getBatch(), taskItem.getFieldsIndex(),
                    locItem != null ? "存在" : "null", locItem != null ? locItem.getAnfme() : null, taskItem.getQty(), taskItem.getAnfme());
            if (Objects.nonNull(locItem)) {
                // 扣减量:优先用本次拣料数量 taskItem.getQty();若为 0 且 taskItem.anfme 已为剩余数量,则扣减 = 原库位 - 剩余(用 BigDecimal 避免 Double 精度问题)
                Double deductQty = QuantityUtils.isPositive(taskItem.getQty())
                        ? taskItem.getQty()
                        : (taskItem.getAnfme() != null && QuantityUtils.compare(taskItem.getAnfme(), locItem.getAnfme()) < 0
                                ? QuantityUtils.subtract(locItem.getAnfme(), taskItem.getAnfme())
                                : 0.0);
                Double newAnfme = QuantityUtils.subtract(locItem.getAnfme(), deductQty);
                log.info("[拣料入库-扣减库位] locItemId={}, deductQty={}, 原anfme={}, newAnfme={}, 操作={}",
                        locItem.getId(), deductQty, locItem.getAnfme(), newAnfme, QuantityUtils.isNonPositive(newAnfme) ? "删除" : "更新");
                if (QuantityUtils.isNonPositive(newAnfme)) {
                    // 数量小于等于0,删除库位明细
                    locItemService.removeById(locItem.getId());
                } else {
                    // 更新库位明细数量
                    locItem.setAnfme(newAnfme)
                            .setUpdateBy(loginUserId)
                            .setUpdateTime(new Date());
                    if (!locItemService.updateById(locItem)) {
                        throw new CoolException("库位明细数量扣减失败!!");
                    }
                }
            } else {
                log.warn("[拣料入库-扣减库位] 未查到库位明细 locId={}, matnrId={}, batch={}, fieldsIndex={},未扣减",
                        loc.getId(), taskItem.getMatnrId(), taskItem.getBatch(), taskItem.getFieldsIndex());
            }
        }
    }
@@ -1606,8 +2438,10 @@
            List<TaskItem> items = orderMap.get(key);
            //保存入出库明细
            saveStockItems(items, task, pakinItem.getId(), pakinItem.getAsnCode(), pakinItem.getWkType(), pakinItem.getType(), loginUserId);
            //移出收货区库存, 修改组托状态
            removeReceiptStock(pakinItem, loginUserId);
            // DirectWaitPakin 启用时组托来自收货区,入库完成后移出收货区库存;未启用时 source 为 null 不执行
            if (Objects.nonNull(pakinItem.getSource())) {
                removeReceiptStock(pakinItem, loginUserId);
            }
        });
        Set<Long> pkinItemIds = taskItems.stream().map(TaskItem::getSource).collect(Collectors.toSet());
@@ -1631,6 +2465,8 @@
        if (!this.update(new LambdaUpdateWrapper<Task>().eq(Task::getId, task.getId()).set(Task::getTaskStatus, TaskStsType.UPDATED_IN.id))) {
            throw new CoolException("任务状态修改失败!!");
        }
        // 9.1 入/出库结果上报:入库完成后通知云仓
        reportInOutResultToCloud(task, loc, taskItems, pkinItemIds, true);
    }
    /**
@@ -1643,6 +2479,10 @@
     */
    @Transactional(rollbackFor = Exception.class)
    public synchronized void removeReceiptStock(WaitPakinItem pakinItem, Long loginUserId) {
        // 如果source为null,说明组托明细不在收货区,无需移除收货区库存
        if (Objects.isNull(pakinItem.getSource())) {
            return;
        }
        WarehouseAreasItem itemServiceOne = warehouseAreasItemService.getOne(new LambdaQueryWrapper<WarehouseAreasItem>()
                .eq(WarehouseAreasItem::getId, pakinItem.getSource()));
        if (Objects.isNull(itemServiceOne)) {
@@ -1686,12 +2526,28 @@
                throw new CoolException("库位不存在!!");
            }
            LocItem item = new LocItem();
            LocItem locItem = locItemService.getOne(new LambdaQueryWrapper<LocItem>()
            // 构建查询条件:需要同时匹配物料ID、库位ID、批次和票号
            LambdaQueryWrapper<LocItem> locItemWrapper = new LambdaQueryWrapper<LocItem>()
                    .eq(LocItem::getMatnrId, taskItem.getMatnrId())
                    .eq(LocItem::getLocId, loc.getId())
                    .eq(StringUtils.isNotBlank(taskItem.getBatch()), LocItem::getBatch, taskItem.getBatch())
                    .eq(StringUtils.isNotBlank(taskItem.getFieldsIndex()), LocItem::getFieldsIndex, taskItem.getFieldsIndex()));
                    .eq(LocItem::getLocId, loc.getId());
            // 批次匹配:如果taskItem有批次,则必须匹配;如果taskItem没有批次,则查询批次为null或空字符串的记录
            if (StringUtils.isNotBlank(taskItem.getBatch())) {
                locItemWrapper.eq(LocItem::getBatch, taskItem.getBatch());
            } else {
                locItemWrapper.and(wrapper -> wrapper.isNull(LocItem::getBatch).or().eq(LocItem::getBatch, ""));
            }
            // 票号暂不使用,不按票号匹配,只查票号为 null 或空的库位明细
            // if (StringUtils.isNotBlank(taskItem.getFieldsIndex())) {
            //     locItemWrapper.eq(LocItem::getFieldsIndex, taskItem.getFieldsIndex());
            // } else {
            locItemWrapper.and(wrapper -> wrapper.isNull(LocItem::getFieldsIndex).or().eq(LocItem::getFieldsIndex, ""));
            // }
            LocItem locItem = locItemService.getOne(locItemWrapper);
            if (Objects.isNull(locItem)) {
                // 库位明细不存在,创建新的库位明细
                BeanUtils.copyProperties(taskItem, item);
                item.setLocCode(loc.getCode())
                        .setId(null)
@@ -1703,13 +2559,14 @@
                    throw new CoolException("库位明细更新失败!!");
                }
            } else {
                logger.error("当前票号:"  + locItem.getFieldsIndex()  + " 已在库内,请检查后再操作!!");
//                throw new CoolException("当前票号已在库内,请检查后再操作!!");
//                locItem.setAnfme(Math.round((locItem.getAnfme() + taskItem.getAnfme()) * 1000000) / 1000000.0)
//                        .setUpdateTime(new Date());
//                if (!locItemService.saveOrUpdate(locItem)) {
//                    throw new CoolException("库位明细更新失败!!");
//                }
                // 库位明细已存在,累加数量
                Double newAnfme = Math.round((locItem.getAnfme() + taskItem.getAnfme()) * 1000000) / 1000000.0;
                locItem.setAnfme(newAnfme)
                        .setUpdateBy(loginUserId)
                        .setUpdateTime(new Date());
                if (!locItemService.updateById(locItem)) {
                    throw new CoolException("库位明细数量更新失败!!");
                }
            }
        });
@@ -1778,4 +2635,72 @@
            }
        }
    }
    /**
     * 9.1 入/出库结果上报待办
     * @param isInbound true 入库完成,false 出库完成
     * @param pkinItemIds 入库时组托明细 ID 集合,用于查 asnCode 作为 orderNo;出库时传 null,用 taskItem.platOrderCode
     */
    private void reportInOutResultToCloud(Task task, Loc loc, List<TaskItem> taskItems, Set<Long> pkinItemIds, boolean isInbound) {
        try {
            String locId = isInbound ? task.getTargLoc() : task.getOrgLoc();
            String wareHouseId = null;
            if (loc.getWarehouseId() != null) {
                Warehouse wh = warehouseService.getById(loc.getWarehouseId());
                if (wh != null) {
                    wareHouseId = wh.getCode();
                }
            }
            if (wareHouseId == null) {
                log.warn("入/出库结果上报待办跳过:仓库编码为空,taskId={}", task.getId());
                return;
            }
            Map<Long, String> sourceToOrderNo = new HashMap<>();
            if (isInbound && pkinItemIds != null && !pkinItemIds.isEmpty()) {
                List<WaitPakinItem> pakinItems = waitPakinItemService.list(new LambdaQueryWrapper<WaitPakinItem>().in(WaitPakinItem::getId, pkinItemIds));
                for (WaitPakinItem p : pakinItems) {
                    if (p.getAsnCode() != null) {
                        sourceToOrderNo.put(p.getId(), p.getAsnCode());
                    }
                }
            }
            ObjectMapper om = new ObjectMapper();
            Date now = new Date();
            for (TaskItem item : taskItems) {
                String orderNo = isInbound ? sourceToOrderNo.get(item.getSource()) : (item.getPlatOrderCode() != null ? item.getPlatOrderCode() : item.getPlatWorkCode());
                if (orderNo == null && isInbound) {
                    orderNo = item.getPlatOrderCode() != null ? item.getPlatOrderCode() : item.getPlatWorkCode();
                }
                if (orderNo == null || item.getMatnrCode() == null) {
                    continue;
                }
                InOutResultReportParam param = new InOutResultReportParam()
                        .setOrderNo(orderNo)
                        .setPlanNo(item.getPlatWorkCode())
                        .setLineId(item.getPlatItemId())
                        .setWareHouseId(wareHouseId)
                        .setLocId(locId)
                        .setMatNr(item.getMatnrCode())
                        .setQty(item.getAnfme() != null ? String.valueOf(item.getAnfme()) : "0")
                        .setBatch(item.getBatch());
                try {
                    String requestBody = om.writeValueAsString(param);
                    CloudWmsNotifyLog notifyLog = new CloudWmsNotifyLog()
                            .setReportType(cloudWmsNotifyLogService.getReportTypeInOutResult())
                            .setRequestBody(requestBody)
                            .setNotifyStatus(cloudWmsNotifyLogService.getNotifyStatusPending())
                            .setRetryCount(0)
                            .setBizRef("taskId=" + task.getId() + ",orderNo=" + orderNo)
                            .setCreateTime(now)
                            .setUpdateTime(now);
                    cloudWmsNotifyLogService.fillFromConfig(notifyLog);
                    cloudWmsNotifyLogService.save(notifyLog);
                } catch (JsonProcessingException e) {
                    log.warn("入/出库结果上报待办落库失败(不影响主流程),taskId={},orderNo={}:{}", task.getId(), orderNo, e.getMessage());
                }
            }
        } catch (Exception e) {
            log.warn("入/出库结果上报待办失败,taskId={},isInbound={}:{}", task.getId(), isInbound, e.getMessage());
        }
    }
}