diff --git a/db/mysql/maku.sql b/db/mysql/maku.sql index ce4aeb6..ffa7b55 100644 --- a/db/mysql/maku.sql +++ b/db/mysql/maku.sql @@ -290,6 +290,8 @@ INSERT INTO sys_dict_type (id, dict_type, dict_name, remark, sort, version, dele INSERT INTO sys_dict_type (id, dict_type, dict_name, remark, sort, version, deleted, creator, create_time, updater, update_time) VALUES (6, 'success_fail', '状态', '操作状态:成功 | 失败', 0, 0, 0, 10000, now(), 10000, now()); INSERT INTO sys_dict_type (id, dict_type, dict_name, remark, sort, version, deleted, creator, create_time, updater, update_time) VALUES (7, 'login_operation', '操作信息', '登录管理', 0, 0, 0, 10000, now(), 10000, now()); INSERT INTO sys_dict_type (id, dict_type, dict_name, remark, sort, version, deleted, creator, create_time, updater, update_time) VALUES (8, 'params_type', '系统参数', '参数管理', 0, 0, 0, 10000, now(), 10000, now()); +INSERT INTO sys_dict_type (id, dict_type, dict_name, remark, sort, version, deleted, creator, create_time, updater, update_time) VALUES (9, 'user_super_admin', '用户是否是超管','用户是否是超管', 0, 0, 0, 10000, now(), 10000, now()); + INSERT INTO sys_dict_data (id, dict_type_id, dict_label, dict_value, remark, sort, version, deleted, creator, create_time, updater, update_time) VALUES (1, 1, '停用', '0', '', 1, 0, 0, 10000, now(), 10000, now()); INSERT INTO sys_dict_data (id, dict_type_id, dict_label, dict_value, remark, sort, version, deleted, creator, create_time, updater, update_time) VALUES (2, 1, '正常', '1', '', 0, 0, 0, 10000, now(), 10000, now()); @@ -313,5 +315,8 @@ INSERT INTO sys_dict_data (id, dict_type_id, dict_label, dict_value, remark, sor INSERT INTO sys_dict_data (id, dict_type_id, dict_label, dict_value, remark, sort, version, deleted, creator, create_time, updater, update_time) VALUES (20, 7, '账号密码错误', '3', '', 3, 0, 0, 10000, now(), 10000, now()); INSERT INTO sys_dict_data (id, dict_type_id, dict_label, dict_value, remark, sort, version, deleted, creator, create_time, updater, update_time) VALUES (21, 8, '否', '0', '', 1, 0, 0, 10000,now(), 10000, now()); INSERT INTO sys_dict_data (id, dict_type_id, dict_label, dict_value, remark, sort, version, deleted, creator, create_time, updater, update_time) VALUES (22, 8, '是', '1', '', 0, 0, 0, 10000, now(), 10000, now()); +INSERT INTO sys_dict_data (id, dict_type_id, dict_label, dict_value, remark, sort, version, deleted, creator, create_time, updater, update_time) VALUES (23, 9, '是', '1', '', 1, 0, 0, 10000, now(), 10000, now()); +INSERT INTO sys_dict_data (id, dict_type_id, dict_label, dict_value, remark, sort, version, deleted, creator, create_time, updater, update_time) VALUES (24, 9, '否', '0', '', 0, 0, 0, 10000, now(), 10000, now()); + INSERT INTO sys_params (param_name, param_type, param_key, param_value, remark, version, deleted, creator, create_time, updater, update_time) VALUES ('用户登录-验证码开关', 1, 'LOGIN_CAPTCHA', 'false', '是否开启验证码(true:开启,false:关闭)', 0, 0, 10000, now(), 10000, now()); diff --git a/maku-boot-system/src/main/java/net/maku/system/controller/SysDictTypeController.java b/maku-boot-system/src/main/java/net/maku/system/controller/SysDictTypeController.java index 7b43253..7d62e23 100644 --- a/maku-boot-system/src/main/java/net/maku/system/controller/SysDictTypeController.java +++ b/maku-boot-system/src/main/java/net/maku/system/controller/SysDictTypeController.java @@ -93,4 +93,13 @@ public class SysDictTypeController { return Result.ok(dictList); } + @GetMapping("refreshTransCache") + @Operation(summary = "刷新字典翻译缓存数据") + @PreAuthorize("hasAuthority('sys:dict:refreshTransCache')") + public Result refreshTransCache() { + sysDictTypeService.refreshTransCache(); + return Result.ok(); + } + + } \ No newline at end of file diff --git a/maku-boot-system/src/main/java/net/maku/system/service/SysDictTypeService.java b/maku-boot-system/src/main/java/net/maku/system/service/SysDictTypeService.java index 50800ed..1b4cff3 100644 --- a/maku-boot-system/src/main/java/net/maku/system/service/SysDictTypeService.java +++ b/maku-boot-system/src/main/java/net/maku/system/service/SysDictTypeService.java @@ -34,4 +34,9 @@ public interface SysDictTypeService extends BaseService { */ List getDictList(); + /** + * 刷新字典缓存 + */ + void refreshTransCache(); + } \ No newline at end of file diff --git a/maku-boot-system/src/main/java/net/maku/system/service/impl/SysDictTypeServiceImpl.java b/maku-boot-system/src/main/java/net/maku/system/service/impl/SysDictTypeServiceImpl.java index 0057e69..92d2e4f 100644 --- a/maku-boot-system/src/main/java/net/maku/system/service/impl/SysDictTypeServiceImpl.java +++ b/maku-boot-system/src/main/java/net/maku/system/service/impl/SysDictTypeServiceImpl.java @@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.fhs.trans.service.impl.DictionaryTransService; import lombok.AllArgsConstructor; import net.maku.framework.common.exception.ServerException; import net.maku.framework.common.page.PageResult; @@ -20,11 +21,16 @@ import net.maku.system.query.SysDictTypeQuery; import net.maku.system.service.SysDictTypeService; import net.maku.system.vo.SysDictTypeVO; import net.maku.system.vo.SysDictVO; +import org.springframework.beans.factory.InitializingBean; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import javax.annotation.Resource; import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.stream.Collectors; /** * 字典类型 @@ -33,9 +39,11 @@ import java.util.List; */ @Service @AllArgsConstructor -public class SysDictTypeServiceImpl extends BaseServiceImpl implements SysDictTypeService { +public class SysDictTypeServiceImpl extends BaseServiceImpl implements SysDictTypeService, InitializingBean { private final SysDictDataDao sysDictDataDao; + private final DictionaryTransService dictionaryTransService; + @Override public PageResult page(SysDictTypeQuery query) { IPage page = baseMapper.selectPage(getPage(query), getWrapper(query)); @@ -121,4 +129,28 @@ public class SysDictTypeServiceImpl extends BaseServiceImpl { + //获取所有的字典项数据 + List dataList = sysDictDataDao.selectList(new LambdaQueryWrapper<>()); + //根据类型分组 + Map> dictTypeDataMap = dataList.stream().collect(Collectors + .groupingBy(SysDictDataEntity::getDictTypeId)); + List dictTypeEntities = super.list(); + List tempDataEntityList; + for (SysDictTypeEntity dictTypeEntity : dictTypeEntities) { + if (dictTypeDataMap.containsKey(dictTypeEntity.getId())) { + dictionaryTransService.refreshCache(dictTypeEntity.getDictType(), dictTypeDataMap.get(dictTypeEntity.getId()) + .stream().collect(Collectors.toMap(SysDictDataEntity::getDictValue, SysDictDataEntity::getDictLabel))); + } + } + return null; + }); + } } \ No newline at end of file diff --git a/maku-boot-system/src/main/java/net/maku/system/service/impl/SysLogLoginServiceImpl.java b/maku-boot-system/src/main/java/net/maku/system/service/impl/SysLogLoginServiceImpl.java index 4a73b91..49fb40c 100644 --- a/maku-boot-system/src/main/java/net/maku/system/service/impl/SysLogLoginServiceImpl.java +++ b/maku-boot-system/src/main/java/net/maku/system/service/impl/SysLogLoginServiceImpl.java @@ -4,6 +4,7 @@ import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.fhs.trans.service.impl.TransService; import lombok.AllArgsConstructor; import lombok.SneakyThrows; import net.maku.framework.common.page.PageResult; @@ -32,6 +33,8 @@ import java.util.List; @AllArgsConstructor public class SysLogLoginServiceImpl extends BaseServiceImpl implements SysLogLoginService { + private final TransService transService; + @Override public PageResult page(SysLogLoginQuery query) { IPage page = baseMapper.selectPage(getPage(query), getWrapper(query)); @@ -73,7 +76,7 @@ public class SysLogLoginServiceImpl extends BaseServiceImpl list = list(); List sysLogLoginVOS = SysLogLoginConvert.INSTANCE.convertList(list); - + transService.transBatch(sysLogLoginVOS); // 写到浏览器打开 ExcelUtils.excelExport(SysLogLoginVO.class, "system_login_log_excel" + DateUtils.format(new Date()), null, sysLogLoginVOS); } diff --git a/maku-boot-system/src/main/java/net/maku/system/service/impl/SysUserServiceImpl.java b/maku-boot-system/src/main/java/net/maku/system/service/impl/SysUserServiceImpl.java index 7f4942c..7d0b9c3 100644 --- a/maku-boot-system/src/main/java/net/maku/system/service/impl/SysUserServiceImpl.java +++ b/maku-boot-system/src/main/java/net/maku/system/service/impl/SysUserServiceImpl.java @@ -2,6 +2,7 @@ package net.maku.system.service.impl; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.fhs.trans.service.impl.TransService; import lombok.AllArgsConstructor; import lombok.SneakyThrows; import net.maku.framework.common.constant.Constant; @@ -41,6 +42,7 @@ import java.util.Map; public class SysUserServiceImpl extends BaseServiceImpl implements SysUserService { private final SysUserRoleService sysUserRoleService; private final SysUserPostService sysUserPostService; + private final TransService transService; @Override public PageResult page(SysUserQuery query) { @@ -184,6 +186,7 @@ public class SysUserServiceImpl extends BaseServiceImpl result) { + ExcelUtils.parseDict(result); List sysUserEntities = SysUserConvert.INSTANCE.convertListEntity(result); sysUserEntities.forEach(user -> user.setPassword(password)); saveBatch(sysUserEntities); @@ -197,7 +200,7 @@ public class SysUserServiceImpl extends BaseServiceImpl list = list(Wrappers.lambdaQuery(SysUserEntity.class).eq(SysUserEntity::getSuperAdmin, SuperAdminEnum.NO.getValue())); List userExcelVOS = SysUserConvert.INSTANCE.convert2List(list); - + transService.transBatch(userExcelVOS); // 写到浏览器打开 ExcelUtils.excelExport(SysUserExcelVO.class, "system_user_excel" + DateUtils.format(new Date()), null, userExcelVOS); } diff --git a/maku-boot-system/src/main/java/net/maku/system/vo/SysLogLoginVO.java b/maku-boot-system/src/main/java/net/maku/system/vo/SysLogLoginVO.java index 80fc691..bfc1851 100644 --- a/maku-boot-system/src/main/java/net/maku/system/vo/SysLogLoginVO.java +++ b/maku-boot-system/src/main/java/net/maku/system/vo/SysLogLoginVO.java @@ -7,6 +7,9 @@ import com.alibaba.excel.metadata.GlobalConfiguration; import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.fasterxml.jackson.annotation.JsonFormat; +import com.fhs.core.trans.anno.Trans; +import com.fhs.core.trans.constant.TransType; +import com.fhs.core.trans.vo.TransPojo; import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; import net.maku.framework.common.excel.DateConverter; @@ -22,7 +25,7 @@ import java.util.Date; */ @Data @Schema(description = "登录日志") -public class SysLogLoginVO implements Serializable { +public class SysLogLoginVO implements Serializable, TransPojo { private static final long serialVersionUID = 1L; @ExcelIgnore @@ -45,48 +48,25 @@ public class SysLogLoginVO implements Serializable { @Schema(description = "User Agent") private String userAgent; - @ExcelProperty(value = "登录状态", converter = StatusConverter.class) + @ExcelIgnore + @Trans(type = TransType.DICTIONARY, key = "success_fail", ref = "statusLabel") @Schema(description = "登录状态 0:失败 1:成功") private Integer status; - @ExcelProperty(value = "操作信息", converter = OperationConverter.class) + @ExcelProperty(value = "登录状态") + private String statusLabel; + + @ExcelIgnore + @Trans(type = TransType.DICTIONARY, key = "login_operation", ref = "operationLabel") @Schema(description = "操作信息 0:登录成功 1:退出成功 2:验证码错误 3:账号密码错误") private Integer operation; + @ExcelProperty(value = "操作信息") + private String operationLabel; + @ExcelProperty(value = "创建时间", converter = DateConverter.class) @Schema(description = "创建时间") @JsonFormat(pattern = DateUtils.DATE_TIME_PATTERN) private Date createTime; - - public static class StatusConverter implements Converter { - @Override - public WriteCellData convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { - String dateValue = ""; - if (value == 0) { - dateValue = "失败"; - } else if (value == 1) { - dateValue = "成功"; - } - return new WriteCellData<>(dateValue); - } - } - - public static class OperationConverter implements Converter { - @Override - public WriteCellData convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { - String dateValue = ""; - if (value == 0) { - dateValue = "登录成功"; - } else if (value == 1) { - dateValue = "退出成功"; - } else if (value == 2) { - dateValue = "验证码错误"; - } else if (value == 3) { - dateValue = "账号密码错误"; - } - return new WriteCellData<>(dateValue); - } - } - } \ No newline at end of file diff --git a/maku-boot-system/src/main/java/net/maku/system/vo/SysUserExcelVO.java b/maku-boot-system/src/main/java/net/maku/system/vo/SysUserExcelVO.java index f3c298f..e9276aa 100644 --- a/maku-boot-system/src/main/java/net/maku/system/vo/SysUserExcelVO.java +++ b/maku-boot-system/src/main/java/net/maku/system/vo/SysUserExcelVO.java @@ -1,11 +1,15 @@ package net.maku.system.vo; +import com.alibaba.excel.annotation.ExcelIgnore; import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.converters.Converter; 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.fhs.core.trans.anno.Trans; +import com.fhs.core.trans.constant.TransType; +import com.fhs.core.trans.vo.TransPojo; import lombok.Data; import net.maku.framework.common.excel.DateConverter; import net.maku.framework.common.utils.DateUtils; @@ -22,18 +26,28 @@ import java.util.Date; * @author 阿沐 babamu@126.com */ @Data -public class SysUserExcelVO implements Serializable { +public class SysUserExcelVO implements Serializable, TransPojo { private static final long serialVersionUID = 1L; + /** + * 本属性对于导出无用,只是用于翻译 + */ + @ExcelIgnore + private Long id; + @ExcelProperty("用户名") private String username; @ExcelProperty("姓名") private String realName; - @ExcelProperty(value = "性别", converter = GenderConverter.class) + @ExcelIgnore + @Trans(type = TransType.DICTIONARY, key = "user_gender", ref = "genderLabel") private Integer gender; + @ExcelProperty(value = "性别") + private String genderLabel; + @ExcelProperty("邮箱") private String email; @@ -43,54 +57,21 @@ public class SysUserExcelVO implements Serializable { @ExcelProperty("机构ID") private Long orgId; - @ExcelProperty(value = "状态", converter = StatusConverter.class) + @ExcelIgnore + @Trans(type = TransType.DICTIONARY, key = "user_status", ref = "statusLabel") private Integer status; - @ExcelProperty(value = "超级管理员", converter = SuperConverter.class) + @ExcelProperty(value = "状态") + private String statusLabel; + + @ExcelIgnore + @Trans(type = TransType.DICTIONARY, key = "user_super_admin", ref = "superAdminLabel") private Integer superAdmin; + @ExcelProperty(value = "超级管理员") + private String superAdminLabel; + @ExcelProperty(value = "创建时间", converter = DateConverter.class) private Date createTime; - public static class GenderConverter implements Converter { - - @Override - public Integer convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { - String dateString = cellData.getStringValue(); - return UserGenderEnum.getValueByName(dateString); - } - - @Override - public WriteCellData convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { - return new WriteCellData<>(UserGenderEnum.getNameByValue(value)); - } - } - - public static class StatusConverter implements Converter { - - @Override - public Integer convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { - String dateString = cellData.getStringValue(); - return UserStatusEnum.getValueByName(dateString); - } - - @Override - public WriteCellData convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { - return new WriteCellData<>(UserStatusEnum.getNameByValue(value)); - } - } - - public static class SuperConverter implements Converter { - - @Override - public Integer convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { - String dateString = cellData.getStringValue(); - return SuperAdminEnum.getValueByName(dateString); - } - - @Override - public WriteCellData convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { - return new WriteCellData<>(SuperAdminEnum.getNameByValue(value)); - } - } } diff --git a/maku-framework/pom.xml b/maku-framework/pom.xml index 8cefa44..1554cb0 100644 --- a/maku-framework/pom.xml +++ b/maku-framework/pom.xml @@ -64,5 +64,13 @@ com.alibaba easyexcel + + com.fhs-opensource + easy-trans-mybatis-plus-extend + + + com.fhs-opensource + easy-trans-spring-boot-starter + \ No newline at end of file diff --git a/maku-framework/src/main/java/net/maku/framework/common/utils/ExcelUtils.java b/maku-framework/src/main/java/net/maku/framework/common/utils/ExcelUtils.java index a659b7d..84897dd 100644 --- a/maku-framework/src/main/java/net/maku/framework/common/utils/ExcelUtils.java +++ b/maku-framework/src/main/java/net/maku/framework/common/utils/ExcelUtils.java @@ -3,6 +3,14 @@ package net.maku.framework.common.utils; import cn.hutool.core.util.URLUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.support.ExcelTypeEnum; +import com.fhs.common.spring.SpringContextUtil; +import com.fhs.common.utils.ConverterUtils; +import com.fhs.core.trans.anno.Trans; +import com.fhs.core.trans.constant.TransType; +import com.fhs.core.trans.util.ReflectUtils; +import com.fhs.core.trans.vo.TransPojo; +import com.fhs.trans.service.impl.DictionaryTransService; +import lombok.SneakyThrows; import net.maku.framework.common.excel.ExcelDataListener; import net.maku.framework.common.excel.ExcelFinishCallBack; import org.apache.commons.lang3.StringUtils; @@ -12,7 +20,9 @@ import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.lang.reflect.Field; import java.util.List; +import java.util.stream.Collectors; /** * The type Excel utils. @@ -137,4 +147,51 @@ public class ExcelUtils { } } + /** + * 解析字典数据到字段上 + * 比如 T中有 genderLabel字段 为男 需要给 gender 字段自动设置为0 + * + * @param datas 需要被反向解析的数据 + * @param + */ + @SneakyThrows + public static void parseDict(List datas) { + //没有数据就不需要初始化 + if (datas == null || datas.isEmpty() || datas.get(0) == null) { + return; + } + Class clazz = datas.get(0).getClass(); + //拿到所有需要反向翻译的字段 + List fields = ReflectUtils.getAnnotationField(clazz, Trans.class); + //过滤出字典翻译 + fields = fields.stream().filter(field -> { + return TransType.DICTIONARY.equals(field.getAnnotation(Trans.class).type()); + }).collect(Collectors.toList()); + DictionaryTransService dictionaryTransService = SpringContextUtil.getBeanByClass(DictionaryTransService.class); + for (T data : datas) { + for (Field field : fields) { + Trans trans = field.getAnnotation(Trans.class); + //key不能为空并且ref不为空的才自动处理 + if (!StringUtils.isBlank(trans.key()) && !StringUtils.isBlank(trans.ref())) { + Field ref = ReflectUtils.getDeclaredField(clazz, trans.ref()); + ref.setAccessible(true); + //获取字典反向值 + String value = dictionaryTransService.getUnTransMap().get(trans.key() + "_" + ref.get(data)); + if (StringUtils.isBlank(value)) { + continue; + } + //一般目标字段是int或者string字段 后面有添加单独抽离方法 + if(Integer.class.equals(field.getType())){ + field.setAccessible(true); + field.set(data,ConverterUtils.toInteger(value)); + }else{ + field.setAccessible(true); + field.set(data,ConverterUtils.toString(value)); + } + } + } + } + + } + } diff --git a/maku-server/src/main/resources/application.yml b/maku-server/src/main/resources/application.yml index 99e9171..dddef85 100644 --- a/maku-server/src/main/resources/application.yml +++ b/maku-server/src/main/resources/application.yml @@ -61,4 +61,13 @@ mybatis-plus: configuration-properties: prefix: blobType: BLOB - boolValue: TRUE \ No newline at end of file + boolValue: TRUE + +#easy trans 数据翻译组件 +easy-trans: + #启用redis缓存 如果不用redis请设置为false + is-enable-redis: false + #启用全局翻译(拦截所有responseBody进行自动翻译),如果对于性能要求很高可关闭此配置 + is-enable-global: true + #启用平铺模式 + is-enable-tile: true \ No newline at end of file diff --git a/pom.xml b/pom.xml index b106e67..9f67c6f 100644 --- a/pom.xml +++ b/pom.xml @@ -46,6 +46,7 @@ 5.6.89 3.22.3 3.1.1 + 2.0.11 @@ -155,6 +156,16 @@ easyexcel ${easyexcel.version} + + com.fhs-opensource + easy-trans-spring-boot-starter + ${easytrans.version} + + + com.fhs-opensource + easy-trans-mybatis-plus-extend + ${easytrans.version} +