<?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.moka.gdtauto.mapper.TencentAdsAdgroupMapper">

    <!-- 批量插入或更新广告组数据 -->
    <insert id="batchInsertOrUpdate" parameterType="java.util.List">
        INSERT INTO tencent_ads_adgroup (
            account_id,
            adgroup_id,
            adgroup_name,
            configured_status,
            daily_budget,
            created_time,
            marketing_scene,
            last_modified_time,
            is_deleted,
            marketing_goal,
            marketing_asset_outer_spec,
            marketing_target_type,
            marketing_asset_outer_id,
            marketing_asset_outer_name,
            marketing_carrier_type,
            optimization_goal,
            conversion_id,
            site_set,
            targeting,
            bid_mode,
            bid_scene,
            begin_date,
            end_date,
            time_series,
            auto_derived_creative_enabled,
            creative_components
        )
        VALUES
        <foreach collection="list" item="item" separator=",">
            (
                #{item.accountId},
                #{item.adgroupId},
                #{item.adgroupName},
                #{item.configuredStatus},
                #{item.dailyBudget},
                #{item.createdTime},
                #{item.marketingScene},
                #{item.lastModifiedTime},
                #{item.isDeleted},
                #{item.marketingGoal},
                #{item.marketingAssetOuterSpec},
                #{item.marketingTargetType},
                #{item.marketingAssetOuterId},
                #{item.marketingAssetOuterName},
                #{item.marketingCarrierType},
                #{item.optimizationGoal},
                #{item.conversionId},
                #{item.siteSet},
                #{item.targeting},
                #{item.bidMode},
                #{item.bidScene},
                #{item.beginDate},
                #{item.endDate},
                #{item.timeSeries},
                #{item.autoDerivedCreativeEnabled},
                #{item.creativeComponents}
            )
        </foreach>
        ON DUPLICATE KEY UPDATE
            account_id = VALUES(account_id),
            adgroup_name = VALUES(adgroup_name),
            configured_status = VALUES(configured_status),
            daily_budget = VALUES(daily_budget),
            created_time = VALUES(created_time),
            marketing_scene = VALUES(marketing_scene),
            last_modified_time = VALUES(last_modified_time),
            is_deleted = VALUES(is_deleted),
            marketing_goal = VALUES(marketing_goal),
            marketing_asset_outer_spec = VALUES(marketing_asset_outer_spec),
            marketing_target_type = VALUES(marketing_target_type),
            marketing_asset_outer_id = VALUES(marketing_asset_outer_id),
            marketing_asset_outer_name = VALUES(marketing_asset_outer_name),
            marketing_carrier_type = VALUES(marketing_carrier_type),
            optimization_goal = VALUES(optimization_goal),
            conversion_id = VALUES(conversion_id),
            site_set = VALUES(site_set),
            targeting = VALUES(targeting),
            bid_mode = VALUES(bid_mode),
            bid_scene = VALUES(bid_scene),
            begin_date = VALUES(begin_date),
            end_date = VALUES(end_date),
            time_series = VALUES(time_series),
            auto_derived_creative_enabled = VALUES(auto_derived_creative_enabled),
            creative_components = VALUES(creative_components)
    </insert>

    <!-- 按状态统计广告组数量 -->
    <select id="selectCountByStatus" resultType="map">
        SELECT 
            configured_status as status,
            COUNT(*) as count
        FROM tencent_ads_adgroup 
        GROUP BY configured_status 
        ORDER BY count DESC
    </select>

    <!-- 按营销场景统计广告组数量 -->
    <select id="selectCountByMarketingScene" resultType="map">
        SELECT 
            marketing_scene as scene,
            COUNT(*) as count
        FROM tencent_ads_adgroup 
        GROUP BY marketing_scene 
        ORDER BY count DESC
    </select>

    <!-- 按账户统计广告组数量 -->
    <select id="selectCountByAccount" resultType="map">
        SELECT 
            account_id as accountId,
            COUNT(*) as count
        FROM tencent_ads_adgroup 
        GROUP BY account_id 
        ORDER BY count DESC 
        LIMIT 5
    </select>

    <!-- 统计今日新增广告组数量 -->
    <select id="selectTodayCount" resultType="long">
        SELECT COUNT(*) 
        FROM tencent_ads_adgroup 
        WHERE created_time BETWEEN #{startTime} AND #{endTime}
    </select>

    <!-- 统计指定时间段内的广告组数量 -->
    <select id="selectPeriodCount" resultType="long">
        SELECT COUNT(*) 
        FROM tencent_ads_adgroup 
        WHERE created_time BETWEEN #{startTime} AND #{endTime}
    </select>

</mapper>
