<?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="org.xyzh.news.mapper.DataCollectionLogMapper">

    <!-- 基础结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.resource.TbDataCollectionLog">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="config_id" property="configID" jdbcType="VARCHAR"/>
        <result column="collect_count" property="collectCount" jdbcType="INTEGER"/>
        <result column="success_count" property="successCount" jdbcType="INTEGER"/>
        <result column="fail_count" property="failCount" jdbcType="INTEGER"/>
        <result column="status" property="status" jdbcType="INTEGER"/>
        <result column="message" property="message" jdbcType="LONGVARCHAR"/>
        <result column="collect_time" property="collectTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!-- 基础字段 -->
    <sql id="Base_Column_List">
        id, config_id, collect_count, success_count, fail_count, status, 
        message, collect_time
    </sql>

    <!-- 通用条件 -->
    <sql id="Where_Clause">
        <where>
            <if test="configID != null and configID != ''">
                AND config_id = #{configID}
            </if>
            <if test="status != null">
                AND status = #{status}
            </if>
        </where>
    </sql>

    <!-- selectDataCollectionLogs -->
    <select id="selectDataCollectionLogs" resultMap="BaseResultMap">
        SELECT 
        <include refid="Base_Column_List"/>
        FROM tb_data_collection_log
        <include refid="Where_Clause"/>
        ORDER BY collect_time DESC
    </select>

    <!-- 根据记录ID查询记录信息 -->
    <select id="selectByLogId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_data_collection_log
        WHERE id = #{logId}
    </select>

    <!-- 根据配置ID查询记录列表 -->
    <select id="selectByConfigId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_data_collection_log
        WHERE config_id = #{configId}
        ORDER BY collect_time DESC
    </select>

    <!-- 根据状态查询记录列表 -->
    <select id="selectByStatus" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_data_collection_log
        WHERE status = #{status}
        ORDER BY collect_time DESC
    </select>

    <!-- 根据类型查询记录列表 -->
    <select id="selectByType" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_data_collection_log
        ORDER BY collect_time DESC
    </select>

    <!-- 查询最新的记录列表 -->
    <select id="selectLatestLogs" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_data_collection_log
        ORDER BY collect_time DESC
        <if test="limit != null and limit > 0">
            LIMIT #{limit}
        </if>
    </select>

    <!-- 查询采集统计信息 -->
    <select id="selectCollectionStatistics" resultMap="BaseResultMap">
        SELECT
        config_id,
        SUM(collect_count) as collect_count,
        SUM(success_count) as success_count,
        SUM(fail_count) as fail_count,
        MAX(collect_time) as collect_time
        FROM tb_data_collection_log
        WHERE config_id = #{configId}
        GROUP BY config_id
    </select>

    <!-- 插入数据采集记录 -->
    <insert id="insertDataCollectionLog" parameterType="org.xyzh.common.dto.resource.TbDataCollectionLog">
        INSERT INTO tb_data_collection_log (
            id, config_id, collect_count, success_count, fail_count, status,
            message, collect_time
        ) VALUES (
            #{id}, #{configID}, #{collectCount}, #{successCount}, #{failCount}, #{status},
            #{message}, #{collectTime}
        )
    </insert>

    <!-- 更新数据采集记录 -->
    <update id="updateDataCollectionLog" parameterType="org.xyzh.common.dto.resource.TbDataCollectionLog">
        UPDATE tb_data_collection_log
        <set>
            <if test="configID != null and configID != ''">
                config_id = #{configID},
            </if>
            <if test="collectCount != null">
                collect_count = #{collectCount},
            </if>
            <if test="successCount != null">
                success_count = #{successCount},
            </if>
            <if test="failCount != null">
                fail_count = #{failCount},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="message != null and message != ''">
                message = #{message},
            </if>
            <if test="collectTime != null">
                collect_time = #{collectTime},
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <!-- 删除数据采集记录 -->
    <delete id="deleteDataCollectionLog" parameterType="org.xyzh.common.dto.resource.TbDataCollectionLog">
        DELETE FROM tb_data_collection_log
        WHERE id = #{id}
    </delete>

    <!-- 批量插入数据采集记录 -->
    <insert id="batchInsertDataCollectionLogs" parameterType="java.util.List">
        INSERT INTO tb_data_collection_log (
            id, config_id, collect_count, success_count, fail_count, status,
            message, collect_time
        ) VALUES
        <foreach collection="dataCollectionLogList" item="item" separator=",">
            (
                #{item.id}, #{item.configID}, #{item.collectCount}, #{item.successCount}, 
                #{item.failCount}, #{item.status}, #{item.message}, #{item.collectTime}
            )
        </foreach>
    </insert>

    <!-- 批量删除数据采集记录 -->
    <delete id="batchDeleteDataCollectionLogs">
        DELETE FROM tb_data_collection_log
        WHERE id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!-- 分页查询数据采集记录 -->
    <select id="selectDataCollectionLogsPage" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_data_collection_log
        <include refid="Where_Clause" />
        ORDER BY collect_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <!-- 统计数据采集记录总数 -->
    <select id="countDataCollectionLogs" resultType="long">
        SELECT COUNT(1)
        FROM tb_data_collection_log
        <include refid="Where_Clause" />
    </select>

</mapper>
