<?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.starry.admin.modules.system.mapper.SysRoleMapper">
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.starry.admin.modules.system.entity.SysRoleEntity">
        <id column="role_id" property="roleId"/>
        <result column="role_name" property="roleName"/>
        <result column="description" property="description"/>
        <result column="data_scope" property="dataScope"/>
        <result column="menu_check_strictly" property="menuCheckStrictly"/>
        <result column="dept_check_strictly" property="deptCheckStrictly"/>
        <result column="status" property="status"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="updated_time" property="updatedTime"/>
        <result column="updated_by" property="updatedBy"/>
        <result column="create_time" property="createdTime"/>
        <result column="created_by" property="createdBy"/>
        <result column="deleted" property="deleted"/>
        <result column="version" property="version"/>
    </resultMap>

    <sql id="selectRoleVo">
        select distinct r.role_id,
                        r.role_name,
                        r.role_key,
                        r.data_scope,
                        r.menu_check_strictly,
                        r.dept_check_strictly,
                        r.status,
                        r.created_time,
                        r.description
        from sys_role r
                 left join sys_user_role ur on ur.role_id = r.role_id and ur.deleted = 0
                 left join sys_user u on u.user_id = ur.user_id and u.deleted = 0
    </sql>

    <select id="selectRoleById" parameterType="Long" resultMap="BaseResultMap">
        <include refid="selectRoleVo"/>
        where r.deleted = 0
        and r.role_id = #{roleId}
    </select>

    <select id="checkRoleNameUnique" parameterType="String" resultMap="BaseResultMap">
        <include refid="selectRoleVo"/>
        where r.role_name=#{roleName} and r.deleted = 0 limit 1
    </select>

    <select id="checkRoleKeyUnique" parameterType="String" resultMap="BaseResultMap">
        <include refid="selectRoleVo"/>
        where r.role_key=#{roleKey} and r.deleted = 0 limit 1
    </select>

    <select id="selectRolePermissionByUserId" resultMap="BaseResultMap">
        <include refid="selectRoleVo"/>
        where r.deleted = 0
        and ur.user_id = #{userId}
    </select>

    <select id="selectRoleList" resultMap="BaseResultMap">
        <include refid="selectRoleVo"/>
        where r.deleted = 0
    </select>

    <select id="queryAdminRole" parameterType="String" resultMap="BaseResultMap">
        select distinct r.role_id, r.role_name, r.role_key, r.data_scope, r.menu_check_strictly, r.dept_check_strictly,
        r.status, r.description,tenant_id
        from sys_role r
        where r.tenant_id = #{tenantId} and r.role_key = 'admin' order by role_id desc
    </select>

    <delete id="deleteRoleByTenantId">
        delete from sys_role where tenant_id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="countUserRoleByRoleId" resultType="Integer">
        select count(1) from sys_user_role where role_id=#{roleId}
    </select>
</mapper>
