From 73adb73eebf25c3530505659adc6ad4baf971a16 Mon Sep 17 00:00:00 2001 From: lijiahang Date: Fri, 15 Sep 2023 10:59:01 +0800 Subject: [PATCH] review code. --- .../mybatis/core/query/DataQuery.java | 68 ++++++++++++++++++- .../ops/launch/generator/CodeGenerator.java | 2 + .../impl/AuthenticationServiceImpl.java | 53 +++++++++------ 3 files changed, 101 insertions(+), 22 deletions(-) diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/core/query/DataQuery.java b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/core/query/DataQuery.java index 1564c041..c93474fb 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/core/query/DataQuery.java +++ b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/core/query/DataQuery.java @@ -2,14 +2,20 @@ package com.orion.ops.framework.mybatis.core.query; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.baomidou.mybatisplus.core.metadata.TableInfo; +import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.orion.lang.define.wrapper.DataGrid; import com.orion.lang.define.wrapper.IPageRequest; import com.orion.lang.define.wrapper.Pager; import com.orion.lang.utils.Objects1; import com.orion.lang.utils.Valid; import com.orion.lang.utils.collect.Lists; +import com.orion.lang.utils.reflect.Classes; import com.orion.ops.framework.common.constant.Const; +import com.orion.ops.framework.mybatis.core.domain.BaseDO; +import com.orion.spring.SpringHolder; +import java.io.Serializable; import java.util.List; import java.util.Optional; import java.util.function.Function; @@ -40,6 +46,20 @@ public class DataQuery { this.wrapper = wrapper; } + /** + * 通过实体类直接获取 DataQuery + * + * @param entityClass entityClass + * @param entityClass + * @return DataQuery + */ + @SuppressWarnings("unchecked") + public static DataQuery create(Class entityClass) { + TableInfo table = Valid.notNull(TableInfoHelper.getTableInfo(entityClass), "notfound mapper class"); + Class> mapperClass = (Class>) Classes.loadClass(table.getCurrentNamespace()); + return new DataQuery(SpringHolder.getBean(mapperClass)); + } + public static DataQuery of(BaseMapper dao) { Valid.notNull(dao, "dao is null"); return new DataQuery<>(dao); @@ -60,11 +80,45 @@ public class DataQuery { return this; } - public T getOne() { + public DataQuery only() { wrapper.last(Const.LIMIT_1); - return dao.selectOne(wrapper); + return this; } + // -------------------- id -------------------- + + public T get(Serializable id) { + Valid.notNull(id, "id is null"); + return dao.selectById(id); + } + + public R get(Serializable id, Function mapper) { + Valid.notNull(id, "id is null"); + Valid.notNull(mapper, "convert function is null"); + return Objects1.map(dao.selectById(id), mapper); + } + + public Optional optional(Serializable id) { + Valid.notNull(id, "id is null"); + return Optional.ofNullable(dao.selectById(id)); + } + + // -------------------- one -------------------- + + public T getOne() { + return this.only().get(); + } + + public R getOne(Function mapper) { + return this.only().get(mapper); + } + + public Optional optionalOne() { + return this.only().optional(); + } + + // -------------------- get -------------------- + public T get() { return dao.selectOne(wrapper); } @@ -78,6 +132,8 @@ public class DataQuery { return Optional.ofNullable(dao.selectOne(wrapper)); } + // -------------------- list -------------------- + public List list() { return dao.selectList(wrapper); } @@ -91,14 +147,22 @@ public class DataQuery { return dao.selectList(wrapper).stream(); } + // -------------------- statistic -------------------- + public Long count() { return dao.selectCount(wrapper); } + public boolean absent() { + return dao.selectCount(wrapper) == 0; + } + public boolean present() { return dao.selectCount(wrapper) > 0; } + // -------------------- data grid -------------------- + public DataGrid dataGrid() { return this.dataGrid(Function.identity()); } diff --git a/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/CodeGenerator.java b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/CodeGenerator.java index 39d7c474..d4f35994 100644 --- a/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/CodeGenerator.java +++ b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/CodeGenerator.java @@ -345,6 +345,8 @@ public class CodeGenerator { new String[]{"/templates/orion-vue-views-components-table.vue.vm", "${feature}-table.vue", "vue/views/${module}/${feature}/components"}, // enum.types.ts 文件 new String[]{"/templates/orion-vue-views-types-enum.types.ts.vm", "enum.types.ts", "vue/views/${module}/${feature}/types"}, + // const.ts 文件 + new String[]{"/templates/orion-vue-views-types-const.ts.vm", "const.ts", "vue/views/${module}/${feature}/types"}, // form.rules.ts 文件 new String[]{"/templates/orion-vue-views-types-form.rules.ts.vm", "form.rules.ts", "vue/views/${module}/${feature}/types"}, // table.vue 文件 diff --git a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/AuthenticationServiceImpl.java b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/AuthenticationServiceImpl.java index 17d7cad6..654cc9fd 100644 --- a/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/AuthenticationServiceImpl.java +++ b/orion-ops-module-infra/orion-ops-module-infra-service/src/main/java/com/orion/ops/module/infra/service/impl/AuthenticationServiceImpl.java @@ -37,7 +37,6 @@ import java.util.Date; import java.util.List; import java.util.Objects; import java.util.Set; -import java.util.function.Supplier; import java.util.stream.Collectors; /** @@ -77,10 +76,10 @@ public class AuthenticationServiceImpl implements AuthenticationService { UserStatusEnum.checkUserStatus(user.getStatus()); // 设置上次登录时间 this.setLastLoginTime(user.getId()); - // 检查用户缓存 - this.setUserCacheIfPresent(() -> user); - // 删除登陆失败次数缓存 - redisTemplate.delete(UserCacheKeyDefine.LOGIN_FAILED_COUNT.format(request.getUsername())); + // 删除用户缓存 + this.deleteUserCache(user); + // 重设用户缓存 + this.setUserCache(user); // 获取登陆 ip String remoteAddr = Servlets.getRemoteAddr(servletRequest); String location = IpUtils.getLocation(remoteAddr); @@ -115,9 +114,15 @@ public class AuthenticationServiceImpl implements AuthenticationService { } @Override - public LoginUser getLoginUser(Long userId) { - // 获取用户缓存信息 - return this.setUserCacheIfPresent(() -> systemUserDAO.selectById(userId)); + public LoginUser getLoginUser(Long id) { + String userInfoKey = UserCacheKeyDefine.USER_INFO.format(id); + String userInfoCache = redisTemplate.opsForValue().get(userInfoKey); + // 缓存存在 + if (userInfoCache != null) { + return JSON.parseObject(userInfoCache, LoginUser.class); + } + // 设置缓存并返回 + return this.setUserCache(systemUserDAO.selectById(id)); } @Override @@ -251,23 +256,28 @@ public class AuthenticationServiceImpl implements AuthenticationService { systemUserDAO.updateById(update); } + /** + * 删除用户缓存 + * + * @param user user + */ + private void deleteUserCache(SystemUserDO user) { + // 用户信息缓存 + String userInfoKey = UserCacheKeyDefine.USER_INFO.format(user.getId()); + // 登陆失败次数缓存 + String loginFailedCountKey = UserCacheKeyDefine.LOGIN_FAILED_COUNT.format(user.getUsername()); + // 删除缓存 + redisTemplate.delete(Lists.of(userInfoKey, loginFailedCountKey)); + } + /** * 设置用户缓存 * - * @param supplier supplier - * @return 用户缓存 + * @param user user + * @return loginUser */ - private LoginUser setUserCacheIfPresent(Supplier supplier) { - SystemUserDO user = supplier.get(); + private LoginUser setUserCache(SystemUserDO user) { Long id = user.getId(); - String userInfoKey = UserCacheKeyDefine.USER_INFO.format(id); - String userInfoCache = redisTemplate.opsForValue().get(userInfoKey); - // 缓存存在 - if (userInfoCache != null) { - return JSON.parseObject(userInfoCache, LoginUser.class); - } - // 设置缓存 - LoginUser loginUser = SystemUserConvert.MAPPER.toLoginUser(user); // 查询用户角色 List roleIds = systemUserRoleDAO.selectRoleIdByUserId(id); List roleCodeList = permissionService.getRoleCache() @@ -276,6 +286,9 @@ public class AuthenticationServiceImpl implements AuthenticationService { .filter(s -> roleIds.contains(s.getId())) .map(SystemRoleDO::getCode) .collect(Collectors.toList()); + // 设置用户缓存 + String userInfoKey = UserCacheKeyDefine.USER_INFO.format(id); + LoginUser loginUser = SystemUserConvert.MAPPER.toLoginUser(user); loginUser.setRoles(roleCodeList); RedisUtils.setJson(userInfoKey, UserCacheKeyDefine.USER_INFO, loginUser); return loginUser;