From a97eb69a7233fc36a794832210fb1b0de037f2bb Mon Sep 17 00:00:00 2001
From: liding <756868258@qq.com>
Date: 星期三, 15 四月 2026 16:39:53 +0800
Subject: [PATCH] fix:生产核算-对应工序

---
 src/main/java/com/ruoyi/production/service/impl/ProductionProductMainServiceImpl.java |   81 ++++++++++++++++++++++++++--------------
 1 files changed, 52 insertions(+), 29 deletions(-)

diff --git a/src/main/java/com/ruoyi/production/service/impl/ProductionProductMainServiceImpl.java b/src/main/java/com/ruoyi/production/service/impl/ProductionProductMainServiceImpl.java
index 578ea2d..9d9101e 100644
--- a/src/main/java/com/ruoyi/production/service/impl/ProductionProductMainServiceImpl.java
+++ b/src/main/java/com/ruoyi/production/service/impl/ProductionProductMainServiceImpl.java
@@ -93,6 +93,7 @@
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public Boolean addProductMain(ProductionProductMainDto dto) {
         SysUser user = userMapper.selectUserById(dto.getUserId());
         ProductionProductMain productionProductMain = new ProductionProductMain();
@@ -143,28 +144,32 @@
         productionProductMain.setDeviceId(dto.getDeviceId());
         productionProductMainMapper.insert(productionProductMain);
         /* 鏂板鎶ュ伐鎶曞叆琛� */
+        ProductWorkOrder WorkOrder = productWorkOrderMapper.selectById(dto.getWorkOrderId());
+        if (WorkOrder == null) {
+            throw new RuntimeException("宸ュ崟涓嶅瓨鍦�");
+        }
+
+        ProductOrder order = productOrderMapper.selectById(WorkOrder.getProductOrderId());
+        if (order == null) {
+            throw new RuntimeException("浜у搧璁㈠崟涓嶅瓨鍦�");
+        }
         List<DrawMaterialDto> drawMaterialList = dto.getDrawMaterialList();
         if (!CollectionUtils.isEmpty(drawMaterialList)) {
-            // 1. 鎵归噺鏌ヨ鏁版嵁
-            ProductWorkOrder productWorkOrder = productWorkOrderMapper.selectById(dto.getWorkOrderId());
-            if (productWorkOrder == null) {
-                throw new RuntimeException("宸ュ崟涓嶅瓨鍦�");
-            }
 
-            ProductOrder productOrder = productOrderMapper.selectById(productWorkOrder.getProductOrderId());
-            if (productOrder == null) {
-                throw new RuntimeException("浜у搧璁㈠崟涓嶅瓨鍦�");
-            }
-
-            // 2. 瑙f瀽骞舵瀯寤虹墿鏂橫ap
-            List<DrawMaterialDto> existingMaterialList = JSON.parseArray(productOrder.getDrawMaterials(), DrawMaterialDto.class);
+            // 鐗╂枡Map
+            List<DrawMaterialDto> existingMaterialList = JSON.parseArray(order.getDrawMaterials(), DrawMaterialDto.class);
             if (CollectionUtils.isEmpty(existingMaterialList)) {
                 throw new RuntimeException("鍙鐢ㄧ墿鏂欏垪琛ㄤ负绌�");
             }
 
-            Map<Long, DrawMaterialDto> materialMap = existingMaterialList.stream()
-                    .collect(Collectors.toMap(DrawMaterialDto::getProductModelId,
-                            Function.identity()));
+            Map<String, DrawMaterialDto> materialMap = existingMaterialList.stream()
+                    .collect(Collectors.toMap(
+                            materialDto -> materialDto.getProductModelId() + "_" +
+                                    (materialDto.getBatchNo() == null ? "" : materialDto.getBatchNo()) + "_" +
+                                    (materialDto.getCustomer() == null ? "" : materialDto.getCustomer()),
+                            Function.identity(),
+                            (existing, replacement) -> existing
+                    ));
 
             // 澶勭悊鎶ュ伐鐗╂枡
             List<ProductionProductInput> inputList = new ArrayList<>();
@@ -173,9 +178,21 @@
                 Long modelId = drawMaterial.getProductModelId();
                 BigDecimal reportQty = drawMaterial.getReportQty();
 
-                DrawMaterialDto material = materialMap.get(modelId);
+                if (reportQty != null && reportQty.compareTo(new BigDecimal(0)) == 0) {
+                    throw new RuntimeException("浜у搧" + drawMaterial.getProductName() + "- 鍨嬪彿" + drawMaterial.getModel() +
+                            "- 鎵规鍙�=" + drawMaterial.getBatchNo() + "鐨勬枡宸茬敤瀹岋紝璇烽噸鏂伴鏂�");
+
+                }
+
+                String key = drawMaterial.getProductModelId() + "_" +
+                        (drawMaterial.getBatchNo() == null ? "" : drawMaterial.getBatchNo()) + "_" +
+                        (drawMaterial.getCustomer() == null ? "" : drawMaterial.getCustomer());
+
+                DrawMaterialDto material = materialMap.get(key);
                 if (material == null) {
-                    throw new RuntimeException("鐗╂枡涓嶅瓨鍦�: " + modelId);
+                    throw new RuntimeException("鐗╂枡涓嶅瓨鍦�: 浜у搧鍨嬪彿ID=" + modelId +
+                            ", 鎵规鍙�=" + drawMaterial.getBatchNo() +
+                            ", 瀹㈡埛=" + drawMaterial.getCustomer());
                 }
 
                 // 楠岃瘉搴撳瓨
@@ -194,6 +211,8 @@
                 input.setQuantity(reportQty);
                 input.setProductMainId(productionProductMain.getId());
                 input.setRemark(drawMaterial.getRemark());
+                input.setBatchNo(drawMaterial.getBatchNo());
+                input.setCustomer(drawMaterial.getCustomer());
                 inputList.add(input);
             }
 
@@ -201,9 +220,11 @@
                 for (ProductionProductInput productionProductInput : inputList) {
                     productionProductInputMapper.insert(productionProductInput);
                 }
-                productOrder.setDrawMaterials(JSON.toJSONString(existingMaterialList));
-                productOrderMapper.updateById(productOrder);
+                order.setDrawMaterials(JSON.toJSONString(existingMaterialList));
+                productOrderMapper.updateById(order);
             }
+        } else {
+            throw new RuntimeException("璇烽鍙栨姇鍏ユ潗鏂�");
         }
         /*鏂板鎶ュ伐浜у嚭琛�*/
         ProductionProductOutput productionProductOutput = new ProductionProductOutput();
@@ -216,6 +237,8 @@
         //鍚堟牸鏁伴噺=鎶ュ伐鏁伴噺
         BigDecimal productQty = productionProductOutput.getQuantity();
         //鍙湁鍚堟牸鏁伴噺>0鎵嶈兘澧炲姞鐩稿簲鏁版嵁
+        ProductWorkOrder pwo = productWorkOrderMapper.selectById(productionProductMain.getWorkOrderId());
+        String customer = "闀挎不甯傝酱鎵垮埗閫犳湁闄愬叕鍙�";
         if (productQty.compareTo(BigDecimal.ZERO) > 0) {
             /*鏂板璐ㄦ*/
             List<ProductProcessRouteItem> productProcessRouteItems = productProcessRouteItemMapper.selectList(Wrappers.<ProductProcessRouteItem>lambdaQuery().eq(ProductProcessRouteItem::getProductRouteId, productProcessRouteItem.getProductRouteId()));
@@ -228,12 +251,10 @@
                     inspectType = 2;
                     process = null;
                 }
-                ProductWorkOrder productWorkOrder = productWorkOrderMapper.selectById(productionProductMain.getWorkOrderId());
-                ProductOrder productOrder = productOrderMapper.selectById(productWorkOrder.getProductOrderId());
+                ProductOrder productOrder = productOrderMapper.selectById(pwo.getProductOrderId());
                 if (productOrder == null) {
                     throw new RuntimeException("鐢熶骇璁㈠崟涓嶅瓨鍦�");
                 }
-
 
                 Product product = productMapper.selectById(productModel.getProductId());
                 QualityInspect qualityInspect = new QualityInspect();
@@ -249,6 +270,7 @@
                 qualityInspect.setProductModelId(productModel.getId());
                 qualityInspect.setBatchNo(productOrder.getBatchNo());
                 qualityInspect.setManufacturingTeam(productOrder.getManufacturingTeam());
+                qualityInspect.setProductionDate(pwo.getActualStartTime() != null ? pwo.getActualStartTime() : LocalDate.now());
                 qualityInspectMapper.insert(qualityInspect);
                 List<QualityTestStandard> qualityTestStandard = qualityTestStandardMapper.getQualityTestStandardByProductId(product.getId(), inspectType, process);
                 if (qualityTestStandard.size() > 0) {
@@ -266,7 +288,9 @@
                 }
             } else {
                 //鐩存帴鍏ュ簱
-                stockUtils.addStock(productProcessRouteItem.getProductModelId(), productionProductOutput.getQuantity().subtract(productionProductOutput.getScrapQty()), StockInQualifiedRecordTypeEnum.PRODUCTION_REPORT_STOCK_IN.getCode(), productionProductMain.getId());
+                stockUtils.addStock(productProcessRouteItem.getProductModelId(), productionProductOutput.getQuantity().subtract(productionProductOutput.getScrapQty()),
+                        StockInQualifiedRecordTypeEnum.PRODUCTION_REPORT_STOCK_IN.getCode(), productionProductMain.getId(), order.getBatchNo(), customer, pwo.getActualStartTime() != null ? pwo.getActualStartTime() : LocalDate.now()
+                );
             }
             /*鏇存柊宸ュ崟鍜岀敓浜ц鍗�*/
             ProductWorkOrder productWorkOrder = productWorkOrderMapper.selectById(dto.getWorkOrderId());
@@ -292,10 +316,8 @@
                 }
             }
             productOrderMapper.updateById(productOrder);
-            /*娣诲姞鐢熶骇鏍哥畻        鍖哄垎宸ュ簭鏄浠惰繕鏄鏃�*/
-            BigDecimal workHours = (productProcess.getType() == 1)
-                    ? productProcess.getSalaryQuota().multiply(productQty)
-                    : productProcess.getSalaryQuota();
+            /*娣诲姞鐢熶骇鏍哥畻*/
+            BigDecimal workHours = productProcess.getSalaryQuota();
 
             SalesLedgerProductionAccounting salesLedgerProductionAccounting = SalesLedgerProductionAccounting.builder()
                     .productMainId(productionProductMain.getId())
@@ -312,7 +334,8 @@
         //濡傛灉鎶ュ簾鏁伴噺>0,闇�瑕佽繘鍏ユ姤搴熺殑搴撳瓨
         if (ObjectUtils.isNotEmpty(dto.getScrapQty())) {
             if (dto.getScrapQty().compareTo(BigDecimal.ZERO) > 0) {
-                stockUtils.addUnStock(productModel.getId(), dto.getScrapQty(), StockInUnQualifiedRecordTypeEnum.PRODUCTION_SCRAP.getCode(), productionProductMain.getId());
+                stockUtils.addUnStock(productModel.getId(), dto.getScrapQty(), StockInUnQualifiedRecordTypeEnum.PRODUCTION_SCRAP.getCode(), productionProductMain.getId(),
+                        order.getBatchNo(), customer, pwo.getActualStartTime() != null ? pwo.getActualStartTime() : LocalDate.now());
             }
         }
         return true;
@@ -322,7 +345,7 @@
     public Boolean removeProductMain(Long id) {
         //鍒ゆ柇璇ユ潯鎶ュ伐鏄惁涓嶅悎鏍煎鐞�,濡傛灉涓嶅悎鏍煎鐞嗕簡锛屽垯涓嶅厑璁稿垹闄�
         List<QualityInspect> qualityInspects = qualityInspectMapper.selectList(Wrappers.<QualityInspect>lambdaQuery().eq(QualityInspect::getProductMainId, id));
-        if (qualityInspects.size() > 0) {
+        if (!qualityInspects.isEmpty()) {
             List<QualityUnqualified> qualityUnqualifieds = qualityUnqualifiedMapper.selectList(Wrappers.<QualityUnqualified>lambdaQuery()
                     .in(QualityUnqualified::getInspectId, qualityInspects.stream().map(QualityInspect::getId).collect(Collectors.toList())));
             if (qualityUnqualifieds.size() > 0 && qualityUnqualifieds.get(0).getInspectState() == 1) {

--
Gitblit v1.9.3