tuqin
2026-09-24 0891b6708cc09c2672a19661a8555aae0e742df1
src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java
@@ -18,6 +18,7 @@
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.basic.dto.ProductModelExportDto;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
import com.ruoyi.sales.pojo.SalesLedgerProduct;
@@ -27,6 +28,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import jakarta.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
@@ -161,12 +164,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 +298,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();
@@ -329,4 +371,20 @@
        String trimmed = value.trim();
        return trimmed.isEmpty() ? null : trimmed;
    }
    @Override
    public void exportProductModel(HttpServletResponse response, ProductDto productDto) {
        Page<ProductModel> page = new Page<>(-1, -1);
        List<ProductModel> list = modelListPage(page, productDto).getRecords();
        List<ProductModelExportDto> exportList = new ArrayList<>();
        for (ProductModel productModel : list) {
            ProductModelExportDto dto = new ProductModelExportDto();
            dto.setProductCode(productModel.getProductCode());
            dto.setModel(productModel.getModel());
            dto.setUnit(productModel.getUnit());
            exportList.add(dto);
        }
        ExcelUtil<ProductModelExportDto> util = new ExcelUtil<>(ProductModelExportDto.class);
        util.exportExcel(response, exportList, "产品规格型号");
    }
}