cl
2 天以前 a8a10ec2a3fe7d8db166413a92a34926fb178533
rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/TaskServiceImpl.java
@@ -4,7 +4,6 @@
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;
@@ -56,8 +55,13 @@
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestTemplate;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
@@ -175,7 +179,9 @@
                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()))) {
                    .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_S.type)
                    .set(Loc::getBarcode, pakin.getBarcode())
                    .set(Loc::getWeight, task.getWeight()))) {
                throw new CoolException("库位预约失败!!");
            }
            /**获取组拖明细**/
@@ -207,6 +213,7 @@
                    throw new CoolException("组托明细修执行数量修改失败!!");
                }
            });
            syncAsnOrderItemBarcodeByPakin(waitPakinItems, pakin.getBarcode(), loginUserId);
        });
        if (!waitPakinService.update(new LambdaUpdateWrapper<WaitPakin>()
@@ -264,7 +271,8 @@
                    .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_S.type)
                    .set(Loc::getUpdateBy, loginUserId)
                    .set(Loc::getUpdateTime, new Date())
                    .set(Loc::getBarcode, pakin.getBarcode()))) {
                    .set(Loc::getBarcode, pakin.getBarcode())
                    .set(Loc::getWeight, task.getWeight()))) {
                throw new CoolException("库位预约失败!!");
            }
            /**获取组拖明细**/
@@ -296,6 +304,7 @@
                    throw new CoolException("组托明细修执行数量修改失败!!");
                }
            });
            syncAsnOrderItemBarcodeByPakin(waitPakinItems, pakin.getBarcode(), loginUserId);
        });
        if (!waitPakinService.update(new LambdaUpdateWrapper<WaitPakin>()
@@ -406,7 +415,9 @@
            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()))) {
                    .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_S.type)
                    .set(Loc::getBarcode, pakin.getBarcode())
                    .set(Loc::getWeight, task.getWeight()))) {
                throw new CoolException("库位预约失败!!");
            }
            /**获取组拖明细**/
@@ -446,6 +457,7 @@
                    throw new CoolException("组托明细修执行数量修改失败!!");
                }
            });
            syncAsnOrderItemBarcodeByPakin(waitPakinItems, pakin.getBarcode(), loginUserId);
        });
        if (!waitPakinService.update(new LambdaUpdateWrapper<WaitPakin>()
@@ -456,6 +468,27 @@
            throw new CoolException("组拖状态修改失败!!");
        }
        return R.ok("任务生成完毕!");
    }
    /** 组托后将通知单明细条码回写为料箱码 */
    private void syncAsnOrderItemBarcodeByPakin(List<WaitPakinItem> waitPakinItems, String pakinBarcode, Long loginUserId) {
        if (waitPakinItems == null || waitPakinItems.isEmpty() || StringUtils.isBlank(pakinBarcode)) {
            return;
        }
        Set<Long> asnItemIds = waitPakinItems.stream()
                .map(WaitPakinItem::getAsnItemId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
        if (asnItemIds.isEmpty()) {
            return;
        }
        if (!asnOrderItemService.update(new LambdaUpdateWrapper<WkOrderItem>()
                .in(WkOrderItem::getId, asnItemIds)
                .set(WkOrderItem::getBarcode, pakinBarcode)
                .set(WkOrderItem::getUpdateBy, loginUserId)
                .set(WkOrderItem::getUpdateTime, new Date()))) {
            throw new CoolException("通知单明细条码回写失败!!");
        }
    }
    /**
@@ -590,15 +623,44 @@
        if (StringUtils.isNotBlank(task.getTaskCode())) {
            rcsBusTaskNoticeService.notifyTaskStatus(task.getTaskCode(), task.getTaskStatus());
        }
        final Long taskIdForFinish = task.getId();
        if (TransactionSynchronizationManager.isSynchronizationActive()) {
            TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
                @Override
                public void afterCommit() {
                    taskService.runStockFinishAfterManualComplete(taskIdForFinish);
                }
            });
        }
        return task;
    }
    @Override
    public void runStockFinishAfterManualComplete(Long taskId) {
        if (taskId == null) {
            return;
        }
        try {
            Task t = taskService.getById(taskId);
            if (t == null) {
                return;
            }
            if (Objects.equals(t.getTaskStatus(), TaskStsType.COMPLETE_IN.id)) {
                taskService.complateInTask(Collections.singletonList(t));
            } else if (Objects.equals(t.getTaskStatus(), TaskStsType.COMPLETE_OUT.id)) {
                taskService.completeTask(Collections.singletonList(t));
            }
        } catch (Exception e) {
            log.warn("手动完结后立即库存收尾失败,将由定时任务重试,taskId={}:{}", taskId, e.getMessage());
        }
    }
    /**
     * 全版出库完结:扣除库位数量,将库位状态设为空
     * 全板出库完结:扣除库位数量,将库位状态设为空
     *
     * @param id 任务ID
     * @param loginUserId 登录用户ID
     * @param notifyRcsFromAdmin 管理后台全版出库完结接口为 true 时通知 RCS;定时/PDA 等为 false
     * @param notifyRcsFromAdmin 管理后台全板出库完结接口为 true 时通知 RCS;定时/PDA 等为 false
     * @return 任务对象
     */
    @Override
@@ -612,9 +674,9 @@
            throw new CoolException("任务不存在!!");
        }
        // 检查任务类型是否为全版出库
        // 检查任务类型是否为全板出库
        if (!task.getTaskType().equals(TaskType.TASK_TYPE_OUT.type)) {
            throw new CoolException("当前任务不是全版出库任务,无法执行此操作!!");
            throw new CoolException("当前任务不是全板出库任务,无法执行此操作!!");
        }
        // 检查任务状态:必须是199(WAVE_SEED)状态才能手动完结
@@ -643,6 +705,7 @@
        // 将库位状态设为空(O状态)
        if (!locService.update(new LambdaUpdateWrapper<Loc>()
                .set(Loc::getBarcode, null)
                .set(Loc::getWeight, null)
                .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_O.type)
                .set(Loc::getUpdateBy, loginUserId)
                .set(Loc::getUpdateTime, new Date())
@@ -802,6 +865,7 @@
        /**修改库位状态为F.在库*/
        if (!locService.update(new LambdaUpdateWrapper<Loc>()
                .set(Loc::getBarcode, task.getBarcode())
                .set(Loc::getWeight, task.getWeight())
                .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_F.type)
                .set(Loc::getUpdateBy, loginUserId)
                .set(Loc::getUpdateTime, new Date())
@@ -811,6 +875,7 @@
        if (!locService.update(new LambdaUpdateWrapper<Loc>()
                .set(Loc::getBarcode, null)
                .set(Loc::getWeight, null)
                .set(Loc::getUpdateBy, loginUserId)
                .set(Loc::getUpdateTime, new Date())
                .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_O.type)
@@ -873,6 +938,7 @@
//        }
        loc.setUseStatus(LocStsType.LOC_STS_TYPE_F.type)
                .setBarcode(task.getBarcode())
                .setWeight(task.getWeight())
                .setUpdateBy(loginUserId).setUpdateTime(new Date());
        if (!locService.updateById(loc)) {
@@ -945,14 +1011,24 @@
        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()) {
        List<Task> tasksById = this.list(new LambdaQueryWrapper<Task>().in(Task::getId, (Object[]) ids));
        if (tasksById.isEmpty()) {
            throw new CoolException("任务不存在!!");
        }
        // 拣料出库/盘点出库进入再入库阶段后禁止取消(任务类型已由 103/107 变为再入库)
        for (Task t : tasksById) {
            if (TaskType.TASK_TYPE_PICK_IN.type.equals(t.getTaskType())
                    || TaskType.TASK_TYPE_CHECK_IN.type.equals(t.getTaskType())) {
                throw new CoolException("拣料/盘点出库已进入再入库阶段,禁止取消!!");
            }
        }
        List<Task> allTasks = tasksById.stream()
                .filter(t -> list.contains(t.getTaskType()))
                .collect(Collectors.toList());
        if (allTasks.isEmpty()) {
            throw new CoolException("当前任务类型不支持取消!!");
        }
        // 收集需要取消的RCS任务编号和批次编号(不限制状态,只要已下发到RCS就需要取消)
        List<String> rcsTaskCodes = new ArrayList<>();
        String batchNo = null;
@@ -1029,6 +1105,10 @@
                log.info("RCS取消任务响应状态码:{}", exchange.getStatusCode());
                log.info("RCS取消任务响应体:{}", exchange.getBody());
                if (!exchange.getStatusCode().is2xxSuccessful()) {
                    throw new CoolException("RCS取消任务失败:HTTP " + exchange.getStatusCode().value());
                }
                if (Objects.isNull(exchange.getBody())) {
                    log.error("RCS取消任务失败:响应体为空");
                    throw new CoolException("RCS取消任务失败:响应体为空");
@@ -1058,7 +1138,7 @@
            }
        }
        
        // 可取消状态:原 1/101(不含 199);拣料/盘点出库 RCS 执行中(<198);拣料/盘点再入库(53/57)不支持取消
        // 可取消状态:原 1/101(不含 199);拣料/盘点出库 RCS 执行中(<198);再入库(53/57)在方法入口已禁止取消
        List<Task> tasks = this.list(new LambdaQueryWrapper<Task>()
                .in(Task::getTaskType, list)
                .in(Task::getId, (Object[]) ids)
@@ -1090,7 +1170,9 @@
                }
                if (!locService.update(new LambdaUpdateWrapper<Loc>()
                        .eq(Loc::getCode, task.getTargLoc())
                        .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_O.type))) {
                        .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_O.type)
                        .set(Loc::getBarcode, null)
                        .set(Loc::getWeight, null))) {
                    throw new CoolException("移库目标库位状态修改失败!!");
                }
@@ -1142,6 +1224,14 @@
                        .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_F.type))) {
                    throw new CoolException("源库位状态修改失败!!");
                }
            } else if (task.getTaskType().equals(TaskType.TASK_TYPE_EMPITY_OUT.type)) {
                // 空板出库:建单时 D→R,取消恢复为空板 D
                if (!locService.update(new LambdaUpdateWrapper<Loc>()
                        .eq(Loc::getCode, task.getOrgLoc())
                        .eq(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_R.type)
                        .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_D.type))) {
                    throw new CoolException("空板出库源库位恢复空板失败!!");
                }
            }
            if (!Objects.isNull(task.getWarehType()) && task.getWarehType().equals(WarehType.WAREHOUSE_TYPE_AGV.val)) {
@@ -1178,13 +1268,14 @@
                        }
                        loc.setUseStatus(LocStsType.LOC_STS_TYPE_F.type)
                                .setBarcode(task.getBarcode())
                                .setWeight(task.getWeight())
                                .setUpdateBy(loginUserId)
                                .setUpdateTime(new Date());
                        if (!locService.updateById(loc)) {
                            throw new CoolException("库位信息修改失败!!");
                        }
                        //出库
                        if (item.getWkType().equals(Short.parseShort(OrderWorkType.ORDER_WORK_TYPE_STOCK_OUT.type))) {
                        if (Objects.equals(item.getWkType(), OrderWorkType.ORDER_WORK_TYPE_STOCK_OUT.type)) {
                            //库存出库
                        } else if (task.getResource().equals(TaskResouceType.TASK_RESOUCE_WAVE_TYPE.val)) {
                            WaveItem waveItem = waveItemService.getById(item.getSource());
@@ -1206,7 +1297,7 @@
                                throw new CoolException("波次更新失败!!");
                            }
                        } else if (item.getWkType().equals(Short.parseShort(OrderWorkType.ORDER_WORK_TYPE_OTHER.type))) {
                        } else if (Objects.equals(item.getWkType(), OrderWorkType.ORDER_WORK_TYPE_OTHER.type)) {
                            //其它出库
                        } else {
@@ -1252,7 +1343,9 @@
                        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getCode, task.getTaskType() < TaskStsType.UPDATED_IN.id ? task.getTargLoc() : task.getOrgLoc()));
                        if (null != loc && (loc.getUseStatus().equals(LocStsType.LOC_STS_TYPE_S.type)
                                || loc.getUseStatus().equals(LocStsType.LOC_STS_TYPE_R.type))) {
                            loc.setUseStatus(LocStsType.LOC_STS_TYPE_O.type);
                            loc.setUseStatus(LocStsType.LOC_STS_TYPE_O.type)
                                    .setBarcode(null)
                                    .setWeight(null);
                            if (!locService.updateById(loc)) {
                                throw new CoolException("更新库位状态失败!!");
                            }
@@ -1491,7 +1584,8 @@
                throw new CoolException("临时库存更新失败!!");
            }
        }
        loc1.setUseStatus(LocStsType.LOC_STS_TYPE_S.type);
        loc1.setUseStatus(LocStsType.LOC_STS_TYPE_S.type)
                .setWeight(task.getWeight());
        locService.updateById(loc1);
//        if (!locService.updateById(loc1)) {
//            throw new CoolException("库位预约入库失败!!");
@@ -1616,11 +1710,13 @@
            if (!workings.isEmpty()) {
                locItemWorkingService.saveBatch(workings);
            }
            loc.setUseStatus(LocStsType.LOC_STS_TYPE_S.type);
            loc.setUseStatus(LocStsType.LOC_STS_TYPE_S.type)
                    .setWeight(first.getWeight());
            locService.updateById(loc);
        } else {
            loc.setUseStatus(LocStsType.LOC_STS_TYPE_O.type);
            loc.setBarcode(null);
            loc.setWeight(null);
            loc.setUpdateBy(loginUserId);
            loc.setUpdateTime(new Date());
            locService.updateById(loc);
@@ -1659,6 +1755,7 @@
                        .eq(Loc::getId, loc.getId())
                        .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_O.type)
                        .set(Loc::getBarcode, null)
                        .set(Loc::getWeight, null)
                        .set(Loc::getUpdateBy, loginUserId)
                        .set(Loc::getUpdateTime, new Date()))) {
                    throw new CoolException("空板出库库位状态更新失败!!");
@@ -1725,7 +1822,7 @@
            try {
                // 根据任务类型更新库位明细
                if (task.getTaskType().equals(TaskType.TASK_TYPE_OUT.type)) {
                    // 全版出库:不删除库位明细,等待PDA快速拣货确认时再删除
                    // 全板出库:不删除库位明细,等待PDA快速拣货确认时再删除
                    // subtractLocItem(loc); // 已移除,改为在completeFullOutStock中删除
                } else if (!TaskType.TASK_TYPE_PICK_AGAIN_OUT.type.equals(task.getTaskType())) {
                    // 部分出库(如盘点出库):根据TaskItem数量扣减库位明细;拣料出库在生成拣料入库单时扣减
@@ -1738,12 +1835,12 @@
        }
        //添加出入库记录信息
        Map<Short, List<TaskItem>> listMap = taskItems.stream().collect(Collectors.groupingBy(TaskItem::getWkType));
        Map<String, List<TaskItem>> listMap = taskItems.stream().collect(Collectors.groupingBy(TaskItem::getWkType));
        /***获取库存出库值,如果为空表示正常单据出库,非空表明是库存出库
         * 1. 库存出库没有单据信息,单据信息默认为空
         * 2. 单据库存需通过波次查询原始单据信息,将单据信息填入stock中
         * */
        List<TaskItem> list = listMap.get(Short.parseShort(OrderWorkType.ORDER_WORK_TYPE_STOCK_OUT.type));
        List<TaskItem> list = listMap.get(OrderWorkType.ORDER_WORK_TYPE_STOCK_OUT.type);
        if (Objects.isNull(list) || list.isEmpty()) {
            Map<Long, List<TaskItem>> maps = taskItems.stream().collect(Collectors.groupingBy(TaskItem::getSource));
            maps.keySet().forEach(key -> {
@@ -1794,13 +1891,14 @@
            // 拣料出库/盘点出库:在未生成拣料入库单之前保持 R.预约出库,否则下发任务时查不到该库位(只查 F+R)导致“库存不足”
            // 等 PDA 确认并生成拣料入库任务时,再在 pickOrCheckTask 中将目标库位改为 S.预约入库
        } else if (task.getTaskType().equals(TaskType.TASK_TYPE_OUT.type)) {
            // 全版出库:不更新库位状态为O,等待PDA快速拣货确认时再更新
            // 全板出库:不更新库位状态为O,等待PDA快速拣货确认时再更新
            // 库位状态保持原样(R.出库预约状态)
        } else {
            /**修改为库位状态为O.空库*/
            if (!locService.update(new LambdaUpdateWrapper<Loc>()
                    .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_O.type)
                    .set(Loc::getBarcode, null)
                    .set(Loc::getWeight, null)
                    .set(Loc::getUpdateBy, loginUserId)
                    .set(Loc::getUpdateTime, new Date())
                    .eq(Loc::getId, loc.getId()))) {
@@ -1914,13 +2012,13 @@
            if (!asnOrderService.updateById(wkOrder)) {
                throw new CoolException("出库单更新失败!!");
            }
            stock.setWkType(Short.parseShort(wkOrder.getWkType()))
            stock.setWkType(wkOrder.getWkType())
                    .setType(OrderType.ORDER_OUT.type);
        } else if (!Objects.isNull(diffItem)) {
            stock.setWkType(Short.parseShort(OrderWorkType.ORDER_WORK_TYPE_STOCK_CHECK.type))
            stock.setWkType(OrderWorkType.ORDER_WORK_TYPE_STOCK_CHECK.type)
                    .setType(OrderType.ORDER_CHECK.type);
        } else {
            stock.setWkType(Short.parseShort(OrderWorkType.ORDER_WORK_TYPE_STOCK_OUT.type))
            stock.setWkType(OrderWorkType.ORDER_WORK_TYPE_STOCK_OUT.type)
                    .setMemo("库存出库,无单据信息!!")
                    .setType(OrderType.ORDER_OUT.type);
        }
@@ -2046,6 +2144,15 @@
                    log.error("========== RCS任务下发失败 ==========");
                    log.error("站点不存在!!任务编码:{},目标站点:{}", task.getTaskCode(), task.getTargSite());
                    continue;
                }
                // 出库下发前校验站点状态与出库能力
                if (task.getTaskType() >= TaskType.TASK_TYPE_OUT.type) {
                    if (!Integer.valueOf(1).equals(station.getStatus()) || !Integer.valueOf(1).equals(station.getOutAble())) {
                        log.error("========== RCS任务下发失败 ==========");
                        log.error("站点不可出库下发!!任务编码:{},目标站点:{},站点状态(status):{},能出(outAble):{}",
                                task.getTaskCode(), task.getTargSite(), station.getStatus(), station.getOutAble());
                        continue;
                    }
                }
            }
@@ -2219,6 +2326,13 @@
                    log.error("RCS资源访问异常,任务下发失败!任务编码:{},错误信息:{}", task.getTaskCode(), errorMsg);
                }
                continue;
            } catch (HttpStatusCodeException e) {
                long endTime = System.currentTimeMillis();
                log.error("========== RCS任务下发异常 ==========");
                log.error("请求RCS-HTTP状态异常,耗时:{}ms,任务编码:{},status:{},body:{}", (endTime - startTime), task.getTaskCode(), e.getRawStatusCode(), e.getResponseBodyAsString(), e);
                log.error("请求RCS-地址:{}", pubTakUrl);
                log.error("请求RCS-参数:{}", JSONObject.toJSONString(taskParams));
                continue;
            } catch (Exception e) {
                long endTime = System.currentTimeMillis();
                log.error("========== RCS任务下发异常 ==========");
@@ -2344,7 +2458,7 @@
    /**
     * @author Ryan
     * @date 2025/5/20
     * @description: 扣减库存明细(全版出库:删除所有库位明细)
     * @description: 扣减库存明细(全板出库:删除所有库位明细)
     * @version 1.0
     */
    @Transactional(rollbackFor = Exception.class)
@@ -2438,6 +2552,7 @@
        if (TaskType.TASK_TYPE_EMPITY_IN.type.equals(task.getTaskType())) {
            if (!locService.update(new LambdaUpdateWrapper<Loc>().eq(Loc::getCode, task.getTargLoc())
                    .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_D.type)
                    .set(Loc::getWeight, task.getWeight())
                    .set(Loc::getUpdateBy, loginUserId)
                    .set(Loc::getUpdateTime, new Date()))) {
                throw new CoolException("空板入库库位状态修改失败!!");
@@ -2506,6 +2621,10 @@
            WkOrder o = asnOrderService.getById(e.getKey());
            if (o != null) {
                Double newQty = QuantityUtils.roundToScale(QuantityUtils.add(o.getQty() != null ? o.getQty() : 0.0, e.getValue()));
                Double planQty = QuantityUtils.roundToScale(o.getAnfme() != null ? o.getAnfme() : 0.0);
                if (QuantityUtils.compare(newQty, planQty) > 0) {
                    newQty = planQty;
                }
                o.setQty(newQty);
                if (!asnOrderService.updateById(o)) {
                    throw new CoolException("入库单完成数量更新失败!!");
@@ -2516,6 +2635,10 @@
            WkOrderItem oi = asnOrderItemService.getById(e.getKey());
            if (oi != null) {
                Double newQty = QuantityUtils.roundToScale(QuantityUtils.add(oi.getQty() != null ? oi.getQty() : 0.0, e.getValue()));
                Double planQty = QuantityUtils.roundToScale(oi.getAnfme() != null ? oi.getAnfme() : 0.0);
                if (QuantityUtils.compare(newQty, planQty) > 0) {
                    newQty = planQty;
                }
                oi.setQty(newQty);
                if (!asnOrderItemService.updateById(oi)) {
                    throw new CoolException("入库单明细完成数量更新失败!!");
@@ -2524,7 +2647,10 @@
        }
        /**修改库位状态为F.在库*/
        if (!locService.update(new LambdaUpdateWrapper<Loc>().set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_F.type).eq(Loc::getCode, task.getTargLoc()))) {
        if (!locService.update(new LambdaUpdateWrapper<Loc>()
                .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_F.type)
                .set(Loc::getWeight, task.getWeight())
                .eq(Loc::getCode, task.getTargLoc()))) {
            throw new CoolException("库位状态修改失败!!");
        }
        if (!this.update(new LambdaUpdateWrapper<Task>().eq(Task::getId, task.getId()).set(Task::getTaskStatus, TaskStsType.UPDATED_IN.id))) {
@@ -2645,7 +2771,7 @@
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public synchronized void saveStockItems(List<TaskItem> items, Task task, Long id, String code, Short wkType, String type, Long loginUserId) {
    public synchronized void saveStockItems(List<TaskItem> items, Task task, Long id, String code, String wkType, String type, Long loginUserId) {
        Stock stock = new Stock();
        String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_STOCK_CODE, null);
        if (StringUtils.isBlank(ruleCode)) {
@@ -2669,12 +2795,27 @@
        } else {
            stock.setLocCode(task.getOrgLoc());
        }
        BigDecimal trayWeight = task.getWeight();
        BigDecimal groupWeight = null;
        if (trayWeight != null && sum > 0) {
            List<TaskItem> allLines = taskItemService.list(new LambdaQueryWrapper<TaskItem>().eq(TaskItem::getTaskId, task.getId()));
            double totalQty = allLines.stream().mapToDouble(t -> t.getAnfme() != null ? t.getAnfme() : 0.0).sum();
            if (totalQty > 0) {
                groupWeight = trayWeight.multiply(BigDecimal.valueOf(sum)).divide(BigDecimal.valueOf(totalQty), 4, RoundingMode.HALF_UP);
            } else {
                groupWeight = trayWeight;
            }
            stock.setWeight(groupWeight);
        }
        if (!stockService.save(stock)) {
            throw new CoolException("库存保存失败!!");
        }
        List<StockItem> stockItems = new ArrayList<>();
        for (TaskItem item : items) {
        BigDecimal allocatedWeight = BigDecimal.ZERO;
        BigDecimal sumBd = sum > 0 ? BigDecimal.valueOf(sum) : null;
        for (int i = 0; i < items.size(); i++) {
            TaskItem item = items.get(i);
            /**通过任务明细中的taskId查询,获取TASK的目标库位信息*/
            StockItem stockItem = new StockItem();
            BeanUtils.copyProperties(item, stockItem);
@@ -2685,6 +2826,16 @@
                    .setUpdateBy(loginUserId)
                    .setId(null)
                    .setStockId(stock.getId());
            if (groupWeight != null && sumBd != null) {
                if (i == items.size() - 1) {
                    stockItem.setWeight(groupWeight.subtract(allocatedWeight));
                } else {
                    double lineQty = item.getAnfme() != null ? item.getAnfme() : 0.0;
                    BigDecimal lineW = groupWeight.multiply(BigDecimal.valueOf(lineQty)).divide(sumBd, 4, RoundingMode.HALF_UP);
                    stockItem.setWeight(lineW);
                    allocatedWeight = allocatedWeight.add(lineW);
                }
            }
            stockItems.add(stockItem);
        }
        if (!stockItemService.saveBatch(stockItems)) {
@@ -2729,9 +2880,18 @@
                    }
                }
            }
            // 入/出库均仅云仓来源单据参与上报(明细需带 platOrderCode 或 platWorkCode)
            boolean hasCloudSource = taskItems.stream().anyMatch(this::hasCloudOrderRef);
            if (!hasCloudSource) {
                log.info("入/出库结果上报待办跳过:无云仓来源单据,taskId={}", task.getId());
                return;
            }
            ObjectMapper om = new ObjectMapper();
            Date now = new Date();
            for (TaskItem item : taskItems) {
                if (!hasCloudOrderRef(item)) {
                    continue;
                }
                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();
@@ -2747,7 +2907,9 @@
                        .setLocId(locId)
                        .setMatNr(item.getMatnrCode())
                        .setQty(item.getAnfme() != null ? String.valueOf(item.getAnfme()) : "0")
                        .setBatch(item.getBatch());
                        .setBatch(item.getBatch())
                        .setInbound(isInbound)
                        .setBarcode(task.getBarcode());
                try {
                    String requestBody = om.writeValueAsString(param);
                    CloudWmsNotifyLog notifyLog = new CloudWmsNotifyLog()
@@ -2768,4 +2930,12 @@
            log.warn("入/出库结果上报待办失败,taskId={},isInbound={}:{}", task.getId(), isInbound, e.getMessage());
        }
    }
    private boolean hasCloudOrderRef(TaskItem item) {
        if (item == null) {
            return false;
        }
        return StringUtils.isNotBlank(item.getPlatOrderCode())
                || StringUtils.isNotBlank(item.getPlatWorkCode());
    }
}