| | |
| | | Map<String, Long> nodeCache = new HashMap<>(); |
| | | for (int i = 0; i < rows.size(); i++) { |
| | | ProductImportRowDto row = rows.get(i); |
| | | int rowNum = i + 2; |
| | | // 没有名称/编码/规格的行构不成产品数据,按空行跳过 |
| | | if (hasNoProductContent(row)) { |
| | | continue; |
| | | } |
| | | String parentName = trimToNull(row.getProductParentName()); |
| | | String categoryName = trimToNull(row.getProductCategoryName()); |
| | | String productName = trimToNull(row.getProductName()); |
| | | if (productName == null) { |
| | | throw new ServiceException("第 " + rowNum + " 行 [产品名称] 不能为空"); |
| | | throw new ServiceException(String.format("工作表【%s】第 %d 行 [产品名称] 不能为空(该行已填:%s)", |
| | | sheetName, resolveRowNum(row, i), describeRow(row))); |
| | | } |
| | | |
| | | String model = trimToNull(row.getModel()); |
| | |
| | | return keys; |
| | | } |
| | | |
| | | /** |
| | | * 解析时会跳过空行 |
| | | */ |
| | | private static int resolveRowNum(ProductImportRowDto row, int index) { |
| | | return row.getExcelRowNum() != null ? row.getExcelRowNum() : index + 2; |
| | | } |
| | | |
| | | private static boolean hasNoProductContent(ProductImportRowDto row) { |
| | | return trimToNull(row.getProductName()) == null |
| | | && trimToNull(row.getProductCode()) == null |
| | | && trimToNull(row.getModel()) == null; |
| | | } |
| | | |
| | | private static String describeRow(ProductImportRowDto row) { |
| | | StringBuilder description = new StringBuilder(); |
| | | appendFilledColumn(description, "产品父类", row.getProductParentName()); |
| | | appendFilledColumn(description, "产品大类", row.getProductCategoryName()); |
| | | appendFilledColumn(description, "产品名称", row.getProductName()); |
| | | appendFilledColumn(description, "产品编码", row.getProductCode()); |
| | | appendFilledColumn(description, "规格型号", row.getModel()); |
| | | appendFilledColumn(description, "单位", row.getUnit()); |
| | | return description.length() == 0 ? "无" : description.toString(); |
| | | } |
| | | |
| | | private static void appendFilledColumn(StringBuilder description, String columnName, String value) { |
| | | String trimmed = trimToNull(value); |
| | | if (trimmed == null) { |
| | | return; |
| | | } |
| | | if (description.length() > 0) { |
| | | description.append(","); |
| | | } |
| | | description.append(columnName).append('=').append(trimmed); |
| | | } |
| | | |
| | | private static String dedupKey(String productName, String model, String productCode) { |
| | | String normalizedModel = trimToNull(model) == null ? "/" : model.trim(); |
| | | String normalizedCode = productCode == null ? "" : productCode.trim(); |