Skip to content

Commit

Permalink
refactor: 用户角色名称调整为角色名称列表返回,并全局优化 Crane4j 组件的使用方式
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed May 17, 2024
1 parent 1427c13 commit 857a1c9
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,9 @@ public class ContainerConstants extends ContainerPool {
public static final String USER_ROLE_ID_LIST = "UserRoleIdList";

/**
* 角色部门列表
* 用户角色名称列表
*/
public static final String ROLE_DEPT_ID_LIST = "RoleDeptIdList";

/**
* 在线用户最后活跃时间
*/
public static final String ONLINE_USER_LAST_ACTIVE_TIME = "OnlineUserLastActiveTime";
public static final String USER_ROLE_NAME_LIST = "UserRoleNameList";

private ContainerConstants() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

package top.continew.admin.auth.model.resp;

import cn.crane4j.annotation.Assemble;
import cn.crane4j.annotation.Mapping;
import cn.crane4j.annotation.*;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import top.continew.admin.auth.service.OnlineUserService;
import top.continew.admin.common.constant.ContainerConstants;

import java.io.Serial;
Expand All @@ -43,14 +43,14 @@ public class OnlineUserResp implements Serializable {
* ID
*/
@Schema(description = "ID", example = "1")
@Assemble(container = ContainerConstants.USER_NICKNAME, props = @Mapping(ref = "nickname"))
@Assemble(prop = ":nickname", container = ContainerConstants.USER_NICKNAME)
private Long id;

/**
* 令牌
*/
@Schema(description = "令牌", example = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOjEsInJuU3RyIjoiTUd6djdyOVFoeHEwdVFqdFAzV3M5YjVJRzh4YjZPSEUifQ.7q7U3ouoN7WPhH2kUEM7vPe5KF3G_qavSG-vRgIxKvE")
@Assemble(container = ContainerConstants.ONLINE_USER_LAST_ACTIVE_TIME, props = @Mapping(ref = "lastActiveTime"))
@AssembleMethod(prop = ":lastActiveTime", targetType = OnlineUserService.class, method = @ContainerMethod(bindMethod = "getLastActiveTime", type = MappingType.ORDER_OF_KEYS))
private String token;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package top.continew.admin.auth.service.impl;

import cn.crane4j.annotation.AutoOperate;
import cn.crane4j.annotation.ContainerMethod;
import cn.crane4j.annotation.MappingType;
import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
Expand All @@ -30,7 +28,6 @@
import top.continew.admin.auth.model.query.OnlineUserQuery;
import top.continew.admin.auth.model.resp.OnlineUserResp;
import top.continew.admin.auth.service.OnlineUserService;
import top.continew.admin.common.constant.ContainerConstants;
import top.continew.admin.common.model.dto.LoginUser;
import top.continew.admin.common.util.helper.LoginHelper;
import top.continew.starter.core.constant.StringConstants;
Expand Down Expand Up @@ -84,7 +81,6 @@ public List<LoginUser> list(OnlineUserQuery query) {
}

@Override
@ContainerMethod(namespace = ContainerConstants.ONLINE_USER_LAST_ACTIVE_TIME, type = MappingType.ORDER_OF_KEYS)
public LocalDateTime getLastActiveTime(String token) {
long lastActiveTime = StpUtil.getStpLogic().getTokenLastActiveTime(token);
return lastActiveTime == SaTokenDao.NOT_VALUE_EXPIRE ? null : DateUtil.date(lastActiveTime).toLocalDateTime();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class DeptResp extends BaseDetailResp {
*/
@Schema(description = "上级部门 ID", example = "2")
@ConditionOnExpression(value = "#target.parentId != 0")
@AssembleMethod(targetType = DeptService.class, method = @ContainerMethod(bindMethod = "get", resultType = DeptResp.class), props = @Mapping(src = "name", ref = "parentName"))
@AssembleMethod(props = @Mapping(src = "name", ref = "parentName"), targetType = DeptService.class, method = @ContainerMethod(bindMethod = "get", resultType = DeptResp.class))
@ExcelProperty(value = "上级部门 ID", order = 3)
private Long parentId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package top.continew.admin.system.model.resp;

import cn.crane4j.annotation.Assemble;
import cn.crane4j.annotation.Mapping;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
Expand Down Expand Up @@ -81,7 +80,7 @@ public class MessageResp implements Serializable {
* 创建人
*/
@JsonIgnore
@Assemble(container = ContainerConstants.USER_NICKNAME, props = @Mapping(ref = "createUserString"))
@Assemble(prop = ":createUserString", container = ContainerConstants.USER_NICKNAME)
private Long createUser;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@

package top.continew.admin.system.model.resp;

import cn.crane4j.annotation.Assemble;
import cn.crane4j.annotation.Mapping;
import cn.crane4j.annotation.AssembleMethod;
import cn.crane4j.annotation.ContainerMethod;
import cn.crane4j.annotation.MappingType;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import top.continew.admin.common.constant.ContainerConstants;
import top.continew.admin.common.enums.DataScopeEnum;
import top.continew.admin.system.service.RoleDeptService;
import top.continew.starter.extension.crud.converter.ExcelBaseEnumConverter;
import top.continew.starter.extension.crud.model.resp.BaseDetailResp;

Expand All @@ -39,7 +40,7 @@
@Data
@ExcelIgnoreUnannotated
@Schema(description = "角色详情信息")
@Assemble(container = ContainerConstants.ROLE_DEPT_ID_LIST, key = "id", props = @Mapping(ref = "deptIds"))
@AssembleMethod(key = "id", prop = ":deptIds", targetType = RoleDeptService.class, method = @ContainerMethod(bindMethod = "listDeptIdByRoleId", type = MappingType.ORDER_OF_KEYS))
public class RoleDetailResp extends BaseDetailResp {

@Serial
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import cn.crane4j.annotation.AssembleMethod;
import cn.crane4j.annotation.ContainerMethod;
import cn.crane4j.annotation.Mapping;
import cn.crane4j.core.executor.handler.ManyToManyAssembleOperationHandler;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import io.swagger.v3.oas.annotations.media.Schema;
Expand All @@ -46,7 +47,7 @@
@Data
@ExcelIgnoreUnannotated
@Schema(description = "用户详情信息")
@Assemble(container = ContainerConstants.USER_ROLE_ID_LIST, key = "id", props = @Mapping(ref = "roleIds"))
@Assemble(key = "id", prop = ":roleIds", container = ContainerConstants.USER_ROLE_ID_LIST)
public class UserDetailResp extends BaseDetailResp {

@Serial
Expand Down Expand Up @@ -125,7 +126,7 @@ public class UserDetailResp extends BaseDetailResp {
* 部门 ID
*/
@Schema(description = "部门 ID", example = "5")
@AssembleMethod(targetType = DeptService.class, method = @ContainerMethod(bindMethod = "get", resultType = DeptResp.class), props = @Mapping(src = "name", ref = "deptName"))
@AssembleMethod(props = @Mapping(src = "name", ref = "deptName"), targetType = DeptService.class, method = @ContainerMethod(bindMethod = "get", resultType = DeptResp.class))
private Long deptId;

/**
Expand All @@ -139,14 +140,15 @@ public class UserDetailResp extends BaseDetailResp {
* 角色 ID 列表
*/
@Schema(description = "角色 ID 列表", example = "2")
@Assemble(prop = ":roleNames", container = ContainerConstants.USER_ROLE_NAME_LIST, handlerType = ManyToManyAssembleOperationHandler.class)
private List<Long> roleIds;

/**
* 角色
* 角色名称列表
*/
@Schema(description = "角色", example = "测试人员")
@Schema(description = "角色名称列表", example = "测试人员")
@ExcelProperty(value = "角色")
private String roleNames;
private List<String> roleNames;

@Override
public Boolean getDisabled() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@

package top.continew.admin.system.model.resp;

import cn.crane4j.annotation.Assemble;
import cn.crane4j.annotation.AssembleMethod;
import cn.crane4j.annotation.ContainerMethod;
import cn.crane4j.annotation.Mapping;
import cn.crane4j.core.executor.handler.ManyToManyAssembleOperationHandler;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import top.continew.admin.common.constant.ContainerConstants;
import top.continew.admin.common.enums.DisEnableStatusEnum;
import top.continew.admin.common.enums.GenderEnum;
import top.continew.admin.common.util.helper.LoginHelper;
Expand All @@ -30,6 +33,7 @@
import top.continew.starter.security.mask.enums.MaskType;

import java.io.Serial;
import java.util.List;
import java.util.Objects;

/**
Expand All @@ -40,6 +44,7 @@
*/
@Data
@Schema(description = "用户信息")
@Assemble(key = "id", prop = ":roleIds", container = ContainerConstants.USER_ROLE_ID_LIST)
public class UserResp extends BaseDetailResp {

@Serial
Expand Down Expand Up @@ -105,7 +110,7 @@ public class UserResp extends BaseDetailResp {
* 部门 ID
*/
@Schema(description = "部门 ID", example = "5")
@AssembleMethod(targetType = DeptService.class, method = @ContainerMethod(bindMethod = "get", resultType = DeptResp.class), props = @Mapping(src = "name", ref = "deptName"))
@AssembleMethod(props = @Mapping(src = "name", ref = "deptName"), targetType = DeptService.class, method = @ContainerMethod(bindMethod = "get", resultType = DeptResp.class))
private Long deptId;

/**
Expand All @@ -114,6 +119,19 @@ public class UserResp extends BaseDetailResp {
@Schema(description = "所属部门", example = "测试部")
private String deptName;

/**
* 角色 ID 列表
*/
@Schema(description = "角色 ID 列表", example = "2")
@Assemble(prop = ":roleNames", container = ContainerConstants.USER_ROLE_NAME_LIST, handlerType = ManyToManyAssembleOperationHandler.class)
private List<Long> roleIds;

/**
* 角色名称列表
*/
@Schema(description = "角色名称列表", example = "测试人员")
private List<String> roleNames;

@Override
public Boolean getDisabled() {
return this.getIsSystem() || Objects.equals(this.getId(), LoginHelper.getUserId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package top.continew.admin.system.model.resp.log;

import cn.crane4j.annotation.Assemble;
import cn.crane4j.annotation.Mapping;
import cn.crane4j.annotation.condition.ConditionOnPropertyNotNull;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
Expand Down Expand Up @@ -155,7 +154,7 @@ public class LogDetailResp implements Serializable {
*/
@JsonIgnore
@ConditionOnPropertyNotNull
@Assemble(container = ContainerConstants.USER_NICKNAME, props = @Mapping(ref = "createUserString"))
@Assemble(prop = ":createUserString", container = ContainerConstants.USER_NICKNAME)
private Long createUser;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@

package top.continew.admin.system.service.impl;

import cn.crane4j.annotation.ContainerMethod;
import cn.crane4j.annotation.MappingType;
import cn.hutool.core.collection.CollUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.continew.admin.common.constant.ContainerConstants;
import top.continew.admin.system.mapper.RoleDeptMapper;
import top.continew.admin.system.model.entity.RoleDeptDO;
import top.continew.admin.system.service.RoleDeptService;
Expand Down Expand Up @@ -75,7 +72,6 @@ public void deleteByDeptIds(List<Long> deptIds) {
}

@Override
@ContainerMethod(namespace = ContainerConstants.ROLE_DEPT_ID_LIST, type = MappingType.ORDER_OF_KEYS)
public List<Long> listDeptIdByRoleId(Long roleId) {
return baseMapper.selectDeptIdByRoleId(roleId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package top.continew.admin.system.service.impl;

import cn.crane4j.annotation.ContainerMethod;
import cn.crane4j.annotation.MappingType;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
Expand All @@ -25,6 +27,7 @@
import org.springframework.transaction.annotation.Transactional;
import top.continew.admin.auth.service.OnlineUserService;
import top.continew.admin.common.constant.CacheConstants;
import top.continew.admin.common.constant.ContainerConstants;
import top.continew.admin.common.constant.SysConstants;
import top.continew.admin.common.enums.DataScopeEnum;
import top.continew.admin.common.model.dto.RoleDTO;
Expand Down Expand Up @@ -142,6 +145,7 @@ public List<LabelValueResp<Long>> buildDict(List<RoleResp> list) {
}

@Override
@ContainerMethod(namespace = ContainerConstants.USER_ROLE_NAME_LIST, type = MappingType.ORDER_OF_KEYS)
public List<String> listNameByIds(List<Long> ids) {
List<RoleDO> roleList = baseMapper.lambdaQuery().select(RoleDO::getName).in(RoleDO::getId, ids).list();
return roleList.stream().map(RoleDO::getName).toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserResp, UserDetailResp, UserQuery, UserReq> implements UserService, CommonUserService {

private final OnlineUserService onlineUserService;
private final RoleService roleService;
private final UserRoleService userRoleService;
private final FileService fileService;
private final FileStorageService fileStorageService;
Expand Down Expand Up @@ -283,17 +282,6 @@ public String getNicknameById(Long id) {
return baseMapper.selectNicknameById(id);
}

@Override
protected void fill(Object obj) {
super.fill(obj);
if (obj instanceof UserDetailResp detail) {
List<Long> roleIdList = detail.getRoleIds();
if (CollUtil.isNotEmpty(roleIdList)) {
detail.setRoleNames(String.join(StringConstants.CHINESE_COMMA, roleService.listNameByIds(roleIdList)));
}
}
}

@Override
protected QueryWrapper<UserDO> buildQueryWrapper(UserQuery query) {
String description = query.getDescription();
Expand Down

0 comments on commit 857a1c9

Please sign in to comment.