Browse Source

用户列表添加角色

24282 1 năm trước cách đây
mục cha
commit
8caf291b61

+ 14 - 0
ruoyi-common/src/main/java/com/ruoyi/common/core/domain/entity/SysRole.java

@@ -108,6 +108,12 @@ public class SysRole extends BaseEntity {
     @TableField(exist = false)
     private String keyword;
 
+    /**
+     * 用户id
+     */
+    @TableField(exist = false)
+    private Long userId;
+
     public SysRole() {
 
     }
@@ -249,6 +255,14 @@ public class SysRole extends BaseEntity {
         this.keyword = keyword;
     }
 
+    public Long getUserId() {
+        return userId;
+    }
+
+    public void setUserId(Long userId) {
+        this.userId = userId;
+    }
+
     @Override
     public String toString() {
         return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)

+ 11 - 0
ruoyi-common/src/main/java/com/ruoyi/common/core/domain/entity/SysUser.java

@@ -165,6 +165,9 @@ public class SysUser extends BaseEntity {
     @TableField(exist = false)
     private String keyword;
 
+    @TableField(exist = false)
+    private List<SysRole> sysRoleList;
+
     public SysUser() {
 
     }
@@ -382,6 +385,14 @@ public class SysUser extends BaseEntity {
         this.ddUserId = ddUserId;
     }
 
+    public List<SysRole> getSysRoleList() {
+        return sysRoleList;
+    }
+
+    public void setSysRoleList(List<SysRole> sysRoleList) {
+        this.sysRoleList = sysRoleList;
+    }
+
     @Override
     public String toString() {
         return "SysUser{" +

+ 3 - 0
ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserMapper.java

@@ -2,6 +2,7 @@ package com.ruoyi.system.mapper;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.ruoyi.common.annotation.TenantIgnore;
+import com.ruoyi.common.core.domain.entity.SysRole;
 import com.ruoyi.common.core.domain.entity.SysUser;
 import org.apache.ibatis.annotations.Param;
 
@@ -132,4 +133,6 @@ public interface SysUserMapper extends BaseMapper<SysUser> {
 
     List<SysUser> getListByRoleId(Long roleId);
 
+    List<SysRole> getRoleByUserIdList(@Param("userIdList") List<Long> userIdList);
+
 }

+ 16 - 1
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java

@@ -71,7 +71,22 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
     @Override
     @DataScope(deptAlias = "d", userAlias = "u")
     public List<SysUser> selectUserList(SysUser user) {
-        return userMapper.selectUserList(user);
+        List<SysUser> sysUserList = userMapper.selectUserList(user);
+
+        // 赋值角色
+        if (sysUserList.size() == 0) {
+            return sysUserList;
+        }
+        List<Long> userIdList = sysUserList.stream().map(SysUser::getUserId).collect(Collectors.toList());
+        List<SysRole> roleList = baseMapper.getRoleByUserIdList(userIdList);
+
+        Map<Long, List<SysRole>> map = roleList.stream().collect(Collectors.groupingBy(SysRole::getUserId));
+        for (SysUser sysUser : sysUserList) {
+            List<SysRole> sysRoleList = map.getOrDefault(sysUser.getUserId(), Collections.emptyList());
+            sysUser.setSysRoleList(sysRoleList);
+        }
+
+        return sysUserList;
     }
 
     /**

+ 96 - 86
ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml

@@ -223,94 +223,104 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 		limit 1
 	</select>
 	<select id="getListByPostId" resultType="com.ruoyi.common.core.domain.entity.SysUser">
-		select u.user_id,
-			   u.dept_id,
-			   u.user_name,
-			   u.nick_name,
-			   u.email,
-			   u.avatar,
-			   u.phonenumber,
-			   u.password,
-			   u.sex,
-			   u.status,
-			   u.del_flag,
-			   u.login_ip,
-			   u.login_date,
-			   u.create_by,
-			   u.create_time,
-			   u.remark,
-			   u.tenant_id,
-			   u.user_code
-		from sys_user_post sup
-				 inner join sys_user u on sup.user_id = u.user_id
-		where sup.post_id = #{postId}
-	</select>
+        select u.user_id,
+               u.dept_id,
+               u.user_name,
+               u.nick_name,
+               u.email,
+               u.avatar,
+               u.phonenumber,
+               u.password,
+               u.sex,
+               u.status,
+               u.del_flag,
+               u.login_ip,
+               u.login_date,
+               u.create_by,
+               u.create_time,
+               u.remark,
+               u.tenant_id,
+               u.user_code
+        from sys_user_post sup
+                 inner join sys_user u on sup.user_id = u.user_id
+        where sup.post_id = #{postId}
+    </select>
 
-	<select id="getListByRoleId" resultType="com.ruoyi.common.core.domain.entity.SysUser">
-		select u.user_id,
-			   u.dept_id,
-			   u.user_name,
-			   u.nick_name,
-			   u.email,
-			   u.avatar,
-			   u.phonenumber,
-			   u.password,
-			   u.sex,
-			   u.status,
-			   u.del_flag,
-			   u.login_ip,
-			   u.login_date,
-			   u.create_by,
-			   u.create_time,
-			   u.remark,
-			   u.tenant_id,
-			   u.user_code
-		from sys_user_role sur
-				 inner join sys_user u on sur.user_id = u.user_id
-		where sur.role_id = #{roleId}
-	</select>
+    <select id="getListByRoleId" resultType="com.ruoyi.common.core.domain.entity.SysUser">
+        select u.user_id,
+               u.dept_id,
+               u.user_name,
+               u.nick_name,
+               u.email,
+               u.avatar,
+               u.phonenumber,
+               u.password,
+               u.sex,
+               u.status,
+               u.del_flag,
+               u.login_ip,
+               u.login_date,
+               u.create_by,
+               u.create_time,
+               u.remark,
+               u.tenant_id,
+               u.user_code
+        from sys_user_role sur
+                 inner join sys_user u on sur.user_id = u.user_id
+        where sur.role_id = #{roleId}
+    </select>
+    <select id="getRoleByUserIdList" resultType="com.ruoyi.common.core.domain.entity.SysRole">
+        select sr.role_id, sr.role_name, sur.user_id
+        from sys_user_role sur
+        left join sys_role sr on sur.role_id = sr.role_id
+        where sr.status = 0
+        and sur.user_id in
+        <foreach collection="userIdList" item="userId" open="(" separator="," close=")">
+            #{userId}
+        </foreach>
+    </select>
 
-	<insert id="insertUser" parameterType="SysUser" useGeneratedKeys="true" keyProperty="userId">
-		insert into sys_user(
-		<if test="userId != null and userId != 0">user_id,</if>
-		<if test="deptId != null and deptId != 0">dept_id,</if>
-		<if test="userName != null and userName != ''">user_name,</if>
-		<if test="nickName != null and nickName != ''">nick_name,</if>
-		<if test="email != null and email != ''">email,</if>
-		<if test="avatar != null and avatar != ''">avatar,</if>
-		<if test="phonenumber != null and phonenumber != ''">phonenumber,</if>
-		<if test="sex != null and sex != ''">sex,</if>
-		<if test="password != null and password != ''">password,</if>
-		<if test="status != null and status != ''">status,</if>
-		<if test="createBy != null and createBy != ''">create_by,</if>
-		<if test="remark != null and remark != ''">remark,</if>
-		<if test="tenantId != null and tenantId != ''">tenant_id,</if>
-		<if test="userType != null and userType != ''">user_type,</if>
-		<if test="jobNumber != null and jobNumber != ''">job_number,</if>
-		<if test="userCode != null and userCode != ''">user_code,</if>
-		create_time
-		)values(
-		<if test="userId != null and userId != ''">#{userId},</if>
-		<if test="deptId != null and deptId != ''">#{deptId},</if>
-		<if test="userName != null and userName != ''">#{userName},</if>
-		<if test="nickName != null and nickName != ''">#{nickName},</if>
-		<if test="email != null and email != ''">#{email},</if>
-		<if test="avatar != null and avatar != ''">#{avatar},</if>
-		<if test="phonenumber != null and phonenumber != ''">#{phonenumber},</if>
-		<if test="sex != null and sex != ''">#{sex},</if>
-		<if test="password != null and password != ''">#{password},</if>
-		<if test="status != null and status != ''">#{status},</if>
-		<if test="createBy != null and createBy != ''">#{createBy},</if>
-		<if test="remark != null and remark != ''">#{remark},</if>
-		<if test="tenantId != null and tenantId != ''">#{tenantId},</if>
-		<if test="userType != null and userType != ''">#{userType},</if>
-		<if test="jobNumber != null and jobNumber != ''">#{jobNumber},</if>
-		<if test="userCode != null and userCode != ''">#{userCode},</if>
-		sysdate()
-		)
-	</insert>
-	
-	<update id="updateUser" parameterType="SysUser">
+    <insert id="insertUser" parameterType="SysUser" useGeneratedKeys="true" keyProperty="userId">
+        insert into sys_user(
+        <if test="userId != null and userId != 0">user_id,</if>
+        <if test="deptId != null and deptId != 0">dept_id,</if>
+        <if test="userName != null and userName != ''">user_name,</if>
+        <if test="nickName != null and nickName != ''">nick_name,</if>
+        <if test="email != null and email != ''">email,</if>
+        <if test="avatar != null and avatar != ''">avatar,</if>
+        <if test="phonenumber != null and phonenumber != ''">phonenumber,</if>
+        <if test="sex != null and sex != ''">sex,</if>
+        <if test="password != null and password != ''">password,</if>
+        <if test="status != null and status != ''">status,</if>
+        <if test="createBy != null and createBy != ''">create_by,</if>
+        <if test="remark != null and remark != ''">remark,</if>
+        <if test="tenantId != null and tenantId != ''">tenant_id,</if>
+        <if test="userType != null and userType != ''">user_type,</if>
+        <if test="jobNumber != null and jobNumber != ''">job_number,</if>
+        <if test="userCode != null and userCode != ''">user_code,</if>
+        create_time
+        )values(
+        <if test="userId != null and userId != ''">#{userId},</if>
+        <if test="deptId != null and deptId != ''">#{deptId},</if>
+        <if test="userName != null and userName != ''">#{userName},</if>
+        <if test="nickName != null and nickName != ''">#{nickName},</if>
+        <if test="email != null and email != ''">#{email},</if>
+        <if test="avatar != null and avatar != ''">#{avatar},</if>
+        <if test="phonenumber != null and phonenumber != ''">#{phonenumber},</if>
+        <if test="sex != null and sex != ''">#{sex},</if>
+        <if test="password != null and password != ''">#{password},</if>
+        <if test="status != null and status != ''">#{status},</if>
+        <if test="createBy != null and createBy != ''">#{createBy},</if>
+        <if test="remark != null and remark != ''">#{remark},</if>
+        <if test="tenantId != null and tenantId != ''">#{tenantId},</if>
+        <if test="userType != null and userType != ''">#{userType},</if>
+        <if test="jobNumber != null and jobNumber != ''">#{jobNumber},</if>
+        <if test="userCode != null and userCode != ''">#{userCode},</if>
+        sysdate()
+        )
+    </insert>
+
+    <update id="updateUser" parameterType="SysUser">
         update sys_user
         <set>
             <if test="deptId != null and deptId != 0">dept_id = #{deptId},</if>