<?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.sys.PageItemMapper">

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        item_uuid
        , sys.f_get_name_i18n_lang(
        item_name_i18n,,#{i18n}
        )
        as
        itemName,
        item_type,
        group_type,
        parent_uuid,
        display_no,
        doc_code,
        page_code,
        page_codes,
        dt_col_name,
        flg_valid,
        remarks,
        flg_sensitive,
        flg_approval,
        flg_print_item,
        flg_batch_update,
        flg_display,
        flg_can_hidden,
        object_name,
        item_tags
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.common.model.pojo.system.PageItem">
        <id column="item_uuid" property="itemUuid"/>
        <result column="item_type" property="itemType"/>
        <result column="group_type" property="groupType"/>
        <result column="parent_uuid" property="parentUuid"
                typeHandler="UuidTypeHandler"/>
        <result column="display_no" property="displayNo"/>
        <result column="doc_code" property="docCode"/>
        <result column="page_code" property="pageCode"/>
        <result column="page_codes" property="pageCodes"/>
        <result column="dt_col_name" property="dtColName"/>
        <result column="flg_valid" property="flgValid"/>
        <result column="remarks" property="remarks"/>
        <result column="flg_sensitive" property="flgSensitive"/>
        <result column="flg_print_item" property="flgPrintItem"/>
        <result column="flg_batch_update" property="flgBatchUpdate"/>
        <result column="flg_display" property="flgDisplay"/>
        <result column="flg_can_hidden" property="flgCanHidden"/>
        <result column="object_name" property="objectName"/>
        <result column="item_tags" property="itemTags" typeHandler="JsonTypeHandler"/>
    </resultMap>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="itemNameI18n != null and itemNameI18n != ''">
                AND item_name_i18n = #{itemNameI18n}
            </if>
            <if test="itemType != null">
                AND item_type = #{itemType}
            </if>
            <if test="groupType != null">
                AND group_type = #{groupType}
            </if>
            <if test="parentUuid != null and parentUuid != ''">
                AND parent_uuid = #{parentUuid}
            </if>
            <if test="displayNo != null">
                AND display_no = #{displayNo}
            </if>
            <if test="docCode != null and docCode != ''">
                AND doc_code = #{docCode}
            </if>
            <if test="pageCode != null and pageCode != ''">
                AND page_code = #{pageCode}
            </if>
            <if test="pageCodes != null and pageCodes != ''">
                AND page_codes = #{pageCodes}
            </if>
            <if test="dtColName != null and dtColName != ''">
                AND dt_col_name = #{dtColName}
            </if>
            <if test="flgValid != null">
                AND flg_valid = #{flgValid}
            </if>
            <if test="remarks != null and remarks != ''">
                AND remarks = #{remarks}
            </if>
            <if test="flgSensitive != null">
                AND flg_sensitive = #{flgSensitive}
            </if>
            <if test="flgApproval != null">
                AND flg_approval = #{flgApproval}
            </if>
            <if test="flgPrintItem != null">
                AND flg_print_item = #{flgPrintItem}
            </if>
            <if test="flgBatchUpdate != null">
                AND flg_batch_update = #{flgBatchUpdate}
            </if>
            <if test="flgDisplay != null">
                AND flg_display = #{flgDisplay}
            </if>
            <if test="flgCanHidden != null">
                AND flg_can_hidden = #{flgCanHidden}
            </if>
        </where>
    </sql>

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

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

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

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

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

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

    <insert id="insertBatch">
        insert into t_s_page_item
        (
        <trim suffixOverrides=",">
            item_name_i18n,
            item_type,
            group_type,
            parent_uuid,
            display_no,
            doc_code,
            page_code,
            page_codes,
            dt_col_name,
            remarks,
            flg_sensitive,
            flg_print_item,
            flg_batch_update,
            flg_display,
            flg_can_hidden,
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (
            <trim suffixOverrides=",">
                #{item.itemNameI18n},
                #{item.itemType},
                #{item.groupType},
                #{item.parentUuid}::uuid,
                #{item.displayNo},
                #{item.docCode},
                #{item.pageCode},
                #{item.pageCodes},
                #{item.dtColName},
                #{item.remarks},
                #{item.flgSensitive},
                #{item.flgPrintItem},
                #{item.flgBatchUpdate},
                #{item.flgDisplay},
                #{item.flgCanHidden},
            </trim>
            )
        </foreach>
    </insert>

    <!--系统单据的树形结构-->
    <select id="selectTreeByDoc" resultType="java.util.Map">
        WITH wt_doc AS
        (
        SELECT tsd.doc_code AS moduleCode
        , sys.f_get_name_i18n_lang(tsd.doc_name_i18n,#{i18n}) AS moduleName
        , tsd.module_code as parentCode
        , tsm.level_no + 1 AS levelNo
        , tsm.level_code || sys.f_to_str36(tsd.display_no::INT, 3) || '#' AS levelCode
        , 2 AS levelType
        FROM sys.t_s_doc tsd
        INNER JOIN sys.t_s_module tsm ON tsd.module_code = tsm.module_code
        and tsd.flg_valid
        AND tsm.flg_valid
        )
        SELECT *
        FROM (
        SELECT tsm.module_code AS "moduleCode"
        , sys.f_get_name_i18n_lang(tsm.module_name_i18n,#{i18n}) AS "moduleName"
        , sys.f_get_name_i18n_lang(tsm.module_name_i18n,#{i18n}) AS title
        , tsm.parent_code as "parentCode"
        , tsm.level_no AS "levelNo"
        , tsm.level_code AS "levelCode"
        , 1   AS "levelType"
        FROM sys.t_s_module tsm
        WHERE tsm.flg_valid
        AND EXISTS(SELECT 1 FROM wt_doc wd WHERE POSITION(tsm.module_code IN wd.parentCode) = 1)
        UNION ALL
        SELECT td.moduleCode
        , td.moduleName
        , td.moduleName as  title
        , td.parentCode
        , td.levelNo
        , td.levelCode
        , td.levelType
        FROM wt_doc td
        ) t
        ORDER BY t."levelCode"
    </select>

</mapper>
