From b0db65a5cb9364f7d6e105470180f73caac3a45b Mon Sep 17 00:00:00 2001 From: caozehui <2427765068@qq.com> Date: Wed, 13 Nov 2024 08:45:49 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=92=E8=89=B2=E5=85=B3=E8=81=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- user/Readme.md | 4 +- .../gather/user/pojo/constant/RoleConst.java | 28 ++++ .../user/pojo/constant/UserValidMessage.java | 14 +- .../user/controller/SysRoleController.java | 154 ++++++++++++++++++ .../user/user/mapper/SysRoleMapper.java | 13 ++ .../user/user/mapper/SysUserRoleMapper.java | 22 +++ .../user/mapper/mapping/SysRoleMapper.xml | 7 + .../user/mapper/mapping/SysUserRoleMapper.xml | 14 ++ .../user/user/pojo/param/SysRoleParam.java | 76 +++++++++ .../gather/user/user/pojo/po/SysRole.java | 50 ++++++ .../gather/user/user/pojo/po/SysUserRole.java | 29 ++++ .../user/user/service/ISysRoleService.java | 83 ++++++++++ .../user/service/ISysUserRoleService.java | 47 ++++++ .../user/service/impl/SysRoleServiceImpl.java | 141 ++++++++++++++++ .../service/impl/SysUserRoleServiceImpl.java | 73 +++++++++ 15 files changed, 753 insertions(+), 2 deletions(-) create mode 100644 user/src/main/java/com/njcn/gather/user/pojo/constant/RoleConst.java create mode 100644 user/src/main/java/com/njcn/gather/user/user/controller/SysRoleController.java create mode 100644 user/src/main/java/com/njcn/gather/user/user/mapper/SysRoleMapper.java create mode 100644 user/src/main/java/com/njcn/gather/user/user/mapper/SysUserRoleMapper.java create mode 100644 user/src/main/java/com/njcn/gather/user/user/mapper/mapping/SysRoleMapper.xml create mode 100644 user/src/main/java/com/njcn/gather/user/user/mapper/mapping/SysUserRoleMapper.xml create mode 100644 user/src/main/java/com/njcn/gather/user/user/pojo/param/SysRoleParam.java create mode 100644 user/src/main/java/com/njcn/gather/user/user/pojo/po/SysRole.java create mode 100644 user/src/main/java/com/njcn/gather/user/user/pojo/po/SysUserRole.java create mode 100644 user/src/main/java/com/njcn/gather/user/user/service/ISysRoleService.java create mode 100644 user/src/main/java/com/njcn/gather/user/user/service/ISysUserRoleService.java create mode 100644 user/src/main/java/com/njcn/gather/user/user/service/impl/SysRoleServiceImpl.java create mode 100644 user/src/main/java/com/njcn/gather/user/user/service/impl/SysUserRoleServiceImpl.java diff --git a/user/Readme.md b/user/Readme.md index bb53050f..64d9f9ec 100644 --- a/user/Readme.md +++ b/user/Readme.md @@ -1,6 +1,8 @@ #### 简介 用户模块主要包含以下功能: -* 用户管理、角色管理 +* 用户管理 +* 角色管理 +* 资源管理 * 部门管理 * 职位管理(非必须) * 菜单资源管理 diff --git a/user/src/main/java/com/njcn/gather/user/pojo/constant/RoleConst.java b/user/src/main/java/com/njcn/gather/user/pojo/constant/RoleConst.java new file mode 100644 index 00000000..29c567ec --- /dev/null +++ b/user/src/main/java/com/njcn/gather/user/pojo/constant/RoleConst.java @@ -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; + +} diff --git a/user/src/main/java/com/njcn/gather/user/pojo/constant/UserValidMessage.java b/user/src/main/java/com/njcn/gather/user/pojo/constant/UserValidMessage.java index 7ba27131..11e9c403 100644 --- a/user/src/main/java/com/njcn/gather/user/pojo/constant/UserValidMessage.java +++ b/user/src/main/java/com/njcn/gather/user/pojo/constant/UserValidMessage.java @@ -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 = "参数值非法"; } diff --git a/user/src/main/java/com/njcn/gather/user/user/controller/SysRoleController.java b/user/src/main/java/com/njcn/gather/user/user/controller/SysRoleController.java new file mode 100644 index 00000000..ba73b9ce --- /dev/null +++ b/user/src/main/java/com/njcn/gather/user/user/controller/SysRoleController.java @@ -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> list(@RequestBody @Validated SysRoleParam.QueryParam queryParam) { + String methodDescribe = getMethodDescribe("list"); + LogUtil.njcnDebug(log, "{},查询数据为:{}", methodDescribe, queryParam); + Page 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 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 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 delete(@RequestBody List 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> listRoleByIds(@RequestBody List ids) { + String methodDescribe = getMethodDescribe("listRoleByIds"); + List users = sysRoleService.list((new LambdaQueryWrapper().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 selectFunctionComponent(@RequestBody List 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> listRoleByType(@RequestParam("type") Integer type) { + String methodDescribe = getMethodDescribe("listRoleByType"); + LogUtil.njcnDebug(log, "{},查询数据为:{}", methodDescribe, type); + List result = sysRoleService.listRoleByType(type); + return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe); + } + + @OperateInfo(info = LogEnum.SYSTEM_COMMON) + @GetMapping("/listAllRole") + @ApiOperation("查询所有角色") + public HttpResult> listAllRole() { + String methodDescribe = getMethodDescribe("listAllRole"); + List result = sysRoleService.listAllRole(); + return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe); + } + + @OperateInfo(info = LogEnum.SYSTEM_COMMON) + @GetMapping("/simpleList") + @ApiOperation("查询所有角色作为下拉框") + public HttpResult> simpleList() { + String methodDescribe = getMethodDescribe("simpleList"); + List result = sysRoleService.simpleList(); + return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe); + } + +} + diff --git a/user/src/main/java/com/njcn/gather/user/user/mapper/SysRoleMapper.java b/user/src/main/java/com/njcn/gather/user/user/mapper/SysRoleMapper.java new file mode 100644 index 00000000..3a4462f0 --- /dev/null +++ b/user/src/main/java/com/njcn/gather/user/user/mapper/SysRoleMapper.java @@ -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 { + +} + diff --git a/user/src/main/java/com/njcn/gather/user/user/mapper/SysUserRoleMapper.java b/user/src/main/java/com/njcn/gather/user/user/mapper/SysUserRoleMapper.java new file mode 100644 index 00000000..411c500a --- /dev/null +++ b/user/src/main/java/com/njcn/gather/user/user/mapper/SysUserRoleMapper.java @@ -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 { + /** + * 根据用户id获取角色详情 + * + * @param userId 用户id + * @return 角色结果集 + */ + List getRoleListByUserId(String userId); +} + diff --git a/user/src/main/java/com/njcn/gather/user/user/mapper/mapping/SysRoleMapper.xml b/user/src/main/java/com/njcn/gather/user/user/mapper/mapping/SysRoleMapper.xml new file mode 100644 index 00000000..e6f64ef1 --- /dev/null +++ b/user/src/main/java/com/njcn/gather/user/user/mapper/mapping/SysRoleMapper.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/user/src/main/java/com/njcn/gather/user/user/mapper/mapping/SysUserRoleMapper.xml b/user/src/main/java/com/njcn/gather/user/user/mapper/mapping/SysUserRoleMapper.xml new file mode 100644 index 00000000..c9a183b2 --- /dev/null +++ b/user/src/main/java/com/njcn/gather/user/user/mapper/mapping/SysUserRoleMapper.xml @@ -0,0 +1,14 @@ + + + + + + + + diff --git a/user/src/main/java/com/njcn/gather/user/user/pojo/param/SysRoleParam.java b/user/src/main/java/com/njcn/gather/user/user/pojo/param/SysRoleParam.java new file mode 100644 index 00000000..e5ad836e --- /dev/null +++ b/user/src/main/java/com/njcn/gather/user/user/pojo/param/SysRoleParam.java @@ -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 functionIds; + } +} diff --git a/user/src/main/java/com/njcn/gather/user/user/pojo/po/SysRole.java b/user/src/main/java/com/njcn/gather/user/user/pojo/po/SysRole.java new file mode 100644 index 00000000..643b1f5a --- /dev/null +++ b/user/src/main/java/com/njcn/gather/user/user/pojo/po/SysRole.java @@ -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; +} + diff --git a/user/src/main/java/com/njcn/gather/user/user/pojo/po/SysUserRole.java b/user/src/main/java/com/njcn/gather/user/user/pojo/po/SysUserRole.java new file mode 100644 index 00000000..76c9a23d --- /dev/null +++ b/user/src/main/java/com/njcn/gather/user/user/pojo/po/SysUserRole.java @@ -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; + +} + diff --git a/user/src/main/java/com/njcn/gather/user/user/service/ISysRoleService.java b/user/src/main/java/com/njcn/gather/user/user/service/ISysRoleService.java new file mode 100644 index 00000000..8bbb8116 --- /dev/null +++ b/user/src/main/java/com/njcn/gather/user/user/service/ISysRoleService.java @@ -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 { + + /** + * 分页查询角色列表 + * + * @param queryParam 查询参数 + */ + Page 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 ids); + + /** + * 查询所有角色列表 + * + * @return 角色列表 + */ + List listAllRole(); + + /** + * 根据权限类型type获取角色列表 + * + * @param type 角色类型 + * @return 角色列表 + */ + List listRoleByType(Integer type); + + /** + * 根据用户id获取角色 + * + * @param userId 用户id + * @return 角色列表 + */ + List listRoleByUserId(String userId); + + /** + * 查询所有角色作为下拉框 + * + * @return 角色列表 + */ + List simpleList(); + + /** + * 判断角色id是否有相关联的资源和组件 + * + * @param ids 角色id + */ + boolean selectRelevance(List ids); +} diff --git a/user/src/main/java/com/njcn/gather/user/user/service/ISysUserRoleService.java b/user/src/main/java/com/njcn/gather/user/user/service/ISysUserRoleService.java new file mode 100644 index 00000000..d7e458f7 --- /dev/null +++ b/user/src/main/java/com/njcn/gather/user/user/service/ISysUserRoleService.java @@ -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 { + /** + * 根据用户id获取 用户--角色关系数据 + * + * @param userId 用户ID + * @return 关系数据 + */ + List listUserRoleByUserId(String userId); + + /** + * 新增用户和角色的关系 + * + * @param userId 用户id + * @param roleIds 角色id + * @return boolean + */ + boolean addUserRole(String userId, List roleIds); + + /** + * 修改用户和角色的关系 + * + * @param userId 用户id + * @param roleIds 角色id + * @return boolean + */ + boolean updateUserRole(String userId, List roleIds); + + /** + * 根据用户id获取角色详情 + * + * @param userId 用户id + * @return 角色信息 + */ + List listRoleByUserId(String userId); +} diff --git a/user/src/main/java/com/njcn/gather/user/user/service/impl/SysRoleServiceImpl.java b/user/src/main/java/com/njcn/gather/user/user/service/impl/SysRoleServiceImpl.java new file mode 100644 index 00000000..28e6a616 --- /dev/null +++ b/user/src/main/java/com/njcn/gather/user/user/service/impl/SysRoleServiceImpl.java @@ -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 implements ISysRoleService { + + private final ISysUserRoleService sysUserRoleService; + + @Override + public Page listRole(SysRoleParam.QueryParam queryParam) { + QueryWrapper 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 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 listAllRole() { + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.ne("sys_role.state", DataStateEnum.DELETED.getCode()); + return this.baseMapper.selectList(queryWrapper); + } + + @Override + public List listRoleByType(Integer type) { + return this.lambdaQuery().eq(SysRole::getType, type).eq(SysRole::getState, DataStateEnum.ENABLE.getCode()).list(); + } + + @Override + public List listRoleByUserId(String userId) { + return sysUserRoleService.listRoleByUserId(userId); + } + + @Override + public List simpleList() { + LambdaQueryWrapper 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 ids) { + // 判断角色是否和用户绑定 + //List userRoleList = this.userRoleMapper.selectUserRole(ids); + // 判断角色是否和资源绑定 + //List roleFunctionList = this.roleFunctionMapper.selectRoleFunction(ids); + // 判断角色是否和组件绑定 + //List roleComponentList = this.roleComponentMapper.selectRoleComponet(ids); + //if (!userRoleList.isEmpty() || !roleComponentList.isEmpty() || !roleFunctionList.isEmpty()) { + // return true; + //} + return false; + } + + /** + * 校验参数,检查是否存在相同编码的角色 + */ + private void checkCode(SysRoleParam roleParam, boolean isExcludeSelf) { + LambdaQueryWrapper 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); + } + } +} diff --git a/user/src/main/java/com/njcn/gather/user/user/service/impl/SysUserRoleServiceImpl.java b/user/src/main/java/com/njcn/gather/user/user/service/impl/SysUserRoleServiceImpl.java new file mode 100644 index 00000000..6900a59a --- /dev/null +++ b/user/src/main/java/com/njcn/gather/user/user/service/impl/SysUserRoleServiceImpl.java @@ -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 implements ISysUserRoleService { + + private final SysUserRoleMapper sysUserRoleMapper; + + @Override + public List listUserRoleByUserId(String userId) { + return this.lambdaQuery().eq(SysUserRole::getUserId, userId).list(); + } + + @Override + public boolean addUserRole(String userId, List 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 roleIds) { + //删除原有关系 + List userRoleList = this.lambdaQuery().eq(SysUserRole::getUserId, userId).list().stream().map(SysUserRole::getRoleId).collect(Collectors.toList()); + LambdaQueryWrapper 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 listRoleByUserId(String userId) { + return this.baseMapper.getRoleListByUserId(userId); + } +}