Commit fe0d1a1e by zhouhaibao

feat(excel): 添加Excel合并单元格值填充功能

- 在BatchStoreCombinedExcelDTO和BatchStoreExcelDTO中为多个字段添加@FillFromPreviousRow注解
- 新增FillFromPreviousRow自定义注解用于标记需要从上一行填充的字段
- 创建MergedCellHandler组件实现合并单元格值的自动填充逻辑
- 在CreateBusinessApproveExcelListener中注入并使用MergedCellHandler处理数据
- 在StoreExcelImportService中优化产品组合去重逻辑的位置
1 parent 9c365fb7
......@@ -36,6 +36,7 @@ public class BatchStoreCombinedExcelDTO {
private String parkingStore;
@ExcelProperty(value = "*类型", index = 9)
@FillFromPreviousRow
private String productType;
@ExcelProperty(value = "*销售品类", index = 10)
......@@ -54,12 +55,15 @@ public class BatchStoreCombinedExcelDTO {
private BigDecimal revenuePerCar;
@ExcelProperty(value = "*门店单台利润额", index = 15, converter = NumberStringConverter.class)
@FillFromPreviousRow
private BigDecimal storeProfitPerCar;
@ExcelProperty(value = "*交付数", index = 16, converter = IntegerStringConverter.class)
@FillFromPreviousRow
private Integer deliveryCount;
@ExcelProperty(value = "*触客率", index = 17, converter = PercentStringConverter.class)
@FillFromPreviousRow
private BigDecimal customerTouchRate;
@ExcelProperty(value = "*转化率", index = 18, converter = PercentStringConverter.class)
......@@ -93,15 +97,19 @@ public class BatchStoreCombinedExcelDTO {
private BigDecimal productCost;
@ExcelProperty(value = "*人员成本", index = 28, converter = NumberStringConverter.class)
@FillFromPreviousRow
private BigDecimal personnelCost;
@ExcelProperty(value = "*房租", index = 29, converter = NumberStringConverter.class)
@FillFromPreviousRow
private BigDecimal rentCost;
@ExcelProperty(value = "*折旧与摊销", index = 30, converter = NumberStringConverter.class)
@FillFromPreviousRow
private BigDecimal depreciation;
@ExcelProperty(value = "*其他(水电、耗材等)", index = 31, converter = NumberStringConverter.class)
@FillFromPreviousRow
private BigDecimal otherCost;
@ExcelProperty(value = "*原厂膜回收", index = 32, converter = NumberStringConverter.class)
......
......@@ -36,27 +36,34 @@ public class BatchStoreExcelDTO {
private String parkingStore;
@ExcelProperty(value = "*门店单台利润额", index = 15)
@FillFromPreviousRow
private BigDecimal storeProfitPerCar;
@ExcelProperty(value = "*交付数", index = 16)
@FillFromPreviousRow
private Integer deliveryCount;
@ExcelProperty(value = "*触客率", index = 17)
@FillFromPreviousRow
private BigDecimal customerTouchRate;
@ExcelProperty(value = "*产品成本", index = 27)
private BigDecimal productCost;
@ExcelProperty(value = "*人员成本", index = 28)
@FillFromPreviousRow
private BigDecimal personnelCost;
@ExcelProperty(value = "*房租", index = 29)
@FillFromPreviousRow
private BigDecimal rentCost;
@ExcelProperty(value = "*折旧与摊销", index = 30)
@FillFromPreviousRow
private BigDecimal depreciation;
@ExcelProperty(value = "*其他(水电、耗材等)", index = 31)
@FillFromPreviousRow
private BigDecimal otherCost;
......
package cn.com.glsx.dj.oaapproval.dto.excel;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 标记字段需要从上一行填充合并单元格的值
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface FillFromPreviousRow {
}
\ No newline at end of file
package cn.com.glsx.dj.oaapproval.dto.excel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
@Component
@Slf4j
public class MergedCellHandler {
// 存储上一行的值
private Map<String, Object> previousRowValues = new HashMap<>();
/**
* 处理被 @FillFromPreviousRow 注解标记的字段
* @param obj 当前行的数据对象
*/
public void fillMergedCellValues(Object obj) {
if (obj == null) {
return;
}
Class<?> clazz = obj.getClass();
// 遍历所有被 @FillFromPreviousRow 注解标记的字段
for (Field field : clazz.getDeclaredFields()) {
if (field.isAnnotationPresent(FillFromPreviousRow.class)) {
try {
field.setAccessible(true);
Object currentValue = field.get(obj);
// 获取字段名作为key
String fieldName = field.getName();
// 如果当前值为空,使用上一行的值
if (isEmpty(currentValue)) {
Object prevValue = previousRowValues.get(fieldName);
if (prevValue != null) {
field.set(obj, prevValue);
}
} else {
// 更新上一行的值
previousRowValues.put(fieldName, currentValue);
}
} catch (IllegalAccessException e) {
log.error("处理合并单元格字段失败: {}", field.getName(), e);
}
}
}
}
/**
* 检查值是否为空
*/
private boolean isEmpty(Object value) {
if (value == null) {
return true;
}
if (value instanceof String) {
return ((String) value).trim().isEmpty();
}
return false;
}
/**
* 清空上一行的值(用于开始新的一批数据处理)
*/
public void clear() {
previousRowValues.clear();
}
}
\ No newline at end of file
......@@ -3,6 +3,7 @@ package cn.com.glsx.dj.oaapproval.listener;
import cn.com.glsx.dj.oaapproval.dto.excel.BatchStoreCombinedExcelDTO;
import cn.com.glsx.dj.oaapproval.dto.excel.BatchStoreExcelDTO;
import cn.com.glsx.dj.oaapproval.dto.excel.BatchStoreProductExcelDTO;
import cn.com.glsx.dj.oaapproval.dto.excel.MergedCellHandler;
import cn.com.glsx.dj.oaapproval.dto.excel.SingleStoreExcelDTO;
import cn.com.glsx.dj.oaapproval.dto.store.ImportErrorInfo;
import cn.com.glsx.dj.oaapproval.dto.store.StoreProductDetailDTO;
......@@ -23,6 +24,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import org.apache.commons.lang3.StringUtils;
import cn.com.glsx.dj.oaapproval.utils.NumberValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
@Slf4j
public class CreateBusinessApproveExcelListener extends AnalysisEventListener<Object> {
......@@ -45,6 +47,9 @@ public class CreateBusinessApproveExcelListener extends AnalysisEventListener<Ob
private boolean secondRowValidated = false;
@Autowired
private MergedCellHandler mergedCellHandler;
public CreateBusinessApproveExcelListener(CreateBusinessApproveImportExcelContext context, boolean isSingleStoreMode) {
this.context = context;
this.isSingleStoreMode = isSingleStoreMode;
......@@ -210,6 +215,7 @@ public class CreateBusinessApproveExcelListener extends AnalysisEventListener<Ob
this.context.incrementFailRows();
return;
}
mergedCellHandler.fillMergedCellValues(data);
BatchStoreExcelDTO storeDTO = parseStoreInfo(data);
BatchStoreProductExcelDTO productDTO = parseProductInfo(data);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!