<?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.dk.mdm.mapper.core.RoleRightMapper">

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        role_id
        , fun_uuid, app_code, fty_id, op_create_time, op_create_user_id
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.core.RoleRight">
        <id column="role_id" property="roleId"/>
        <result column="fun_uuid" property="funUuid"
                typeHandler="com.dk.common.infrastructure.handler.UuidTypeHandler"/>
        <result column="app_code" property="appCode"/>
        <result column="fty_id" property="ftyId"/>
        <result column="op_create_time" property="opCreateTime"
                typeHandler="com.dk.common.infrastructure.handler.TimestampTypeHandler"/>
        <result column="op_create_user_id" property="opCreateUserId"/>
    </resultMap>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="funUuid != null and funUuid != ''">
                AND fun_uuid = #{funUuid}
            </if>
            <if test="appCode != null and appCode != ''">
                AND app_code = #{appCode}
            </if>
            <if test="ftyId != null">
                AND fty_id = #{ftyId}
            </if>
            <if test="opCreateTime != null">
                AND op_create_time = #{opCreateTime}
            </if>
            <if test="opCreateUserId != null">
                AND op_create_user_id = #{opCreateUserId}
            </if>
        </where>
    </sql>

    <sql id="idsForeach">
        <!-- 根据主键roleId批量操作 -->
        WHERE role_id in
        <foreach collection="ids" index="index" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </sql>

    <!-- 查询表t_role_right，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM core.t_role_right
        <include refid="Condition"/>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!-- 查询表t_role_right，（条件查询）个数 -->
    <select id="countByCond" resultType="Long">
        SELECT
        count(1)
        FROM core.t_role_right
        <include refid="Condition"/>
    </select>


    <!-- 根据主键锁定表t_role_right的一行数据 -->
    <select id="selectByIdForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM core.t_role_right
        WHERE role_id = #{roleId}
        for update
    </select>

    <!-- 根据主键锁定表t_role_right的多行数据 -->
    <select id="selectByIdsForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM core.t_role_right
        <include refid="idsForeach"/>
        for update
    </select>

    <insert id="insertBatch">
        insert into core.t_role_right
        (
        <trim suffixOverrides=",">
            fun_uuid,
            app_code,
            fty_id,
            role_id,
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (
            <trim suffixOverrides=",">
                #{item.funUuid}::uuid,
                #{item.appCode},
                #{item.ftyId},
                #{item.roleId},
            </trim>
            )
        </foreach>
    </insert>

    <!-- 获取功能权限 -->
    <select id="getRoleRight" resultType="java.util.Map">
        select menu.menu_uuid    as "menuUuid",
               menu.menu_uuid    as "id",
               menu.menu_name    as "menuName",
               menu.app_code     as "appCode",
               menu.parent_uuid  as "parentId",
               menu.flg_menu     as "flgMenu",
               roleRight.role_id as "roleId",
               menu.fun_uuid     as "funUuid",
               case
                   when roleRight.role_id is null then false
                   else true end    "checked"
        from sys.t_app_menu menu
                 left join core.t_role_right roleRight
                           on menu.fun_uuid = roleRight.fun_uuid
                               and roleRight.role_id = #{roleId}
                               and roleRight.fty_id = #{ftyId}
        where menu.flg_valid
          and menu.flg_right
          and menu.object_code !='home'
        order by menu.level_code
    </select>
    <!--清空角色权限-->
    <delete id="deleteRoleRight">
        delete
        from core.t_role_right
        where role_id = #{roleId}
          and fty_id = #{ftyId}
    </delete>
    <!--分配用户权限-->
    <select id="userFunByRole" resultType="Boolean">
        select core.f_reset_user_fun_by_role(#{roleId})
    </select>

    <!-- 获取当前的功能权限 -->
    <select id="getRoleRightCurrent" resultType="java.util.Map">
        select
               menu.menu_name    as "menuName",
               menu.fun_uuid     as "funUuid"
        from sys.t_app_menu menu
                 right join core.t_role_right roleRight
                           on menu.fun_uuid = roleRight.fun_uuid
                               and roleRight.role_id = #{roleId}
                               and roleRight.fty_id = #{ftyId}
        where menu.flg_valid
          and menu.flg_right
          and menu.object_code !='home'
        order by menu.level_code
    </select>
</mapper>
