<?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.qmth.teachcloud.common.mapper.SysUserRoleMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.qmth.teachcloud.common.entity.SysUserRole">
        <result column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="role_id" property="roleId"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        select id, user_id, role_id
        from sys_user_role
    </sql>
    <select id="listRoleByUserId" resultMap="com.qmth.teachcloud.common.mapper.SysRoleMapper.BaseResultMap">
        SELECT *
        FROM sys_role a
        WHERE EXISTS(SELECT 1
                     FROM sys_user_role b
                     WHERE a.id = b.role_id
                       AND b.user_id = #{userId})
    </select>

    <select id="listByUserId" resultType="com.qmth.teachcloud.common.bean.dto.MenuDto">
        SELECT b.id,
               b.name,
               b.url,
               b.type,
               b.parent_id as parentId,
               b.school_id as schoolId,
               b.`sequence`,
               b.enable
        FROM sys_user_role a
                 join sys_role_privilege srp on srp.role_id = a.role_id
                 JOIN sys_privilege b ON srp.privilege_id = b.id
        WHERE a.user_id = #{userId}
          and b.school_id = #{schoolId}
    </select>

    <select id="listAll" resultType="com.qmth.teachcloud.common.bean.dto.MenuDto">
        select b.id,
               b.name,
               b.url,
               b.type,
               b.parent_id as parentId,
               b.school_id as schoolId,
               b.`sequence`,
               b.enable
        from sys_privilege b
        where b.school_id = #{schoolId}
    </select>

    <select id="listByRoleId" resultType="com.qmth.teachcloud.common.entity.SysUserRole">
        select
        *
        from
        sys_user_role sur
        join sys_user su on
        su.id = sur.user_id
        <where>
            <if test="roleId != null and roleId != ''">
                and sur.role_id = #{roleId}
            </if>
            <if test="schoolId != null and schoolId != ''">
                and su.school_id = #{schoolId}
            </if>
        </where>
    </select>

    <select id="listByRoleIdsAndSchoolId" resultType="com.qmth.teachcloud.common.entity.SysUserRole">
        select
        *
        from
        sys_user_role sur
        join sys_user su on
        su.id = sur.user_id
        <where>
            <if test="ids != null and ids != '' and ids.size > 0">
                and sur.role_id in
                <foreach collection="ids" item="id" index="index" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="schoolId != null and schoolId != ''">
                and su.school_id = #{schoolId}
            </if>
        </where>
    </select>

</mapper>
