Prechádzať zdrojové kódy

feat(role): 支持角色新增与编辑功能

- 在 AddRoleParam 中增加 roleId 字段以支持编辑角色
-为 ClientAddParam 和 ClientModParam 添加 roleId 校验
- TblRoleService 新增 queryRole 方法用于角色查询
- 实现根据租户和角色信息查询角色逻辑
- WebGatewayImpl 中完善角色添加/编辑业务流程
- 控制器接口描述更新为"新增/编辑角色"
- 用户网关实现中使用传入的角色ID而非默认角色
- 增加后台系统管理员默认租户常量定义
chejianzheng 3 týždňov pred
rodič
commit
76759d6f31

+ 1 - 1
portal-service-application/src/main/java/com/hfln/portal/application/controller/web/WebRoleController.java

@@ -25,7 +25,7 @@ public class WebRoleController {
     private WebGateway webGateway;
 
     @PostMapping("/add")
-    @Operation(summary = "新增角色")
+    @Operation(summary = "新增/编辑角色")
     public ApiResult<Void> addRole(@RequestBody @Valid AddRoleParam params) {
 
         webGateway.addRole(params);

+ 3 - 0
portal-service-common/src/main/java/com/hfln/portal/common/constant/UserConstants.java

@@ -19,4 +19,7 @@ public interface UserConstants {
     // 用户默认角色code
     String ROLE_USER_MANAGER_DEFAULT = "user_manager_default";
 
+
+    // 后台系统管理员默认租户
+    String HFLN_TENANT_CODE = "HFLNXXJSYXGS";
 }

+ 1 - 1
portal-service-common/src/main/java/com/hfln/portal/common/request/web/AddRoleParam.java

@@ -1 +1 @@
-package com.hfln.portal.common.request.web;

import com.hfln.portal.common.vo.BaseVO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import javax.validation.constraints.NotBlank;


@Data
public class AddRoleParam extends BaseVO {

    @Schema(description = "角色编码")
    @NotBlank(message = "角色编码不能为空!")
    private String roleCode;

    @Schema(description = "角色名称")
    @NotBlank(message = "角色名称不能为空!")
    private String roleName;

    @Schema(description = "角色描述")
    private String roleDesc;

    @Schema(description = "所属租户")
    @NotBlank(message = "所属租户不能为空!")
    private Long tenantId;
}
+package com.hfln.portal.common.request.web;

import com.hfln.portal.common.vo.BaseVO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import javax.validation.constraints.NotBlank;


@Data
public class AddRoleParam extends BaseVO {

    @Schema(description = "角色编码")
    @NotBlank(message = "角色编码不能为空!")
    private String roleCode;

    @Schema(description = "角色名称")
    @NotBlank(message = "角色名称不能为空!")
    private String roleName;

    @Schema(description = "角色描述")
    private String roleDesc;

    @Schema(description = "所属租户")
    private Long tenantId;

    @Schema(description = "角色id")
    private Long roleId;
}

+ 5 - 0
portal-service-common/src/main/java/com/hfln/portal/common/request/web/ClientAddParam.java

@@ -4,6 +4,7 @@ import io.swagger.v3.oas.annotations.media.Schema;
 import lombok.Data;
 
 import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.NotNull;
 
 
 @Data
@@ -22,4 +23,8 @@ public class ClientAddParam {
 
     @Schema(description = "用户名")
     private String userName;
+
+    @Schema(description = "角色id")
+    @NotNull(message = "角色id不能为空")
+    private Long roleId;
 }

+ 4 - 0
portal-service-common/src/main/java/com/hfln/portal/common/request/web/ClientModParam.java

@@ -28,4 +28,8 @@ public class ClientModParam {
 
     @Schema(description = "用户名")
     private String userName;
+
+    @Schema(description = "角色id")
+    @NotNull(message = "角色id不能为空")
+    private Long roleId;
 }

+ 35 - 8
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/gateway/impl/WebGatewayImpl.java

@@ -431,16 +431,36 @@ public class WebGatewayImpl implements WebGateway {
 
     @Override
     public void addRole(AddRoleParam param) {
-        //1.检查角色是否存在
-        TblRole exist = tblRoleService.queryByRoleCode(param.getRoleCode());
-        if (Objects.nonNull(exist)) {
-            throw new BizException(ErrorEnum.ROLE_ALREADY_EXISTS.getErrorCode(), ErrorEnum.ROLE_ALREADY_EXISTS.getErrorMessage());
+
+        String userType = (String) StpUtil.getSession().get(UserConstants.SA_USER_TYPE);
+        Long tenantId = (Long) StpUtil.getSession().get(UserConstants.SA_USER_TENANT_ID);
+        if (AdminUserType.getBgManagerTypes().contains(userType)) {
+            tenantId = param.getTenantId();
         }
 
-        //2.保存角色信息
-        TblRole role = CopyUtils.copy(param, TblRole.class);
-        role.setIsDeleted(BasePO.DeleteFlag.NOT_DELETED);
-        tblRoleService.save(role);
+        if (param.getRoleId() == null) {
+            //1.检查角色是否存在
+            TblRole exist = tblRoleService.queryRole(tenantId, param.getRoleCode());
+            if (Objects.nonNull(exist)) {
+                throw new BizException(ErrorEnum.ROLE_ALREADY_EXISTS.getErrorCode(), ErrorEnum.ROLE_ALREADY_EXISTS.getErrorMessage());
+            }
+
+            //2.保存角色信息
+            TblRole role = CopyUtils.copy(param, TblRole.class);
+            role.setIsDeleted(BasePO.DeleteFlag.NOT_DELETED);
+            tblRoleService.save(role);
+        } else {
+
+            // 检测角色 code name 是否重复
+            TblRole exist = tblRoleService.queryRole(tenantId, param.getRoleCode(), param.getRoleName(), param.getRoleId());
+            if (Objects.nonNull(exist)) {
+                throw new BizException(ErrorEnum.ROLE_ALREADY_EXISTS.getErrorCode(), ErrorEnum.ROLE_ALREADY_EXISTS.getErrorMessage());
+            }
+
+            TblRole byId = tblRoleService.getById(param.getRoleId());
+            BeanUtils.copyProperties(param, byId);
+            tblRoleService.updateById(byId);
+        }
     }
 
     @Override
@@ -462,11 +482,18 @@ public class WebGatewayImpl implements WebGateway {
 
     @Override
     public List<RoleListDTO> roleList() {
+
+        String userType = (String) StpUtil.getSession().get(UserConstants.SA_USER_TYPE);
+        Long tenantId = (Long) StpUtil.getSession().get(UserConstants.SA_USER_TENANT_ID);
+
         // 1. 查询所有未被删除的角色
         LambdaQueryWrapper<TblRole> queryWrapper = new LambdaQueryWrapper<>();
         queryWrapper
                 .select(TblRole::getRoleId, TblRole::getRoleCode, TblRole::getRoleName)
                 .eq(TblRole::getIsDeleted, BasePO.DeleteFlag.NOT_DELETED);
+        if (tenantId != null) {
+            queryWrapper.eq(TblRole::getTenantId, tenantId);
+        }
 
         // 2. 查询如果为空,则返回空集合
         List<TblRole> roleList = tblRoleService.list(queryWrapper);

+ 4 - 5
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/gateway/impl/WebUserGatewayImpl.java

@@ -15,7 +15,6 @@ import com.hfln.portal.domain.customer.util.PasswordUtil;
 import com.hfln.portal.domain.exception.ErrorEnum;
 import com.hfln.portal.domain.gateway.WebUserGateway;
 import com.hfln.portal.infrastructure.po.AdminUserInfo;
-import com.hfln.portal.infrastructure.po.TblRole;
 import com.hfln.portal.infrastructure.po.TblUserRole;
 import com.hfln.portal.infrastructure.service.AdminUserService;
 import com.hfln.portal.infrastructure.service.TblOprLogService;
@@ -198,10 +197,10 @@ public class WebUserGatewayImpl implements WebUserGateway {
         adminUserService.save(adminUserInfo);
 
         //3.保存用户角色到 tbl_user_role 表
-        TblRole tblRole = tblRoleService.queryByRoleCode(UserConstants.ROLE_USER_MANAGER_DEFAULT);
+//        TblRole tblRole = tblRoleService.queryByRoleCode(UserConstants.ROLE_USER_MANAGER_DEFAULT);
         TblUserRole userRole = new TblUserRole();
         userRole.setUserId(adminUserInfo.getUserId());
-        userRole.setRoleId(tblRole.getRoleId());
+        userRole.setRoleId(param.getRoleId());
         tblUserRoleService.save(userRole);
 
         logService.saveLog(adminUserInfo.getUserId(),OprLogType.ADD_CLIENT.getCode(), adminUserInfo);
@@ -231,10 +230,10 @@ public class WebUserGatewayImpl implements WebUserGateway {
         adminUserService.save(exist);
 
         //3.保存用户角色到 tbl_user_role 表
-        TblRole tblRole = tblRoleService.queryByRoleCode(UserConstants.ROLE_USER_MANAGER_DEFAULT);
+//        TblRole tblRole = tblRoleService.queryByRoleCode(UserConstants.ROLE_USER_MANAGER_DEFAULT);
         TblUserRole userRole = new TblUserRole();
         userRole.setUserId(exist.getUserId());
-        userRole.setRoleId(tblRole.getRoleId());
+        userRole.setRoleId(param.getRoleId());
         tblUserRoleService.save(userRole);
     }
 

+ 4 - 0
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/service/TblRoleService.java

@@ -12,4 +12,8 @@ public interface TblRoleService extends IService<TblRole> {
     TblRole findAvailableByCode(String roleCode);
 
     TblRole queryByRoleCode(String roleCode);
+
+    TblRole queryRole(Long tenantId, String roleCode);
+
+    TblRole queryRole(Long tenantId, String roleCode, String roleName, Long roleId);
 }

+ 24 - 0
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/service/impl/TblRoleServiceImpl.java

@@ -41,5 +41,29 @@ public class TblRoleServiceImpl extends ServiceImpl<TblRoleMapper, TblRole> impl
         return this.baseMapper.selectOne(queryWrapper);
     }
 
+    @Override
+    public TblRole queryRole(Long tenantId, String roleCode) {
+        LambdaQueryWrapper<TblRole> queryWrapper = new LambdaQueryWrapper<>();
+        if (tenantId != null) {
+            queryWrapper.eq(TblRole::getTenantId, tenantId);
+        }
+        queryWrapper.eq(TblRole::getRoleCode, roleCode);
+        queryWrapper.eq(TblRole::getIsDeleted, BasePO.DeleteFlag.NOT_DELETED);
+        return this.baseMapper.selectOne(queryWrapper);
+    }
+
+    @Override
+    public TblRole queryRole(Long tenantId, String roleCode, String roleName, Long roleId) {
+        LambdaQueryWrapper<TblRole> queryWrapper = new LambdaQueryWrapper<>();
+        if (tenantId != null) {
+            queryWrapper.eq(TblRole::getTenantId, tenantId);
+        }
+
+        queryWrapper.and(qw -> qw.eq(TblRole::getRoleCode, roleCode).or().eq(TblRole::getRoleName, roleName));
+
+        queryWrapper.ne(TblRole::getRoleId, roleId);
+        queryWrapper.eq(TblRole::getIsDeleted, BasePO.DeleteFlag.NOT_DELETED);
+        return this.baseMapper.selectOne(queryWrapper);
+    }
 
 }