<?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.mst.ComMenuMapper">

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        menu_uuid
        , cp_id, staff_id, app_code, display_no, op_update_time, op_update_user_id, op_app_code, op_timestamp, op_db_user
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.response.mst.ComMenuResponse">
        <id column="menu_uuid" property="menuUuid" typeHandler="UuidTypeHandler"/>
        <result column="menu_name" property="menuName"/>
        <result column="object_code" property="objectCode"/>
        <result column="object_path" property="objectPath"/>
        <result column="object_icon" property="objectIcon"/>
        <result column="cp_id" property="cpId"/>
        <result column="staff_id" property="staffId" typeHandler="UuidTypeHandler"/>
        <result column="app_code" property="appCode"/>
        <result column="display_no" property="displayNo"/>
        <result column="op_update_time" property="opUpdateTime" typeHandler="TimestampTypeHandler"/>
        <result column="op_update_user_id" property="opUpdateUserId" typeHandler="UuidTypeHandler"/>
        <result column="op_app_code" property="opAppCode"/>
        <result column="op_timestamp" property="opTimestamp" typeHandler="TimestampTypeHandler"/>
        <result column="op_db_user" property="opDbUser"/>
    </resultMap>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="cpId != null">
                AND cp_id = #{cpId}
            </if>
            <if test="staffId != null and staffId != ''">
                AND staff_id = #{staffId}
            </if>
            <if test="appCode != null and appCode != ''">
                AND app_code = #{appCode}
            </if>
        </where>
    </sql>

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

    <!-- 查询表t_mst_com_menu，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMap">
        SELECT
            sys.f_get_name_i18n ( tam.menu_name_i18n, #{i18n} ) menu_name,
            tmcm.menu_uuid,
            tam.object_code,
            tam.object_path,
            CASE WHEN tam.object_icon IS NULL THEN NULL ELSE tam.object_icon || '.png' END AS object_icon
        FROM
            dkic_b.t_mst_com_menu tmcm
                INNER JOIN sys.t_app_menu tam ON tmcm.menu_uuid = tam.menu_uuid
        WHERE
            tmcm.staff_id = #{staffId} ::uuid
          AND tmcm.app_code = #{appCode}
          AND tmcm.cp_id = #{cpId}
        <if test="gradeCode != null and gradeCode != ''">
            AND (tam.grade_codes is null or #{gradeCode} = any(tam.grade_codes))
        </if>
    </select>

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

    <!-- 根据主键查询表t_mst_com_menu的一行数据 -->
    <select id="selectById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM dkic_b.t_mst_com_menu
        WHERE menu_uuid = #{id}::uuid
    </select>

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

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

    <!--删除常用功能-->
    <delete id="deleteMenuByStaff">
        delete from dkic_b.t_mst_com_menu where staff_id = #{staffId} ::uuid
    </delete>

    <!--删除常用功能-->
    <delete id="deleteMenuByCpId">
        delete from dkic_b.t_mst_com_menu where cp_id = #{cpId}
    </delete>

    <!--新建编辑常用功能-->
    <insert id="save">
        insert into dkic_b.t_mst_com_menu
        (
        <trim suffixOverrides=",">
            menu_uuid,
            cp_id,
            staff_id,
            app_code,
            display_no,
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
        (
        <trim suffixOverrides=",">
            #{item.menuUuid} ::uuid,
            #{item.cpId},
            #{item.staffId}  ::uuid,
            #{item.appCode},
            #{item.displayNo},
        </trim>
            )
        </foreach>
        on conflict (menu_uuid,staff_id,app_code) do update set
        cp_id = excluded."cp_id",
        display_no = excluded."display_no";
    </insert>

    <!--新建编辑常用功能(建企业时使用）-->
    <insert id="saveByCompany">
        insert into dkic_b.t_mst_com_menu
        (
        <trim suffixOverrides=",">
            menu_uuid,
            cp_id,
            staff_id,
            app_code,
            display_no,
        </trim>
        )
        select tam.menu_uuid,
               #{cpId},
               #{staffId} ::uuid,
               #{appCode},
               ROW_NUMBER() OVER () AS row_num
        from sys.t_app_menu tam
        where tam.app_code = #{appCode}
        and tam.object_code = any(#{objectCodeList,typeHandler=StringListTypeHandler})
    </insert>

    <insert id="insertBatch">
        insert into dkic_b.t_mst_com_menu
        (
        <trim suffixOverrides=",">
            cp_id,
            staff_id,
            menu_uuid,
            app_code,
            display_no,
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (
            <trim suffixOverrides=",">
                #{item.cpId},
                #{item.staffId}::uuid,
                #{item.menuUuid} ::uuid,
                #{item.appCode},
                #{item.displayNo},
            </trim>
            )
        </foreach>
    </insert>
</mapper>
