ergehenmeng commented on issue #915:
URL: https://github.com/apache/fesod/issues/915#issuecomment-4531352423

   谢谢回复, 
   
   目前我实现枚举类型的转换是通过 注解+转换器实现的 例子如下:
   
   ```java
   
   @Documented
   @Target(ElementType.FIELD)
   @Retention(RetentionPolicy.RUNTIME)
   public @interface ExcelDesc {
   }
   
   @Slf4j
   public class EnumExcelConverter implements Converter<Object> {
   
       private static final Map<Class<?>, Field> FIELD_MAP = new 
ConcurrentHashMap<>(32);
   
       @Override
       public Class<?> supportJavaTypeKey() {
           return Enum.class;
       }
   
       @Override
       public WriteCellData<?> convertToExcelData(Object value, 
ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
           Field valueAs = this.getAnnotationField(contentProperty);
           return new WriteCellData<>(ReflectUtil.getFieldValue(value, 
valueAs).toString());
       }
   
       /**
        * 获取带有@ExcelDesc注解的属性
        *
        * @param contentProperty 原导出excel的字段
        * @return Field
        */
       private Field getAnnotationField(ExcelContentProperty contentProperty) {
           Class<?> fieldType = contentProperty.getField().getType();
           return FIELD_MAP.computeIfAbsent(fieldType, aClass -> {
               for (Field field : 
contentProperty.getField().getType().getDeclaredFields()) {
                   ExcelDesc excelDesc = field.getAnnotation(ExcelDesc.class);
                   if (excelDesc != null) {
                       return field;
                   }
               }
               log.error("枚举类请使用@ExcelDesc标注要导出为Excel的字段 [{}]", fieldType);
               throw new BusinessException(ErrorCode.ENUM_SUPPORTED);
           });
       }
   }
   
   @Getter
   @AllArgsConstructor
   public enum Gender {
   
       /**
        * 未知
        */
       NONE(0, "未知"),
   
       /**
        * 男
        */
       MALE(1, "男"),
   
       /**
        * 女
        */
       FEMALE(2, "女");
   
       @JsonValue
       @EnumValue
       private final int value;
   
       private final String name;
   
       @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
       public static Gender of(@JsonProperty("value") Integer value) {
           if (value == null) {
               return null;
           }
           return Arrays.stream(Gender.values()).filter(auditState -> 
auditState.value == value)
                   .findFirst().orElseThrow(() -> new 
BusinessException(ErrorCode.USER_TYPE_NULL));
       }
   
   }
   
   // 使用时 是这样的
   @Schema(description = "性别 0:未知 1:男 2:女 ")
   @ExcelProperty(value = "性别", index = 6, converter = EnumExcelConverter.class)
   private Gender sex;
   ```
   数据字典翻译类型的例子如下:
   
   ```java
   @Slf4j
   public class DictConverter implements Converter<Integer> {
   
       @Override
       public Class<?> supportJavaTypeKey() {
           return Integer.class;
       }
   
       @Override
       public WriteCellData<?> convertToExcelData(Integer value, 
ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
           ExcelDict dict = 
contentProperty.getField().getAnnotation(ExcelDict.class);
           if (dict == null) {
               return NumberUtils.formatToCellDataString(value, 
contentProperty);
           }
           SysDictService service = SpringUtil.getBean(SysDictService.class);
           String dictValue = service.getDictValue(dict.value(), value);
           if (dictValue == null) {
               log.warn("导出Excel解析数据字典为空 [{}] [{}]", dict.value(), value);
               return NumberUtils.formatToCellDataString(value, 
contentProperty);
           }
           return new WriteCellData<>(dictValue);
       }
   }
   
   @Documented
   @Target(ElementType.FIELD)
   @Retention(RetentionPolicy.RUNTIME)
   public @interface ExcelDict {
   
       /**
        * 数据字典的key
        *
        * @return key
        */
       String value();
   }
   
   // 使用时
   
       @Schema(description = "反馈类型 数据字典: dict_feed_type")
       @ExcelDict(value = "dict_feed_type")
       @ExcelProperty(value = "反馈类型", index = 3, converter = DictConverter 
.class)
       private Integer feedType;
   ```
   
   上述两种方式太过于繁琐, 且不够优雅,  我理想的方式是通过 实现自定义注解+自定义转换器, 把上面枚举+数据字典都能兼容即用 
DictExcelProperty 注解代替 ExcelProperty注解
   
   ```java
   @Target(ElementType.FIELD)
   @Retention(RetentionPolicy.RUNTIME)
   @Documented
   @ExcelProperty(converter = DictConverter.class)
   public @interface DictExcelProperty {
       
       /**
        * 导出时在Excel中显示的列名
        */
       String value() default "";
       
       /**
        * 字典类型,对应sys_dict_data表的dict_type字段
        * 例如:sys_user_sex, sys_job_status等
        */
       String dictType();
       
       /**
        * 本地数据字典转换 1=是,0=否
        */
       String translate() default "";
       
      @AliasFor(annotation = ExcelProperty.class, index= "value")
       int index() default -1;
   
      @AliasFor(annotation = ExcelProperty.class, index= "order")
       int order() default Integer.MAX_VALUE;
   }
   
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to