<?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.hzs.system.sys.mapper.SysRoleScopeMapper">

    <resultMap type="com.hzs.common.domain.system.sys.SysRoleScope" id="SysRoleDeptResult">
        <result column="ROLE_ID" property="roleId"/>
        <result column="SCOPE_ID" property="scopeId"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="DEL_FLAG" property="delFlag"/>
    </resultMap>

    <!-- 批量删除角色范围关联信息 -->
    <delete id="deleteRoleScope">
        update sys_role_scope
        set del_flag      = 1,
            PK_MODIFIED   = #{userId},
            MODIFIED_TIME = sysdate
        where role_id = #{roleId}
    </delete>

    <!-- 批量新增角色范围信息 -->
    <insert id="batchRoleScope">
        INSERT ALL
        <foreach collection="list" item="item" separator=" ">
            into sys_role_scope(role_id, scope_id, pk_creator)
            VALUES (#{item.roleId}, #{item.scopeId}, #{item.pkCreator})
        </foreach>
        SELECT 1 FROM dual
    </insert>

    <!-- 根据角色查询范围 -->
    <select id="queryScopeByRole" resultMap="SysRoleDeptResult">
        select t.*
        from SYS_ROLE_SCOPE t
        where t.del_flag = 0
          and t.role_id = #{roleId}
        order by t.SCOPE_ID
    </select>

    <!-- 根据管理员查询范围 -->
    <select id="queryScopeByUser" resultType="long">
        select t.SCOPE_ID
        from SYS_ROLE_SCOPE t
        where t.del_flag = 0
          and t.role_id in
              (select sur.role_id from sys_user_role sur where sur.user_id = #{userId})
        order by t.SCOPE_ID
    </select>

</mapper>
