From b4645daf2a9943afb4603e30eef1897cdca157c5 Mon Sep 17 00:00:00 2001 From: zlyx <1242874891@qq.com> Date: Mon, 6 Mar 2023 13:07:05 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E6=96=B0=E5=A2=9E=20=E8=BF=81=E7=A7=BB?= =?UTF-8?q?=204.X=20EasyExcel=E6=9E=9A=E4=B8=BE=E7=B1=BB=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=BF=BB=E8=AF=91=E6=B3=A8=E8=A7=A3=E5=8A=9F=E8=83=BD=20;=20fi?= =?UTF-8?q?x=20=E4=BF=AE=E6=AD=A3=20!pr292=20=E7=9B=B8=E5=85=B3=E9=97=AE?= =?UTF-8?q?=E9=A2=98,=20=E5=9B=9E=E9=80=80=E7=89=B9=E6=AE=8A=E7=B1=BB?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E5=86=99=E6=B3=95=20;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../excel/annotation/ExcelEnumFormat.java | 30 ++++++++ .../excel/convert/ExcelEnumConvert.java | 75 +++++++++++++++++++ .../system/SysProfileController.java | 4 +- .../listener/SysUserImportListener.java | 6 +- 4 files changed, 110 insertions(+), 5 deletions(-) create mode 100644 ruoyi-common/ruoyi-common-excel/src/main/java/com/ruoyi/common/excel/annotation/ExcelEnumFormat.java create mode 100644 ruoyi-common/ruoyi-common-excel/src/main/java/com/ruoyi/common/excel/convert/ExcelEnumConvert.java diff --git a/ruoyi-common/ruoyi-common-excel/src/main/java/com/ruoyi/common/excel/annotation/ExcelEnumFormat.java b/ruoyi-common/ruoyi-common-excel/src/main/java/com/ruoyi/common/excel/annotation/ExcelEnumFormat.java new file mode 100644 index 00000000..d5fda7c4 --- /dev/null +++ b/ruoyi-common/ruoyi-common-excel/src/main/java/com/ruoyi/common/excel/annotation/ExcelEnumFormat.java @@ -0,0 +1,30 @@ +package com.ruoyi.common.excel.annotation; + +import java.lang.annotation.*; + +/** + * 枚举格式化 + * + * @author Liang + */ +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +@Inherited +public @interface ExcelEnumFormat { + + /** + * 字典枚举类型 + */ + Class> enumClass(); + + /** + * 字典枚举类中对应的code属性名称,默认为code + */ + String codeField() default "code"; + + /** + * 字典枚举类中对应的text属性名称,默认为text + */ + String textField() default "text"; + +} diff --git a/ruoyi-common/ruoyi-common-excel/src/main/java/com/ruoyi/common/excel/convert/ExcelEnumConvert.java b/ruoyi-common/ruoyi-common-excel/src/main/java/com/ruoyi/common/excel/convert/ExcelEnumConvert.java new file mode 100644 index 00000000..804f9f0d --- /dev/null +++ b/ruoyi-common/ruoyi-common-excel/src/main/java/com/ruoyi/common/excel/convert/ExcelEnumConvert.java @@ -0,0 +1,75 @@ +package com.ruoyi.common.excel.convert; + +import cn.hutool.core.annotation.AnnotationUtil; +import cn.hutool.core.convert.Convert; +import cn.hutool.core.util.ObjectUtil; +import com.alibaba.excel.converters.Converter; +import com.alibaba.excel.enums.CellDataTypeEnum; +import com.alibaba.excel.metadata.GlobalConfiguration; +import com.alibaba.excel.metadata.data.ReadCellData; +import com.alibaba.excel.metadata.data.WriteCellData; +import com.alibaba.excel.metadata.property.ExcelContentProperty; +import com.ruoyi.common.core.utils.reflect.ReflectUtils; +import com.ruoyi.common.excel.annotation.ExcelEnumFormat; +import lombok.extern.slf4j.Slf4j; + +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; + +/** + * 枚举格式化转换处理 + * + * @author Liang + */ +@Slf4j +public class ExcelEnumConvert implements Converter { + + @Override + public Class supportJavaTypeKey() { + return Object.class; + } + + @Override + public CellDataTypeEnum supportExcelTypeKey() { + return null; + } + + @Override + public Object convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { + Object codeValue = cellData.getData(); + // 如果是空值 + if (ObjectUtil.isNull(codeValue)) { + return null; + } + Map enumValueMap = beforeConvert(contentProperty); + String textValue = enumValueMap.get(codeValue); + return Convert.convert(contentProperty.getField().getType(), textValue); + } + + @Override + public WriteCellData convertToExcelData(Object object, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { + if (ObjectUtil.isNull(object)) { + return new WriteCellData<>(""); + } + Map enumValueMap = beforeConvert(contentProperty); + String value = Convert.toStr(enumValueMap.get(object), ""); + return new WriteCellData<>(value); + } + + private Map beforeConvert(ExcelContentProperty contentProperty) { + ExcelEnumFormat anno = getAnnotation(contentProperty.getField()); + Map enumValueMap = new HashMap<>(); + Enum[] enumConstants = anno.enumClass().getEnumConstants(); + for (Enum enumConstant : enumConstants) { + Object codeValue = ReflectUtils.invokeGetter(enumConstant, anno.codeField()); + String textValue = ReflectUtils.invokeGetter(enumConstant, anno.textField()); + enumValueMap.put(codeValue, textValue); + } + return enumValueMap; + } + + private ExcelEnumFormat getAnnotation(Field field) { + return AnnotationUtil.getAnnotation(field, ExcelEnumFormat.class); + } +} diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/system/SysProfileController.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/system/SysProfileController.java index 3f34a237..521647da 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/system/SysProfileController.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/system/SysProfileController.java @@ -1,10 +1,10 @@ package com.ruoyi.system.controller.system; import cn.dev33.satoken.secure.BCrypt; +import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.io.FileUtil; import com.ruoyi.common.core.constant.UserConstants; import com.ruoyi.common.core.domain.R; -import com.ruoyi.common.core.utils.MapstructUtils; import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.common.core.utils.file.MimeTypeUtils; import com.ruoyi.common.log.annotation.Log; @@ -60,7 +60,7 @@ public class SysProfileController extends BaseController { @Log(title = "个人信息", businessType = BusinessType.UPDATE) @PutMapping public R updateProfile(@RequestBody SysUserProfileBo profile) { - SysUserBo user = MapstructUtils.convert(profile, SysUserBo.class); + SysUserBo user = BeanUtil.toBean(profile, SysUserBo.class); if (StringUtils.isNotEmpty(user.getPhonenumber()) && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) { return R.fail("修改用户'" + user.getUserName() + "'失败,手机号码已存在"); diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/listener/SysUserImportListener.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/listener/SysUserImportListener.java index ba56880d..b201e3d1 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/listener/SysUserImportListener.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/listener/SysUserImportListener.java @@ -1,11 +1,11 @@ package com.ruoyi.system.listener; +import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.crypto.digest.BCrypt; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.ruoyi.common.core.exception.ServiceException; -import com.ruoyi.common.core.utils.MapstructUtils; import com.ruoyi.common.core.utils.SpringUtils; import com.ruoyi.common.core.utils.ValidatorUtils; import com.ruoyi.common.excel.core.ExcelListener; @@ -55,7 +55,7 @@ public class SysUserImportListener extends AnalysisEventListener").append(successNum).append("、账号 ").append(user.getUserName()).append(" 导入成功"); } else if (isUpdateSupport) { Long userId = sysUser.getUserId(); - SysUserBo user = MapstructUtils.convert(userVo, SysUserBo.class); + SysUserBo user = BeanUtil.toBean(userVo, SysUserBo.class); user.setUserId(userId); ValidatorUtils.validate(user); userService.checkUserAllowed(user);