gongchunyi
17 小时以前 ddc95002ef4c7593a006443690d5ae6bba693227
fix: 产品导入限制
已修改3个文件
54 ■■■■■ 文件已修改
src/main/java/com/ruoyi/basic/dto/ProductImportRowDto.java 5 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java 43 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/basic/dto/ProductImportRowDto.java
@@ -1,5 +1,6 @@
package com.ruoyi.basic.dto;
import com.ruoyi.common.utils.poi.ExcelRowAware;
import com.ruoyi.framework.aspectj.lang.annotation.Excel;
import lombok.Data;
@@ -7,7 +8,7 @@
 * 产品批量导入行(三个sheet:原料/半成品/成品)
 */
@Data
public class ProductImportRowDto {
public class ProductImportRowDto implements ExcelRowAware {
    @Excel(name = "产品父类")
    private String productParentName;
@@ -26,4 +27,6 @@
    @Excel(name = "单位")
    private String unit;
    private Integer excelRowNum;
}
src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java
@@ -161,12 +161,16 @@
            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());
@@ -291,6 +295,41 @@
        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();
src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java
@@ -535,6 +535,9 @@
                        ReflectUtils.invokeSetter(entity, propertyName, val);
                    }
                }
                if (entity instanceof ExcelRowAware) {
                    ((ExcelRowAware) entity).setExcelRowNum(i + 1);
                }
                list.add(entity);
            }
        }
@@ -714,6 +717,9 @@
                        ReflectUtils.invokeSetter(entity, propertyName, val);
                    }
                }
                if (entity instanceof ExcelRowAware) {
                    ((ExcelRowAware) entity).setExcelRowNum(i + 1);
                }
                list.add(entity);
            }
        }