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

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        data_code
        , sys.f_get_name_i18n(
        #{lan},
        data_name_i18n
        )
        as
        data_name,
        type_code,
        display_no,
        flg_valid,
        remarks
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.common.model.pojo.system.BasicData">
        <id column="data_code" property="dataCode"/>
        <result column="type_code" property="typeCode"/>
        <result column="display_no" property="displayNo"/>
        <result column="flg_valid" property="flgValid"/>
        <result column="remarks" property="remarks"/>
    </resultMap>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="dataNameI18n != null and dataNameI18n != ''">
                AND data_name_i18n = #{dataNameI18n}
            </if>
            <if test="typeCode != null and typeCode != ''">
                AND type_code = #{typeCode}
            </if>
            <if test="typeCodeList != null and typeCodeList.size()>0">
                AND type_code = any(#{typeCodeList, typeHandler=StringListTypeHandler})
            </if>
            <if test="displayNo != null">
                AND display_no = #{displayNo}
            </if>
            <if test="flgValid != null">
                AND flg_valid = #{flgValid}
            </if>
            <if test="remarks != null and remarks != ''">
                AND remarks = #{remarks}
            </if>
        </where>
    </sql>

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

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

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


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

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

    <insert id="insertBatch">
        insert into sys.t_s_basic_data
        (
        <trim suffixOverrides=",">
            data_name_i18n,
            type_code,
            display_no,
            remarks,
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (
            <trim suffixOverrides=",">
                #{item.dataNameI18n},
                #{item.typeCode},
                #{item.displayNo},
                #{item.remarks},
            </trim>
            )
        </foreach>
    </insert>

    <!--    保存恒洁组织结构数据-->
    <select id="resetData" resultType="Object">
        select core.f_reset_hg_iap_dict_array(
                       #{data, typeHandler=JsonTypeHandler})
    </select>
</mapper>
