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

    <!-- 基础结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.resource.TbDataCollectionConfig">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="source_url" property="sourceUrl" jdbcType="VARCHAR"/>
        <result column="source_type" property="sourceType" jdbcType="VARCHAR"/>
        <result column="frequency" property="frequency" jdbcType="VARCHAR"/>
        <result column="category_id" property="categoryID" jdbcType="VARCHAR"/>
        <result column="status" property="status" jdbcType="INTEGER"/>
        <result column="last_collect_time" property="lastCollectTime" jdbcType="TIMESTAMP"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="updater" property="updater" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="delete_time" property="deleteTime" jdbcType="TIMESTAMP"/>
        <result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
    </resultMap>

    <!-- 基础字段 -->
    <sql id="Base_Column_List">
        id, name, source_url, source_type, frequency, category_id, status, 
        last_collect_time, creator, updater, create_time, update_time, 
        delete_time, deleted
    </sql>

    <!-- 通用条件 -->
    <sql id="Where_Clause">
        <where>
            deleted = 0
            <if test="name != null and name != ''">
                AND name LIKE CONCAT('%', #{name}, '%')
            </if>
            <if test="sourceType != null and sourceType != ''">
                AND source_type = #{sourceType}
            </if>
            <if test="frequency != null and frequency != ''">
                AND frequency = #{frequency}
            </if>
            <if test="categoryID != null and categoryID != ''">
                AND category_id = #{categoryID}
            </if>
            <if test="status != null">
                AND status = #{status}
            </if>
        </where>
    </sql>

    <!-- selectDataCollectionConfigs -->
    <select id="selectDataCollectionConfigs" resultMap="BaseResultMap">
        SELECT 
        <include refid="Base_Column_List"/>
        FROM tb_data_collection_config
        <include refid="Where_Clause"/>
        ORDER BY create_time DESC
    </select>

    <!-- 根据配置ID查询配置信息 -->
    <select id="selectByConfigId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_data_collection_config
        WHERE id = #{configId} AND deleted = 0
    </select>

    <!-- 根据名称查询配置 -->
    <select id="selectByName" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_data_collection_config
        WHERE name = #{name} AND deleted = 0
    </select>

    <!-- 根据状态查询配置列表 -->
    <select id="selectByStatus" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_data_collection_config
        WHERE status = #{status} AND deleted = 0
        ORDER BY create_time DESC
    </select>

    <!-- 根据类型查询配置列表 -->
    <select id="selectByType" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_data_collection_config
        WHERE source_type = #{type} AND deleted = 0
        ORDER BY create_time DESC
    </select>

    <!-- 查询启用的配置列表 -->
    <select id="selectActiveConfigs" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_data_collection_config
        WHERE status = 1 AND deleted = 0
        ORDER BY create_time DESC
    </select>

    <!-- 检查配置名称是否存在 -->
    <select id="countByName" resultType="int">
        SELECT COUNT(1)
        FROM tb_data_collection_config
        WHERE name = #{name} AND deleted = 0
        <if test="excludeId != null and excludeId != ''">
            AND id != #{excludeId}
        </if>
    </select>

    <!-- 插入数据采集配置 -->
    <insert id="insertDataCollectionConfig" parameterType="org.xyzh.common.dto.resource.TbDataCollectionConfig">
        INSERT INTO tb_data_collection_config (
            id, name, source_url, source_type, frequency, category_id, status,
            last_collect_time, creator, updater, create_time, update_time,
            delete_time, deleted
        ) VALUES (
            #{id}, #{name}, #{sourceUrl}, #{sourceType}, #{frequency}, #{categoryID}, #{status},
            #{lastCollectTime}, #{creator}, #{updater}, #{createTime}, #{updateTime},
            #{deleteTime}, #{deleted}
        )
    </insert>

    <!-- 更新数据采集配置 -->
    <update id="updateDataCollectionConfig" parameterType="org.xyzh.common.dto.resource.TbDataCollectionConfig">
        UPDATE tb_data_collection_config
        <set>
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="sourceUrl != null and sourceUrl != ''">
                source_url = #{sourceUrl},
            </if>
            <if test="sourceType != null and sourceType != ''">
                source_type = #{sourceType},
            </if>
            <if test="frequency != null and frequency != ''">
                frequency = #{frequency},
            </if>
            <if test="categoryID != null and categoryID != ''">
                category_id = #{categoryID},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="lastCollectTime != null">
                last_collect_time = #{lastCollectTime},
            </if>
            <if test="updater != null and updater != ''">
                updater = #{updater},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="deleteTime != null">
                delete_time = #{deleteTime},
            </if>
            <if test="deleted != null">
                deleted = #{deleted},
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <!-- 删除数据采集配置 -->
    <delete id="deleteDataCollectionConfig" parameterType="org.xyzh.common.dto.resource.TbDataCollectionConfig">
        DELETE FROM tb_data_collection_config
        WHERE id = #{id}
    </delete>

    <!-- 批量插入数据采集配置 -->
    <insert id="batchInsertDataCollectionConfigs" parameterType="java.util.List">
        INSERT INTO tb_data_collection_config (
            id, name, source_url, source_type, frequency, category_id, status,
            last_collect_time, creator, updater, create_time, update_time,
            delete_time, deleted
        ) VALUES
        <foreach collection="dataCollectionConfigList" item="item" separator=",">
            (
                #{item.id}, #{item.name}, #{item.sourceUrl}, #{item.sourceType}, #{item.frequency}, 
                #{item.categoryID}, #{item.status}, #{item.lastCollectTime}, #{item.creator}, 
                #{item.updater}, #{item.createTime}, #{item.updateTime}, #{item.deleteTime}, #{item.deleted}
            )
        </foreach>
    </insert>

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

    <!-- 分页查询数据采集配置 -->
    <select id="selectDataCollectionConfigsPage" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_data_collection_config
        <include refid="Where_Clause" />
        ORDER BY create_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <!-- 统计数据采集配置总数 -->
    <select id="countDataCollectionConfigs" resultType="long">
        SELECT COUNT(1)
        FROM tb_data_collection_config
        <include refid="Where_Clause" />
    </select>

</mapper>
