<?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.xhkjedu.suser.mapper.system.UserRoleMapper">
    <delete id="deleteByUserid">
        delete from t_user_role where userid=#{userid}
    </delete>

    <!-- 根据用户id获取关联角色 -->
    <select id="listRolesByUserid" resultType="com.xhkjedu.suser.vo.system.UserRoleVo">
        SELECT ur.roleid,r.rolename
        FROM t_user_role ur LEFT JOIN t_role r ON ur.roleid=r.roleid
        WHERE ur.userid=#{userid}
    </select>

    <!-- 获取角色下用户 -->
    <select id="listUsersByRoleid" resultType="com.xhkjedu.suser.model.system.TUserRole">
        SELECT ur.userid,u.username FROM t_user_role ur
        LEFT JOIN t_user u ON ur.userid=u.userid WHERE ur.roleid=#{roleid} and u.userstate=1
    </select>

    <!--获取有常用模块的用户id集合-->
    <select id="listUserId" resultType="java.lang.Integer">
        SELECT um.userid FROM t_user_module um LEFT JOIN t_role_module rm ON um.moduleid=rm.moduleid
        WHERE rm.roleid=#{roleid} GROUP BY um.userid
    </select>
    <!--根据用户id删除没有权限的常用模块-->
    <delete id="deleteNoRoleByUserId">
        DELETE FROM t_user_module WHERE userid=#{userid} AND moduleid
        NOT IN(SELECT rm.moduleid FROM t_user u LEFT JOIN t_user_role ur ON u.userid=ur.userid
        LEFT JOIN t_role_module rm ON ur.roleid=rm.roleid
        WHERE u.userid=#{userid} GROUP BY rm.moduleid)
    </delete>
</mapper>
