|
|
@ -6,9 +6,9 @@ import com.google.common.collect.Maps;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.opsli.api.wrapper.system.dict.DictModel;
|
|
|
|
import org.opsli.api.wrapper.system.dict.DictModel;
|
|
|
|
import org.opsli.common.annotation.DictType;
|
|
|
|
|
|
|
|
import org.opsli.common.enums.ExcelOperate;
|
|
|
|
import org.opsli.common.enums.ExcelOperate;
|
|
|
|
import org.opsli.plugins.excel.ExcelPlugin;
|
|
|
|
import org.opsli.plugins.excel.ExcelPlugin;
|
|
|
|
|
|
|
|
import org.opsli.plugins.excel.annotation.ExcelInfo;
|
|
|
|
import org.opsli.plugins.excel.exception.ExcelPluginException;
|
|
|
|
import org.opsli.plugins.excel.exception.ExcelPluginException;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
@ -33,27 +33,27 @@ public class ExcelUtil extends ExcelPlugin {
|
|
|
|
public <T> List<T> readExcel(MultipartFile excel, Class<T> rowModel) throws ExcelPluginException {
|
|
|
|
public <T> List<T> readExcel(MultipartFile excel, Class<T> rowModel) throws ExcelPluginException {
|
|
|
|
List<T> ts = super.readExcel(excel, rowModel);
|
|
|
|
List<T> ts = super.readExcel(excel, rowModel);
|
|
|
|
// 处理数据
|
|
|
|
// 处理数据
|
|
|
|
return this.handleDict(ts, rowModel, ExcelOperate.READ);
|
|
|
|
return this.handleDatas(ts, rowModel, ExcelOperate.READ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public <T> List<T> readExcel(MultipartFile excel, Class<T> rowModel, String sheetName) throws ExcelPluginException {
|
|
|
|
public <T> List<T> readExcel(MultipartFile excel, Class<T> rowModel, String sheetName) throws ExcelPluginException {
|
|
|
|
List<T> ts = super.readExcel(excel, rowModel, sheetName);
|
|
|
|
List<T> ts = super.readExcel(excel, rowModel, sheetName);
|
|
|
|
// 处理数据
|
|
|
|
// 处理数据
|
|
|
|
return this.handleDict(ts, rowModel, ExcelOperate.READ);
|
|
|
|
return this.handleDatas(ts, rowModel, ExcelOperate.READ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public <T> List<T> readExcel(MultipartFile excel, Class<T> rowModel, String sheetName, int headLineNum) throws ExcelPluginException {
|
|
|
|
public <T> List<T> readExcel(MultipartFile excel, Class<T> rowModel, String sheetName, int headLineNum) throws ExcelPluginException {
|
|
|
|
List<T> ts = super.readExcel(excel, rowModel, sheetName, headLineNum);
|
|
|
|
List<T> ts = super.readExcel(excel, rowModel, sheetName, headLineNum);
|
|
|
|
// 处理数据
|
|
|
|
// 处理数据
|
|
|
|
return this.handleDict(ts, rowModel, ExcelOperate.READ);
|
|
|
|
return this.handleDatas(ts, rowModel, ExcelOperate.READ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public <T> void writeExcel(HttpServletResponse response, List<T> list, String fileName, String sheetName, Class<T> classType, ExcelTypeEnum excelTypeEnum) throws ExcelPluginException {
|
|
|
|
public <T> void writeExcel(HttpServletResponse response, List<T> list, String fileName, String sheetName, Class<T> classType, ExcelTypeEnum excelTypeEnum) throws ExcelPluginException {
|
|
|
|
// 处理数据
|
|
|
|
// 处理数据
|
|
|
|
List<T> ts = this.handleDict(list, classType, ExcelOperate.WRITE);
|
|
|
|
List<T> ts = this.handleDatas(list, classType, ExcelOperate.WRITE);
|
|
|
|
super.writeExcel(response, ts, fileName, sheetName, classType, excelTypeEnum);
|
|
|
|
super.writeExcel(response, ts, fileName, sheetName, classType, excelTypeEnum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -65,7 +65,7 @@ public class ExcelUtil extends ExcelPlugin {
|
|
|
|
* @param <T>
|
|
|
|
* @param <T>
|
|
|
|
* @return
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private <T> List<T> handleDict(List<T> datas, Class<T> typeClazz, ExcelOperate operate){
|
|
|
|
private <T> List<T> handleDatas(List<T> datas, Class<T> typeClazz, ExcelOperate operate){
|
|
|
|
|
|
|
|
|
|
|
|
// 空处理
|
|
|
|
// 空处理
|
|
|
|
if(datas == null || datas.size() == 0){
|
|
|
|
if(datas == null || datas.size() == 0){
|
|
|
@ -75,33 +75,55 @@ public class ExcelUtil extends ExcelPlugin {
|
|
|
|
// 字段名 - 字典code
|
|
|
|
// 字段名 - 字典code
|
|
|
|
Map<String,String> fieldAndTypeCode = Maps.newHashMap();
|
|
|
|
Map<String,String> fieldAndTypeCode = Maps.newHashMap();
|
|
|
|
// 字典code - 字典值
|
|
|
|
// 字典code - 字典值
|
|
|
|
Map<String,List<DictModel>> typeCodeAndValue = Maps.newHashMap();
|
|
|
|
Map<String,List<DictModel>> typeCodeAndValue = null;
|
|
|
|
|
|
|
|
|
|
|
|
Field[] fields = ReflectUtil.getFields(typeClazz);
|
|
|
|
Field[] fields = ReflectUtil.getFields(typeClazz);
|
|
|
|
for (Field field : fields) {
|
|
|
|
for (Field field : fields) {
|
|
|
|
DictType dictType = field.getAnnotation(DictType.class);
|
|
|
|
ExcelInfo excelInfo = field.getAnnotation(ExcelInfo.class);
|
|
|
|
if(dictType != null){
|
|
|
|
if(excelInfo != null){
|
|
|
|
String typeCode = dictType.value();
|
|
|
|
// 字典
|
|
|
|
fieldAndTypeCode.put(field.getName(), typeCode);
|
|
|
|
String dictType = excelInfo.dictType();
|
|
|
|
|
|
|
|
if(StringUtils.isNotEmpty(dictType)){
|
|
|
|
|
|
|
|
fieldAndTypeCode.put(field.getName(), dictType);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 如果有字典
|
|
|
|
|
|
|
|
if(fieldAndTypeCode.size() != 0){
|
|
|
|
|
|
|
|
typeCodeAndValue = this.getDictMap(fieldAndTypeCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 如果没有字典 则直接返回
|
|
|
|
// 数据字典赋值
|
|
|
|
if(fieldAndTypeCode.size() == 0){
|
|
|
|
for (T data : datas) {
|
|
|
|
|
|
|
|
// 处理字典
|
|
|
|
|
|
|
|
this.handleDict(data, operate, fieldAndTypeCode, typeCodeAndValue);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return datas;
|
|
|
|
return datas;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 取Redis 值
|
|
|
|
// ========================= 处理字典 =========================
|
|
|
|
for (Map.Entry<String, String> entry : fieldAndTypeCode.entrySet()) {
|
|
|
|
|
|
|
|
String typeCode = entry.getValue();
|
|
|
|
/**
|
|
|
|
List<DictModel> dictList = DictUtil.getDictList(typeCode);
|
|
|
|
* 处理字典
|
|
|
|
// 如果字典 List 为空 则走下一个
|
|
|
|
* @param data 数据
|
|
|
|
if(dictList == null || dictList.size() == 0) continue;
|
|
|
|
* @param operate excel操作类型
|
|
|
|
typeCodeAndValue.put(typeCode, dictList);
|
|
|
|
* @param fieldAndTypeCode 字段名 - 字典code
|
|
|
|
|
|
|
|
* @param typeCodeAndValue 字典code - 字典值
|
|
|
|
|
|
|
|
* @param <T>
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private <T> void handleDict(T data, ExcelOperate operate, Map<String,String> fieldAndTypeCode,
|
|
|
|
|
|
|
|
Map<String,List<DictModel>> typeCodeAndValue
|
|
|
|
|
|
|
|
){
|
|
|
|
|
|
|
|
// 如果没有字典 则直接退出
|
|
|
|
|
|
|
|
if(fieldAndTypeCode.size() == 0 || typeCodeAndValue == null || typeCodeAndValue.size() == 0){
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 数据字典赋值
|
|
|
|
// 数据字典赋值
|
|
|
|
for (T data : datas) {
|
|
|
|
|
|
|
|
for (Map.Entry<String, String> entry : fieldAndTypeCode.entrySet()) {
|
|
|
|
for (Map.Entry<String, String> entry : fieldAndTypeCode.entrySet()) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
String fieldName = entry.getKey();
|
|
|
|
String fieldName = entry.getKey();
|
|
|
@ -120,7 +142,24 @@ public class ExcelUtil extends ExcelPlugin {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return datas;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 获得字典缓存Map
|
|
|
|
|
|
|
|
* @param fieldAndTypeCode
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public Map<String,List<DictModel>> getDictMap(Map<String,String> fieldAndTypeCode){
|
|
|
|
|
|
|
|
// 字典code - 字典值
|
|
|
|
|
|
|
|
Map<String,List<DictModel>> typeCodeAndValue = Maps.newHashMap();
|
|
|
|
|
|
|
|
// 取Redis 值
|
|
|
|
|
|
|
|
for (Map.Entry<String, String> entry : fieldAndTypeCode.entrySet()) {
|
|
|
|
|
|
|
|
String typeCode = entry.getValue();
|
|
|
|
|
|
|
|
List<DictModel> dictList = DictUtil.getDictList(typeCode);
|
|
|
|
|
|
|
|
// 如果字典 List 为空 则走下一个
|
|
|
|
|
|
|
|
if(dictList == null || dictList.size() == 0) continue;
|
|
|
|
|
|
|
|
typeCodeAndValue.put(typeCode, dictList);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return typeCodeAndValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -152,4 +191,8 @@ public class ExcelUtil extends ExcelPlugin {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ========================= 反射字段 =========================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|