15 小时以前 5b2c9ae479a1b7a0c4e3b15910b090f8b20cf4fe
src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java
@@ -1,25 +1,43 @@
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.apache.commons.collections4.ListUtils;
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.util.Map;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
/**
@@ -33,22 +51,57 @@
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("产品名称不能为空");
        }
        // 产品表 - 规格表变成一对一
        if (productModelDto.getId() == null) {
            Product product1 = new Product();
            product1.setProductName(productModelDto.getProductName());
            productMapper.insert(product1);
            ProductModel productModel = new ProductModel();
            BeanUtils.copyProperties(productModelDto,productModel);
            return productModelMapper.insert(productModel);
            productModel.setProductId(product1.getId());
            productModelMapper.insert(productModel);
            if(CollectionUtils.isNotEmpty(productModelDto.getTempFileIds())){
                commonFileService.migrateTempFilesToFormal(productModel.getId(), productModelDto.getTempFileIds());
            }
            return 1;
        } else {
            Product product = productMapper.selectById(productModelDto.getProductId());
            if(product != null){
                product.setProductName(productModelDto.getProductName());
                productMapper.updateById(product);
            }
            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));
    }
@@ -72,25 +125,140 @@
        return productModelMapper.selectPage(page, queryWrapper);
    }
    @Override
    public Boolean importProduct(MultipartFile file) {
    @Transactional(rollbackFor = Exception.class)
    public void importProduct(MultipartFile file, HttpServletResponse response) {
        ExcelUtil<ProductModelExcelCopyDto> productModelExcelUtil = new ExcelUtil<>(ProductModelExcelCopyDto.class);
        List<ProductModelExcelCopyDto> productModelList = null;
        try {
            ExcelUtil<ProductModel> productModelExcelUtil = new ExcelUtil<>(ProductModel.class);
            List<ProductModel> productModelList = productModelExcelUtil.importExcel(file.getInputStream());
            Map<String, List<ProductModel>> collect = productModelList.stream().collect(Collectors.groupingBy(ProductModel::getProductName));
            collect.forEach((k,v)->{
                Product product = productMapper.selectOne(new LambdaQueryWrapper<Product>().eq(Product::getProductName, k).last("LIMIT 1"));
                if (product != null) {
                    v.forEach(productModel -> {
                        productModel.setProductId(product.getId());
                    });
                    this.saveOrUpdateBatch(v);
                }
            });
            return true;
            productModelList = productModelExcelUtil.importExcel(file.getInputStream());
        }catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("文件解析失败");
        }
        return false;
        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()
//                                    )
//                            ));
            List<ProductModelExcelCopyErrorDto> errorList = new ArrayList<>();
            // 2. 遍历分组结果处理数据
            for (ProductModelExcelCopyDto entry : productModelList) {
                String productName = entry.getProductName(); // 产品名称
                String drawingNumber = entry.getModel(); // 图纸编号
                String model = entry.getModel();
                // 3. 查询/新增产品(按产品名称+图纸编号,更精准)
                Product product = getOrCreateProduct(productName, drawingNumber);
                // 4. 批量处理产品型号(按图纸编号+型号)
                processProductModel(entry, product.getId(), model, drawingNumber,errorList);
            }
            if(CollectionUtils.isNotEmpty(errorList)){
                // 5. 批量处理错误数据
                ExcelUtil<ProductModelExcelCopyErrorDto> errorExcelUtil = new ExcelUtil<>(ProductModelExcelCopyErrorDto.class);
                errorExcelUtil.exportExcel(response,errorList, "错误数据");
            }
        }
    }
    /**
     * 抽取通用方法:查询或新增产品
     */
    private Product getOrCreateProduct(String productName, String model) {
        Product product = product = new Product();
        product.setProductName(productName);
//            product.setModel(model); // 补充型号,字段更完整
        product.setParentId(null);
        productMapper.insert(product);
        return product;
    }
    /**
     * 抽取通用方法:处理产品型号(新增/更新)
     */
    private void processProductModel(ProductModelExcelCopyDto dto,
                                     Long productId,
                                     String model,
                                     String drawingNumber,
                                     List<ProductModelExcelCopyErrorDto> errorList) {
        // 查询所有工艺路线
        List<ProductProcess> productRoutes = productProcessMapper.selectList(new QueryWrapper<ProductProcess>());
        if(CollectionUtils.isEmpty(productRoutes)){
            productRoutes = new ArrayList<>();
        }
        // 查询条件:型号+图纸编号(更精准,符合分组逻辑)
        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解析后的树形节点
     * @param parentId 父节点ID(顶级节点传null/0)
     */
    @Transactional(rollbackFor = Exception.class) // 事务保证,失败则回滚
    public void recursiveSaveProduct(ProductModelExcelDto excelDto, Long parentId) {
        // 1. 构建当前节点的Product实体
        Product product = new Product();
        product.setProductName(excelDto.getProductName());
        product.setParentId(parentId); // 父节点ID
        // 2. 插入当前节点,MyBatis会自动回填product的id属性
        productMapper.insert(product);
        Long currentId = product.getId(); // 获取当前节点主键
        // 3. 递归处理子节点
        if (excelDto.getChildren() != null && !excelDto.getChildren().isEmpty()) {
            for (ProductModelExcelDto childDto : excelDto.getChildren()) {
                recursiveSaveProduct(childDto, currentId); // 父ID为当前节点ID
            }
        }
        // 4. (可选)处理items数据(如果需要插入到关联表)
         if (excelDto.getItems() != null && !excelDto.getItems().isEmpty()) {
             for (ProductModelExcelItemDto item : excelDto.getItems()) {
                 // 插入item关联数据,关联currentId
                 // 构建ProductModel实体
                 ProductModel productModel = new ProductModel();
                 productModel.setProductId(currentId); // 关联当前产品节点ID
                 productModel.setModel(item.getModel()); // 从ItemDTO获取型号
                 productModel.setUnit(item.getUnit());   // 从ItemDTO获取单位
                 productModel.setDrawingNumber(item.getDrawingNumber()); // 图纸编号
                 productModel.setProductType(item.getProductType());
                 // 插入product_model表
                 productModelMapper.insert(productModel);
             }
         }
    }
}