10 天以前 56b8b84689ccf5389ee02814bcb8bfe407f05a87
src/main/java/com/ruoyi/production/service/impl/ProductionOrderServiceImpl.java
@@ -2,66 +2,41 @@
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basic.dto.StorageBlobVO;
import com.ruoyi.basic.mapper.StorageAttachmentMapper;
import com.ruoyi.basic.mapper.StorageBlobMapper;
import com.ruoyi.basic.pojo.StorageAttachment;
import com.ruoyi.basic.pojo.StorageBlob;
import com.ruoyi.basic.utils.FileUtil;
import com.ruoyi.common.constant.StorageAttachmentConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.production.bean.dto.ProductionOrderDto;
import com.ruoyi.production.bean.vo.ProductionBomStructureVo;
import com.ruoyi.production.bean.vo.ProductionOrderPickVo;
import com.ruoyi.production.bean.vo.ProductionOrderVo;
import com.ruoyi.production.mapper.ProductionBomStructureMapper;
import com.ruoyi.production.mapper.ProductionOperationTaskMapper;
import com.ruoyi.production.mapper.ProductionOrderBomMapper;
import com.ruoyi.production.mapper.ProductionOrderMapper;
import com.ruoyi.production.mapper.ProductionOrderPickMapper;
import com.ruoyi.production.mapper.ProductionOrderPickRecordMapper;
import com.ruoyi.production.mapper.ProductionOrderRoutingMapper;
import com.ruoyi.production.mapper.ProductionOrderRoutingOperationMapper;
import com.ruoyi.production.mapper.ProductionOrderRoutingOperationParamMapper;
import com.ruoyi.production.mapper.ProductionPlanMapper;
import com.ruoyi.production.mapper.ProductionProductMainMapper;
import com.ruoyi.production.pojo.ProductionOrderPick;
import com.ruoyi.production.pojo.ProductionOrderPickRecord;
import com.ruoyi.production.pojo.ProductionBomStructure;
import com.ruoyi.production.pojo.ProductionOperationTask;
import com.ruoyi.production.pojo.ProductionOrder;
import com.ruoyi.production.pojo.ProductionOrderBom;
import com.ruoyi.production.pojo.ProductionOrderRouting;
import com.ruoyi.production.pojo.ProductionOrderRoutingOperation;
import com.ruoyi.production.pojo.ProductionOrderRoutingOperationParam;
import com.ruoyi.production.pojo.ProductionPlan;
import com.ruoyi.production.pojo.ProductionProductMain;
import com.ruoyi.production.bean.vo.ProductionPlanVo;
import com.ruoyi.production.enums.ProductOrderStatusEnum;
import com.ruoyi.production.mapper.*;
import com.ruoyi.production.pojo.*;
import com.ruoyi.production.service.ProductionOrderService;
import com.ruoyi.sales.mapper.SalesLedgerMapper;
import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
import com.ruoyi.sales.pojo.SalesLedger;
import com.ruoyi.sales.pojo.SalesLedgerProduct;
import com.ruoyi.production.service.ProductionOrderService;
import com.ruoyi.technology.mapper.TechnologyBomMapper;
import com.ruoyi.technology.mapper.TechnologyBomStructureMapper;
import com.ruoyi.technology.mapper.TechnologyRoutingMapper;
import com.ruoyi.technology.mapper.TechnologyRoutingOperationMapper;
import com.ruoyi.technology.mapper.TechnologyRoutingOperationParamMapper;
import com.ruoyi.technology.pojo.TechnologyBom;
import com.ruoyi.technology.pojo.TechnologyBomStructure;
import com.ruoyi.technology.pojo.TechnologyRouting;
import com.ruoyi.technology.pojo.TechnologyRoutingOperation;
import com.ruoyi.technology.pojo.TechnologyRoutingOperationParam;
import com.ruoyi.stock.mapper.StockInventoryMapper;
import com.ruoyi.stock.pojo.StockInventory;
import com.ruoyi.technology.mapper.*;
import com.ruoyi.technology.pojo.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
@Service
@@ -79,29 +54,41 @@
    private final ProductionOrderPickMapper productionOrderPickMapper;
    private final ProductionOrderPickRecordMapper productionOrderPickRecordMapper;
    private final ProductionPlanMapper productionPlanMapper;
    private final StockInventoryMapper stockInventoryMapper;
    private final StorageAttachmentMapper storageAttachmentMapper;
    private final StorageBlobMapper storageBlobMapper;
    private final SalesLedgerMapper salesLedgerMapper;
    private final SalesLedgerProductMapper salesLedgerProductMapper;
    private final TechnologyRoutingMapper technologyRoutingMapper;
    private final TechnologyRoutingOperationMapper technologyRoutingOperationMapper;
    private final TechnologyRoutingOperationParamMapper technologyRoutingOperationParamMapper;
    private final TechnologyOperationMapper technologyOperationMapper;
    private final TechnologyBomMapper technologyBomMapper;
    private final TechnologyBomStructureMapper technologyBomStructureMapper;
    private final FileUtil fileUtil;
    @Override
    public com.baomidou.mybatisplus.core.metadata.IPage<ProductionOrderVo> pageProductionOrder(Page<ProductionOrderDto> page, ProductionOrderDto dto) {
        Page<ProductionOrder> entityPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        return this.page(entityPage, buildQueryWrapper(dto)).convert(item -> BeanUtil.copyProperties(item, ProductionOrderVo.class));
    public IPage<ProductionOrderVo> pageProductionOrder(Page<ProductionOrderDto> page, ProductionOrderDto dto) {
        Page<ProductionOrderVo> result = (Page<ProductionOrderVo>) baseMapper.pageProductionOrder(page, dto);
        fillProductImages(result.getRecords());
        return result;
    }
    @Override
    public List<ProductionOrderVo> listProductionOrder(ProductionOrderDto dto) {
        return BeanUtil.copyToList(this.list(buildQueryWrapper(dto)), ProductionOrderVo.class);
        List<ProductionOrderVo> records = baseMapper.listProductionOrder(dto);
        fillProductImages(records);
        return records;
    }
    @Override
    public ProductionOrderVo getProductionOrderInfo(Long id) {
        ProductionOrder item = this.getById(id);
        return item == null ? null : BeanUtil.copyProperties(item, ProductionOrderVo.class);
        ProductionOrderVo item = baseMapper.getProductionOrderInfo(id);
        if (item == null) {
            return null;
        }
        fillProductImages(java.util.Collections.singletonList(item));
        return item;
    }
    @Override
@@ -122,7 +109,6 @@
        if (!saved) {
            return false;
        }
        syncProductionPlanIssueStatus(oldOrder, productionOrder);
        boolean needSync = productionOrder.getTechnologyRoutingId() != null
                && (oldOrder == null
                || !Objects.equals(oldOrder.getTechnologyRoutingId(), productionOrder.getTechnologyRoutingId())
@@ -131,11 +117,7 @@
                || productionOrderRoutingMapper.selectCount(Wrappers.<ProductionOrderRouting>lambdaQuery()
                        .eq(ProductionOrderRouting::getProductionOrderId, productionOrder.getId())) == 0);
        if (needSync) {
            // 工艺、产品或数量变化后,订单快照必须和当前下单数据重新对齐。
            syncProductionOrderSnapshot(productionOrder.getId());
        } else {
            // 未重建快照时,也要确保备料主单和订单数量保持同步。
            upsertOrderPick(productionOrder);
        }
        return true;
    }
@@ -154,20 +136,74 @@
    }
    @Override
    public Integer bindingRoute(ProductionOrderDto productionOrderDto) {
        if (productionOrderDto == null || productionOrderDto.getId() == null) {
            throw new ServiceException("生产订单ID不能为空");
        }
        ProductionOrder productionOrder = this.getById(productionOrderDto.getId());
        if (productionOrder == null) {
            throw new ServiceException("生产订单不存在");
        }
        Long targetRoutingId = productionOrderDto.getTechnologyRoutingId() == null
                ? productionOrder.getTechnologyRoutingId()
                : productionOrderDto.getTechnologyRoutingId();
        if (targetRoutingId == null) {
            throw new ServiceException("工艺路线ID不能为空");
        }
        TechnologyRouting targetRouting = technologyRoutingMapper.selectById(targetRoutingId);
        if (targetRouting == null) {
            throw new ServiceException("工艺路线不存在");
        }
        if (productionOrder.getProductModelId() != null
                && !Objects.equals(productionOrder.getProductModelId(), targetRouting.getProductModelId())) {
            throw new ServiceException("工艺路线与生产订单产品规格不匹配");
        }
        if (ProductOrderStatusEnum.isStarted(productionOrder.getStatus())
                && !Objects.equals(productionOrder.getTechnologyRoutingId(), targetRoutingId)) {
            throw new ServiceException("生产订单已开工,不能修改工艺路线");
        }
        if (!Objects.equals(productionOrder.getTechnologyRoutingId(), targetRoutingId)) {
            ProductionOrder update = new ProductionOrder();
            update.setId(productionOrder.getId());
            update.setTechnologyRoutingId(targetRoutingId);
            if (!this.updateById(update)) {
                throw new ServiceException("绑定工艺路线失败");
            }
        }
        // 绑定路线仅重建订单侧快照数据
        return syncProductionOrderSnapshot(productionOrder.getId());
    }
    @Override
    public List<ProductionPlanVo> getSource(Long id) {
        ProductionOrder productionOrder = baseMapper.selectById(id);
        if (productionOrder != null && productionOrder.getProductionPlanIds() != null) {
            List<Long> planIds = parsePlanIds(productionOrder.getProductionPlanIds());
            return productionPlanMapper.getSource(planIds);
        }
        return null;
    }
    @Override
    public int syncProductionOrderSnapshot(Long productionOrderId) {
        ProductionOrder productionOrder = this.getById(productionOrderId);
        if (productionOrder == null) {
            throw new ServiceException("Production order not found");
            throw new ServiceException("生产订单不存在");
        }
        if (productionOrder.getTechnologyRoutingId() == null) {
            throw new ServiceException("technologyRoutingId is required");
            throw new ServiceException("工艺路线ID不能为空");
        }
        TechnologyRouting technologyRouting = technologyRoutingMapper.selectById(productionOrder.getTechnologyRoutingId());
        if (technologyRouting == null) {
            throw new ServiceException("Technology routing not found");
            throw new ServiceException("工艺路线不存在");
        }
        // 订单快照按“先清后建”处理,保证工艺路线、工序、参数、BOM 全部来自同一版本。
        clearProductionSnapshot(productionOrderId);
        ProductionOrderBom orderBom = syncProductionOrderBomSnapshot(productionOrder, technologyRouting);
        ProductionOrderRouting orderRouting = new ProductionOrderRouting();
        orderRouting.setProductionOrderId(productionOrder.getId());
@@ -176,6 +212,7 @@
        orderRouting.setProcessRouteCode(technologyRouting.getProcessRouteCode());
        orderRouting.setDescription(technologyRouting.getDescription());
        orderRouting.setBomId(technologyRouting.getBomId());
        orderRouting.setOrderBomId(orderBom == null ? null : orderBom.getId());
        productionOrderRoutingMapper.insert(orderRouting);
        int syncedParamCount = 0;
@@ -184,15 +221,24 @@
                        .eq(TechnologyRoutingOperation::getTechnologyRoutingId, technologyRouting.getId())
                        .orderByAsc(TechnologyRoutingOperation::getDragSort)
                        .orderByAsc(TechnologyRoutingOperation::getId));
        Map<Long, String> operationNameMap = technologyOperationMapper.selectBatchIds(
                        routingOperations.stream()
                                .map(TechnologyRoutingOperation::getTechnologyOperationId)
                                .filter(Objects::nonNull)
                                .collect(Collectors.toSet()))
                .stream()
                .collect(Collectors.toMap(TechnologyOperation::getId, TechnologyOperation::getName, (a, b) -> a));
        for (TechnologyRoutingOperation sourceOperation : routingOperations) {
            // 订单工序保存的是工艺工序快照,后续报工只依赖快照,不再直接引用工艺主数据。
            ProductionOrderRoutingOperation targetOperation = new ProductionOrderRoutingOperation();
            targetOperation.setProductionOrderId(productionOrder.getId());
            targetOperation.setTechnologyRoutingOperationId(sourceOperation.getId());
            targetOperation.setTechnologyRoutingId(orderRouting.getId());
            targetOperation.setOrderRoutingId(orderRouting.getId());
            targetOperation.setProductModelId(sourceOperation.getProductModelId());
            targetOperation.setDragSort(sourceOperation.getDragSort());
            targetOperation.setIsQuality(sourceOperation.getIsQuality());
            targetOperation.setOperationName(operationNameMap.get(sourceOperation.getTechnologyOperationId()));
            targetOperation.setTechnologyOperationId(sourceOperation.getTechnologyOperationId());
            productionOrderRoutingOperationMapper.insert(targetOperation);
            ProductionOperationTask task = new ProductionOperationTask();
@@ -212,7 +258,7 @@
                // 工序执行参数同样做快照,避免工艺参数调整影响已下达订单。
                ProductionOrderRoutingOperationParam targetParam = new ProductionOrderRoutingOperationParam();
                targetParam.setProductionOrderId(productionOrder.getId());
                targetParam.setTechnologyRoutingOperationId(targetOperation.getId());
                targetParam.setProductionOrderRoutingOperationId(targetOperation.getId());
                targetParam.setTechnologyRoutingOperationParamId(sourceParam.getId());
                targetParam.setParamId(sourceParam.getParamId());
                targetParam.setTechnologyOperationId(sourceParam.getTechnologyOperationId());
@@ -229,19 +275,16 @@
                syncedParamCount++;
            }
        }
        syncProductionOrderBomSnapshot(productionOrder, technologyRouting);
        upsertOrderPick(productionOrder);
        return syncedParamCount;
    }
    private void syncProductionOrderBomSnapshot(ProductionOrder productionOrder, TechnologyRouting technologyRouting) {
    private ProductionOrderBom syncProductionOrderBomSnapshot(ProductionOrder productionOrder, TechnologyRouting technologyRouting) {
        if (technologyRouting.getBomId() == null) {
            return;
            return null;
        }
        TechnologyBom technologyBom = technologyBomMapper.selectById(technologyRouting.getBomId());
        if (technologyBom == null) {
            throw new ServiceException("Technology BOM not found");
            throw new ServiceException("工艺BOM不存在");
        }
        List<TechnologyBomStructure> structureList = technologyBomStructureMapper.selectList(
                Wrappers.<TechnologyBomStructure>lambdaQuery()
@@ -254,10 +297,9 @@
        orderBom.setProductionOrderId(productionOrder.getId());
        orderBom.setBomId(Long.valueOf(technologyBom.getId()));
        orderBom.setProductModelId(root != null ? root.getProductModelId() : productionOrder.getProductModelId());
        orderBom.setTechnologyOperationId(root == null ? null : root.getOperationId());
        orderBom.setUnitQuantity(root != null && root.getUnitQuantity() != null ? root.getUnitQuantity() : BigDecimal.ONE);
        orderBom.setDemandedQuantity(orderQuantity);
        orderBom.setUnit(root == null ? null : root.getUnit());
        orderBom.setRemark(technologyBom.getRemark());
        orderBom.setBomNo(technologyBom.getBomNo());
        orderBom.setVersion(technologyBom.getVersion());
        productionOrderBomMapper.insert(orderBom);
        Map<Long, Long> idMap = new HashMap<>();
@@ -270,11 +312,12 @@
            target.setProductModelId(source.getProductModelId());
            target.setTechnologyOperationId(source.getOperationId());
            target.setUnitQuantity(source.getUnitQuantity());
            target.setDemandedQuantity(resolveBomDemandQuantity(source, orderQuantity));
            target.setDemandedQuantity(source.getUnitQuantity().multiply(orderQuantity));
            target.setUnit(source.getUnit());
            productionBomStructureMapper.insert(target);
            idMap.put(source.getId(), target.getId());
        }
        return orderBom;
    }
    private void clearProductionSnapshot(Long productionOrderId) {
@@ -283,7 +326,7 @@
                Wrappers.<ProductionOrderPickRecord>lambdaQuery()
                        .eq(ProductionOrderPickRecord::getProductionOrderId, productionOrderId)) > 0;
        if (hasPickRecord) {
            throw new ServiceException("Production order pick records already exist, snapshot cannot be regenerated");
            throw new ServiceException("生产订单已存在领料记录,不能重新生成快照");
        }
        List<Long> taskIds = productionOperationTaskMapper.selectList(
                        Wrappers.<ProductionOperationTask>lambdaQuery()
@@ -295,7 +338,7 @@
                    Wrappers.<ProductionProductMain>lambdaQuery()
                            .in(ProductionProductMain::getProductionOperationTaskId, taskIds)) > 0;
            if (started) {
                throw new ServiceException("Production order already started, snapshot cannot be regenerated");
                throw new ServiceException("生产订单已开工,不能重新生成快照");
            }
            productionOperationTaskMapper.delete(Wrappers.<ProductionOperationTask>lambdaQuery()
                    .eq(ProductionOperationTask::getProductionOrderId, productionOrderId));
@@ -318,7 +361,6 @@
        ProductionOrder query = dto == null ? new ProductionOrder() : dto;
        return Wrappers.<ProductionOrder>lambdaQuery()
                .eq(query.getId() != null, ProductionOrder::getId, query.getId())
                .eq(query.getSalesLedgerId() != null, ProductionOrder::getSalesLedgerId, query.getSalesLedgerId())
                .eq(query.getProductModelId() != null, ProductionOrder::getProductModelId, query.getProductModelId())
                .eq(query.getTechnologyRoutingId() != null, ProductionOrder::getTechnologyRoutingId, query.getTechnologyRoutingId())
                .like(query.getNpsNo() != null && !query.getNpsNo().trim().isEmpty(), ProductionOrder::getNpsNo, query.getNpsNo())
@@ -368,15 +410,14 @@
    private void validateAndFillOrder(ProductionOrder productionOrder, ProductionOrder oldOrder) {
        if (productionOrder == null) {
            throw new ServiceException("Production order is required");
            throw new ServiceException("生产订单不能为空");
        }
        fillFromSalesLedgerProduct(productionOrder);
        fillFromProductionPlans(productionOrder);
        if (productionOrder.getProductModelId() == null) {
            throw new ServiceException("productModelId is required");
            throw new ServiceException("产品规格ID不能为空");
        }
        if (defaultDecimal(productionOrder.getQuantity()).compareTo(BigDecimal.ZERO) <= 0) {
            throw new ServiceException("quantity must be greater than 0");
            throw new ServiceException("下单数量必须大于0");
        }
        if (productionOrder.getTechnologyRoutingId() == null) {
            // 未显式指定工艺路线时,按产品规格选最新一条工艺作为默认路线。
@@ -385,47 +426,16 @@
                            .eq(TechnologyRouting::getProductModelId, productionOrder.getProductModelId())
                            .orderByDesc(TechnologyRouting::getId)
                            .last("limit 1"));
            if (technologyRouting == null) {
                throw new ServiceException("No technology routing found for the product model");
            if (technologyRouting != null) {
                productionOrder.setTechnologyRoutingId(technologyRouting.getId());
            }
            productionOrder.setTechnologyRoutingId(technologyRouting.getId());
        }
        if (oldOrder != null && ProductOrderStatusEnum.isStarted(oldOrder.getStatus())) {
            // 开工后只允许修正非核心字段,核心生产依据锁定。
            if (!Objects.equals(oldOrder.getProductModelId(), productionOrder.getProductModelId())
                    || !Objects.equals(oldOrder.getTechnologyRoutingId(), productionOrder.getTechnologyRoutingId())
                    || compareDecimal(oldOrder.getQuantity(), productionOrder.getQuantity()) != 0) {
                throw new ServiceException("Started production orders cannot modify product, routing or quantity");
            }
        }
    }
    private void fillFromSalesLedgerProduct(ProductionOrder productionOrder) {
        if (productionOrder.getSaleLedgerProductId() == null) {
            return;
        }
        // 销售明细是订单来源时,以销售明细为准回填销售台账、产品规格和默认数量。
        SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectById(productionOrder.getSaleLedgerProductId().longValue());
        if (salesLedgerProduct == null) {
            throw new ServiceException("Sales ledger product not found");
        }
        if (productionOrder.getSalesLedgerId() == null) {
            productionOrder.setSalesLedgerId(salesLedgerProduct.getSalesLedgerId());
        } else if (!Objects.equals(productionOrder.getSalesLedgerId(), salesLedgerProduct.getSalesLedgerId())) {
            throw new ServiceException("salesLedgerId does not match the sales ledger product");
        }
        if (productionOrder.getProductModelId() == null) {
            productionOrder.setProductModelId(salesLedgerProduct.getProductModelId());
        } else if (!Objects.equals(productionOrder.getProductModelId(), salesLedgerProduct.getProductModelId())) {
            throw new ServiceException("productModelId does not match the sales ledger product");
        }
        if (productionOrder.getQuantity() == null || productionOrder.getQuantity().compareTo(BigDecimal.ZERO) <= 0) {
            productionOrder.setQuantity(salesLedgerProduct.getQuantity());
        }
        if (productionOrder.getPlanCompleteTime() == null && productionOrder.getSalesLedgerId() != null) {
            SalesLedger salesLedger = salesLedgerMapper.selectById(productionOrder.getSalesLedgerId());
            if (salesLedger != null && salesLedger.getDeliveryDate() != null) {
                productionOrder.setPlanCompleteTime(salesLedger.getDeliveryDate());
                throw new ServiceException("生产订单已开工,不能修改产品、工艺路线或数量");
            }
        }
    }
@@ -438,22 +448,22 @@
        // 多计划合并转单时,所有计划必须属于同一规格,且只能下发一次。
        List<ProductionPlan> productionPlans = productionPlanMapper.selectBatchIds(planIds);
        if (productionPlans.size() != planIds.size()) {
            throw new ServiceException("Some production plans do not exist");
            throw new ServiceException("部分生产计划不存在");
        }
        Set<Long> productModelIds = productionPlans.stream()
                .map(ProductionPlan::getProductModelId)
                .collect(Collectors.toSet());
        if (productModelIds.size() > 1) {
            throw new ServiceException("Selected production plans must belong to the same product model");
            throw new ServiceException("所选生产计划必须属于同一产品规格");
        }
        if (Boolean.TRUE.equals(productionPlans.stream().anyMatch(item -> Boolean.TRUE.equals(item.getIssued())))) {
            throw new ServiceException("Selected production plans already issued");
        if (productionPlans.stream().anyMatch(item -> item.getStatus() != null && item.getStatus() == 2)) {
            throw new ServiceException("所选生产计划已下发");
        }
        ProductionPlan firstPlan = productionPlans.get(0);
        if (productionOrder.getProductModelId() == null) {
            productionOrder.setProductModelId(firstPlan.getProductModelId());
        } else if (!Objects.equals(productionOrder.getProductModelId(), firstPlan.getProductModelId())) {
            throw new ServiceException("productModelId does not match the production plans");
            throw new ServiceException("产品规格ID与生产计划不一致");
        }
        if (productionOrder.getQuantity() == null || productionOrder.getQuantity().compareTo(BigDecimal.ZERO) <= 0) {
            productionOrder.setQuantity(productionPlans.stream()
@@ -471,76 +481,73 @@
        productionOrder.setProductionPlanIds(formatPlanIds(planIds));
    }
    private void syncProductionPlanIssueStatus(ProductionOrder oldOrder, ProductionOrder newOrder) {
        // 只处理本次增量变化,避免无关计划被重复写状态。
        Set<Long> oldIds = new LinkedHashSet<>(parsePlanIds(oldOrder == null ? null : oldOrder.getProductionPlanIds()));
        Set<Long> newIds = new LinkedHashSet<>(parsePlanIds(newOrder == null ? null : newOrder.getProductionPlanIds()));
        Set<Long> toRelease = new LinkedHashSet<>(oldIds);
        toRelease.removeAll(newIds);
        Set<Long> toIssue = new LinkedHashSet<>(newIds);
        toIssue.removeAll(oldIds);
        if (!toRelease.isEmpty()) {
            updatePlanIssuedFlag(new ArrayList<>(toRelease), false);
        }
        if (!toIssue.isEmpty()) {
            updatePlanIssuedFlag(new ArrayList<>(toIssue), true);
        }
    }
    private void releaseProductionPlanIssueStatus(ProductionOrder productionOrder) {
        if (productionOrder == null) {
            return;
        }
        List<Long> planIds = parsePlanIds(productionOrder.getProductionPlanIds());
        if (!planIds.isEmpty()) {
            updatePlanIssuedFlag(planIds, false);
            // 生产订单删除--对应的生产计划的已下发数量要减去
            updatePlanIssuedFlag(planIds, productionOrder.getQuantity());
        }
    }
    private void updatePlanIssuedFlag(List<Long> planIds, boolean issued) {
    //生产订单删除,生产计划的已下发数量对应变更
    private void updatePlanIssuedFlag(List<Long> planIds, BigDecimal remainingAssignedQuantity) {
        if (planIds == null || planIds.isEmpty()) {
            return;
        }
        List<ProductionPlan> plans = productionPlanMapper.selectBatchIds(planIds);
        //下发数量减去
        List<ProductionPlan> updates = new ArrayList<>();
        for (ProductionPlan plan : plans) {
            BigDecimal requiredQuantity = Optional.ofNullable(plan.getQtyRequired()).orElse(BigDecimal.ZERO);
            if (requiredQuantity.compareTo(BigDecimal.ZERO) < 0) {
                requiredQuantity = BigDecimal.ZERO;
            }
            BigDecimal remainingQuantity = resolveRemainingQuantity(plan);
            BigDecimal historicalIssuedQuantity = requiredQuantity.subtract(remainingQuantity);
            BigDecimal issuedQuantity = remainingAssignedQuantity.min(historicalIssuedQuantity);
            remainingAssignedQuantity = remainingAssignedQuantity.subtract(issuedQuantity);
            BigDecimal totalIssuedQuantity = historicalIssuedQuantity.subtract(issuedQuantity);
            int planStatus = resolvePlanStatus(requiredQuantity, totalIssuedQuantity);
            ProductionPlan update = new ProductionPlan();
            update.setId(plan.getId());
            update.setIssued(issued);
            productionPlanMapper.updateById(update);
            update.setStatus(planStatus);
            update.setQuantityIssued(totalIssuedQuantity);
            updates.add(update);
        }
        if (!updates.isEmpty()) {
            productionPlanMapper.updateById(updates);
        }
    }
    private void upsertOrderPick(ProductionOrder productionOrder) {
        if (productionOrder == null || productionOrder.getId() == null) {
            return;
        }
        // 订单下达后自动生成一张备料主单,后续领料记录都挂在这张单上。
        ProductionOrderPick orderPick = productionOrderPickMapper.selectOne(
                Wrappers.<ProductionOrderPick>lambdaQuery()
                        .eq(ProductionOrderPick::getProductionOrderId, productionOrder.getId())
                        .last("limit 1"));
        if (orderPick == null) {
            orderPick = new ProductionOrderPick();
            orderPick.setProductionOrderId(productionOrder.getId());
        }
        orderPick.setProductModelId(productionOrder.getProductModelId() == null ? null : Math.toIntExact(productionOrder.getProductModelId()));
        orderPick.setQuantity(defaultDecimal(productionOrder.getQuantity()));
        orderPick.setRemark("下单自动生成");
        if (orderPick.getId() == null) {
            productionOrderPickMapper.insert(orderPick);
        } else {
            productionOrderPickMapper.updateById(orderPick);
        }
    }
    private BigDecimal resolveBomDemandQuantity(TechnologyBomStructure source, BigDecimal orderQuantity) {
        // 工艺 BOM 中的需求量按“单件需求 * 订单数量”展开成订单级需求。
        BigDecimal baseQuantity = source.getDemandedQuantity() != null ? source.getDemandedQuantity() : source.getUnitQuantity();
        baseQuantity = baseQuantity == null ? BigDecimal.ZERO : baseQuantity;
        if (baseQuantity.compareTo(BigDecimal.ZERO) <= 0) {
    private BigDecimal resolveRemainingQuantity(ProductionPlan plan) {
        if (plan == null) {
            return BigDecimal.ZERO;
        }
        return baseQuantity.multiply(orderQuantity);
        BigDecimal requiredQuantity = Optional.ofNullable(plan.getQtyRequired()).orElse(BigDecimal.ZERO);
        if (requiredQuantity.compareTo(BigDecimal.ZERO) <= 0) {
            return BigDecimal.ZERO;
        }
        BigDecimal issuedQuantity = Optional.ofNullable(plan.getQuantityIssued()).orElse(BigDecimal.ZERO);
        if (issuedQuantity.compareTo(BigDecimal.ZERO) <= 0) {
            return requiredQuantity;
        }
        if (issuedQuantity.compareTo(requiredQuantity) >= 0) {
            return BigDecimal.ZERO;
        }
        return requiredQuantity.subtract(issuedQuantity);
    }
    private int resolvePlanStatus(BigDecimal requiredQuantity, BigDecimal issuedQuantity) {
        if (requiredQuantity == null || requiredQuantity.compareTo(BigDecimal.ZERO) <= 0) {
            return 0;
        }
        if (issuedQuantity == null || issuedQuantity.compareTo(BigDecimal.ZERO) <= 0) {
            return 0;
        }
        return issuedQuantity.compareTo(requiredQuantity) < 0 ? 1 : 2;
    }
    private List<Long> parsePlanIds(String productionPlanIds) {
@@ -574,10 +581,10 @@
            return null;
        }
        if (productionPlan.getPromisedDeliveryDate() != null) {
            return productionPlan.getPromisedDeliveryDate().toLocalDate();
            return productionPlan.getPromisedDeliveryDate();
        }
        if (productionPlan.getRequiredDate() != null) {
            return productionPlan.getRequiredDate().toLocalDate();
            return productionPlan.getRequiredDate();
        }
        return null;
    }
@@ -585,4 +592,134 @@
    private int compareDecimal(BigDecimal left, BigDecimal right) {
        return defaultDecimal(left).compareTo(defaultDecimal(right));
    }
    private void fillProductImages(List<ProductionOrderVo> records) {
        if (records == null || records.isEmpty()) {
            return;
        }
        List<Long> productModelIds = records.stream()
                .map(ProductionOrderVo::getProductModelId)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());
        if (productModelIds.isEmpty()) {
            return;
        }
        List<StorageAttachment> attachments = storageAttachmentMapper.selectList(
                Wrappers.<StorageAttachment>lambdaQuery()
                        .in(StorageAttachment::getRecordId, productModelIds)
                        .eq(StorageAttachment::getApplication, StorageAttachmentConstants.StorageAttachmentImage)
                        .eq(StorageAttachment::getDeleted, 0L)
                        .orderByAsc(StorageAttachment::getId));
        if (attachments == null || attachments.isEmpty()) {
            return;
        }
        Map<Long, List<StorageAttachment>> attachmentMap = attachments.stream()
                .collect(Collectors.groupingBy(StorageAttachment::getRecordId, java.util.LinkedHashMap::new, Collectors.toList()));
        List<Long> blobIds = attachments.stream()
                .map(StorageAttachment::getStorageBlobId)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());
        if (blobIds.isEmpty()) {
            return;
        }
        Map<Long, StorageBlob> blobMap = storageBlobMapper.selectBatchIds(blobIds).stream()
                .filter(Objects::nonNull)
                .collect(Collectors.toMap(StorageBlob::getId, item -> item));
        for (ProductionOrderVo record : records) {
            List<StorageAttachment> modelAttachments = attachmentMap.get(record.getProductModelId());
            if (modelAttachments == null || modelAttachments.isEmpty()) {
                continue;
            }
            List<StorageBlobVO> images = modelAttachments.stream()
                    .map(StorageAttachment::getStorageBlobId)
                    .map(blobMap::get)
                    .filter(Objects::nonNull)
                    .map(this::toStorageBlobVO)
                    .collect(Collectors.toList());
            if (!images.isEmpty()) {
                record.setProductImages(images);
            }
        }
    }
    private StorageBlobVO toStorageBlobVO(StorageBlob blob) {
        StorageBlobVO vo = BeanUtil.copyProperties(blob, StorageBlobVO.class);
        vo.setPreviewURL(fileUtil.buildSignedPreviewUrl(vo));
        vo.setDownloadURL(fileUtil.buildSignedDownloadUrl(vo));
        return vo;
    }
    @Override
    public List<ProductionOrderPickVo> pick(Long productionOrderId) {
        if (productionOrderId == null) {
            return Collections.emptyList();
        }
        ProductionOrderBom orderBom = productionOrderBomMapper.selectOne(
                Wrappers.<ProductionOrderBom>lambdaQuery()
                        .eq(ProductionOrderBom::getProductionOrderId, productionOrderId)
                        .orderByDesc(ProductionOrderBom::getId)
                        .last("limit 1"));
        if (orderBom == null || orderBom.getId() == null) {
            return Collections.emptyList();
        }
        List<ProductionBomStructureVo> bomStructureList = productionBomStructureMapper.pickByBomId(orderBom.getId());
        if (bomStructureList == null || bomStructureList.isEmpty()) {
            return Collections.emptyList();
        }
        List<Long> productModelIds = bomStructureList.stream()
                .map(ProductionBomStructureVo::getProductModelId)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());
        Map<Long, BigDecimal> stockQuantityMap = new HashMap<>();
        Map<Long, LinkedHashSet<String>> stockBatchNoMap = new HashMap<>();
        if (!productModelIds.isEmpty()) {
            List<StockInventory> stockList = stockInventoryMapper.selectList(
                    Wrappers.<StockInventory>lambdaQuery()
                            .in(StockInventory::getProductModelId, productModelIds));
            for (StockInventory stockItem : stockList) {
                if (stockItem == null || stockItem.getProductModelId() == null) {
                    continue;
                }
                Long productModelId = stockItem.getProductModelId();
                stockQuantityMap.merge(productModelId, defaultDecimal(stockItem.getQualitity()), BigDecimal::add);
                String batchNo = stockItem.getBatchNo();
                if (batchNo != null && !batchNo.trim().isEmpty()) {
                    stockBatchNoMap.computeIfAbsent(productModelId, key -> new LinkedHashSet<>()).add(batchNo);
                }
            }
        }
        List<ProductionOrderPickVo> result = new ArrayList<>(bomStructureList.size());
        for (ProductionBomStructureVo structure : bomStructureList) {
            if (structure == null || structure.getProductModelId() == null) {
                continue;
            }
            Long productModelId = structure.getProductModelId();
            ProductionOrderPickVo vo = new ProductionOrderPickVo();
            vo.setProductModelId(productModelId);
            vo.setOperationName(structure.getOperationName());
            vo.setTechnologyOperationId(structure.getTechnologyOperationId());
            vo.setProductName(structure.getProductName());
            vo.setModel(structure.getModel());
            vo.setDemandedQuantity(defaultDecimal(structure.getDemandedQuantity()));
            vo.setUnit(structure.getUnit());
            List<String> batchNoList = stockBatchNoMap.get(productModelId) == null
                    ? Collections.emptyList()
                    : new ArrayList<>(stockBatchNoMap.get(productModelId));
            vo.setBatchNoList(batchNoList);
            vo.setStockQuantity(stockQuantityMap.getOrDefault(productModelId, BigDecimal.ZERO));
            vo.setBom(true);
            result.add(vo);
        }
        return result;
    }
}