From dddb0b322e5e325c3cf141288f3e03d804fd1c9f Mon Sep 17 00:00:00 2001 From: Parker Date: Thu, 24 Sep 2020 13:40:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96Excel=E5=AF=BC=E5=87=BA?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../opsli/api/base/warpper/ApiWrapper.java | 16 +-- .../org/opsli/api/wrapper/test/TestModel.java | 12 +- .../java/org/opsli/core/utils/ExcelUtil.java | 115 ++++++++++++------ .../plugins/excel/annotation/ExcelInfo.java | 30 +++++ 4 files changed, 121 insertions(+), 52 deletions(-) create mode 100644 opsli-plugins/opsli-plugins-excel/src/main/java/org/opsli/plugins/excel/annotation/ExcelInfo.java diff --git a/opsli-api/src/main/java/org/opsli/api/base/warpper/ApiWrapper.java b/opsli-api/src/main/java/org/opsli/api/base/warpper/ApiWrapper.java index 06fe93d8..5918f7ba 100644 --- a/opsli-api/src/main/java/org/opsli/api/base/warpper/ApiWrapper.java +++ b/opsli-api/src/main/java/org/opsli/api/base/warpper/ApiWrapper.java @@ -11,7 +11,7 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.experimental.Accessors; import org.apache.poi.ss.usermodel.FillPatternType; -import org.opsli.plugins.excel.annotation.CellStyleFormat; +import org.opsli.plugins.excel.annotation.ExcelInfo; import org.springframework.format.annotation.DateTimeFormat; import java.io.Serializable; @@ -46,24 +46,24 @@ public abstract class ApiWrapper implements Serializable { /** ID */ @TableId + @ApiModelProperty(value = "ID") @ExcelIgnore - //@ApiModelProperty(value = "ID") //@ExcelProperty(value = "ID", order = 1000) - @CellStyleFormat + //@ExcelInfo private String id; /** 创建人 */ + @ApiModelProperty(value = "创建人") @ExcelIgnore - //@ApiModelProperty(value = "创建人") //@ExcelProperty(value = "创建人", order = 1001) - @CellStyleFormat + //@ExcelInfo private String createBy; /** 创建时间 */ @ApiModelProperty(value = "创建时间") @ExcelIgnore //@ExcelProperty(value = "创建时间", order = 1002) - //@CellStyleFormat + //@ExcelInfo @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date createTime; @@ -72,14 +72,14 @@ public abstract class ApiWrapper implements Serializable { @ApiModelProperty(value = "修改人") @ExcelIgnore //@ExcelProperty(value = "修改人", order = 1003) - //@CellStyleFormat + //@ExcelInfo private String updateBy; /** 更新时间 */ @ApiModelProperty(value = "修改时间") @ExcelIgnore //@ExcelProperty(value = "修改时间", order = 1004) - //@CellStyleFormat + //@ExcelInfo @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date updateTime; diff --git a/opsli-api/src/main/java/org/opsli/api/wrapper/test/TestModel.java b/opsli-api/src/main/java/org/opsli/api/wrapper/test/TestModel.java index 2e845472..fd0abd10 100644 --- a/opsli-api/src/main/java/org/opsli/api/wrapper/test/TestModel.java +++ b/opsli-api/src/main/java/org/opsli/api/wrapper/test/TestModel.java @@ -1,14 +1,11 @@ package org.opsli.api.wrapper.test; import com.alibaba.excel.annotation.ExcelProperty; -import com.alibaba.excel.annotation.write.style.*; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; -import org.apache.poi.ss.usermodel.FillPatternType; import org.opsli.api.base.warpper.ApiWrapper; -import org.opsli.common.annotation.DictType; -import org.opsli.plugins.excel.annotation.CellStyleFormat; +import org.opsli.plugins.excel.annotation.ExcelInfo; /** * @BelongsProject: opsli-boot @@ -26,18 +23,17 @@ public class TestModel extends ApiWrapper { @ApiModelProperty(value = "名称") @ExcelProperty(value = "名称", order = 1) - @CellStyleFormat + @ExcelInfo private String name; @ApiModelProperty(value = "分类") - @DictType("testType") @ExcelProperty(value = "分类", order = 2) - @CellStyleFormat + @ExcelInfo(dictType = "testType") private String type; @ApiModelProperty(value = "备注") @ExcelProperty(value = "备注", order = 3) - @CellStyleFormat + @ExcelInfo private String remark; } diff --git a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/utils/ExcelUtil.java b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/utils/ExcelUtil.java index 3a6e8346..71308a4f 100644 --- a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/utils/ExcelUtil.java +++ b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/utils/ExcelUtil.java @@ -6,9 +6,9 @@ import com.google.common.collect.Maps; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.opsli.api.wrapper.system.dict.DictModel; -import org.opsli.common.annotation.DictType; import org.opsli.common.enums.ExcelOperate; import org.opsli.plugins.excel.ExcelPlugin; +import org.opsli.plugins.excel.annotation.ExcelInfo; import org.opsli.plugins.excel.exception.ExcelPluginException; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; @@ -33,27 +33,27 @@ public class ExcelUtil extends ExcelPlugin { public List readExcel(MultipartFile excel, Class rowModel) throws ExcelPluginException { List ts = super.readExcel(excel, rowModel); // 处理数据 - return this.handleDict(ts, rowModel, ExcelOperate.READ); + return this.handleDatas(ts, rowModel, ExcelOperate.READ); } @Override public List readExcel(MultipartFile excel, Class rowModel, String sheetName) throws ExcelPluginException { List ts = super.readExcel(excel, rowModel, sheetName); // 处理数据 - return this.handleDict(ts, rowModel, ExcelOperate.READ); + return this.handleDatas(ts, rowModel, ExcelOperate.READ); } @Override public List readExcel(MultipartFile excel, Class rowModel, String sheetName, int headLineNum) throws ExcelPluginException { List ts = super.readExcel(excel, rowModel, sheetName, headLineNum); // 处理数据 - return this.handleDict(ts, rowModel, ExcelOperate.READ); + return this.handleDatas(ts, rowModel, ExcelOperate.READ); } @Override public void writeExcel(HttpServletResponse response, List list, String fileName, String sheetName, Class classType, ExcelTypeEnum excelTypeEnum) throws ExcelPluginException { // 处理数据 - List ts = this.handleDict(list, classType, ExcelOperate.WRITE); + List ts = this.handleDatas(list, classType, ExcelOperate.WRITE); super.writeExcel(response, ts, fileName, sheetName, classType, excelTypeEnum); } @@ -65,7 +65,7 @@ public class ExcelUtil extends ExcelPlugin { * @param * @return */ - private List handleDict(List datas, Class typeClazz, ExcelOperate operate){ + private List handleDatas(List datas, Class typeClazz, ExcelOperate operate){ // 空处理 if(datas == null || datas.size() == 0){ @@ -75,22 +75,82 @@ public class ExcelUtil extends ExcelPlugin { // 字段名 - 字典code Map fieldAndTypeCode = Maps.newHashMap(); // 字典code - 字典值 - Map> typeCodeAndValue = Maps.newHashMap(); + Map> typeCodeAndValue = null; Field[] fields = ReflectUtil.getFields(typeClazz); for (Field field : fields) { - DictType dictType = field.getAnnotation(DictType.class); - if(dictType != null){ - String typeCode = dictType.value(); - fieldAndTypeCode.put(field.getName(), typeCode); + ExcelInfo excelInfo = field.getAnnotation(ExcelInfo.class); + if(excelInfo != null){ + // 字典 + String dictType = excelInfo.dictType(); + if(StringUtils.isNotEmpty(dictType)){ + fieldAndTypeCode.put(field.getName(), dictType); + } } } - // 如果没有字典 则直接返回 - if(fieldAndTypeCode.size() == 0){ - return datas; + // 如果有字典 + if(fieldAndTypeCode.size() != 0){ + typeCodeAndValue = this.getDictMap(fieldAndTypeCode); } + // 数据字典赋值 + for (T data : datas) { + // 处理字典 + this.handleDict(data, operate, fieldAndTypeCode, typeCodeAndValue); + } + + + return datas; + } + + // ========================= 处理字典 ========================= + + /** + * 处理字典 + * @param data 数据 + * @param operate excel操作类型 + * @param fieldAndTypeCode 字段名 - 字典code + * @param typeCodeAndValue 字典code - 字典值 + * @param + * @return + */ + private void handleDict(T data, ExcelOperate operate, Map fieldAndTypeCode, + Map> typeCodeAndValue + ){ + // 如果没有字典 则直接退出 + if(fieldAndTypeCode.size() == 0 || typeCodeAndValue == null || typeCodeAndValue.size() == 0){ + return; + } + + // 数据字典赋值 + for (Map.Entry entry : fieldAndTypeCode.entrySet()) { + try { + String fieldName = entry.getKey(); + String typeCode = entry.getValue(); + String fieldValue = (String) ReflectUtil.getFieldValue(data, fieldName); + List dictModels = typeCodeAndValue.get(typeCode); + // 匹配字典 + String dictVal = this.matchingDict(dictModels, fieldValue, operate); + if(StringUtils.isEmpty(dictVal)){ + continue; + } + // 赋值 + ReflectUtil.setFieldValue(data, fieldName, dictVal); + }catch (Exception e){ + log.error(e.getMessage(), e); + } + } + } + + /** + * 获得字典缓存Map + * @param fieldAndTypeCode + * @return + */ + public Map> getDictMap(Map fieldAndTypeCode){ + // 字典code - 字典值 + Map> typeCodeAndValue = Maps.newHashMap(); // 取Redis 值 for (Map.Entry entry : fieldAndTypeCode.entrySet()) { String typeCode = entry.getValue(); @@ -99,28 +159,7 @@ public class ExcelUtil extends ExcelPlugin { if(dictList == null || dictList.size() == 0) continue; typeCodeAndValue.put(typeCode, dictList); } - - // 数据字典赋值 - for (T data : datas) { - for (Map.Entry entry : fieldAndTypeCode.entrySet()) { - try { - String fieldName = entry.getKey(); - String typeCode = entry.getValue(); - String fieldValue = (String) ReflectUtil.getFieldValue(data, fieldName); - List dictModels = typeCodeAndValue.get(typeCode); - // 匹配字典 - String dictVal = this.matchingDict(dictModels, fieldValue, operate); - if(StringUtils.isEmpty(dictVal)){ - continue; - } - // 赋值 - ReflectUtil.setFieldValue(data, fieldName, dictVal); - }catch (Exception e){ - log.error(e.getMessage(), e); - } - } - } - return datas; + return typeCodeAndValue; } /** @@ -152,4 +191,8 @@ public class ExcelUtil extends ExcelPlugin { } return val; } + + // ========================= 反射字段 ========================= + + } diff --git a/opsli-plugins/opsli-plugins-excel/src/main/java/org/opsli/plugins/excel/annotation/ExcelInfo.java b/opsli-plugins/opsli-plugins-excel/src/main/java/org/opsli/plugins/excel/annotation/ExcelInfo.java new file mode 100644 index 00000000..4058a11b --- /dev/null +++ b/opsli-plugins/opsli-plugins-excel/src/main/java/org/opsli/plugins/excel/annotation/ExcelInfo.java @@ -0,0 +1,30 @@ +package org.opsli.plugins.excel.annotation; + + + +import java.lang.annotation.*; + +/** + * @BelongsProject: opsli-boot + * @BelongsPackage: org.opsli.common.annotation + * @Author: Parker + * @CreateTime: 2020-09-16 16:36 + * @Description: 字典标示 + + * + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +@Documented +public @interface ExcelInfo { + + /** 字典类型 code */ + String dictType() default ""; + /** 字段反射类型 */ + //Class reflectFieldType() default Class.class; + /** 字段反射名称 */ + //String reflectFieldName() default ""; + /** 字段样式 */ + CellStyleFormat cellStyleFormat() default @CellStyleFormat(); + +}