<?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.ga.core.mapper.upload.UploadColumnMapper">

    <resultMap id="ColumnMap" type="com.ga.core.vo.upload.UploadTemplateColumnVO">
        <id     property="columnId"          column="column_id"/>
        <result property="templateId"        column="template_id"/>
        <result property="sortOrder"         column="sort_order"/>
        <result property="sourceType"        column="source_type"/>
        <result property="sourceColumn"      column="source_column"/>
        <result property="sourceHeader"      column="source_header"/>
        <result property="targetField"       column="target_field"/>
        <result property="targetType"        column="target_type"/>
        <result property="transformRule"     column="transform_rule"/>
        <result property="transformParam"    column="transform_param"/>
        <result property="dateFormat"        column="date_format"/>
        <result property="numberDivide"      column="number_divide"/>
        <result property="numberMultiply"    column="number_multiply"/>
        <result property="fixedValue"        column="fixed_value"/>
        <result property="defaultValue"      column="default_value"/>
        <result property="lookupTable"       column="lookup_table"/>
        <result property="lookupSourceField" column="lookup_source_field"/>
        <result property="lookupTargetField" column="lookup_target_field"/>
        <result property="codeMappingGroup"  column="code_map_group"/>
        <result property="isRequired"        column="is_required"/>
        <result property="validationRegex"   column="validation_regex"/>
        <result property="validationMessage" column="validation_message"/>
        <result property="description"       column="description"/>
    </resultMap>

    <select id="selectByTemplateId" resultMap="ColumnMap">
        SELECT column_id, template_id, sort_order, source_type, source_column, source_header,
               target_field, target_type, transform_rule, transform_param, date_format,
               number_divide, number_multiply, fixed_value, default_value,
               lookup_table, lookup_source_field, lookup_target_field, code_map_group,
               is_required, validation_regex, validation_message, description
        FROM upload_template_column
        WHERE template_id = #{templateId}
        ORDER BY sort_order ASC
    </select>

    <!-- forEach 로 다건 INSERT -->
    <insert id="insertBatch">
        INSERT INTO upload_template_column (
            template_id, sort_order, source_type, source_column, source_header,
            target_field, target_type, transform_rule, transform_param, date_format,
            number_divide, number_multiply, fixed_value, default_value,
            lookup_table, lookup_source_field, lookup_target_field, code_map_group,
            is_required, validation_regex, validation_message, description
        ) VALUES
        <foreach collection="columns" item="c" separator=",">
        (
            #{templateId}, #{c.sortOrder}, #{c.sourceType}, #{c.sourceColumn}, #{c.sourceHeader},
            #{c.targetField}, #{c.targetType}, #{c.transformRule}, #{c.transformParam}, #{c.dateFormat},
            #{c.numberDivide}, #{c.numberMultiply}, #{c.fixedValue}, #{c.defaultValue},
            #{c.lookupTable}, #{c.lookupSourceField}, #{c.lookupTargetField}, #{c.codeMappingGroup},
            #{c.isRequired}, #{c.validationRegex}, #{c.validationMessage}, #{c.description}
        )
        </foreach>
    </insert>

    <delete id="deleteByTemplateId">
        DELETE FROM upload_template_column WHERE template_id = #{templateId}
    </delete>

</mapper>
