From 467615249613c312a984097d3ebb673ebef3e10d Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期五, 20 三月 2026 16:08:58 +0800
Subject: [PATCH] fix: 生产订单内的工艺路线新增工序时基本参数未同步到子表

---
 src/main/java/com/ruoyi/production/service/impl/ProductOrderServiceImpl.java |  250 +++++++++++++++++++++++++++++++------------------
 1 files changed, 157 insertions(+), 93 deletions(-)

diff --git a/src/main/java/com/ruoyi/production/service/impl/ProductOrderServiceImpl.java b/src/main/java/com/ruoyi/production/service/impl/ProductOrderServiceImpl.java
index 2ef08c1..ef1db88 100644
--- a/src/main/java/com/ruoyi/production/service/impl/ProductOrderServiceImpl.java
+++ b/src/main/java/com/ruoyi/production/service/impl/ProductOrderServiceImpl.java
@@ -1,23 +1,29 @@
 package com.ruoyi.production.service.impl;
 
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
+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.common.enums.StockOutQualifiedRecordTypeEnum;
-import com.ruoyi.common.enums.StockInUnQualifiedRecordTypeEnum;
+import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.procurementrecord.utils.StockUtils;
 import com.ruoyi.production.dto.ProductOrderDto;
+import com.ruoyi.production.dto.ProductOrderSourceDto;
 import com.ruoyi.production.dto.ProductStructureDto;
+import com.ruoyi.production.enums.ProductOrderStatusEnum;
 import com.ruoyi.production.mapper.*;
 import com.ruoyi.production.pojo.*;
+import com.ruoyi.production.service.IProductionOrderAppendixService;
 import com.ruoyi.production.service.ProductOrderService;
+import com.ruoyi.productionPlan.mapper.ProductOrderPlanMapper;
+import com.ruoyi.productionPlan.mapper.ProductionPlanMapper;
+import com.ruoyi.productionPlan.pojo.ProductOrderPlan;
+import com.ruoyi.productionPlan.pojo.ProductionPlan;
 import com.ruoyi.quality.mapper.QualityInspectMapper;
-import com.ruoyi.quality.pojo.QualityInspect;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 import java.math.BigDecimal;
 import java.time.LocalDate;
@@ -50,6 +56,12 @@
     private ProductionProductMainMapper productionProductMainMapper;
 
     @Autowired
+    private ProductOrderPlanMapper productOrderPlanMapper;
+
+    @Autowired
+    private ProductionPlanMapper productionPlanMapper;
+
+    @Autowired
     private ProductionProductOutputMapper productionProductOutputMapper;
 
     @Autowired
@@ -64,8 +76,11 @@
     @Autowired
     private StockUtils stockUtils;
 
+    @Autowired
+    private IProductionOrderAppendixService productionOrderAppendixService;
+
     @Override
-    public IPage<ProductOrderDto> pageProductOrder(Page page, ProductOrderDto productOrder) {
+    public IPage<ProductOrderDto> pageProductOrder(Page<ProductOrder> page, ProductOrderDto productOrder) {
         return productOrderMapper.pageProductOrder(page, productOrder);
     }
 
@@ -122,6 +137,52 @@
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
+    public Boolean revoke(List<Long> ids) {
+        List<ProductOrder> orders = productOrderMapper.selectBatchIds(ids);
+        if (orders.isEmpty()) {
+            throw new RuntimeException("鐢熶骇璁㈠崟涓嶅瓨鍦�");
+        }
+        for (ProductOrder order : orders) {
+            if (!ProductOrderStatusEnum.canRevoke(order.getStatus())) {
+                throw new RuntimeException("鍙湁銆愬緟寮�濮嬨�戠姸鎬佺殑璁㈠崟鎵嶅彲浠ユ挙鍥�");
+            }
+        }
+
+        // 鍥為��鐢熶骇璁″垝
+        List<ProductOrderPlan> productOrderPlans = productOrderPlanMapper.selectList(Wrappers.<ProductOrderPlan>lambdaQuery().in(ProductOrderPlan::getProductOrderId, ids));
+        for (ProductOrderPlan productOrderPlan : productOrderPlans) {
+            ProductionPlan productionPlan = productionPlanMapper.selectById(productOrderPlan.getProductionPlanId());
+            if (productionPlan != null) {
+                BigDecimal newAssigned = productionPlan.getAssignedQuantity().subtract(productOrderPlan.getAssignedQuantity());
+                if (newAssigned.compareTo(BigDecimal.ZERO) < 0) {
+                    newAssigned = BigDecimal.ZERO;
+                }
+                productionPlan.setAssignedQuantity(newAssigned);
+                BigDecimal volume = productionPlan.getVolume() == null ? BigDecimal.ZERO : productionPlan.getVolume();
+                int status;
+                if (newAssigned.compareTo(BigDecimal.ZERO) == 0) {
+                    status = 0; // 鏈笅鍙�
+                } else if (newAssigned.compareTo(volume) < 0) {
+                    status = 1; // 閮ㄥ垎涓嬪彂
+                } else {
+                    status = 2; // 宸蹭笅鍙�
+                }
+                productionPlan.setStatus(status);
+                productionPlanMapper.updateById(productionPlan);
+            }
+        }
+
+        // 灏嗚鍗曠姸鎬佹敼涓哄凡鍙栨秷
+        for (ProductOrder order : orders) {
+            order.setStatus(ProductOrderStatusEnum.CANCEL.getCode());
+        }
+        updateBatchById(orders);
+
+        return true;
+    }
+
+    @Override
     public List<ProcessRoute> listProcessRoute(Long productModelId) {
         return productOrderMapper.listProcessRoute(productModelId);
     }
@@ -133,104 +194,95 @@
 
     @Override
     public Boolean addProductOrder(ProductOrder productOrder) {
-        String string = generateNextOrderNo(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")));
-        productOrder.setNpsNo(string);
+        fillAndSaveProductOrder(productOrder);
+        return true;
+    }
+
+    @Override
+    public Long insertProductOrder(ProductOrder productOrder) {
+        fillAndSaveProductOrder(productOrder);
+        return productOrder.getId();
+    }
+
+    private void fillAndSaveProductOrder(ProductOrder productOrder) {
+        String orderNo = generateNextOrderNo(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")));
+        productOrder.setNpsNo(orderNo);
         productOrder.setCompleteQuantity(BigDecimal.ZERO);
         this.save(productOrder);
         if (ObjectUtils.isNotEmpty(productOrder.getRouteId())) {
             this.bindingRoute(productOrder);
         }
-        return true;
     }
 
     @Override
-    public Boolean delete(Long[] ids) {
+    @Transactional(rollbackFor = Exception.class)
+    public Boolean delete(Long id) {
+        ProductOrder order = productOrderMapper.selectById(id);
 
-        //鎵归噺鏌ヨproductOrder
-        List<ProductOrder> productOrders = productOrderMapper.selectList(
-                new LambdaQueryWrapper<ProductOrder>()
-                        .in(ProductOrder::getId, ids)
-        );
-        if (!org.springframework.util.CollectionUtils.isEmpty(productOrders)) {
-
-
-            // 鎵归噺鏌ヨprocessRouteItems
-            List<ProductProcessRouteItem> allRouteItems = productProcessRouteItemMapper.selectList(
-                    new LambdaQueryWrapper<ProductProcessRouteItem>()
-                            .in(ProductProcessRouteItem::getProductOrderId, ids)
-            );
-
-            if (!com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isEmpty(allRouteItems)) {
-                // 鑾峰彇瑕佸垹闄ょ殑宸ュ簭椤笽D
-                List<Long> routeItemIds = allRouteItems.stream()
-                        .map(ProductProcessRouteItem::getId)
-                        .collect(Collectors.toList());
-
-                // 鏌ヨ鍏宠仈鐨勫伐鍗旾D
-                List<ProductWorkOrder> workOrders = productWorkOrderMapper.selectList(
-                        new LambdaQueryWrapper<ProductWorkOrder>()
-                                .in(ProductWorkOrder::getProductProcessRouteItemId, routeItemIds)
-                );
-                if (!com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isEmpty(workOrders)) {
-                    List<Long> workOrderIds = workOrders.stream()
-                            .map(ProductWorkOrder::getId)
-                            .collect(Collectors.toList());
-
-                    // 鏌ヨ鍏宠仈鐨勭敓浜т富琛↖D
-                    List<ProductionProductMain> productMains = productionProductMainMapper.selectList(
-                            new LambdaQueryWrapper<ProductionProductMain>()
-                                    .in(ProductionProductMain::getWorkOrderId, workOrderIds)
-                    );
-                    List<Long> productMainIds = productMains.stream()
-                            .map(ProductionProductMain::getId)
-                            .collect(Collectors.toList());
-
-                    // 鍒犻櫎浜у嚭琛ㄣ�佹姇鍏ヨ〃鏁版嵁
-                    if (!com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isEmpty(productMainIds)) {
-                        productionProductOutputMapper.deleteByProductMainIds(productMainIds);
-                        productionProductInputMapper.deleteByProductMainIds(productMainIds);
-                        List<QualityInspect> qualityInspects = qualityInspectMapper.selectList(
-                                new LambdaQueryWrapper<QualityInspect>()
-                                        .in(QualityInspect::getProductMainId, productMainIds)
-                        );
-                        //鍒犻櫎鍑哄簱璁板綍
-                        for (Long productMainId : productMainIds) {
-                            //鍒犻櫎鐢熶骇鍑哄簱璁板綍
-                            stockUtils.deleteStockOutRecord(productMainId, StockOutQualifiedRecordTypeEnum.PRODUCTION_REPORT_STOCK_OUT.getCode());
-                            //鍒犻櫎鎶ュ簾鐨勫叆搴撹褰�
-                            stockUtils.deleteStockInRecord(productMainId, StockInUnQualifiedRecordTypeEnum.PRODUCTION_SCRAP.getCode());
-                        }
-                        qualityInspects.forEach(qualityInspect -> {
-                            //inspectState=1 宸叉彁浜� 涓嶈兘鍒犻櫎
-                            if (qualityInspect.getInspectState() == 1) {
-                                throw new RuntimeException("宸叉彁浜ょ殑妫�楠屽崟涓嶈兘鍒犻櫎");
-                            }
-                        });
-                        qualityInspectMapper.deleteByProductMainIds(productMainIds);
-                        salesLedgerProductionAccountingMapper.delete(new LambdaQueryWrapper<SalesLedgerProductionAccounting>()
-                                .in(SalesLedgerProductionAccounting::getProductMainId, productMainIds));
-                    }
-
-                    // 鍒犻櫎鐢熶骇涓昏〃鏁版嵁
-                    productionProductMainMapper.deleteByWorkOrderIds(workOrderIds);
-
-                    // 鍒犻櫎宸ュ崟鏁版嵁
-                    productWorkOrderMapper.delete(new LambdaQueryWrapper<ProductWorkOrder>()
-                            .in(ProductWorkOrder::getProductProcessRouteItemId, routeItemIds));
-                }
-            }
-            // 鎵归噺鍒犻櫎processRouteItem
-            productProcessRouteItemMapper.delete(new LambdaQueryWrapper<ProductProcessRouteItem>()
-                    .in(ProductProcessRouteItem::getProductOrderId, ids));
-
-            // 鎵归噺鍒犻櫎productProcessRoute
-            productProcessRouteMapper.delete(new LambdaQueryWrapper<ProductProcessRoute>()
-                    .in(ProductProcessRoute::getProductOrderId, ids));
-
-            // 鎵归噺鍒犻櫎productOrder
-            productOrderMapper.delete(new LambdaQueryWrapper<ProductOrder>()
-                    .in(ProductOrder::getId, ids));
+        if (order == null) {
+            throw new RuntimeException("鐢熶骇璁㈠崟涓嶅瓨鍦�");
         }
+        if (!ProductOrderStatusEnum.canDelete(order.getStatus())) {
+            throw new RuntimeException("鍙湁銆愬緟寮�濮嬨�佸凡鍙栨秷銆戠姸鎬佺殑璁㈠崟鎵嶅彲浠ュ垹闄�");
+        }
+
+        //  鏄惁宸茬敓浜�
+        List<ProductWorkOrder> productWorkOrders = productWorkOrderMapper.selectList(Wrappers.<ProductWorkOrder>lambdaQuery().eq(ProductWorkOrder::getProductOrderId, id));
+
+        if (!productWorkOrders.isEmpty()) {
+            List<Long> workOrderIds = productWorkOrders.stream()
+                    .map(ProductWorkOrder::getId)
+                    .collect(Collectors.toList());
+
+            List<ProductionProductMain> productionProductMains = productionProductMainMapper.selectList(Wrappers.<ProductionProductMain>lambdaQuery().in(ProductionProductMain::getWorkOrderId, workOrderIds));
+            if (!productionProductMains.isEmpty()) {
+                throw new RuntimeException("鐢熶骇璁㈠崟宸茬粡寮�濮嬬敓浜�,涓嶈兘鍒犻櫎");
+            }
+
+            //  鍒犻櫎宸ュ崟
+            productWorkOrderMapper.delete(Wrappers.<ProductWorkOrder>lambdaQuery().eq(ProductWorkOrder::getProductOrderId, id));
+        }
+
+        //  鍥為��鐢熶骇璁″垝
+        List<ProductOrderPlan> productOrderPlans = productOrderPlanMapper.selectList(Wrappers.<ProductOrderPlan>lambdaQuery().eq(ProductOrderPlan::getProductOrderId, id));
+
+        for (ProductOrderPlan productOrderPlan : productOrderPlans) {
+            ProductionPlan productionPlan = productionPlanMapper.selectById(productOrderPlan.getProductionPlanId());
+
+            if (productionPlan != null) {
+                //  鍥為��鏁伴噺
+                BigDecimal newAssigned = productionPlan.getAssignedQuantity()
+                        .subtract(productOrderPlan.getAssignedQuantity());
+
+                if (newAssigned.compareTo(BigDecimal.ZERO) < 0) {
+                    newAssigned = BigDecimal.ZERO;
+                }
+                productionPlan.setAssignedQuantity(newAssigned);
+                BigDecimal volume = productionPlan.getVolume() == null ? BigDecimal.ZERO : productionPlan.getVolume();
+
+                int status;
+                if (newAssigned.compareTo(BigDecimal.ZERO) == 0) {
+                    status = 0; // 鏈笅鍙�
+                } else if (newAssigned.compareTo(volume) < 0) {
+                    status = 1; // 閮ㄥ垎涓嬪彂
+                } else {
+                    status = 2; // 宸蹭笅鍙�
+                }
+                productionPlan.setStatus(status);
+
+                productionPlanMapper.updateById(productionPlan);
+            }
+        }
+
+        // 鍒犻櫎涓棿琛�
+        productOrderPlanMapper.delete(Wrappers.<ProductOrderPlan>lambdaQuery().eq(ProductOrderPlan::getProductOrderId, id));
+
+        //  鍒犻櫎闄勮〃鐨勫伐鑹鸿矾绾夸笌BOM
+        productionOrderAppendixService.deleteData(order.getId(), order.getRouteId());
+
+        //  鍒犻櫎璁㈠崟
+        productOrderMapper.deleteById(id);
+
         return true;
     }
 
@@ -264,5 +316,17 @@
         return "SC" + datePrefix + String.format("%04d", sequence);
     }
 
+    @Override
+    public List<ProductOrderSourceDto> productOrderSource(Long orderId) {
+        if (orderId == null) {
+            throw new ServiceException("鏌ヨ璁㈠崟鏁版嵁涓嶈兘涓虹┖");
+        }
+        ProductOrder productOrder = getById(orderId);
+        if (productOrder == null) {
+            throw new ServiceException("鏌ヨ澶辫触,鐢熶骇璁㈠崟涓嶅瓨鍦�");
+        }
 
+        List<ProductOrderSourceDto> list = baseMapper.productOrderSource(orderId);
+        return list;
+    }
 }

--
Gitblit v1.9.3