com.ludii.excel.parse.DefaultCellValueTransform Maven / Gradle / Ivy
package com.ludii.excel.parse;
import com.ludii.excel.config.Configuration;
import com.ludii.excel.handler.TypeHandler;
import com.ludii.excel.handler.TypeHandlerRegistry;
import com.ludii.excel.handler.VoidTypeHandler;
import com.ludii.excel.utils.ExcelUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author 陆迪
* @date 2022/3/24
*/
public class DefaultCellValueTransform implements CellValueTransform {
private final Logger log = LoggerFactory.getLogger(DefaultCellValueTransform.class);
private final TypeHandlerRegistry typeHandlerRegistry;
public DefaultCellValueTransform() {
this(new Configuration());
}
public DefaultCellValueTransform(Configuration configuration) {
this.typeHandlerRegistry = configuration.getTypeHandlerRegistry();
}
@Override
public Object transformReadValue(Object cellValue, ExcelFieldConfigDefinedItem configDefinedItem) {
if (cellValue == null) {
return null;
}
ExcelFieldDefined excelFieldDefined = configDefinedItem.getExcelField();
// 字典转换 优先级最高
String dictType = excelFieldDefined.dictType();
cellValue = transformReadValueByDictType(cellValue, dictType);
// 如果存在自定义转换,则直接返回自定义转换的结果
TypeHandler> typeHandler;
Class extends TypeHandler>> typeHandlerClazz = excelFieldDefined.typeHandler();
Class> valueType = configDefinedItem.getValueType();
if (typeHandlerClazz == null || typeHandlerClazz.equals(VoidTypeHandler.class)) {
typeHandler = typeHandlerRegistry.getTypeHandler(valueType);
} else {
typeHandler = typeHandlerRegistry.getMappingTypeHandler(typeHandlerClazz);
}
cellValue = typeHandler.transformReadValue(cellValue);
return cellValue;
}
/**
* 判断是否需要从字典表进行数据转换
*/
public Object transformReadValueByDictType(Object cellValue, String dictType) {
if (ExcelUtils.isBlank(dictType)) {
return cellValue;
}
//TODO 通过字典获取,后续再完善
log.debug("{}", cellValue);
return cellValue;
}
}