| | |
| | | |
| | | import java.time.LocalDate; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.AbstractMap; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | |
| | | throw new RuntimeException("产品名称不能为空"); |
| | | } |
| | | Product product = productMapper.selectOne(new LambdaQueryWrapper<Product>() |
| | | .eq(Product::getProductName, productModelDto.getProductName())); |
| | | // .eq(Product::getModel, productModelDto.getModel()) |
| | | .eq(Product::getProductName, productModelDto.getProductName())); |
| | | if (productModelDto.getId() == null) { |
| | | if(product == null){ |
| | | product = new Product(); |
| | |
| | | productModel.setProductId(product.getId()); |
| | | return productModelMapper.insert(productModel); |
| | | } else { |
| | | if(product != null){ |
| | | product.setProductName(productModelDto.getProductName()); |
| | | productMapper.updateById(product); |
| | | Product product1 = productMapper.selectById(productModelDto.getProductId()); |
| | | if(product1 != null){ |
| | | product1.setProductName(productModelDto.getProductName()); |
| | | productMapper.updateById(product1); |
| | | } |
| | | |
| | | return productModelMapper.updateById(productModelDto); |
| | |
| | | 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()); |
| | | List<Product> productList = productMapper.selectList(new LambdaQueryWrapper<Product>() |
| | | .isNull(Product::getParentId)); |
| | | if(CollectionUtils.isEmpty(productList)) { |
| | | throw new RuntimeException("请先添加父级产品"); |
| | | } |
| | | // List<Product> productList = productMapper.selectList(new LambdaQueryWrapper<Product>() |
| | | // .isNull(Product::getParentId)); |
| | | // if(CollectionUtils.isEmpty(productList)) { |
| | | // throw new RuntimeException("请先添加父级产品"); |
| | | // } |
| | | if(CollectionUtils.isNotEmpty(productModelList)){ |
| | | // 2. 按产品名称分组 |
| | | Map<String, List<ProductModelExcelCopyDto>> groupedByProductName = productModelList.stream() |
| | | .collect(Collectors.groupingBy(ProductModelExcelCopyDto::getProductName)); |
| | | for (Map.Entry<String, List<ProductModelExcelCopyDto>> entry : groupedByProductName.entrySet()) { |
| | | // 根据名称查询是否存在产品 |
| | | Product product = productMapper.selectOne(new LambdaQueryWrapper<Product>() |
| | | .eq(Product::getProductName, entry.getKey()) |
| | | .last("limit 1")); |
| | | if(product == null){ |
| | | product = new Product(); |
| | | product.setProductName(entry.getKey()); |
| | | product.setParentId(null); // 父节点ID |
| | | // 2. 插入当前节点,MyBatis会自动回填product的id属性 |
| | | productMapper.insert(product); |
| | | // 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; |
| | | } |
| | | Product finalProduct = product; |
| | | entry.getValue().forEach(productModelExcelDto -> { |
| | | // 根据图纸编号查询 |
| | | ProductModel productModel = productModelMapper.selectOne(new LambdaQueryWrapper<ProductModel>() |
| | | .eq(ProductModel::getModel, productModelExcelDto.getModel()) |
| | | .last("limit 1")); |
| | | if(productModel == null){ |
| | | productModel = new ProductModel(); |
| | | BeanUtils.copyProperties(productModelExcelDto,productModel); |
| | | if(productModelExcelDto.getProductType() == null) { |
| | | productModel.setProductType(1); |
| | | } |
| | | productModel.setProductId(finalProduct.getId()); |
| | | productModelMapper.insert(productModel); |
| | | }else{ |
| | | BeanUtils.copyProperties(productModelExcelDto,productModel); |
| | | productModel.setProductId(finalProduct.getId()); |
| | | productModelMapper.updateById(productModel); |
| | | } |
| | | }); |
| | | ProductModelExcelCopyDto firstDto = dtoList.get(0); |
| | | String model = firstDto.getModel(); |
| | | |
| | | // 3. 查询/新增产品(按产品名称+图纸编号,更精准) |
| | | Product product = getOrCreateProduct(productName, drawingNumber); |
| | | |
| | | // 4. 批量处理产品型号(按图纸编号+型号) |
| | | processProductModel(dtoList, product.getId(), model, drawingNumber); |
| | | } |
| | | } |
| | | // List<ProductModelExcelDto> productModelExcelDtos = OrderUtils.buildTree(productModelList); |
| | | // if(CollectionUtils.isNotEmpty(productModelExcelDtos)){ |
| | | // recursiveSaveProduct(productModelExcelDtos.get(0),null); |
| | | // } |
| | | return true; |
| | | }catch (Exception e) { |
| | | e.printStackTrace(); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 抽取通用方法:查询或新增产品 |
| | | */ |
| | | 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) { |
| | | for (ProductModelExcelCopyDto dto : dtoList) { |
| | | // 查询条件:型号+图纸编号(更精准,符合分组逻辑) |
| | | ProductModel productModel = productModelMapper.selectOne(new LambdaQueryWrapper<ProductModel>() |
| | | .eq(ProductModel::getModel, model) |
| | | .last("limit 1")); |
| | | |
| | | if (productModel == null) { |
| | | productModel = new ProductModel(); |
| | | BeanUtils.copyProperties(dto, productModel); |
| | | // 兜底默认值,避免空值 |
| | | if (productModel.getProductType() == null) { |
| | | productModel.setProductType(1); |
| | | } |
| | | productModel.setProductId(productId); |
| | | productModelMapper.insert(productModel); |
| | | } else { |
| | | BeanUtils.copyProperties(dto, productModel); |
| | | productModel.setProductId(productId); |
| | | productModelMapper.updateById(productModel); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 递归导入树形产品数据 |
| | | * @param excelDto Excel解析后的树形节点 |
| | | * @param parentId 父节点ID(顶级节点传null/0) |