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

    <resultMap id="UploadTemplateVOMap" type="com.ga.core.vo.upload.UploadTemplateVO">
        <id     property="templateId"   column="template_id"/>
        <result property="templateCode" column="template_code"/>
        <result property="templateName" column="template_name"/>
        <result property="category"     column="category"/>
        <result property="targetTable"  column="target_table"/>
        <result property="companyCode"  column="company_code"/>
        <result property="headerRow"    column="header_row"/>
        <result property="dataStartRow" column="data_start_row"/>
        <result property="sheetIndex"   column="sheet_index"/>
        <result property="sheetName"    column="sheet_name"/>
        <result property="fileEncoding" column="file_encoding"/>
        <result property="delimiter"    column="delimiter"/>
        <result property="skipEmptyRow" column="skip_empty_row"/>
        <result property="maxErrorCount" column="max_error_count"/>
        <result property="batchSize"    column="batch_size"/>
        <result property="description"  column="description"/>
        <result property="isActive"     column="is_active"/>
        <result property="createdAt"    column="created_at"/>
        <result property="createdBy"    column="created_by"/>
        <result property="updatedAt"    column="updated_at"/>
        <result property="updatedBy"    column="updated_by"/>
    </resultMap>

    <resultMap id="UploadTemplateRespMap" type="com.ga.core.vo.upload.UploadTemplateResp">
        <id     property="templateId"   column="template_id"/>
        <result property="templateCode" column="template_code"/>
        <result property="templateName" column="template_name"/>
        <result property="category"     column="category"/>
        <result property="targetTable"  column="target_table"/>
        <result property="companyCode"  column="company_code"/>
        <result property="headerRow"    column="header_row"/>
        <result property="dataStartRow" column="data_start_row"/>
        <result property="sheetIndex"   column="sheet_index"/>
        <result property="sheetName"    column="sheet_name"/>
        <result property="fileEncoding" column="file_encoding"/>
        <result property="delimiter"    column="delimiter"/>
        <result property="skipEmptyRow" column="skip_empty_row"/>
        <result property="maxErrorCount" column="max_error_count"/>
        <result property="batchSize"    column="batch_size"/>
        <result property="description"  column="description"/>
        <result property="isActive"     column="is_active"/>
        <result property="createdAt"    column="created_at"/>
        <result property="updatedAt"    column="updated_at"/>
    </resultMap>

    <sql id="templateCols">
        t.template_id, t.template_code, t.template_name, t.category, t.target_table,
        t.company_code, t.header_row, t.data_start_row, t.sheet_index, t.sheet_name,
        t.file_encoding, t.delimiter, t.skip_empty_row, t.max_error_count, t.batch_size,
        t.description, t.is_active, t.created_at, t.updated_at
    </sql>

    <select id="selectList" parameterType="com.ga.core.vo.upload.UploadTemplateSearchParam"
            resultMap="UploadTemplateRespMap">
        SELECT <include refid="templateCols"/>
        FROM upload_template t
        <where>
            <if test="category != null and category != ''">
                AND t.category = #{category}
            </if>
            <if test="companyCode != null and companyCode != ''">
                AND t.company_code = #{companyCode}
            </if>
            <if test="isActive != null and isActive != ''">
                AND t.is_active = #{isActive}
            </if>
            <if test="searchKeyword != null and searchKeyword != ''">
                AND (t.template_code LIKE CONCAT('%', #{searchKeyword}, '%')
                  OR t.template_name LIKE CONCAT('%', #{searchKeyword}, '%'))
            </if>
        </where>
        ORDER BY t.template_id DESC
    </select>

    <select id="selectById" resultMap="UploadTemplateRespMap">
        SELECT <include refid="templateCols"/>
        FROM upload_template t
        WHERE t.template_id = #{templateId}
    </select>

    <select id="selectByCode" resultMap="UploadTemplateVOMap">
        SELECT t.template_id, t.template_code, t.template_name, t.category, t.target_table,
               t.company_code, t.header_row, t.data_start_row, t.sheet_index, t.sheet_name,
               t.file_encoding, t.delimiter, t.skip_empty_row, t.max_error_count, t.batch_size,
               t.description, t.is_active, t.created_at, t.created_by, t.updated_at, t.updated_by
        FROM upload_template t
        WHERE t.template_code = #{templateCode}
    </select>

    <select id="existsByCode" resultType="int">
        SELECT COUNT(*) FROM upload_template WHERE template_code = #{templateCode}
    </select>

    <insert id="insert" parameterType="com.ga.core.vo.upload.UploadTemplateVO"
            useGeneratedKeys="true" keyProperty="templateId">
        INSERT INTO upload_template (
            template_code, template_name, category, target_table, company_code,
            header_row, data_start_row, sheet_index, sheet_name, file_encoding,
            delimiter, skip_empty_row, max_error_count, batch_size,
            description, is_active, created_at, created_by
        ) VALUES (
            #{templateCode}, #{templateName}, #{category}, #{targetTable}, #{companyCode},
            #{headerRow}, #{dataStartRow}, #{sheetIndex}, #{sheetName}, #{fileEncoding},
            #{delimiter}, #{skipEmptyRow}, #{maxErrorCount}, #{batchSize},
            #{description}, #{isActive}, NOW(), #{createdBy}
        )
    </insert>

    <update id="update" parameterType="com.ga.core.vo.upload.UploadTemplateVO">
        UPDATE upload_template SET
            template_name  = #{templateName},
            category       = #{category},
            target_table   = #{targetTable},
            company_code   = #{companyCode},
            header_row     = #{headerRow},
            data_start_row = #{dataStartRow},
            sheet_index    = #{sheetIndex},
            sheet_name     = #{sheetName},
            file_encoding  = #{fileEncoding},
            delimiter      = #{delimiter},
            skip_empty_row = #{skipEmptyRow},
            max_error_count = #{maxErrorCount},
            batch_size     = #{batchSize},
            description    = #{description},
            is_active      = #{isActive},
            updated_at     = NOW(),
            updated_by     = #{updatedBy}
        WHERE template_id = #{templateId}
    </update>

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

</mapper>
