From 4bcd26a4975d6722bff65690e7a8331cd0afd520 Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期二, 31 三月 2026 17:46:19 +0800
Subject: [PATCH] yys 1.销售,采购台账产品反着来 2.产品图纸带到生产订单来

---
 src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java |  237 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 230 insertions(+), 7 deletions(-)

diff --git a/src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java b/src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java
index 754d93f..066d886 100644
--- a/src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java
+++ b/src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java
@@ -1,18 +1,42 @@
 package com.ruoyi.basic.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.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.ruoyi.basic.dto.ProductDto;
-import com.ruoyi.basic.dto.ProductModelDto;
+import com.ruoyi.basic.dto.*;
+import com.ruoyi.basic.mapper.ProductMapper;
 import com.ruoyi.basic.mapper.ProductModelMapper;
+import com.ruoyi.basic.pojo.Product;
 import com.ruoyi.basic.pojo.ProductModel;
 import com.ruoyi.basic.service.IProductModelService;
+import com.ruoyi.common.enums.FileNameType;
+import com.ruoyi.common.exception.ServiceException;
+import com.ruoyi.common.utils.OrderUtils;
+import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.bean.BeanUtils;
+import com.ruoyi.common.utils.poi.ExcelUtil;
+import com.ruoyi.common.utils.uuid.IdUtils;
+import com.ruoyi.other.mapper.TempFileMapper;
+import com.ruoyi.other.service.impl.TempFileServiceImpl;
+import com.ruoyi.production.mapper.ProductProcessMapper;
+import com.ruoyi.production.pojo.ProductProcess;
+import com.ruoyi.sales.mapper.CommonFileMapper;
+import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
+import com.ruoyi.sales.pojo.SalesLedgerProduct;
+import com.ruoyi.sales.service.impl.CommonFileServiceImpl;
 import lombok.AllArgsConstructor;
+import org.apache.commons.collections4.CollectionUtils;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.multipart.MultipartFile;
 
-import java.util.Arrays;
-import java.util.List;
+import java.io.IOException;
+import java.time.LocalDate;
+import java.time.format.DateTimeFormatter;
+import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * 銆愯濉啓鍔熻兘鍚嶇О銆慡ervice涓氬姟灞傚鐞�
@@ -24,22 +48,61 @@
 @AllArgsConstructor
 public class ProductModelServiceImpl extends ServiceImpl<ProductModelMapper, ProductModel> implements IProductModelService {
 
+    private final ProductMapper productMapper;
+    private final SalesLedgerProductMapper salesLedgerProductMapper;
     private ProductModelMapper productModelMapper;
+    private final CommonFileServiceImpl commonFileService;
+    private final ProductProcessMapper productProcessMapper;
+
+    private final TempFileServiceImpl tempFileService;
 
     @Override
-    public int addOrEditProductModel(ProductModelDto productModelDto) {
-
+    public int addOrEditProductModel(ProductModelDto productModelDto) throws IOException {
+        if(StringUtils.isEmpty(productModelDto.getProductName())){
+            throw new RuntimeException("浜у搧鍚嶇О涓嶈兘涓虹┖");
+        }
+        Product product = productMapper.selectOne(new LambdaQueryWrapper<Product>()
+//                        .eq(Product::getModel, productModelDto.getModel())
+                        .eq(Product::getProductName, productModelDto.getProductName()));
         if (productModelDto.getId() == null) {
+            if(product == null){
+                product = new Product();
+                product.setProductName(productModelDto.getProductName());
+                productMapper.insert(product);
+            }
             ProductModel productModel = new ProductModel();
             BeanUtils.copyProperties(productModelDto,productModel);
-            return productModelMapper.insert(productModel);
+            productModel.setProductId(product.getId());
+            productModelMapper.insert(productModel);
+            if(CollectionUtils.isNotEmpty(productModelDto.getTempFileIds())){
+                commonFileService.migrateTempFilesToFormal(productModel.getId(), productModelDto.getTempFileIds());
+            }
+            return 1;
         } else {
+            Product product1 = productMapper.selectById(productModelDto.getProductId());
+            if(product1 != null){
+                product1.setProductName(productModelDto.getProductName());
+                productMapper.updateById(product1);
+            }
+            commonFileService.deleteByBusinessIds(Collections.singletonList(productModelDto.getId()), FileNameType.PRODUCT_MODEL.getValue());
+            if(CollectionUtils.isNotEmpty(productModelDto.getTempFileIds())){
+                commonFileService.migrateTempFilesToFormal(productModelDto.getId(), productModelDto.getTempFileIds());
+            }
             return productModelMapper.updateById(productModelDto);
         }
+
     }
+
+
 
     @Override
     public int delProductModel(Long[] ids) {
+        List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(new QueryWrapper<SalesLedgerProduct>()
+                .lambda().in(SalesLedgerProduct::getProductModelId, ids));
+        if (salesLedgerProducts != null && salesLedgerProducts.size() > 0) {
+
+            throw new RuntimeException("宸茬粡瀛樺湪璇ヤ骇鍝佺殑閿�鍞彴璐﹀拰閲囪喘鍙拌处");
+        }
         return productModelMapper.deleteBatchIds(Arrays.asList(ids));
     }
 
@@ -49,4 +112,164 @@
         queryWrapper.eq(ProductModel::getProductId, productDto.getId());
         return productModelMapper.selectList(queryWrapper);
     }
+
+    /**
+     * 鏍规嵁id鏌ヨ浜у搧瑙勬牸鍒嗛〉鏌ヨ
+     * @param page
+     * @param productDto
+     * @return
+     */
+    @Override
+    public IPage<ProductModel> modelListPage(Page page, ProductDto productDto) {
+        LambdaQueryWrapper<ProductModel> queryWrapper = new LambdaQueryWrapper<>();
+        queryWrapper.eq(ProductModel::getProductId, productDto.getId());
+        return productModelMapper.selectPage(page, queryWrapper);
+    }
+
+
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public Boolean importProduct(MultipartFile file) {
+        try {
+            ExcelUtil<ProductModelExcelCopyDto> productModelExcelUtil = new ExcelUtil<>(ProductModelExcelCopyDto.class);
+            List<ProductModelExcelCopyDto> productModelList = productModelExcelUtil.importExcel(file.getInputStream());
+            if(CollectionUtils.isNotEmpty(productModelList)){
+                // 2. 鎸変骇鍝佸悕绉�,鍥剧焊缂栧彿鍒嗙粍
+                Map<Map.Entry<String, String>, List<ProductModelExcelCopyDto>> groupedByProductNameAndDrawingNumber =
+                        productModelList.stream()
+                                .collect(Collectors.groupingBy(
+                                        dto -> new AbstractMap.SimpleEntry<>(
+                                                dto.getProductName(),
+                                                dto.getModel()
+                                        )
+                                ));
+                // 2. 閬嶅巻鍒嗙粍缁撴灉澶勭悊鏁版嵁
+                for (Map.Entry<Map.Entry<String, String>, List<ProductModelExcelCopyDto>> entry : groupedByProductNameAndDrawingNumber.entrySet()) {
+                    Map.Entry<String, String> groupKey = entry.getKey();
+                    String productName = groupKey.getKey(); // 浜у搧鍚嶇О
+                    String drawingNumber = groupKey.getValue(); // 鍥剧焊缂栧彿
+                    List<ProductModelExcelCopyDto> dtoList = entry.getValue();
+
+                    // 绌哄垪琛ㄨ烦杩囷紝閬垮厤鍚庣画NPE
+                    if (CollectionUtils.isEmpty(dtoList)) {
+                        continue;
+                    }
+                    ProductModelExcelCopyDto firstDto = dtoList.get(0);
+                    String model = firstDto.getModel();
+
+                    // 3. 鏌ヨ/鏂板浜у搧锛堟寜浜у搧鍚嶇О+鍥剧焊缂栧彿锛屾洿绮惧噯锛�
+                    Product product = getOrCreateProduct(productName, drawingNumber);
+
+                    // 4. 鎵归噺澶勭悊浜у搧鍨嬪彿锛堟寜鍥剧焊缂栧彿+鍨嬪彿锛�
+                    processProductModel(dtoList, product.getId(), model, drawingNumber);
+                }
+            }
+            return true;
+        }catch (Exception e) {
+            e.printStackTrace();
+        }
+        return false;
+    }
+
+    /**
+     * 鎶藉彇閫氱敤鏂规硶锛氭煡璇㈡垨鏂板浜у搧
+     */
+    private Product getOrCreateProduct(String productName, String model) {
+        // 绮惧噯鏌ヨ锛氫骇鍝佸悕绉�+鍨嬪彿锛堥伩鍏嶅悓鍚嶄笉鍚屽瀷鍙风殑闂锛�
+        Product product = productMapper.selectOne(new LambdaQueryWrapper<Product>()
+                .eq(Product::getProductName, productName)
+//                .eq(Product::getModel, model)
+                .last("limit 1"));
+
+        if (product == null) {
+            product = new Product();
+            product.setProductName(productName);
+//            product.setModel(model); // 琛ュ厖鍨嬪彿锛屽瓧娈垫洿瀹屾暣
+            product.setParentId(null);
+            productMapper.insert(product);
+        }
+        return product;
+    }
+
+    /**
+     * 鎶藉彇閫氱敤鏂规硶锛氬鐞嗕骇鍝佸瀷鍙凤紙鏂板/鏇存柊锛�
+     */
+    private void processProductModel(List<ProductModelExcelCopyDto> dtoList,
+                                     Long productId, String model, String drawingNumber) {
+        // 鏌ヨ鎵�鏈夊伐鑹鸿矾绾�
+        List<ProductProcess> productRoutes = productProcessMapper.selectList(new QueryWrapper<ProductProcess>());
+        if(CollectionUtils.isEmpty(productRoutes)){
+            throw new ServiceException("璇峰厛鍒涘缓浜у搧宸ヨ壓璺嚎");
+        }
+        for (ProductModelExcelCopyDto dto : dtoList) {
+            // 鏌ヨ鏉′欢锛氬瀷鍙�+鍥剧焊缂栧彿锛堟洿绮惧噯锛岀鍚堝垎缁勯�昏緫锛�
+            ProductModel productModel = productModelMapper.selectOne(new LambdaQueryWrapper<ProductModel>()
+                    .eq(ProductModel::getModel, model)
+                    .last("limit 1"));
+            // 閫氳繃宸ヨ壓璺嚎鍚嶇О鍖归厤鏈�鏂颁竴鏉″伐鑹鸿矾绾�
+            ProductProcess productRoute = productRoutes.stream()
+                    .filter(route -> route.getName().equals(dto.getProcessRoute()))
+                    .max(Comparator.comparing(ProductProcess::getCreateTime))
+                    .orElse(null);
+            if (productModel == null) {
+                productModel = new ProductModel();
+                BeanUtils.copyProperties(dto, productModel);
+                if (productRoute != null) {
+                    productModel.setRouteId(productRoute.getId());
+                }
+                // 鍏滃簳榛樿鍊硷紝閬垮厤绌哄��
+                if (productModel.getProductType() == null) {
+                    productModel.setProductType(1);
+                }
+                productModel.setProductId(productId);
+                productModelMapper.insert(productModel);
+            } else {
+                productModel.setRouteId(productRoute != null ? productRoute.getId() : null);
+                BeanUtils.copyProperties(dto, productModel);
+                productModel.setProductId(productId);
+                productModelMapper.updateById(productModel);
+            }
+        }
+    }
+
+    /**
+     * 閫掑綊瀵煎叆鏍戝舰浜у搧鏁版嵁
+     * @param excelDto Excel瑙f瀽鍚庣殑鏍戝舰鑺傜偣
+     * @param parentId 鐖惰妭鐐笽D锛堥《绾ц妭鐐逛紶null/0锛�
+     */
+    @Transactional(rollbackFor = Exception.class) // 浜嬪姟淇濊瘉锛屽け璐ュ垯鍥炴粴
+    public void recursiveSaveProduct(ProductModelExcelDto excelDto, Long parentId) {
+        // 1. 鏋勫缓褰撳墠鑺傜偣鐨凱roduct瀹炰綋
+        Product product = new Product();
+        product.setProductName(excelDto.getProductName());
+        product.setParentId(parentId); // 鐖惰妭鐐笽D
+
+        // 2. 鎻掑叆褰撳墠鑺傜偣锛孧yBatis浼氳嚜鍔ㄥ洖濉玴roduct鐨刬d灞炴��
+        productMapper.insert(product);
+        Long currentId = product.getId(); // 鑾峰彇褰撳墠鑺傜偣涓婚敭
+
+        // 3. 閫掑綊澶勭悊瀛愯妭鐐�
+        if (excelDto.getChildren() != null && !excelDto.getChildren().isEmpty()) {
+            for (ProductModelExcelDto childDto : excelDto.getChildren()) {
+                recursiveSaveProduct(childDto, currentId); // 鐖禝D涓哄綋鍓嶈妭鐐笽D
+            }
+        }
+
+        // 4. 锛堝彲閫夛級澶勭悊items鏁版嵁锛堝鏋滈渶瑕佹彃鍏ュ埌鍏宠仈琛級
+         if (excelDto.getItems() != null && !excelDto.getItems().isEmpty()) {
+             for (ProductModelExcelItemDto item : excelDto.getItems()) {
+                 // 鎻掑叆item鍏宠仈鏁版嵁锛屽叧鑱攃urrentId
+                 // 鏋勫缓ProductModel瀹炰綋
+                 ProductModel productModel = new ProductModel();
+                 productModel.setProductId(currentId); // 鍏宠仈褰撳墠浜у搧鑺傜偣ID
+                 productModel.setModel(item.getModel()); // 浠嶪temDTO鑾峰彇鍨嬪彿
+                 productModel.setUnit(item.getUnit());   // 浠嶪temDTO鑾峰彇鍗曚綅
+                 productModel.setDrawingNumber(item.getDrawingNumber()); // 鍥剧焊缂栧彿
+                 productModel.setProductType(item.getProductType());
+                 // 鎻掑叆product_model琛�
+                 productModelMapper.insert(productModel);
+             }
+         }
+    }
 }

--
Gitblit v1.9.3