<?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="org.dromara.member.mapper.AppRoleMapper">

    <resultMap type="org.dromara.member.domain.AppRole" id="AppRoleResult">
        <id property="roleId" column="role_id"/>
        <result property="roleName" column="role_name"/>
        <result property="roleKey" column="role_key"/>
        <result property="roleSort" column="role_sort"/>
        <result property="status" column="status"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <resultMap type="org.dromara.member.domain.vo.AppRoleVo" id="AppRoleVoResult">
        <id property="roleId" column="role_id"/>
        <result property="roleName" column="role_name"/>
        <result property="roleKey" column="role_key"/>
        <result property="roleSort" column="role_sort"/>
        <result property="status" column="status"/>
        <result property="createTime" column="create_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectAppRoleVo">
        select distinct r.role_id,
                        r.role_name,
                        r.role_key,
                        r.role_sort,
                        r.status,
                        r.del_flag,
                        r.create_time,
                        r.remark
        from app_role r
                 left join app_user_role ur on ur.role_id = r.role_id
                 left join app_user u on u.user_id = ur.user_id
    </sql>

    <select id="selectRolesByUserId" parameterType="Long" resultMap="AppRoleVoResult">
        <include refid="selectAppRoleVo"/>
        WHERE r.del_flag = '0' and ur.user_id = #{userId}
    </select>

    <select id="selectRolePermissionByUserId" parameterType="Long" resultType="String">
        select distinct r.role_key
        from app_role r
                 left join app_user_role ur on ur.role_id = r.role_id
        WHERE r.del_flag = '0'
          and ur.user_id = #{userId}
    </select>

    <select id="selectRoleAll" resultMap="AppRoleVoResult">
        <include refid="selectAppRoleVo"/>
        where r.del_flag = '0'
    </select>

    <select id="selectRoleListByUserId" parameterType="Long" resultType="Long">
        select r.role_id
        from app_role r
                 left join app_user_role ur on ur.role_id = r.role_id
                 left join app_user u on u.user_id = ur.user_id
        where u.user_id = #{userId}
    </select>

    <select id="checkRoleNameUnique" parameterType="String" resultType="int">
        select count(1)
        from app_role r
        where r.role_name = #{roleName}
          and r.del_flag = '0'
    </select>

    <select id="checkRoleKeyUnique" parameterType="String" resultType="int">
        select count(1)
        from app_role r
        where r.role_key = #{roleKey}
          and r.del_flag = '0'
    </select>

</mapper>