package org.springblade.common.excel;
|
|
import com.alibaba.excel.converters.Converter;
|
import com.alibaba.excel.enums.CellDataTypeEnum;
|
import com.alibaba.excel.metadata.CellData;
|
import com.alibaba.excel.metadata.GlobalConfiguration;
|
import com.alibaba.excel.metadata.property.ExcelContentProperty;
|
import com.github.xiaoymin.knife4j.core.util.StrUtil;
|
import org.springblade.common.cache.DictBizCache;
|
import org.springblade.modules.system.entity.DictBiz;
|
import org.springframework.stereotype.Component;
|
|
import java.lang.reflect.Field;
|
import java.util.List;
|
|
/**
|
* 字典映射
|
*
|
* @author zhongrj
|
* @date 2023-11-17
|
*/
|
@Component
|
public class ExcelDictConverter implements Converter<String> {
|
|
@Override
|
public Class supportJavaTypeKey() {
|
return Integer.class;
|
}
|
|
@Override
|
public CellDataTypeEnum supportExcelTypeKey() {
|
return CellDataTypeEnum.STRING;
|
}
|
|
/**
|
* 导入excel 解析到java 对象
|
* @param cellData
|
* @param excelContentProperty
|
* @param globalConfiguration
|
* @return
|
* @throws Exception
|
*/
|
@Override
|
public String convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
|
// 获取字典类型
|
Field field = excelContentProperty.getField();
|
ExcelDictItemLabel excel = field.getAnnotation(ExcelDictItemLabel.class);
|
String dictType = excel.type();
|
// 为空返回
|
String dictLabel = cellData.getStringValue();
|
if (StrUtil.isBlank(dictLabel)) {
|
return dictLabel;
|
}
|
// 查询字典
|
List<DictBiz> list = DictBizCache.getList(dictType);
|
//解析返回
|
String key = "";
|
//对学历进行单独处理
|
if (dictType.equals("educationType")){
|
for (DictBiz dictBiz : list) {
|
if (dictBiz.getDictValue().indexOf(dictLabel)>-1){
|
key = dictBiz.getDictKey();
|
break;
|
}
|
}
|
}else{
|
for (DictBiz dictBiz : list) {
|
if (dictBiz.getDictValue().equals(dictLabel)){
|
key = dictBiz.getDictKey();
|
break;
|
}
|
}
|
}
|
|
// 返回key
|
return key;
|
}
|
|
/**
|
* java 导出到 excel
|
* @param dictValue
|
* @param excelContentProperty
|
* @param globalConfiguration
|
* @return
|
* @throws Exception
|
*/
|
@Override
|
public CellData convertToExcelData(String dictValue, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
|
// 获取字典类型
|
Field field = excelContentProperty.getField();
|
ExcelDictItem excel = field.getAnnotation(ExcelDictItem.class);
|
String dictType = excel.type();
|
List<DictBiz> list = DictBizCache.getList(dictType);
|
String value = "";
|
//解析返回
|
for (DictBiz dictBiz : list) {
|
if (dictBiz.getDictKey().equals(dictValue)){
|
value = dictBiz.getDictValue();
|
break;
|
}
|
}
|
return new CellData(value);
|
}
|
}
|