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

    <resultMap id="HistoryRespMap" type="com.ga.core.vo.upload.UploadHistoryResp">
        <id     property="historyId"      column="history_id"/>
        <result property="templateId"     column="template_id"/>
        <result property="templateCode"   column="template_code"/>
        <result property="templateName"   column="template_name"/>
        <result property="fileName"       column="file_name"/>
        <result property="fileSize"       column="file_size"/>
        <result property="totalCount"     column="total_count"/>
        <result property="successCount"   column="success_count"/>
        <result property="errorCount"     column="error_count"/>
        <result property="skipCount"      column="skip_count"/>
        <result property="errorFileId"    column="error_file_id"/>
        <result property="status"         column="status"/>
        <result property="errorMessage"   column="error_message"/>
        <result property="startedAt"      column="started_at"/>
        <result property="finishedAt"     column="finished_at"/>
        <result property="durationSec"    column="duration_sec"/>
        <result property="createdBy"      column="created_by"/>
        <result property="createdByName"  column="created_by_name"/>
    </resultMap>

    <sql id="historyCols">
        h.history_id, h.template_id, h.template_code,
        t.template_name,
        h.file_name, h.file_size, h.total_count, h.success_count, h.error_count,
        h.skip_count, h.error_file_id, h.status, h.error_message,
        h.started_at, h.finished_at, h.duration_sec, h.created_by,
        u.login_id AS created_by_name
    </sql>

    <select id="selectList" parameterType="com.ga.core.vo.upload.UploadTemplateSearchParam"
            resultMap="HistoryRespMap">
        SELECT <include refid="historyCols"/>
        FROM upload_history h
        LEFT JOIN upload_template t ON t.template_id = h.template_id
        LEFT JOIN users u           ON u.user_id = h.created_by
        <where>
            <if test="searchKeyword != null and searchKeyword != ''">
                AND (h.template_code LIKE CONCAT('%', #{searchKeyword}, '%')
                  OR h.file_name LIKE CONCAT('%', #{searchKeyword}, '%'))
            </if>
            <if test="status != null and status != ''">
                AND h.status = #{status}
            </if>
            <if test="startDate != null and startDate != ''">
                AND h.started_at >= #{startDate}::timestamp
            </if>
            <if test="endDate != null and endDate != ''">
                AND h.started_at &lt; (#{endDate}::date + INTERVAL '1 day')
            </if>
        </where>
        ORDER BY h.history_id DESC
    </select>

    <select id="selectByTemplate" resultMap="HistoryRespMap">
        SELECT <include refid="historyCols"/>
        FROM upload_history h
        LEFT JOIN upload_template t ON t.template_id = h.template_id
        LEFT JOIN users u           ON u.user_id = h.created_by
        WHERE h.template_id = #{templateId}
        <if test="param.status != null and param.status != ''">
            AND h.status = #{param.status}
        </if>
        ORDER BY h.history_id DESC
    </select>

    <insert id="insert" parameterType="com.ga.core.vo.upload.UploadHistoryVO"
            useGeneratedKeys="true" keyProperty="historyId">
        INSERT INTO upload_history (
            template_id, template_code, file_name, file_size,
            total_count, success_count, error_count, skip_count,
            error_file_id, status, error_message, started_at, finished_at,
            duration_sec, created_by
        ) VALUES (
            #{templateId}, #{templateCode}, #{fileName}, #{fileSize},
            #{totalCount}, #{successCount}, #{errorCount}, #{skipCount},
            #{errorFileId}, #{status}, #{errorMessage}, #{startedAt}, #{finishedAt},
            #{durationSec}, #{createdBy}
        )
    </insert>

    <update id="updateStatus">
        UPDATE upload_history SET
            status        = #{status},
            error_message = #{errorMessage},
            success_count = #{successCount},
            error_count   = #{errorCount},
            skip_count    = #{skipCount},
            total_count   = #{totalCount},
            error_file_id = #{errorFileId},
            duration_sec  = #{durationSec},
            finished_at   = NOW()
        WHERE history_id = #{historyId}
    </update>

</mapper>
