| | |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.HashMap; |
| | | import java.util.HashSet; |
| | |
| | | return AjaxResult.error("未找到 [原料/半成品/成品] 工作表,或文件中没有数据"); |
| | | } |
| | | |
| | | // 去重键:产品名称 + 规格型号 + 编码(空编码按空串参与比较) |
| | | Set<String> seenKeys = loadExistingProductKeys(sheetMap); |
| | | |
| | | int successCount = 0; |
| | | int skipCount = 0; |
| | | Set<String> generatedCodes = new HashSet<>(); |
| | |
| | | } |
| | | Long topId = findOrCreateTopProduct(sheetName); |
| | | Map<String, Long> nodeCache = new HashMap<>(); |
| | | Map<Long, Set<String>> codeCache = new HashMap<>(); |
| | | for (int i = 0; i < rows.size(); i++) { |
| | | ProductImportRowDto row = rows.get(i); |
| | | int rowNum = i + 2; |
| | |
| | | String productName = trimToNull(row.getProductName()); |
| | | if (productName == null) { |
| | | throw new ServiceException("第 " + rowNum + " 行 [产品名称] 不能为空"); |
| | | } |
| | | |
| | | String model = trimToNull(row.getModel()); |
| | | if (model == null) { |
| | | model = "/"; |
| | | } |
| | | String code = trimToNull(row.getProductCode()); |
| | | if (!seenKeys.add(dedupKey(productName, model, code))) { |
| | | skipCount++; |
| | | continue; |
| | | } |
| | | |
| | | String path = String.valueOf(topId); |
| | |
| | | path = path + ">" + productName; |
| | | Long leafId = ensureChildNode(nodeCache, path, currentParentId, productName); |
| | | |
| | | String code = trimToNull(row.getProductCode()); |
| | | if (code == null) { |
| | | code = generateUniqueCode(generatedCodes); |
| | | } |
| | | Set<String> codes = codeCache.computeIfAbsent(leafId, this::loadProductCodes); |
| | | if (!codes.add(code)) { |
| | | skipCount++; |
| | | continue; |
| | | } |
| | | |
| | | ProductModel productModel = new ProductModel(); |
| | | productModel.setProductId(leafId); |
| | | productModel.setProductCode(code); |
| | | productModel.setModel(StringUtils.isEmpty(row.getModel()) ? "/" : row.getModel().trim()); |
| | | productModel.setModel(model); |
| | | productModel.setUnit(trimToNull(row.getUnit())); |
| | | productModelMapper.insert(productModel); |
| | | successCount++; |
| | |
| | | return product.getId(); |
| | | } |
| | | |
| | | private Set<String> loadProductCodes(Long productId) { |
| | | List<ProductModel> list = productModelMapper.selectList(new LambdaQueryWrapper<ProductModel>() |
| | | .eq(ProductModel::getProductId, productId)); |
| | | Set<String> codes = new HashSet<>(); |
| | | for (ProductModel productModel : list) { |
| | | if (productModel.getProductCode() != null) { |
| | | codes.add(productModel.getProductCode().trim()); |
| | | /** |
| | | * 预加载库中已存在的去重键(产品名称 + 规格型号 + 编码),并作为本次导入的初始集合。 |
| | | */ |
| | | private Set<String> loadExistingProductKeys(Map<String, List<ProductImportRowDto>> sheetMap) { |
| | | Set<String> productNames = new HashSet<>(); |
| | | for (List<ProductImportRowDto> rows : sheetMap.values()) { |
| | | if (CollectionUtils.isEmpty(rows)) { |
| | | continue; |
| | | } |
| | | for (ProductImportRowDto row : rows) { |
| | | String productName = trimToNull(row.getProductName()); |
| | | if (productName != null) { |
| | | productNames.add(productName); |
| | | } |
| | | } |
| | | return codes; |
| | | } |
| | | Set<String> keys = new HashSet<>(); |
| | | if (productNames.isEmpty()) { |
| | | return keys; |
| | | } |
| | | |
| | | List<Product> products = productMapper.selectList(new LambdaQueryWrapper<Product>() |
| | | .in(Product::getProductName, productNames)); |
| | | if (CollectionUtils.isEmpty(products)) { |
| | | return keys; |
| | | } |
| | | Map<Long, String> nameById = new HashMap<>(); |
| | | List<Long> productIds = new ArrayList<>(); |
| | | for (Product product : products) { |
| | | nameById.put(product.getId(), product.getProductName()); |
| | | productIds.add(product.getId()); |
| | | } |
| | | |
| | | List<ProductModel> productModels = productModelMapper.selectList(new LambdaQueryWrapper<ProductModel>() |
| | | .in(ProductModel::getProductId, productIds)); |
| | | for (ProductModel productModel : productModels) { |
| | | String productName = nameById.get(productModel.getProductId()); |
| | | if (productName != null) { |
| | | keys.add(dedupKey(productName, productModel.getModel(), productModel.getProductCode())); |
| | | } |
| | | } |
| | | return keys; |
| | | } |
| | | |
| | | private static String dedupKey(String productName, String model, String productCode) { |
| | | String normalizedModel = trimToNull(model) == null ? "/" : model.trim(); |
| | | String normalizedCode = productCode == null ? "" : productCode.trim(); |
| | | return productName.trim() + "\u0001" + normalizedModel + "\u0001" + normalizedCode; |
| | | } |
| | | |
| | | private String generateUniqueCode(Set<String> generatedCodes) { |