review code.

This commit is contained in:
lijiahang
2023-09-15 10:59:01 +08:00
parent 1665820459
commit 73adb73eeb
3 changed files with 101 additions and 22 deletions

View File

@@ -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<T> {
this.wrapper = wrapper;
}
/**
* 通过实体类直接获取 DataQuery
*
* @param entityClass entityClass
* @param <T> entityClass
* @return DataQuery
*/
@SuppressWarnings("unchecked")
public static <T extends BaseDO> DataQuery<T> create(Class<T> entityClass) {
TableInfo table = Valid.notNull(TableInfoHelper.getTableInfo(entityClass), "notfound mapper class");
Class<BaseMapper<T>> mapperClass = (Class<BaseMapper<T>>) Classes.loadClass(table.getCurrentNamespace());
return new DataQuery<T>(SpringHolder.getBean(mapperClass));
}
public static <T> DataQuery<T> of(BaseMapper<T> dao) {
Valid.notNull(dao, "dao is null");
return new DataQuery<>(dao);
@@ -60,11 +80,45 @@ public class DataQuery<T> {
return this;
}
public T getOne() {
public DataQuery<T> 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> R get(Serializable id, Function<T, R> mapper) {
Valid.notNull(id, "id is null");
Valid.notNull(mapper, "convert function is null");
return Objects1.map(dao.selectById(id), mapper);
}
public Optional<T> 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> R getOne(Function<T, R> mapper) {
return this.only().get(mapper);
}
public Optional<T> optionalOne() {
return this.only().optional();
}
// -------------------- get --------------------
public T get() {
return dao.selectOne(wrapper);
}
@@ -78,6 +132,8 @@ public class DataQuery<T> {
return Optional.ofNullable(dao.selectOne(wrapper));
}
// -------------------- list --------------------
public List<T> list() {
return dao.selectList(wrapper);
}
@@ -91,14 +147,22 @@ public class DataQuery<T> {
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<T> dataGrid() {
return this.dataGrid(Function.identity());
}

View File

@@ -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 文件

View File

@@ -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<SystemUserDO> 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<Long> roleIds = systemUserRoleDAO.selectRoleIdByUserId(id);
List<String> 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;