From f4c288c55d08c04cd026508b358beebfcdce5fc2 Mon Sep 17 00:00:00 2001
From: liding <756868258@qq.com>
Date: 星期五, 22 五月 2026 09:29:09 +0800
Subject: [PATCH] feat(product): 添加产品型号向下复制功能并优化生产报工重量计算 - 在ProductController中新增downCopy接口实现产品型号批量复制功能 - 将ProductionProductMainDto中的bomInputQty字段重命名为inputWeight - 在ProductionProductMainServiceImpl中添加JSON解析逻辑支持从otherData中提取投入重量 - 新增resolveInputWeight、findParameterValue、findFieldValue等工具方法处理复杂参数解析 - 为ProductModelDto添加targetProductId字段用于指定复制目标 - 修复销售台账按调度员ID和姓名分组的SQL查询问题 - 优化库存服务中剩余数量计算的空值处理逻辑 - 完善生产投料数量为空时的默认值处理机制

---
 src/main/java/com/ruoyi/production/service/impl/ProductWorkOrderServiceImpl.java |  286 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 281 insertions(+), 5 deletions(-)

diff --git a/src/main/java/com/ruoyi/production/service/impl/ProductWorkOrderServiceImpl.java b/src/main/java/com/ruoyi/production/service/impl/ProductWorkOrderServiceImpl.java
index 3d2d7e3..b73d629 100644
--- a/src/main/java/com/ruoyi/production/service/impl/ProductWorkOrderServiceImpl.java
+++ b/src/main/java/com/ruoyi/production/service/impl/ProductWorkOrderServiceImpl.java
@@ -2,26 +2,302 @@
 
 
 import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
+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.deepoove.poi.XWPFTemplate;
+import com.deepoove.poi.data.PictureRenderData;
+import com.deepoove.poi.data.Pictures;
+import com.ruoyi.common.utils.MatrixToImageWriter;
 import com.ruoyi.production.dto.ProductWorkOrderDto;
-import com.ruoyi.production.mapper.ProductWorkOrderMapper;
-import com.ruoyi.production.pojo.ProductWorkOrder;
+import com.ruoyi.production.dto.ProductStructureDto;
+import com.ruoyi.production.mapper.*;
+import com.ruoyi.production.pojo.*;
 import com.ruoyi.production.service.ProductWorkOrderService;
-import lombok.AllArgsConstructor;
+import com.ruoyi.quality.mapper.QualityInspectMapper;
+import com.ruoyi.quality.mapper.QualityUnqualifiedMapper;
+import com.ruoyi.quality.pojo.QualityInspect;
+import com.ruoyi.quality.pojo.QualityUnqualified;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import javax.servlet.http.HttpServletResponse;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.net.URLEncoder;
+import java.util.*;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
 @Service
-@AllArgsConstructor
 @Transactional(rollbackFor = Exception.class)
 public class ProductWorkOrderServiceImpl extends ServiceImpl<ProductWorkOrderMapper, ProductWorkOrder> implements ProductWorkOrderService {
 
+    @Autowired
     private ProductWorkOrderMapper productWorkOrdermapper;
+    @Autowired
+    private ProductWorkOrderFileMapper productWorkOrderFileMapper;
+    @Autowired
+    private ProductProcessRouteItemMapper productProcessRouteItemMapper;
+    @Autowired
+    private ProductProcessMapper productProcessMapper;
+    @Autowired
+    private ProductProcessRouteMapper productProcessRouteMapper;
+    @Autowired
+    private ProductStructureMapper productStructureMapper;
+    @Autowired
+    private ProductionProductMainMapper productionProductMainMapper;
+    @Autowired
+    private ProductionProductOutputMapper productionProductOutputMapper;
+    @Autowired
+    private ProductionProductInputMapper productionProductInputMapper;
+    @Autowired
+    private QualityUnqualifiedMapper qualityUnqualifiedMapper;
+    @Autowired
+    private QualityInspectMapper qualityInspectMapper;
+
+    @Value("${file.temp-dir}")
+    private String tempDir;
 
     @Override
     public IPage<ProductWorkOrderDto> listPage(Page<ProductWorkOrderDto> page, ProductWorkOrderDto productWorkOrder) {
-        return productWorkOrdermapper.pageProductWorkOrder(page, productWorkOrder);
+        IPage<ProductWorkOrderDto> productWorkOrderDtoIPage = productWorkOrdermapper.pageProductWorkOrder(page, productWorkOrder);
+        Map<String, BigDecimal> bomInputQtyCache = new HashMap<>();
+        productWorkOrderDtoIPage.getRecords().forEach(record -> {
+            // 鏄惁鑳芥姤宸ワ細 1. 绗竴涓伐搴忚兘鎶ュ伐 2. 涓婁竴涓伐搴忓凡鎶ュ伐 3. 涔嬪墠鐨勫伐搴忔湭琚殧绂�
+            Integer currentDragSort = record.getDragSort();
+            if (currentDragSort == null || currentDragSort == 1) {
+                record.setIsCanReport(true);
+            } else {
+                // 涓婁竴涓伐搴忔槸鍚﹀凡鎶ュ伐
+                boolean isPreviousReported = productionProductMainMapper.checkPreviousProcessReported(
+                        record.getId(),
+                        currentDragSort
+                );
+                record.setIsCanReport(isPreviousReported);
+
+                // 涔嬪墠鐨勫伐搴忔槸鍚﹁闅旂
+                if (record.getIsCanReport()) {
+                    // 鏌ヨ鎵�鏈変箣鍓嶇殑宸ュ簭锛堟帓搴忓彿灏忎簬褰撳墠宸ュ簭锛�
+                    List<ProductProcessRouteItem> previousItems = productProcessRouteItemMapper.selectList(
+                            Wrappers.<ProductProcessRouteItem>lambdaQuery()
+                                    .eq(ProductProcessRouteItem::getProductRouteId, record.getProductRouteId())
+                                    .lt(ProductProcessRouteItem::getDragSort, currentDragSort)
+                    );
+
+                    if (CollectionUtils.isNotEmpty(previousItems)) {
+                        // 鎻愬彇涔嬪墠宸ュ簭鐨勫悕绉板垪琛�
+                        List<String> previousProcessNames = new ArrayList<>();
+                        for (ProductProcessRouteItem item : previousItems) {
+                            ProductProcess process = productProcessMapper.selectById(item.getProcessId());
+                            if (process != null) {
+                                previousProcessNames.add(process.getName());
+                            }
+                        }
+
+                        // 妫�鏌ヤ箣鍓嶇殑宸ュ簭鏄惁鏈夎闅旂鐨勪笉鍚堟牸璁板綍
+                        List<QualityUnqualified> unqualifiedList = Collections.emptyList();
+                        if (CollectionUtils.isNotEmpty(previousProcessNames)) {
+                            unqualifiedList = qualityUnqualifiedMapper.selectUnqualifiedByProductOrderAndProcessNames(
+                                    record.getProductOrderId(),
+                                    previousProcessNames
+                            );
+                        }
+                        if (CollectionUtils.isNotEmpty(unqualifiedList)) {
+                            record.setIsCanReport(false);
+                        }
+                    }
+                }
+            }
+
+            // 鏌ヨ褰撳墠瀹為檯鐨勫悎鏍肩巼
+            // 鏌ヨ鍏宠仈浜у嚭琛ㄦ暟鎹�
+            List<ProductionProductMain> productionProductMains = productionProductMainMapper.selectList(Wrappers.<ProductionProductMain>lambdaQuery().eq(ProductionProductMain::getWorkOrderId, record.getId()));
+            BigDecimal totalScrapQty = BigDecimal.ZERO;
+            if (CollectionUtils.isNotEmpty(productionProductMains)) {
+                // 璁$畻鎶ュ簾鏁伴噺
+                List<Long> mainIds = productionProductMains.stream().map(ProductionProductMain::getId).collect(Collectors.toList());
+                List<ProductionProductOutput> productionProductOutputs = productionProductOutputMapper.selectList(Wrappers.<ProductionProductOutput>lambdaQuery().in(ProductionProductOutput::getProductMainId, mainIds));
+                totalScrapQty = productionProductOutputs.stream().map(ProductionProductOutput::getScrapQty).reduce(BigDecimal.ZERO, BigDecimal::add);
+            }
+            if (record.getCompleteQuantity().compareTo(BigDecimal.ZERO) > 0) {
+                record.setActualQualifiedRate((record.getCompleteQuantity().subtract(totalScrapQty))
+                        .divide(record.getCompleteQuantity(), 2, RoundingMode.HALF_UP)
+                        .multiply(new BigDecimal("100"))
+                );
+            } else {
+                record.setActualQualifiedRate((record.getCompleteQuantity().subtract(totalScrapQty)).multiply(BigDecimal.valueOf(100)));
+            }
+
+            // 鏌ヨ褰撳墠宸ュ崟瀵瑰簲浜у搧缁撴瀯涓殑鏍囧噯鎶曞叆鏁伴噺
+            String bomInputQtyCacheKey = record.getProductRouteId() + "_" + record.getProcessId() + "_" + record.getPlanQuantity();
+            BigDecimal bomInputQty = bomInputQtyCache.get(bomInputQtyCacheKey);
+            if (bomInputQty == null) {
+                bomInputQty = BigDecimal.ZERO;
+                ProductProcessRoute productProcessRoute = productProcessRouteMapper.selectById(record.getProductRouteId());
+                if (productProcessRoute != null && productProcessRoute.getBomId() != null && record.getProcessId() != null) {
+                    List<ProductStructureDto> productStructureDtos = productStructureMapper.listBybomAndProcess(
+                            productProcessRoute.getBomId(),
+                            record.getProcessId()
+                    );
+
+                    if (CollectionUtils.isEmpty(productStructureDtos)) {
+                        bomInputQty = record.getPlanQuantity() == null ? BigDecimal.ZERO : record.getPlanQuantity();
+                    } else {
+                        Set<Long> parentIds = productStructureDtos.stream()
+                                .map(ProductStructureDto::getParentId)
+                                .filter(Objects::nonNull)
+                                .collect(Collectors.toSet());
+
+                        Map<Long, ProductStructureDto> parentMap = new HashMap<>();
+                        if (CollectionUtils.isNotEmpty(parentIds)) {
+                            parentMap = productStructureMapper.selectByIds(parentIds)
+                                    .stream()
+                                    .collect(Collectors.toMap(ProductStructureDto::getId, Function.identity(), (a, b) -> a));
+                        }
+
+                        BigDecimal planQty = record.getPlanQuantity() == null ? BigDecimal.ZERO : record.getPlanQuantity();
+                        for (ProductStructureDto productStructureDto : productStructureDtos) {
+                            BigDecimal childQty = productStructureDto.getUnitQuantity();
+                            if (childQty == null || childQty.compareTo(BigDecimal.ZERO) <= 0) {
+                                continue;
+                            }
+
+                            BigDecimal parentQty = BigDecimal.ONE;
+                            if (productStructureDto.getParentId() != null) {
+                                ProductStructureDto parent = parentMap.get(productStructureDto.getParentId());
+                                if (parent != null && parent.getUnitQuantity() != null && parent.getUnitQuantity().compareTo(BigDecimal.ZERO) > 0) {
+                                    parentQty = parent.getUnitQuantity();
+                                }
+                            }
+
+                            if (parentQty.compareTo(BigDecimal.ZERO) <= 0) {
+                                continue;
+                            }
+
+                            bomInputQty = bomInputQty.add(
+                                    childQty.divide(parentQty, 6, RoundingMode.HALF_UP).multiply(planQty)
+                            );
+                        }
+                    }
+                }
+                bomInputQtyCache.put(bomInputQtyCacheKey, bomInputQty);
+            }
+            record.setBomInputQty(bomInputQty);
+        });
+        return productWorkOrderDtoIPage;
     }
 
+    @Override
+    public int updateProductWorkOrder(ProductWorkOrderDto productWorkOrderDto) {
+        return productWorkOrdermapper.updateById(productWorkOrderDto);
+    }
+
+    @Override
+    public void down(HttpServletResponse response, ProductWorkOrder productWorkOrder) {
+        ProductWorkOrderDto productWorkOrderDto = productWorkOrdermapper.getProductWorkOrderFlowCard(productWorkOrder.getId());
+        String codePath;
+        try {
+            codePath = new MatrixToImageWriter().code(productWorkOrderDto.getId().toString(), tempDir);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+        /*鑾峰彇闄勪欢鍥剧墖绫诲瀷*/
+        List<Map<String, Object>> images = new ArrayList<>();
+        List<ProductWorkOrderFile> productWorkOrderFiles = productWorkOrderFileMapper.selectList(Wrappers.<ProductWorkOrderFile>lambdaQuery().eq(ProductWorkOrderFile::getWorkOrderId, productWorkOrder.getId()));
+        if (CollectionUtils.isNotEmpty(productWorkOrderFiles)) {
+            productWorkOrderFiles.forEach(productWorkOrderFile -> {
+                Map<String, Object> image = new HashMap<>();
+                PictureRenderData pictureRenderData = Pictures.ofLocal( productWorkOrderFile.getUrl()).sizeInCm(17, 20).create();
+                image.put("url", pictureRenderData);
+                images.add(image);
+            });
+        }
+        InputStream inputStream = this.getClass().getResourceAsStream("/static/work-order-template.docx");
+        XWPFTemplate template = XWPFTemplate.compile(inputStream).render(
+                new HashMap<String, Object>() {{
+                    put("process", productWorkOrderDto.getProcessName());
+                    put("workOrderNo", productWorkOrderDto.getWorkOrderNo());
+                    put("productOrderNpsNo", productWorkOrderDto.getProductOrderNpsNo());
+                    put("productName", productWorkOrderDto.getProductName());
+                    put("planQuantity", productWorkOrderDto.getPlanQuantity());
+                    put("model", productWorkOrderDto.getModel());
+                    put("unit", productWorkOrderDto.getUnit());
+                    put("completeQuantity", productWorkOrderDto.getCompleteQuantity());
+                    put("scrapQty", productWorkOrderDto.getScrapQty());
+                    put("planStartTime", productWorkOrderDto.getPlanStartTime());
+                    put("planEndTime", productWorkOrderDto.getPlanEndTime());
+                    put("actualStartTime", productWorkOrderDto.getActualStartTime());
+                    put("actualEndTime", productWorkOrderDto.getActualEndTime());
+                    put("twoCode", Pictures.ofLocal(codePath).create());
+                    put("images", images.isEmpty()?null:images);
+                }});
+
+        try {
+            response.setContentType("application/msword");
+            String fileName = URLEncoder.encode(
+                    "娴佽浆鍗�", "UTF-8");
+            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
+            response.setHeader("Content-disposition",
+                    "attachment;filename=" + fileName + ".docx");
+            OutputStream os = response.getOutputStream();
+            template.write(os);
+            os.flush();
+            os.close();
+            inputStream.close();
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new RuntimeException("瀵煎嚭澶辫触");
+        }
+    }
+
+    @Override
+    public List<ProductWorkOrderDto> getByProductOrderId(Long productOrderId) {
+        List<ProductWorkOrderDto> productWorkOrderDtos = productWorkOrdermapper.getByProductOrderId(productOrderId);
+        if (CollectionUtils.isNotEmpty(productWorkOrderDtos)) {
+            productWorkOrderDtos.forEach(productWorkOrderDto -> {
+                // 鏌ヨ鍏宠仈浜у嚭琛ㄦ暟鎹�
+                List<ProductionProductMain> productionProductMains = productionProductMainMapper.selectList(Wrappers.<ProductionProductMain>lambdaQuery().eq(ProductionProductMain::getWorkOrderId, productWorkOrderDto.getId()));
+                // 鏌ヨ鎶ュ簾鏁伴噺
+                BigDecimal scrapQty = BigDecimal.ZERO;
+                // 鏌ヨ涓嶈壇鏁伴噺
+                BigDecimal defectiveQuantity = BigDecimal.ZERO;
+                // 鏌ヨ鎶曞叆鏁伴噺
+                BigDecimal inputQty = BigDecimal.ZERO;
+                if (CollectionUtils.isNotEmpty(productionProductMains)) {
+                    // 璁$畻鎶ュ簾鏁伴噺
+                    List<Long> mainIds = productionProductMains.stream().map(ProductionProductMain::getId).collect(Collectors.toList());
+                    List<ProductionProductOutput> productionProductOutputs = productionProductOutputMapper.selectList(Wrappers.<ProductionProductOutput>lambdaQuery().in(ProductionProductOutput::getProductMainId, mainIds));
+                    List<QualityInspect> qualityInspects = qualityInspectMapper.selectList(Wrappers.<QualityInspect>lambdaQuery().in(QualityInspect::getProductMainId, mainIds));
+                   if (CollectionUtils.isNotEmpty(productionProductOutputs)) {
+                       scrapQty = productionProductOutputs.stream().map(ProductionProductOutput::getScrapQty).reduce(BigDecimal.ZERO, BigDecimal::add);
+                   }
+                    List<ProductionProductInput> productionProductInputs = productionProductInputMapper.selectList(Wrappers.<ProductionProductInput>lambdaQuery().in(ProductionProductInput::getProductMainId, mainIds));
+                    if (CollectionUtils.isNotEmpty(productionProductInputs)) {
+                        inputQty = productionProductInputs.stream().map(ProductionProductInput::getQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
+                    }
+                   if (CollectionUtils.isNotEmpty(qualityInspects)) {
+                        defectiveQuantity = qualityInspects.stream().map(QualityInspect::getDefectiveQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
+                   }
+                }
+                BigDecimal totalQuantity = productWorkOrderDto.getCompleteQuantity().add(scrapQty);
+                if (totalQuantity.compareTo(BigDecimal.ZERO) > 0) {
+                    productWorkOrderDto.setDefectiveRate(defectiveQuantity.divide(productWorkOrderDto.getCompleteQuantity(), 4, RoundingMode.HALF_UP));
+                } else {
+                    productWorkOrderDto.setDefectiveRate(defectiveQuantity.multiply(BigDecimal.valueOf(100)));
+                }
+                productWorkOrderDto.setInputQty(inputQty);
+                productWorkOrderDto.setScrapQty(scrapQty);
+                productWorkOrderDto.setTotalQty(totalQuantity);
+                productWorkOrderDto.setDefectiveQuantity(defectiveQuantity);
+                productWorkOrderDto.setCompleteQty(productWorkOrderDto.getCompleteQuantity().subtract(defectiveQuantity));
+            });
+        }
+        return productWorkOrderDtos;
+    }
 }

--
Gitblit v1.9.3