角色关联
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
#### 简介
|
||||
用户模块主要包含以下功能:
|
||||
* 用户管理、角色管理
|
||||
* 用户管理
|
||||
* 角色管理
|
||||
* 资源管理
|
||||
* 部门管理
|
||||
* 职位管理(非必须)
|
||||
* 菜单资源管理
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.njcn.gather.user.pojo.constant;
|
||||
|
||||
/**
|
||||
* @author caozehui
|
||||
* @data 2024/11/11
|
||||
*/
|
||||
public interface RoleConst {
|
||||
/**
|
||||
* 角色类型:0-超级管理员
|
||||
*/
|
||||
int TYPE_SUPER_ADMINISTRATOR = 0;
|
||||
|
||||
/**
|
||||
* 角色类型:1-管理员
|
||||
*/
|
||||
int TYPE_ADMINISTRATOR = 1;
|
||||
|
||||
/**
|
||||
* 角色类型:2-用户
|
||||
*/
|
||||
int TYPE_USER = 2;
|
||||
|
||||
/**
|
||||
* 角色类型:3-APP角色
|
||||
*/
|
||||
int TYPE_APP = 3;
|
||||
|
||||
}
|
||||
@@ -32,5 +32,17 @@ public interface UserValidMessage {
|
||||
|
||||
String OLD_PASSWORD_NOT_BLANK = "旧密码不能为空,请检查oldPassword参数";
|
||||
|
||||
String NEW_PASSWORD_NOT_BLANK = "新密码格式错误,请检查newPassword参数";
|
||||
String NEW_PASSWORD_NOT_BLANK = "新密码不能为空,请检查newPassword参数";
|
||||
|
||||
String PID_NOT_BLANK = "父节点id不能为空,请检查pid参数";
|
||||
|
||||
String SORT_NOT_NULL = "排序不能为空,请检查sort参数";
|
||||
|
||||
String PATH_NOT_BLANK = "路径不能为空,请检查path参数";
|
||||
|
||||
String PATH_FORMAT_ERROR = "路径格式错误,请检查path参数";
|
||||
|
||||
String TYPE_NOT_BLANK = "类型不能为空,请检查type参数";
|
||||
|
||||
String PARAM_FORMAT_ERROR = "参数值非法";
|
||||
}
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
package com.njcn.gather.user.user.controller;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.njcn.gather.user.user.pojo.param.SysRoleParam;
|
||||
import com.njcn.gather.user.user.pojo.po.SysRole;
|
||||
import com.njcn.web.utils.HttpResultUtil;
|
||||
import com.njcn.common.pojo.annotation.OperateInfo;
|
||||
import com.njcn.common.pojo.constant.OperateType;
|
||||
import com.njcn.common.pojo.enums.common.LogEnum;
|
||||
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
|
||||
import com.njcn.common.pojo.response.HttpResult;
|
||||
import com.njcn.common.utils.LogUtil;
|
||||
import com.njcn.gather.user.user.service.ISysRoleService;
|
||||
import com.njcn.web.controller.BaseController;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiImplicitParam;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* @author caozehui
|
||||
* @date 2024-11-11
|
||||
*/
|
||||
@Slf4j
|
||||
@Api(tags = "角色管理")
|
||||
@RestController
|
||||
@RequestMapping("/sysRole")
|
||||
@RequiredArgsConstructor
|
||||
public class SysRoleController extends BaseController {
|
||||
private final ISysRoleService sysRoleService;
|
||||
|
||||
@OperateInfo(info = LogEnum.SYSTEM_COMMON)
|
||||
@PostMapping("/list")
|
||||
@ApiOperation("查询角色信息")
|
||||
@ApiImplicitParam(name = "queryParam", value = "查询参数", required = true)
|
||||
public HttpResult<Page<SysRole>> list(@RequestBody @Validated SysRoleParam.QueryParam queryParam) {
|
||||
String methodDescribe = getMethodDescribe("list");
|
||||
LogUtil.njcnDebug(log, "{},查询数据为:{}", methodDescribe, queryParam);
|
||||
Page<SysRole> result = sysRoleService.listRole(queryParam);
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
|
||||
}
|
||||
|
||||
@OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.ADD)
|
||||
@PostMapping("/add")
|
||||
@ApiOperation("新增角色信息")
|
||||
@ApiImplicitParam(name = "roleParam", value = "角色信息", required = true)
|
||||
public HttpResult<Object> add(@RequestBody @Validated SysRoleParam sysRoleParam) {
|
||||
String methodDescribe = getMethodDescribe("add");
|
||||
LogUtil.njcnDebug(log, "{},角色信息数据为:{}", methodDescribe, sysRoleParam);
|
||||
boolean result = sysRoleService.addRole(sysRoleParam);
|
||||
if (result) {
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
|
||||
} else {
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
|
||||
}
|
||||
}
|
||||
|
||||
@OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.UPDATE)
|
||||
@PutMapping("/update")
|
||||
@ApiOperation("修改角色信息")
|
||||
@ApiImplicitParam(name = "updateParam", value = "角色信息", required = true)
|
||||
public HttpResult<Object> update(@RequestBody @Validated SysRoleParam.UpdateParam updateParam) {
|
||||
String methodDescribe = getMethodDescribe("update");
|
||||
LogUtil.njcnDebug(log, "{},角色信息数据为:{}", methodDescribe, updateParam);
|
||||
boolean result = sysRoleService.updateRole(updateParam);
|
||||
if (result) {
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
|
||||
} else {
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
|
||||
}
|
||||
}
|
||||
|
||||
@OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.DELETE)
|
||||
@PostMapping("/delete")
|
||||
@ApiOperation("删除角色信息")
|
||||
@ApiImplicitParam(name = "ids", value = "角色id集合", required = true)
|
||||
public HttpResult<Object> delete(@RequestBody List<String> ids) {
|
||||
String methodDescribe = getMethodDescribe("delete");
|
||||
LogUtil.njcnDebug(log, "{},角色信息数据为:{}", methodDescribe, ids);
|
||||
boolean result = sysRoleService.deleteRole(ids);
|
||||
if (result) {
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
|
||||
} else {
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
|
||||
}
|
||||
}
|
||||
|
||||
@OperateInfo(info = LogEnum.SYSTEM_COMMON)
|
||||
@PostMapping("/listRoleByIds")
|
||||
@ApiOperation("根据角色id集合查询角色信息")
|
||||
@ApiImplicitParam(name = "ids", value = "角色id集合", required = true)
|
||||
public HttpResult<List<SysRole>> listRoleByIds(@RequestBody List<String> ids) {
|
||||
String methodDescribe = getMethodDescribe("listRoleByIds");
|
||||
List<SysRole> users = sysRoleService.list((new LambdaQueryWrapper<SysRole>().in(CollUtil.isNotEmpty(ids), SysRole::getId, ids)));
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, users, methodDescribe);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据角色id查询相关联的资源和组件
|
||||
*/
|
||||
@OperateInfo(info = LogEnum.SYSTEM_COMMON)
|
||||
@PostMapping("/selectFunctionComponent")
|
||||
@ApiOperation("根据角色id查询相关联的资源和组件")
|
||||
@ApiImplicitParam(name = "ids", value = "角色索引", required = true)
|
||||
public HttpResult<Object> selectFunctionComponent(@RequestBody List<String> ids) {
|
||||
String methodDescribe = getMethodDescribe("selectFunctionComponent");
|
||||
LogUtil.njcnDebug(log, "{},查询数据为:{}", methodDescribe, ids);
|
||||
boolean result = sysRoleService.selectRelevance(ids);
|
||||
if (result) {
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.DELETE_PID_EXIST, null, methodDescribe);
|
||||
} else {
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.DELETE_PID_UNEXIST, null, methodDescribe);
|
||||
}
|
||||
}
|
||||
|
||||
@OperateInfo(info = LogEnum.SYSTEM_COMMON)
|
||||
@PostMapping("/listRoleByType")
|
||||
@ApiOperation("根据权限类型查询相关角色")
|
||||
@ApiImplicitParam(name = "type", value = "权限类型", required = true)
|
||||
public HttpResult<List<SysRole>> listRoleByType(@RequestParam("type") Integer type) {
|
||||
String methodDescribe = getMethodDescribe("listRoleByType");
|
||||
LogUtil.njcnDebug(log, "{},查询数据为:{}", methodDescribe, type);
|
||||
List<SysRole> result = sysRoleService.listRoleByType(type);
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
|
||||
}
|
||||
|
||||
@OperateInfo(info = LogEnum.SYSTEM_COMMON)
|
||||
@GetMapping("/listAllRole")
|
||||
@ApiOperation("查询所有角色")
|
||||
public HttpResult<List<SysRole>> listAllRole() {
|
||||
String methodDescribe = getMethodDescribe("listAllRole");
|
||||
List<SysRole> result = sysRoleService.listAllRole();
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
|
||||
}
|
||||
|
||||
@OperateInfo(info = LogEnum.SYSTEM_COMMON)
|
||||
@GetMapping("/simpleList")
|
||||
@ApiOperation("查询所有角色作为下拉框")
|
||||
public HttpResult<List<SysRole>> simpleList() {
|
||||
String methodDescribe = getMethodDescribe("simpleList");
|
||||
List<SysRole> result = sysRoleService.simpleList();
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.njcn.gather.user.user.mapper;
|
||||
|
||||
import com.github.yulichang.base.MPJBaseMapper;
|
||||
import com.njcn.gather.user.user.pojo.po.SysRole;
|
||||
|
||||
/**
|
||||
* @author caozehui
|
||||
* @date 2024-11-11
|
||||
*/
|
||||
public interface SysRoleMapper extends MPJBaseMapper<SysRole> {
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.njcn.gather.user.user.mapper;
|
||||
|
||||
import com.github.yulichang.base.MPJBaseMapper;
|
||||
import com.njcn.gather.user.user.pojo.po.SysRole;
|
||||
import com.njcn.gather.user.user.pojo.po.SysUserRole;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author caozehui
|
||||
* @date 2024-11-12
|
||||
*/
|
||||
public interface SysUserRoleMapper extends MPJBaseMapper<SysUserRole> {
|
||||
/**
|
||||
* 根据用户id获取角色详情
|
||||
*
|
||||
* @param userId 用户id
|
||||
* @return 角色结果集
|
||||
*/
|
||||
List<SysRole> getRoleListByUserId(String userId);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.njcn.gather.user.user.mapper.SysRoleMapper">
|
||||
|
||||
|
||||
</mapper>
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.njcn.gather.user.user.mapper.SysUserRoleMapper">
|
||||
|
||||
|
||||
<select id="getRoleListByUserId" resultType="com.njcn.gather.user.user.pojo.po.SysRole">
|
||||
select b.*
|
||||
from sys_user_role a
|
||||
inner join sys_role b on a.role_id = b.id
|
||||
where a.user_id = #{userId}
|
||||
and b.state = 1
|
||||
</select>
|
||||
</mapper>
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.njcn.gather.user.user.pojo.param;
|
||||
|
||||
import com.njcn.common.pojo.constant.PatternRegex;
|
||||
import com.njcn.gather.user.pojo.constant.UserValidMessage;
|
||||
import com.njcn.web.pojo.param.BaseParam;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.hibernate.validator.constraints.Range;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Pattern;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author caozehui
|
||||
* @data 2024/11/11
|
||||
*/
|
||||
@Data
|
||||
public class SysRoleParam {
|
||||
|
||||
|
||||
@ApiModelProperty("名称")
|
||||
@NotBlank(message = UserValidMessage.NAME_NOT_BLANK)
|
||||
@Pattern(regexp = PatternRegex.DEPT_NAME_REGEX, message = UserValidMessage.NAME_FORMAT_ERROR)
|
||||
private String name;
|
||||
|
||||
@ApiModelProperty("角色代码")
|
||||
@NotNull(message = UserValidMessage.CODE_NOT_BLANK)
|
||||
private String code;
|
||||
|
||||
/**
|
||||
* 角色类型 0:超级管理员;1:管理员;2:普通用户
|
||||
*/
|
||||
@ApiModelProperty("角色类型")
|
||||
@Range(min = 0, max = 2, message = UserValidMessage.PARAM_FORMAT_ERROR)
|
||||
private Integer type;
|
||||
|
||||
@ApiModelProperty("角色描述")
|
||||
private String remark;
|
||||
|
||||
/**
|
||||
* 更新操作实体
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public static class UpdateParam extends SysRoleParam {
|
||||
|
||||
@ApiModelProperty("id")
|
||||
@NotBlank(message = UserValidMessage.ID_NOT_BLANK)
|
||||
@Pattern(regexp = PatternRegex.SYSTEM_ID, message = UserValidMessage.ID_FORMAT_ERROR)
|
||||
private String id;
|
||||
|
||||
}
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public static class QueryParam extends BaseParam {
|
||||
@ApiModelProperty("名称")
|
||||
private String name;
|
||||
|
||||
@ApiModelProperty("角色编码")
|
||||
private String code;
|
||||
|
||||
@ApiModelProperty("角色类型")
|
||||
private Integer type;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class RoleFunctionParam {
|
||||
private String roleId;
|
||||
|
||||
private List<String> functionIds;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.njcn.gather.user.user.pojo.po;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.njcn.db.mybatisplus.bo.BaseEntity;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author caozehui
|
||||
* @date 2024-11-11
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@TableName("sys_role")
|
||||
public class SysRole extends BaseEntity implements Serializable {
|
||||
private static final long serialVersionUID = 183697621480953314L;
|
||||
|
||||
/**
|
||||
* 角色表Id
|
||||
*/
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* 角色名称
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 角色代码,有需要用做匹配时候用(关联字典表id)
|
||||
*/
|
||||
private String code;
|
||||
|
||||
/**
|
||||
* 0-超级管理员;1-管理员角色;2-普通角色,默认普通角色
|
||||
*/
|
||||
private Integer type;
|
||||
|
||||
/**
|
||||
* 角色描述
|
||||
*/
|
||||
private String remark;
|
||||
|
||||
/**
|
||||
* 角色状态0-删除;1-正常;默认正常
|
||||
*/
|
||||
private Integer state;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.njcn.gather.user.user.pojo.po;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.njcn.db.mybatisplus.bo.BaseEntity;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author caozehui
|
||||
* @date 2024-11-12
|
||||
*/
|
||||
@Data
|
||||
@TableName("sys_user_role")
|
||||
public class SysUserRole implements Serializable {
|
||||
private static final long serialVersionUID = 725290952766199948L;
|
||||
/**
|
||||
* 用户Id
|
||||
*/
|
||||
private String userId;
|
||||
|
||||
/**
|
||||
* 角色Id
|
||||
*/
|
||||
private String roleId;
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
package com.njcn.gather.user.user.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.njcn.gather.user.user.pojo.param.SysRoleParam;
|
||||
import com.njcn.gather.user.user.pojo.po.SysRole;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author caozehui
|
||||
* @date 2024-11-11
|
||||
*/
|
||||
public interface ISysRoleService extends IService<SysRole> {
|
||||
|
||||
/**
|
||||
* 分页查询角色列表
|
||||
*
|
||||
* @param queryParam 查询参数
|
||||
*/
|
||||
Page<SysRole> listRole(SysRoleParam.QueryParam queryParam);
|
||||
|
||||
/**
|
||||
* 新增角色
|
||||
*
|
||||
* @param sysRoleParam 角色参数
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean addRole(SysRoleParam sysRoleParam);
|
||||
|
||||
/**
|
||||
* 更新角色
|
||||
*
|
||||
* @param updateParam 更新参数
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean updateRole(SysRoleParam.UpdateParam updateParam);
|
||||
|
||||
/**
|
||||
* 删除角色
|
||||
*
|
||||
* @param ids 角色id列表
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean deleteRole(List<String> ids);
|
||||
|
||||
/**
|
||||
* 查询所有角色列表
|
||||
*
|
||||
* @return 角色列表
|
||||
*/
|
||||
List<SysRole> listAllRole();
|
||||
|
||||
/**
|
||||
* 根据权限类型type获取角色列表
|
||||
*
|
||||
* @param type 角色类型
|
||||
* @return 角色列表
|
||||
*/
|
||||
List<SysRole> listRoleByType(Integer type);
|
||||
|
||||
/**
|
||||
* 根据用户id获取角色
|
||||
*
|
||||
* @param userId 用户id
|
||||
* @return 角色列表
|
||||
*/
|
||||
List<SysRole> listRoleByUserId(String userId);
|
||||
|
||||
/**
|
||||
* 查询所有角色作为下拉框
|
||||
*
|
||||
* @return 角色列表
|
||||
*/
|
||||
List<SysRole> simpleList();
|
||||
|
||||
/**
|
||||
* 判断角色id是否有相关联的资源和组件
|
||||
*
|
||||
* @param ids 角色id
|
||||
*/
|
||||
boolean selectRelevance(List<String> ids);
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.njcn.gather.user.user.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.njcn.gather.user.user.pojo.po.SysRole;
|
||||
import com.njcn.gather.user.user.pojo.po.SysUserRole;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author caozehui
|
||||
* @date 2024-11-12
|
||||
*/
|
||||
public interface ISysUserRoleService extends IService<SysUserRole> {
|
||||
/**
|
||||
* 根据用户id获取 用户--角色关系数据
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @return 关系数据
|
||||
*/
|
||||
List<SysUserRole> listUserRoleByUserId(String userId);
|
||||
|
||||
/**
|
||||
* 新增用户和角色的关系
|
||||
*
|
||||
* @param userId 用户id
|
||||
* @param roleIds 角色id
|
||||
* @return boolean
|
||||
*/
|
||||
boolean addUserRole(String userId, List<String> roleIds);
|
||||
|
||||
/**
|
||||
* 修改用户和角色的关系
|
||||
*
|
||||
* @param userId 用户id
|
||||
* @param roleIds 角色id
|
||||
* @return boolean
|
||||
*/
|
||||
boolean updateUserRole(String userId, List<String> roleIds);
|
||||
|
||||
/**
|
||||
* 根据用户id获取角色详情
|
||||
*
|
||||
* @param userId 用户id
|
||||
* @return 角色信息
|
||||
*/
|
||||
List<SysRole> listRoleByUserId(String userId);
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
package com.njcn.gather.user.user.service.impl;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.njcn.common.pojo.enums.common.DataStateEnum;
|
||||
import com.njcn.common.pojo.exception.BusinessException;
|
||||
import com.njcn.gather.user.pojo.enums.UserResponseEnum;
|
||||
import com.njcn.gather.user.user.mapper.SysRoleMapper;
|
||||
import com.njcn.gather.user.user.pojo.param.SysRoleParam;
|
||||
import com.njcn.gather.user.user.pojo.po.SysRole;
|
||||
import com.njcn.gather.user.user.service.ISysRoleService;
|
||||
import com.njcn.gather.user.user.service.ISysUserRoleService;
|
||||
import com.njcn.web.factory.PageFactory;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author caozehui
|
||||
* @date 2024-11-11
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {
|
||||
|
||||
private final ISysUserRoleService sysUserRoleService;
|
||||
|
||||
@Override
|
||||
public Page<SysRole> listRole(SysRoleParam.QueryParam queryParam) {
|
||||
QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
|
||||
if (ObjectUtil.isNotNull(queryParam)) {
|
||||
queryWrapper.eq(StrUtil.isNotBlank(queryParam.getName()), "sys_role.name", queryParam.getName());
|
||||
queryWrapper.eq(StrUtil.isNotBlank(queryParam.getCode()), "sys_role.code", queryParam.getCode());
|
||||
queryWrapper.eq(ObjectUtil.isNotNull(queryParam.getType()), "sys_role.type", queryParam.getType());
|
||||
}
|
||||
// if (queryParam.getType().equals(0)) {
|
||||
// queryWrapper.in("sys_role.type", queryParam.getType(), 1);
|
||||
// } else if (queryParam.getType().equals(1)) {
|
||||
// queryWrapper.eq("sys_role.type", 2);
|
||||
// }
|
||||
queryWrapper.eq("sys_role.state", DataStateEnum.ENABLE.getCode());
|
||||
return this.baseMapper.selectPage(new Page<>(PageFactory.getPageNum(queryParam), PageFactory.getPageSize(queryParam)), queryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addRole(SysRoleParam sysRoleParam) {
|
||||
checkCode(sysRoleParam, false);
|
||||
SysRole role = new SysRole();
|
||||
BeanUtil.copyProperties(sysRoleParam, role);
|
||||
//默认为正常状态
|
||||
role.setState(DataStateEnum.ENABLE.getCode());
|
||||
return this.save(role);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateRole(SysRoleParam.UpdateParam updateParam) {
|
||||
checkCode(updateParam, true);
|
||||
SysRole role = new SysRole();
|
||||
BeanUtil.copyProperties(updateParam, role);
|
||||
return this.updateById(role);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteRole(List<String> ids) {
|
||||
//todo
|
||||
|
||||
// 删除角色和用户的绑定
|
||||
//this.baseMapper.deleteUserRole(ids);
|
||||
// 删除角色和资源的绑定
|
||||
//this.baseMapper.deleteFunctionRole(ids);
|
||||
// 删除角色和组件的绑定
|
||||
//this.baseMapper.deleteComponentRole(ids);
|
||||
return this.lambdaUpdate().set(SysRole::getState, DataStateEnum.DELETED.getCode()).in(SysRole::getId, ids).update();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SysRole> listAllRole() {
|
||||
QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.ne("sys_role.state", DataStateEnum.DELETED.getCode());
|
||||
return this.baseMapper.selectList(queryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SysRole> listRoleByType(Integer type) {
|
||||
return this.lambdaQuery().eq(SysRole::getType, type).eq(SysRole::getState, DataStateEnum.ENABLE.getCode()).list();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SysRole> listRoleByUserId(String userId) {
|
||||
return sysUserRoleService.listRoleByUserId(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SysRole> simpleList() {
|
||||
LambdaQueryWrapper<SysRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
|
||||
// lambdaQueryWrapper.select(SysRole::getId, SysRole::getName).eq(SysRole::getState, DataStateEnum.ENABLE.getCode()).eq(SysRole::getType, RoleConst.TYPE_USER);
|
||||
lambdaQueryWrapper.select(SysRole::getId, SysRole::getName).eq(SysRole::getState, DataStateEnum.ENABLE.getCode());
|
||||
return this.baseMapper.selectList(lambdaQueryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean selectRelevance(List<String> ids) {
|
||||
// 判断角色是否和用户绑定
|
||||
//List<UserRole> userRoleList = this.userRoleMapper.selectUserRole(ids);
|
||||
// 判断角色是否和资源绑定
|
||||
//List<RoleFunction> roleFunctionList = this.roleFunctionMapper.selectRoleFunction(ids);
|
||||
// 判断角色是否和组件绑定
|
||||
//List<RoleComponent> roleComponentList = this.roleComponentMapper.selectRoleComponet(ids);
|
||||
//if (!userRoleList.isEmpty() || !roleComponentList.isEmpty() || !roleFunctionList.isEmpty()) {
|
||||
// return true;
|
||||
//}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验参数,检查是否存在相同编码的角色
|
||||
*/
|
||||
private void checkCode(SysRoleParam roleParam, boolean isExcludeSelf) {
|
||||
LambdaQueryWrapper<SysRole> roleLambdaQueryWrapper = new LambdaQueryWrapper<>();
|
||||
roleLambdaQueryWrapper.eq(SysRole::getName, roleParam.getName()).eq(SysRole::getState, DataStateEnum.ENABLE.getCode());
|
||||
//更新的时候,需排除当前记录
|
||||
if (isExcludeSelf) {
|
||||
if (roleParam instanceof SysRoleParam.UpdateParam) {
|
||||
roleLambdaQueryWrapper.ne(SysRole::getId, ((SysRoleParam.UpdateParam) roleParam).getId());
|
||||
}
|
||||
}
|
||||
int countByAccount = this.count(roleLambdaQueryWrapper);
|
||||
//大于等于1个则表示重复
|
||||
if (countByAccount >= 1) {
|
||||
throw new BusinessException(UserResponseEnum.ROLE_NAME_REPEAT);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.njcn.gather.user.user.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.njcn.gather.user.user.mapper.SysUserRoleMapper;
|
||||
import com.njcn.gather.user.user.pojo.po.SysRole;
|
||||
import com.njcn.gather.user.user.pojo.po.SysUserRole;
|
||||
import com.njcn.gather.user.user.service.ISysUserRoleService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author caozehui
|
||||
* @date 2024-11-12
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements ISysUserRoleService {
|
||||
|
||||
private final SysUserRoleMapper sysUserRoleMapper;
|
||||
|
||||
@Override
|
||||
public List<SysUserRole> listUserRoleByUserId(String userId) {
|
||||
return this.lambdaQuery().eq(SysUserRole::getUserId, userId).list();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addUserRole(String userId, List<String> roleIds) {
|
||||
boolean result = false;
|
||||
if (!CollectionUtil.isEmpty(roleIds)) {
|
||||
SysUserRole userRole = new SysUserRole();
|
||||
userRole.setUserId(userId);
|
||||
roleIds.forEach(id -> {
|
||||
userRole.setRoleId(id);
|
||||
this.save(userRole);
|
||||
});
|
||||
result = true;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateUserRole(String userId, List<String> roleIds) {
|
||||
//删除原有关系
|
||||
List<String> userRoleList = this.lambdaQuery().eq(SysUserRole::getUserId, userId).list().stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
|
||||
LambdaQueryWrapper<SysUserRole> lambdaQuery = Wrappers.lambdaQuery();
|
||||
if (!CollectionUtils.isEmpty(userRoleList)) {
|
||||
lambdaQuery.eq(SysUserRole::getUserId, userId).in(SysUserRole::getRoleId, userRoleList);
|
||||
sysUserRoleMapper.delete(lambdaQuery);
|
||||
}
|
||||
//新增关系
|
||||
roleIds.forEach(role -> {
|
||||
SysUserRole userRole = new SysUserRole();
|
||||
userRole.setUserId(userId);
|
||||
userRole.setRoleId(role);
|
||||
this.save(userRole);
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SysRole> listRoleByUserId(String userId) {
|
||||
return this.baseMapper.getRoleListByUserId(userId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user