| | |
| | | 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; |
| | |
| | | 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); |
| | | } |
| | | Product product1 = new Product(); |
| | | product1.setProductName(productModelDto.getProductName()); |
| | | productMapper.insert(product1); |
| | | ProductModel productModel = new ProductModel(); |
| | | BeanUtils.copyProperties(productModelDto,productModel); |
| | | productModel.setProductId(product.getId()); |
| | | productModel.setProductId(product1.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); |
| | | 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())){ |
| | |
| | | } |
| | | 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() |
| | | ) |
| | | )); |
| | | // 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 (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(); |
| | | |
| | | for (ProductModelExcelCopyDto entry : productModelList) { |
| | | String productName = entry.getProductName(); // 产品名称 |
| | | String drawingNumber = entry.getModel(); // 图纸编号 |
| | | String model = entry.getModel(); |
| | | // 3. 查询/新增产品(按产品名称+图纸编号,更精准) |
| | | Product product = getOrCreateProduct(productName, drawingNumber); |
| | | |
| | | // 4. 批量处理产品型号(按图纸编号+型号) |
| | | processProductModel(dtoList, product.getId(), model, drawingNumber,errorList); |
| | | processProductModel(entry, product.getId(), model, drawingNumber,errorList); |
| | | } |
| | | if(CollectionUtils.isNotEmpty(errorList)){ |
| | | // 5. 批量处理错误数据 |
| | |
| | | * 抽取通用方法:查询或新增产品 |
| | | */ |
| | | 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 product = product = new Product(); |
| | | product.setProductName(productName); |
| | | // product.setModel(model); // 补充型号,字段更完整 |
| | | product.setParentId(null); |
| | | productMapper.insert(product); |
| | | } |
| | | product.setParentId(null); |
| | | productMapper.insert(product); |
| | | return product; |
| | | } |
| | | |
| | | /** |
| | | * 抽取通用方法:处理产品型号(新增/更新) |
| | | */ |
| | | private void processProductModel(List<ProductModelExcelCopyDto> dtoList, |
| | | private void processProductModel(ProductModelExcelCopyDto dto, |
| | | Long productId, |
| | | String model, |
| | | String drawingNumber, |
| | |
| | | if(CollectionUtils.isEmpty(productRoutes)){ |
| | | productRoutes = new ArrayList<>(); |
| | | } |
| | | 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); |
| | | // 查询条件:型号+图纸编号(更精准,符合分组逻辑) |
| | | 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); |
| | | } |
| | | } |
| | | |