|
|
@@ -1,6 +1,8 @@
|
|
|
package cn.tr.module.export.handler;
|
|
|
|
|
|
import cn.hutool.core.convert.Convert;
|
|
|
+import cn.hutool.core.lang.Pair;
|
|
|
+import cn.hutool.core.thread.ThreadUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.tr.module.export.annotation.ExcelPropertySupport;
|
|
|
@@ -20,7 +22,7 @@ import java.util.*;
|
|
|
*/
|
|
|
|
|
|
public abstract class AbstractCascadeSelectConverter<T> implements Converter<T> {
|
|
|
-
|
|
|
+ private static final ThreadLocal<List<Pair<String, String>>> threadLocal = ThreadUtil.createThreadLocal(false);
|
|
|
@Override
|
|
|
public Class<T> supportJavaTypeKey() {
|
|
|
return doSupportJavaTypeKey();
|
|
|
@@ -34,7 +36,7 @@ public abstract class AbstractCascadeSelectConverter<T> implements Converter<T>
|
|
|
ExcelPropertySupport excelPropertySupport = contentProperty.getField().getAnnotation(ExcelPropertySupport.class);
|
|
|
String content = cellData.getStringValue();
|
|
|
//校验是否存在
|
|
|
- T javaValue = excelConverterJavaValue(content,getParams(excelPropertySupport));
|
|
|
+ T javaValue = excelConverterJavaValue(content,getSelectPairs(getParams(excelPropertySupport)));
|
|
|
if(ObjectUtil.isNull(javaValue)){
|
|
|
throw new UnsupportedOperationException("所填值{"+content+"}不合法,请重新下载模板导入");
|
|
|
}
|
|
|
@@ -47,7 +49,7 @@ public abstract class AbstractCascadeSelectConverter<T> implements Converter<T>
|
|
|
//java 转execl,java中填写的是key值,填写value(key)放入execl中
|
|
|
ExcelPropertySupport excelPropertySupport = contentProperty.getField().getAnnotation(ExcelPropertySupport.class);
|
|
|
if(excelPropertySupport!=null){
|
|
|
- cellData.setStringValue(javaConverterExcelCellValue(value,getParams(excelPropertySupport)));
|
|
|
+ cellData.setStringValue(javaConverterExcelCellValue(value,getSelectPairs(getParams(excelPropertySupport))));
|
|
|
}
|
|
|
return cellData;
|
|
|
}
|
|
|
@@ -62,10 +64,10 @@ public abstract class AbstractCascadeSelectConverter<T> implements Converter<T>
|
|
|
/**
|
|
|
* 根据所给的值将java的值转变为execl所展示的值
|
|
|
* @param content execl内容
|
|
|
- * @param params {@link ExcelSelect#param()}
|
|
|
+ * @param selectValues 选择框内容
|
|
|
* @return
|
|
|
*/
|
|
|
- public T excelConverterJavaValue(String content,Collection<String> params){
|
|
|
+ public T excelConverterJavaValue(String content,List<Pair<String, String>> selectValues){
|
|
|
if(doSupportJavaTypeKey().equals(Boolean.class)){
|
|
|
if(StrUtil.equals(content,"1")||StrUtil.equals(content,"true")){
|
|
|
return Convert.convert(doSupportJavaTypeKey(),Boolean.TRUE);
|
|
|
@@ -73,7 +75,12 @@ public abstract class AbstractCascadeSelectConverter<T> implements Converter<T>
|
|
|
return Convert.convert(doSupportJavaTypeKey(),Boolean.FALSE);
|
|
|
}
|
|
|
}
|
|
|
- return Convert.convert(doSupportJavaTypeKey(), content);
|
|
|
+ for (Pair<String, String> pair : selectValues) {
|
|
|
+ if (ObjectUtil.equals(content, pair.getValue())) {
|
|
|
+ return (T) pair.getKey();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -91,17 +98,48 @@ public abstract class AbstractCascadeSelectConverter<T> implements Converter<T>
|
|
|
public Map<String, List<String>> buildCascadeSelectList(Collection<String> params){return new HashMap<>();};
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取选择框中的键值对
|
|
|
+ * @param params
|
|
|
+ * @return 在excel转java 和 java转excel时使用
|
|
|
+ */
|
|
|
+ public List<Pair<String, String>> getSelectPairs(Collection<String> params){
|
|
|
+ List<Pair<String, String>> selectPairs = threadLocal.get();
|
|
|
+ if(ObjectUtil.isNull(selectPairs)){
|
|
|
+ selectPairs=doGetSelectPairs(params);
|
|
|
+ }
|
|
|
+ threadLocal.set(selectPairs);
|
|
|
+ return selectPairs;
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取选择框中的键值对
|
|
|
+ * @param params
|
|
|
+ * @return
|
|
|
+ * <pre>
|
|
|
+ * java : excel
|
|
|
+ * key: value
|
|
|
+ * <pre/>
|
|
|
+ */
|
|
|
+ public abstract List<Pair<String, String>> doGetSelectPairs(Collection<String> params);
|
|
|
+
|
|
|
/**
|
|
|
* 根据所给的值将java的值转变为execl所展示的值
|
|
|
* @param value java所给的value值
|
|
|
- * @param params {@link ExcelSelect#param()}
|
|
|
+ * @param selectValues 选择框键值对
|
|
|
* @return
|
|
|
*/
|
|
|
- public String javaConverterExcelCellValue(T value,Collection<String> params){
|
|
|
+ public String javaConverterExcelCellValue(T value,List<Pair<String, String>> selectValues){
|
|
|
if(ObjectUtil.isNull(value)){
|
|
|
return "";
|
|
|
}
|
|
|
- return String.valueOf(value);
|
|
|
+ for (Pair<String, String> pair : selectValues) {
|
|
|
+ if (ObjectUtil.equals(value, pair.getKey())) {
|
|
|
+ return pair.getValue();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
};
|
|
|
|
|
|
public abstract Class<T> doSupportJavaTypeKey();
|