<?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.DictionaryItemMapper">

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        item_code,
       sys.f_get_name_i18n_lang(item_name_i18n,#{i18n}) as title,
       item_type,
       parent_code,
       display_no,
       level_no,
       level_code,
       sys_ids,
       flg_valid,
       remarks
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.sys.DictionaryItem">
        <id column="item_code" property="itemCode"/>
        <result column="item_name_i18n" property="itemNameI18n"
                typeHandler="JsonTypeHandler"/>
        <result column="item_type" property="itemType"/>
        <result column="parent_code" property="parentCode"/>
        <result column="display_no" property="displayNo"/>
        <result column="level_no" property="levelNo"/>
        <result column="level_code" property="levelCode"/>
        <result column="sys_ids" property="sysIds" typeHandler="ShortListTypeHandler"/>
        <result column="flg_valid" property="flgValid"/>
        <result column="remarks" property="remarks"/>
    </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="parentCode != null and parentCode != ''">
                AND parent_code = #{parentCode}
            </if>
            <if test="displayNo != null">
                AND display_no = #{displayNo}
            </if>
            <if test="levelNo != null">
                AND level_no = #{levelNo}
            </if>
            <if test="levelCode != null and levelCode != ''">
                AND level_code = #{levelCode}
            </if>
            <if test="sysIds != null and sysIds.size()>0">
                AND sys_ids @> (#{sysIds,typeHandler=ShortListTypeHandler})
            </if>
            <if test="flgValid != null">
                AND flg_valid = #{flgValid}
            </if>
            <if test="remarks != null and remarks != ''">
                AND remarks = #{remarks}
            </if>
            <if test="textValue != null and textValue != ''">
                AND item_name_i18n ->>  #{i18n} like concat('%', #{textValue}, '%')
            </if>
        </where>
    </sql>

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

    <!-- 查询表t_s_dictionary_item，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM sys.t_s_dictionary_item
        <include refid="Condition"/>
        order by display_no
    </select>

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


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

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

    <insert id="insertBatch">
        insert into t_s_dictionary_item
        (
        <trim suffixOverrides=",">
            item_name_i18n,
            item_type,
            parent_code,
            display_no,
            level_no,
            level_code,
            sys_ids,
            remarks,
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (
            <trim suffixOverrides=",">
                #{item.itemNameI18n},
                #{item.itemType},
                #{item.parentCode},
                #{item.displayNo},
                #{item.levelNo},
                #{item.levelCode},
                #{item.sysIds},
                #{item.remarks},
            </trim>
            )
        </foreach>
    </insert>
</mapper>
