<?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="cn.com.ctop.kuaishou.modules.batch.mapper.KuaiShouCampaignMapper">
    <insert id="replaceBatch">
        replace into ctop_kuaishou_campaign(
        id,
        account_id,
        campaign_id,
        campaign_name,
        status,
        day_budget,
        campaign_type,
        put_status,
        create_channel,
        put_create_time,
        put_update_time,
        create_time,
        update_time,
        bid_type)
        values
        <foreach collection="campaigns" item="campaign" separator=",">
            (#{campaign.id},
            #{campaign.accountId},
            #{campaign.campaignId},
            #{campaign.campaignName},
            #{campaign.status},
            #{campaign.dayBudget},
            #{campaign.campaignType},
            #{campaign.putStatus},
            #{campaign.createChannel},
            #{campaign.putCreateTime},
            #{campaign.putUpdateTime},
            #{campaign.createTime},
            #{campaign.updateTime},
            #{campaign.bidType}
            )
        </foreach>
    </insert>

    <select id="queryIdsByPutStatus" resultType="java.lang.Long">
        SELECT campaign_id
        FROM ctop_kuaishou_campaign
        WHERE account_id = #{accountId}
          AND put_status = #{putStatus}
    </select>
    <select id="queryCampaignIdByAccountId" resultType="java.lang.Long">
        SELECT campaign_id
        FROM ctop_kuaishou_campaign
        WHERE account_id = #{accountId}
    </select>
    <select id="getNewDataByAccountIdAndDate" resultType="java.lang.Integer">
        select count(1)
        from ctop_kuaishou_campaign
        where account_id = #{accountId}
          and DATE_FORMAT(put_create_time, '%Y-%m-%d') = #{statDate}
    </select>
    <select id="getCampaignIdByAccountAndName" resultType="java.lang.Long">
        select campaign_id
        from ctop_kuaishou_campaign
        where account_id = #{accountId}
          and campaign_name like CONCAT('%', CONCAT(#{name}, '%'))
        order by put_create_time DESC limit 1


    </select>
    <select id="getCampaignIdByAccountAndNameAndDate" resultType="java.lang.Long">
        select campaign_id
        from ctop_kuaishou_campaign
        where account_id = #{accountId}
          and campaign_name like CONCAT('%', CONCAT(#{name}, '%'))
          and put_create_time &gt;= #{nowDate}
        order by put_create_time DESC limit 1

    </select>

    <select id="getCampaignIdsByParameters" resultType="com.alibaba.fastjson.JSONObject">
        SELECT t.campaign_id AS 'campaignId', IFNULL(t1.count, 0) AS 'count'
        FROM ctop_kuaishou_campaign t
                 LEFT JOIN (
            SELECT campaign_id,
                   sum(1) AS 'count'
            FROM ctop_kuaishou_group
            WHERE campaign_id IN (SELECT campaign_id
                                  FROM ctop_kuaishou_campaign
                                  WHERE account_id = #{accountId}
                                    AND campaign_name LIKE CONCAT('%', CONCAT(#{typeName}, '%'))
                                    AND put_create_time >= #{nowDate})) t1 ON t.campaign_id = t1.campaign_id
        WHERE account_id = #{accountId}
          AND campaign_name LIKE CONCAT('%', CONCAT(#{typeName}, '%'))
          AND put_create_time >= #{nowDate}
    </select>
</mapper>
