<?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.paper.library.common.mapper.SysRoleMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.qmth.paper.library.common.entity.SysRole">
        <result column="id" property="id"/>
        <result column="school_id" property="schoolId"/>
        <result column="name" property="name"/>
        <result column="enable" property="enable"/>
        <result column="type" property="type"/>
        <result column="default_role" property="defaultRole"/>
        <result column="source" property="source"/>
        <result column="interpret" property="interpret"/>
        <result column="create_id" property="createId"/>
        <result column="create_time" property="createTime"/>
        <result column="update_id" property="updateId"/>
        <result column="update_time" property="updateTime"/>
        <result column="default_role" property="defaultRole"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        select id,
               school_id,
               name,
               enable,
               type,
               create_id,
               create_time,
               update_id,
               update_time,
               default_role,
               source,
               interpret
        from sys_role
    </sql>

    <select id="listRolesByUserId" resultMap="BaseResultMap">
        SELECT distinct a.id,
                        a.name,
                        a.type,
                        a.default_role
        FROM sys_role a
                 JOIN
             sys_user_role b ON a.id = b.role_id
        WHERE b.user_id = #{id}
    </select>

    <select id="listPage" resultMap="BaseResultMap">
        select * from(
        <include refid="Base_Column_List"></include>
        <where>
            <if test="schoolId != null">
                and school_id = #{schoolId}
            </if>
            <if test="name != null and name != ''">
                and name like concat('%', #{name}, '%')
            </if>
            <if test="enable != null">
                and enable = #{enable}
            </if>
        </where>
        union
        <include refid="Base_Column_List"></include>
        where school_id is null and default_role = 1 and (type <![CDATA[ <> ]]> 'ADMIN' or type is null)
        ) t
        ORDER BY t.create_time
    </select>

    <select id="listRolesBySchoolId" resultMap="BaseResultMap">
        <include refid="Base_Column_List"/>
        <where>
            enable = 1
            <if test="schoolId != null">
                and school_id = #{schoolId}
            </if>
            <if test="schoolId == null">
                and school_id is null
            </if>
        </where>
        union
        <include refid="Base_Column_List"/>
        where school_id is null and default_role = 1 and (type <![CDATA[ <> ]]> 'ADMIN' or type is null)
    </select>
</mapper>
