From 60e1129e0eb1d0189f217cd566e6bf6efd0b2243 Mon Sep 17 00:00:00 2001 From: thinkgem Date: Thu, 5 Mar 2020 18:08:00 +0800 Subject: [PATCH] =?UTF-8?q?Excel=E5=B7=A5=E5=85=B7=E7=B1=BB=E4=BC=98?= =?UTF-8?q?=E5=8C=96=EF=BC=8C=E6=96=B0=E5=A2=9E=20FieldType=20=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=EF=BC=8C=E8=A7=84=E8=8C=83=E5=8C=96=E7=BC=96=E7=A8=8B?= =?UTF-8?q?=EF=BC=8C=E9=98=B2=E6=AD=A2=E8=AF=AF=E7=94=A8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/utils/excel/ExcelExport.java | 99 +++++++++++-------- .../common/utils/excel/ExcelImport.java | 72 ++++++++------ .../utils/excel/annotation/ExcelField.java | 13 ++- .../utils/excel/fieldtype/FieldType.java | 34 +++++++ .../utils/excel/fieldtype/MoneyType.java | 24 ++--- .../utils/excel/fieldtype/AreaType.java | 32 +++--- .../utils/excel/fieldtype/CompanyType.java | 31 +++--- .../utils/excel/fieldtype/OfficeType.java | 31 +++--- .../utils/excel/fieldtype/PostListType.java | 34 +++---- .../jeesite/modules/sys/entity/EmpUser.java | 19 ++-- 10 files changed, 204 insertions(+), 185 deletions(-) create mode 100644 common/src/main/java/com/jeesite/common/utils/excel/fieldtype/FieldType.java diff --git a/common/src/main/java/com/jeesite/common/utils/excel/ExcelExport.java b/common/src/main/java/com/jeesite/common/utils/excel/ExcelExport.java index 59fef31a..5ad06e1c 100644 --- a/common/src/main/java/com/jeesite/common/utils/excel/ExcelExport.java +++ b/common/src/main/java/com/jeesite/common/utils/excel/ExcelExport.java @@ -9,16 +9,15 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.math.BigDecimal; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Set; import javax.servlet.http.HttpServletResponse; @@ -41,10 +40,9 @@ import org.apache.poi.xssf.usermodel.XSSFRichTextString; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.fasterxml.jackson.annotation.JsonFormat; import com.jeesite.common.codec.EncodeUtils; import com.jeesite.common.collect.ListUtils; -import com.jeesite.common.collect.SetUtils; +import com.jeesite.common.collect.MapUtils; import com.jeesite.common.lang.ObjectUtils; import com.jeesite.common.lang.StringUtils; import com.jeesite.common.reflect.ReflectUtils; @@ -52,11 +50,12 @@ import com.jeesite.common.utils.excel.annotation.ExcelField; import com.jeesite.common.utils.excel.annotation.ExcelField.Align; import com.jeesite.common.utils.excel.annotation.ExcelField.Type; import com.jeesite.common.utils.excel.annotation.ExcelFields; +import com.jeesite.common.utils.excel.fieldtype.FieldType; /** * 导出Excel文件(导出“XLSX”格式,支持大数据量导出 @see org.apache.poi.ss.SpreadsheetVersion) * @author ThinkGem - * @version 2020-2-19 + * @version 2020-3-5 */ public class ExcelExport implements Closeable{ @@ -88,9 +87,9 @@ public class ExcelExport implements Closeable{ private List annotationList; /** - * 用于清理缓存 + * 存储字段类型临时数据 */ - private Set> fieldTypes = SetUtils.newHashSet(); + private Map, FieldType> fieldTypes = MapUtils.newHashMap(); /** * 构造函数 @@ -249,7 +248,11 @@ public class ExcelExport implements Closeable{ } } headerList.add(headerTitle); - headerWidthList.add(ef.width()); + if (ef.words() != -1) { + headerWidthList.add(ef.words() * 256); + }else { + headerWidthList.add(ef.width()); + } } // 创建工作表 this.createSheet(sheetName, title, headerList, headerWidthList); @@ -426,7 +429,7 @@ public class ExcelExport implements Closeable{ * @return 单元格对象 */ public Cell addCell(Row row, int column, Object val){ - return this.addCell(row, column, val, Align.AUTO, Class.class, null); + return this.addCell(row, column, val, Align.AUTO, FieldType.class, null); } /** @@ -438,20 +441,17 @@ public class ExcelExport implements Closeable{ * @param dataFormat 数值格式(例如:0.00,yyyy-MM-dd) * @return 单元格对象 */ - public Cell addCell(Row row, int column, Object val, Align align, Class fieldType, String dataFormat){ + @SuppressWarnings("unchecked") + public Cell addCell(Row row, int column, Object val, Align align, Class fieldType, String dataFormat){ Cell cell = row.createCell(column); - String defaultDataFormat = "@"; + String defaultDataFormat = null; try { if(val == null){ - cell.setCellValue(""); - }else if(fieldType != Class.class){ - fieldTypes.add(fieldType); // 先存起来,方便完成后清理缓存 - cell.setCellValue((String)fieldType.getMethod("setValue", Object.class).invoke(null, val)); - try{ - defaultDataFormat = (String)fieldType.getMethod("getDataFormat").invoke(null); - } catch (Exception ex) { - defaultDataFormat = "@"; - } + cell.setCellValue(StringUtils.EMPTY); + }else if(fieldType != FieldType.class){ + FieldType ft = getFieldType(fieldType); + cell.setCellValue(ft.setValue(val)); + defaultDataFormat = ft.getDataFormat(); }else{ if(val instanceof String) { cell.setCellValue((String) val); @@ -474,10 +474,11 @@ public class ExcelExport implements Closeable{ defaultDataFormat = "yyyy-MM-dd HH:mm"; }else { // 如果没有指定 fieldType,切自行根据类型查找相应的转换类(com.jeesite.common.utils.excel.fieldtype.值的类名+Type) - Class fieldType2 = Class.forName(this.getClass().getName().replaceAll(this.getClass().getSimpleName(), + fieldType = (Class)Class.forName(this.getClass().getName().replaceAll(this.getClass().getSimpleName(), "fieldtype."+val.getClass().getSimpleName()+"Type")); - fieldTypes.add(fieldType2); // 先存起来,方便完成后清理缓存 - cell.setCellValue((String)fieldType2.getMethod("setValue", Object.class).invoke(null, val)); + FieldType ft = getFieldType(fieldType); + cell.setCellValue(ft.setValue(val)); + defaultDataFormat = ft.getDataFormat(); } } // if (val != null){ @@ -485,9 +486,12 @@ public class ExcelExport implements Closeable{ if (style == null){ style = wb.createCellStyle(); style.cloneStyleFrom(styles.get("data"+(align.value()>=1&&align.value()<=3?align.value():""))); - if (dataFormat != null){ + if (StringUtils.isNotBlank(dataFormat)){ defaultDataFormat = dataFormat; } + if (defaultDataFormat == null) { + defaultDataFormat = "@"; + } style.setDataFormat(wb.createDataFormat().getFormat(defaultDataFormat)); styles.put("data_column_" + column, style); } @@ -499,6 +503,15 @@ public class ExcelExport implements Closeable{ } return cell; } + + private FieldType getFieldType(Class fieldType) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { + FieldType ft = fieldTypes.get(fieldType); + if (ft == null) { + ft = fieldType.getDeclaredConstructor().newInstance(); + fieldTypes.put(fieldType, ft); + } + return ft; + } /** * 添加数据(通过annotation.ExportField添加数据) @@ -535,17 +548,20 @@ public class ExcelExport implements Closeable{ log.info(ex.toString()); val = ""; } - String dataFormat = ef.dataFormat(); - try { - // 获取Json格式化注解的格式化参数 - JsonFormat jf = e.getClass().getMethod("get"+StringUtils.capitalize(ef.attrName())).getAnnotation(JsonFormat.class); - if (jf != null && jf.pattern() != null){ - dataFormat = jf.pattern(); - } - } catch (Exception e1) { - // 如果获取失败,则使用默认。 - } - this.addCell(row, colunm++, val, ef.align(), ef.fieldType(), dataFormat); +// // 如果没有设置格式,则获取Json格式化注解的格式化参数(建议使用 ef.dataFormat 指定格式) +// String dataFormat = ef.dataFormat(); +// if (StringUtils.isBlank(dataFormat)) { +// try { +// JsonFormat jf = e.getClass().getMethod("get"+StringUtils.capitalize(ef.attrName())) +// .getAnnotation(JsonFormat.class); +// if (jf != null && jf.pattern() != null){ +// dataFormat = jf.pattern(); +// } +// } catch (Exception e1) { +// // 如果获取失败,则使用默认。 +// } +// } + this.addCell(row, colunm++, val, ef.align(), ef.fieldType(), ef.dataFormat()); sb.append(val + ", "); } log.debug("Write success: ["+row.getRowNum()+"] "+sb.toString()); @@ -607,17 +623,14 @@ public class ExcelExport implements Closeable{ @Override public void close() { + fieldTypes.clear(); if (wb instanceof SXSSFWorkbook){ ((SXSSFWorkbook)wb).dispose(); } - Iterator> it = fieldTypes.iterator(); - while(it.hasNext()){ - Class clazz = it.next(); - try { - clazz.getMethod("clearCache").invoke(null); - } catch (Exception e) { - // 报错忽略,有可能没实现此方法 - } + try { + wb.close(); + } catch (IOException e) { + e.printStackTrace(); } } diff --git a/common/src/main/java/com/jeesite/common/utils/excel/ExcelImport.java b/common/src/main/java/com/jeesite/common/utils/excel/ExcelImport.java index 72a852ac..e8da823c 100644 --- a/common/src/main/java/com/jeesite/common/utils/excel/ExcelImport.java +++ b/common/src/main/java/com/jeesite/common/utils/excel/ExcelImport.java @@ -9,6 +9,7 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.text.DecimalFormat; import java.util.Collections; @@ -16,9 +17,8 @@ import java.util.Comparator; import java.util.Date; import java.util.Iterator; import java.util.List; -import java.util.Set; +import java.util.Map; -import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.formula.eval.ErrorEval; @@ -37,7 +37,7 @@ import org.springframework.web.multipart.MultipartFile; import com.jeesite.common.callback.MethodCallback; import com.jeesite.common.collect.ListUtils; -import com.jeesite.common.collect.SetUtils; +import com.jeesite.common.collect.MapUtils; import com.jeesite.common.lang.DateUtils; import com.jeesite.common.lang.ObjectUtils; import com.jeesite.common.lang.StringUtils; @@ -45,11 +45,12 @@ import com.jeesite.common.reflect.ReflectUtils; import com.jeesite.common.utils.excel.annotation.ExcelField; import com.jeesite.common.utils.excel.annotation.ExcelField.Type; import com.jeesite.common.utils.excel.annotation.ExcelFields; +import com.jeesite.common.utils.excel.fieldtype.FieldType; /** * 导入Excel文件(支持“XLS”和“XLSX”格式) * @author ThinkGem - * @version 2020-2-19 + * @version 2020-3-5 */ public class ExcelImport implements Closeable { @@ -71,9 +72,9 @@ public class ExcelImport implements Closeable { private int headerNum; /** - * 用于清理缓存 + * 存储字段类型临时数据 */ - private Set> fieldTypes = SetUtils.newHashSet(); + private Map, FieldType> fieldTypes = MapUtils.newHashMap(); /** * 构造函数 @@ -263,9 +264,9 @@ public class ExcelImport implements Closeable { try{ Cell cell = row.getCell(column); if (cell != null){ - if (cell.getCellTypeEnum() == CellType.NUMERIC){ + if (cell.getCellType() == CellType.NUMERIC){ val = cell.getNumericCellValue(); - if (HSSFDateUtil.isCellDateFormatted(cell)) { + if (DateUtil.isCellDateFormatted(cell)) { val = DateUtil.getJavaDate((Double) val); // POI Excel 日期格式转换 }else{ if ((Double) val % 1 > 0){ @@ -274,17 +275,17 @@ public class ExcelImport implements Closeable { val = new DecimalFormat("0").format(val); } } - }else if (cell.getCellTypeEnum() == CellType.STRING) { + }else if (cell.getCellType() == CellType.STRING) { val = cell.getStringCellValue(); - }else if (cell.getCellTypeEnum() == CellType.FORMULA){ + }else if (cell.getCellType() == CellType.FORMULA){ try { val = cell.getStringCellValue(); } catch (Exception e) { FormulaEvaluator evaluator = cell.getSheet().getWorkbook() .getCreationHelper().createFormulaEvaluator(); - evaluator.evaluateFormulaCellEnum(cell); + evaluator.evaluateFormulaCell(cell); CellValue cellValue = evaluator.evaluate(cell); - switch (cellValue.getCellTypeEnum()) { + switch (cellValue.getCellType()) { case NUMERIC: val = cellValue.getNumberValue(); break; @@ -301,9 +302,9 @@ public class ExcelImport implements Closeable { val = cell.getCellFormula(); } } - }else if (cell.getCellTypeEnum() == CellType.BOOLEAN){ + }else if (cell.getCellType() == CellType.BOOLEAN){ val = cell.getBooleanCellValue(); - }else if (cell.getCellTypeEnum() == CellType.ERROR){ + }else if (cell.getCellType() == CellType.ERROR){ val = cell.getErrorCellValue(); } } @@ -348,6 +349,7 @@ public class ExcelImport implements Closeable { * @param isThrowException 遇见错误是否抛出异常 * @param groups 导入分组 */ + @SuppressWarnings("unchecked") public List getDataList(Class cls, MethodCallback exceptionCallback, String... groups) throws InstantiationException, IllegalAccessException{ List annotationList = ListUtils.newArrayList(); // Get annotation field @@ -426,9 +428,9 @@ public class ExcelImport implements Closeable { //log.debug("Import value type: ["+i+","+column+"] " + valType); try { if (StringUtils.isNotBlank(ef.attrName())){ - if (ef.fieldType() != Class.class){ - fieldTypes.add(ef.fieldType()); // 先存起来,方便完成后清理缓存 - val = ef.fieldType().getMethod("getValue", String.class).invoke(null, val); + if (ef.fieldType() != FieldType.class){ + FieldType ft = getFieldType(ef.fieldType()); + val = ft.getValue(ObjectUtils.toString(val)); } }else{ if (val != null){ @@ -454,15 +456,15 @@ public class ExcelImport implements Closeable { val = DateUtil.getJavaDate((Double)val); // POI Excel 日期格式转换 } }else{ - if (ef.fieldType() != Class.class){ - fieldTypes.add(ef.fieldType()); // 先存起来,方便完成后清理缓存 - val = ef.fieldType().getMethod("getValue", String.class).invoke(null, val.toString()); + if (ef.fieldType() != FieldType.class){ + FieldType ft = getFieldType(ef.fieldType()); + val = ft.getValue(ObjectUtils.toString(val)); }else{ // 如果没有指定 fieldType,切自行根据类型查找相应的转换类(com.jeesite.common.utils.excel.fieldtype.值的类名+Type) - Class fieldType2 = Class.forName(this.getClass().getName().replaceAll(this.getClass().getSimpleName(), + Class fieldType = (Class)Class.forName(this.getClass().getName().replaceAll(this.getClass().getSimpleName(), "fieldtype."+valType.getSimpleName()+"Type")); - fieldTypes.add(fieldType2); // 先存起来,方便完成后清理缓存 - val = fieldType2.getMethod("getValue", String.class).invoke(null, val.toString()); + FieldType ft = getFieldType(fieldType); + val = ft.getValue(ObjectUtils.toString(val)); } } } @@ -496,16 +498,22 @@ public class ExcelImport implements Closeable { return dataList; } + private FieldType getFieldType(Class fieldType) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { + FieldType ft = fieldTypes.get(fieldType); + if (ft == null) { + ft = fieldType.getDeclaredConstructor().newInstance(); + fieldTypes.put(fieldType, ft); + } + return ft; + } + @Override - public void close() { - Iterator> it = fieldTypes.iterator(); - while(it.hasNext()){ - Class clazz = it.next(); - try { - clazz.getMethod("clearCache").invoke(null); - } catch (Exception e) { - // 报错忽略,有可能没实现此方法 - } + public void close() throws IOException { + fieldTypes.clear(); + try { + wb.close(); + } catch (IOException e) { + e.printStackTrace(); } } diff --git a/common/src/main/java/com/jeesite/common/utils/excel/annotation/ExcelField.java b/common/src/main/java/com/jeesite/common/utils/excel/annotation/ExcelField.java index 4d0fe218..899f931b 100644 --- a/common/src/main/java/com/jeesite/common/utils/excel/annotation/ExcelField.java +++ b/common/src/main/java/com/jeesite/common/utils/excel/annotation/ExcelField.java @@ -8,10 +8,12 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import com.jeesite.common.utils.excel.fieldtype.FieldType; + /** * Excel注解定义 * @author ThinkGem - * @version 2013-03-10 + * @version 2020-3-5 */ @Target({ElementType.METHOD, ElementType.FIELD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @@ -59,6 +61,11 @@ public @interface ExcelField { */ int width() default -1; + /** + * 指定导出列宽可以显示的字符个数(words*256=width,1个汉字占2个字符) v4.2.0 + */ + int words() default -1; + /** * 导出字段字段排序(升序) */ @@ -79,12 +86,12 @@ public @interface ExcelField { * MoneyType.class 金额类型转换(保留两位) * DateTimeType.class 日期时间类型转换 yyyy-MM-dd HH:mm:ss */ - Class fieldType() default Class.class; + Class fieldType() default FieldType.class; /** * 数值格式(例如:数值:0.00;日期:yyyy-MM-dd;金额:¥#,##0.00) */ - String dataFormat() default "@"; + String dataFormat() default ""; /** * 字段归属组(针对每一种业务的导入、导出) imp、exp diff --git a/common/src/main/java/com/jeesite/common/utils/excel/fieldtype/FieldType.java b/common/src/main/java/com/jeesite/common/utils/excel/fieldtype/FieldType.java new file mode 100644 index 00000000..80f46e99 --- /dev/null +++ b/common/src/main/java/com/jeesite/common/utils/excel/fieldtype/FieldType.java @@ -0,0 +1,34 @@ +/** + * Copyright (c) 2013-Now http://jeesite.com All rights reserved. + */ +package com.jeesite.common.utils.excel.fieldtype; + +/** + * Excel字段类型转换 + * @author ThinkGem + * @version 2020-3-5 + */ +public interface FieldType { + + /** + * 获取对象值(导入) + */ + default public Object getValue(String val) { + return null; + } + + /** + * 获取对象值(导出) + */ + default public String setValue(Object val) { + return null; + } + + /** + * 获取对象值格式(导出) + */ + default public String getDataFormat() { + return null; + } + +} diff --git a/common/src/main/java/com/jeesite/common/utils/excel/fieldtype/MoneyType.java b/common/src/main/java/com/jeesite/common/utils/excel/fieldtype/MoneyType.java index fa4cb497..e456bd04 100644 --- a/common/src/main/java/com/jeesite/common/utils/excel/fieldtype/MoneyType.java +++ b/common/src/main/java/com/jeesite/common/utils/excel/fieldtype/MoneyType.java @@ -12,38 +12,32 @@ import org.apache.commons.lang3.StringUtils; /** * 金额类型转换(保留两位) * @author ThinkGem - * @version 2015-9-29 + * @version 2020-3-5 * @example fieldType = MoneyType.class */ -public class MoneyType { +public class MoneyType implements FieldType { + + private NumberFormat nf = new DecimalFormat(",##0.00"); /** * 获取对象值(导入) */ - public static Object getValue(String val) { - return val == null ? "" : StringUtils.replace(val, ",", ""); + public Object getValue(String val) { + return val == null ? StringUtils.EMPTY : StringUtils.replace(val, ",", StringUtils.EMPTY); } /** * 获取对象值(导出) */ - public static String setValue(Object val) { - NumberFormat nf = new DecimalFormat(",##0.00"); - return val == null ? "" : nf.format(val); + public String setValue(Object val) { + return val == null ? StringUtils.EMPTY : nf.format(val); } /** * 获取对象值格式(导出) */ - public static String getDataFormat() { + public String getDataFormat() { return "0.00"; } - /** - * 清理缓存 - */ - public static void clearCache(){ - - } - } diff --git a/modules/core/src/main/java/com/jeesite/common/utils/excel/fieldtype/AreaType.java b/modules/core/src/main/java/com/jeesite/common/utils/excel/fieldtype/AreaType.java index f04172ef..fdba963c 100644 --- a/modules/core/src/main/java/com/jeesite/common/utils/excel/fieldtype/AreaType.java +++ b/modules/core/src/main/java/com/jeesite/common/utils/excel/fieldtype/AreaType.java @@ -5,8 +5,6 @@ package com.jeesite.common.utils.excel.fieldtype; import java.util.List; -import org.springframework.core.NamedThreadLocal; - import com.jeesite.common.lang.StringUtils; import com.jeesite.modules.sys.entity.Area; import com.jeesite.modules.sys.utils.AreaUtils; @@ -14,23 +12,22 @@ import com.jeesite.modules.sys.utils.AreaUtils; /** * 字段类型转换 * @author ThinkGem - * @version 2018-08-11 + * @version 2020-3-5 * @example fieldType = AreaType.class */ -public class AreaType { +public class AreaType implements FieldType { - private static ThreadLocal> cache = new NamedThreadLocal<>("AreaType"); + private List list; + + public AreaType() { + list = AreaUtils.getAreaAllList(); + } /** * 获取对象值(导入) */ - public static Object getValue(String val) { - List cacheList = cache.get(); - if (cacheList == null){ - cacheList = AreaUtils.getAreaAllList(); - cache.set(cacheList); - } - for (Area e : cacheList){ + public Object getValue(String val) { + for (Area e : list){ if (StringUtils.trimToEmpty(val).equals(e.getAreaName())){ return e; } @@ -41,18 +38,11 @@ public class AreaType { /** * 获取对象值(导出) */ - public static String setValue(Object val) { + public String setValue(Object val) { if (val != null && ((Area)val).getAreaName() != null){ return ((Area)val).getAreaName(); } - return ""; - } - - /** - * 清理缓存 - */ - public static void clearCache(){ - cache.remove(); + return StringUtils.EMPTY; } } diff --git a/modules/core/src/main/java/com/jeesite/common/utils/excel/fieldtype/CompanyType.java b/modules/core/src/main/java/com/jeesite/common/utils/excel/fieldtype/CompanyType.java index 157cbc3b..bc3d8659 100644 --- a/modules/core/src/main/java/com/jeesite/common/utils/excel/fieldtype/CompanyType.java +++ b/modules/core/src/main/java/com/jeesite/common/utils/excel/fieldtype/CompanyType.java @@ -5,8 +5,6 @@ package com.jeesite.common.utils.excel.fieldtype; import java.util.List; -import org.springframework.core.NamedThreadLocal; - import com.jeesite.common.lang.StringUtils; import com.jeesite.modules.sys.entity.Company; import com.jeesite.modules.sys.utils.EmpUtils; @@ -14,23 +12,22 @@ import com.jeesite.modules.sys.utils.EmpUtils; /** * 字段类型转换 * @author ThinkGem - * @version 2018-08-11 + * @version 2020-3-5 * @example fieldType = CompanyType.class */ -public class CompanyType { +public class CompanyType implements FieldType { - private static ThreadLocal> cache = new NamedThreadLocal<>("CompanyType"); + private List list; + + public CompanyType() { + list = EmpUtils.getCompanyAllList(); + } /** * 获取对象值(导入) */ - public static Object getValue(String val) { - List cacheList = cache.get(); - if (cacheList == null){ - cacheList = EmpUtils.getCompanyAllList(); - cache.set(cacheList); - } - for (Company e : cacheList){ + public Object getValue(String val) { + for (Company e : list){ if (StringUtils.trimToEmpty(val).equals(e.getCompanyName())){ return e; } @@ -41,17 +38,11 @@ public class CompanyType { /** * 设置对象值(导出) */ - public static String setValue(Object val) { + public String setValue(Object val) { if (val != null && ((Company)val).getCompanyName() != null){ return ((Company)val).getCompanyName(); } - return ""; + return StringUtils.EMPTY; } - /** - * 清理缓存 - */ - public static void clearCache(){ - cache.remove(); - } } diff --git a/modules/core/src/main/java/com/jeesite/common/utils/excel/fieldtype/OfficeType.java b/modules/core/src/main/java/com/jeesite/common/utils/excel/fieldtype/OfficeType.java index b7fc3413..e2bbf373 100644 --- a/modules/core/src/main/java/com/jeesite/common/utils/excel/fieldtype/OfficeType.java +++ b/modules/core/src/main/java/com/jeesite/common/utils/excel/fieldtype/OfficeType.java @@ -5,8 +5,6 @@ package com.jeesite.common.utils.excel.fieldtype; import java.util.List; -import org.springframework.core.NamedThreadLocal; - import com.jeesite.common.lang.StringUtils; import com.jeesite.modules.sys.entity.Office; import com.jeesite.modules.sys.utils.EmpUtils; @@ -14,23 +12,22 @@ import com.jeesite.modules.sys.utils.EmpUtils; /** * 字段类型转换 * @author ThinkGem - * @version 2018-08-11 + * @version 2020-3-5 * @example fieldType = OfficeType.class */ -public class OfficeType { +public class OfficeType implements FieldType { - private static ThreadLocal> cache = new NamedThreadLocal<>("OfficeType"); + private List list; + + public OfficeType() { + list = EmpUtils.getOfficeAllList(); + } /** * 获取对象值(导入) */ - public static Object getValue(String val) { - List cacheList = cache.get(); - if (cacheList == null){ - cacheList = EmpUtils.getOfficeAllList(); - cache.set(cacheList); - } - for (Office e : cacheList){ + public Object getValue(String val) { + for (Office e : list){ if (StringUtils.trimToEmpty(val).equals(e.getOfficeName())){ return e; } @@ -41,17 +38,11 @@ public class OfficeType { /** * 设置对象值(导出) */ - public static String setValue(Object val) { + public String setValue(Object val) { if (val != null && ((Office)val).getOfficeName() != null){ return ((Office)val).getOfficeName(); } - return ""; + return StringUtils.EMPTY; } - /** - * 清理缓存 - */ - public static void clearCache(){ - cache.remove(); - } } diff --git a/modules/core/src/main/java/com/jeesite/common/utils/excel/fieldtype/PostListType.java b/modules/core/src/main/java/com/jeesite/common/utils/excel/fieldtype/PostListType.java index e08e7c72..9c169baa 100644 --- a/modules/core/src/main/java/com/jeesite/common/utils/excel/fieldtype/PostListType.java +++ b/modules/core/src/main/java/com/jeesite/common/utils/excel/fieldtype/PostListType.java @@ -5,8 +5,6 @@ package com.jeesite.common.utils.excel.fieldtype; import java.util.List; -import org.springframework.core.NamedThreadLocal; - import com.jeesite.common.collect.ListUtils; import com.jeesite.common.lang.StringUtils; import com.jeesite.common.utils.SpringUtils; @@ -16,26 +14,24 @@ import com.jeesite.modules.sys.service.PostService; /** * 字段类型转换 * @author ThinkGem - * @version 2018-08-11 + * @version 2020-3-5 * @example fieldType = PostListType.class */ -public class PostListType { - - private static PostService postService = SpringUtils.getBean(PostService.class); - private static ThreadLocal> cache = new NamedThreadLocal<>("PostListType"); +public class PostListType implements FieldType { + private List postList; + + public PostListType() { + PostService postService = SpringUtils.getBean(PostService.class); + postList = postService.findList(new Post()); + } + /** * 获取对象值(导入) */ - public static Object getValue(String val) { - List postList = ListUtils.newArrayList(); - List cacheList = cache.get(); - if (cacheList == null){ - cacheList = postService.findList(new Post()); - cache.set(cacheList); - } + public Object getValue(String val) { for (String s : StringUtils.split(val, ",")) { - for (Post e : cacheList) { + for (Post e : postList) { if (StringUtils.trimToEmpty(s).equals(e.getPostName())) { postList.add(e); } @@ -47,7 +43,7 @@ public class PostListType { /** * 设置对象值(导出) */ - public static String setValue(Object val) { + public String setValue(Object val) { if (val != null) { @SuppressWarnings("unchecked") List postList = (List) val; @@ -56,10 +52,4 @@ public class PostListType { return ""; } - /** - * 清理缓存 - */ - public static void clearCache(){ - cache.remove(); - } } diff --git a/modules/core/src/main/java/com/jeesite/modules/sys/entity/EmpUser.java b/modules/core/src/main/java/com/jeesite/modules/sys/entity/EmpUser.java index bad3c215..beda0091 100644 --- a/modules/core/src/main/java/com/jeesite/modules/sys/entity/EmpUser.java +++ b/modules/core/src/main/java/com/jeesite/modules/sys/entity/EmpUser.java @@ -32,10 +32,10 @@ import com.jeesite.common.utils.excel.fieldtype.OfficeType; attrName="employee", columns={ @Column(includeEntity=BaseEntity.class), @Column(includeEntity=DataEntity.class), - @Column(name="emp_code", attrName="empCode", label="员工编码", isPK=true), - @Column(name="emp_no", attrName="empNo", label="员工工号"), - @Column(name="emp_name", attrName="empName", label="员工姓名", queryType=QueryType.LIKE), - @Column(name="emp_name_en", attrName="empNameEn", label="英文名", queryType=QueryType.LIKE), + @Column(name="emp_code", attrName="empCode", label="员工编码", isPK=true), + @Column(name="emp_no", attrName="empNo", label="员工工号"), + @Column(name="emp_name", attrName="empName", label="员工姓名", queryType=QueryType.LIKE), + @Column(name="emp_name_en", attrName="empNameEn", label="英文名", queryType=QueryType.LIKE), }), @JoinTable(type=Type.LEFT_JOIN, entity=Office.class, alias="o", on="o.office_code=e.office_code", attrName="employee.office", @@ -66,9 +66,9 @@ import com.jeesite.common.utils.excel.fieldtype.OfficeType; @JoinTable(type=Type.LEFT_JOIN, entity=Area.class, alias="ar", on="ar.area_code = c.area_code", attrName="employee.company.area", columns={ - @Column(name="area_code", label="区域代码", isPK=true), - @Column(name="area_name", label="区域名称", isQuery=false), - @Column(name="area_type", label="区域类型"), + @Column(name="area_code", label="区域代码", isPK=true), + @Column(name="area_name", label="区域名称", isQuery=false), + @Column(name="area_type", label="区域类型"), }), }, extWhereKeys="dsfOffice, dsfCompany", @@ -97,11 +97,12 @@ public class EmpUser extends User { @ExcelField(title="电子邮箱", attrName="email", align=Align.LEFT, sort=50), @ExcelField(title="手机号码", attrName="mobile", align=Align.CENTER, sort=60), @ExcelField(title="办公电话", attrName="phone", align=Align.CENTER, sort=70), - @ExcelField(title="性别", attrName="sex", dictType="sys_user_sex", width=10*256, align=Align.CENTER, sort=75), + @ExcelField(title="性别", attrName="sex", dictType="sys_user_sex", words=10, align=Align.CENTER, sort=75), @ExcelField(title="员工编码", attrName="employee.empCode", align=Align.CENTER, sort=80), @ExcelField(title="员工姓名", attrName="employee.empName", align=Align.CENTER, sort=95), @ExcelField(title="拥有角色编号", attrName="userRoleString", align=Align.LEFT, sort=800, type=ExcelField.Type.IMPORT), - @ExcelField(title="最后登录日期", attrName="lastLoginDate", width=20*256, align=Align.CENTER, sort=900, type=ExcelField.Type.EXPORT, dataFormat="yyyy-MM-dd HH:mm"), + @ExcelField(title="建档日期", attrName="createDate", words=15, align=Align.CENTER, sort=900, type=ExcelField.Type.EXPORT, dataFormat="yyyy-MM-dd"), + @ExcelField(title="最后登录", attrName="lastLoginDate", words=20, align=Align.CENTER, sort=900, type=ExcelField.Type.EXPORT), }) public Employee getEmployee(){ Employee employee = (Employee)super.getRefObj();