<?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.report.mapper.KuaishouReportHourlyCreativeStatisticMapper">
	<insert id="updateCreativeReportHourlyStatistic">
        REPLACE INTO `ctop_kuaishou_report_hourly_creative_statistic` (
	`id`,
	`account_id`,
	`account_name`,
	`unit_id`,
	`unit_name`,
	`campaign_id`,
	`campaign_name`,
	`creative_id`,
	`creative_name`,
	`stat_date`,
	`stat_hour`,
	`charge`,
	`photo_click`,
	`photo_show`,
	`aclick`,
	`bclick`,
	`photo_click_ratio`,
	`action_ratio`,
	`impression_1k_cost`,
	`event_credit_grant_landing_page_cost`,
	`event_credit_grant_landing_page`,
	`event_jin_jian_landing_page_cost`,
	`event_jin_jian_landing_page`,
	`form_action_ratio`,
	`form_cost`,
	`form_count`,
	`event_order_paid_cost`,
	`event_order_paid_purchase_amount`,
	`event_order_paid`,
	`event_credit_grant_app_ratio`,
	`event_credit_grant_app_cost`,
	`event_credit_grant_app`,
	`event_pay_purchase_amount_first_day`,
	`event_jin_jian_app_cost`,
	`event_jin_jian_app`,
	`event_register_cost`,
	`event_register`,
	`event_pay_roi`,
	`event_pay_purchase_amount`,
	`event_pay`,
	`event_pay_first_day_roi`,
	`event_pay_first_day`,
	`photo_click_cost`,
	`action_cost`,
	`share`,
	`comment`,
	`photo_like`,
	`follow`,
	`report`,
	`block`,
	`negative`,
	`activation`,
	`submit`,
	`cancel_follow`,
	`download_started`,
	`download_completed`,
	`create_time`,
	`update_time`,
	`signature`,
	`url`
) SELECT
	*
FROM
	(
		SELECT
			ckrhc.`id`,
			ckrhc.`account_id`,
			cua.account_name,
			ckg.unit_id,
			ckg.unit_name,
			ckc.campaign_id,
			ckc.campaign_name,
			ckrhc.`creative_id`,
			ckr.creative_name,
			ckrhc.`stat_date`,
			ckrhc.`stat_hour`,
			ckrhc.`charge`,
			ckrhc.`photo_click`,
			ckrhc.`photo_show`,
			ckrhc.`aclick`,
			ckrhc.`bclick`,
			ckrhc.`photo_click_ratio`,
			ckrhc.`action_ratio`,
			ckrhc.`impression_1k_cost`,
			ckrhc.`event_credit_grant_landing_page_cost`,
			ckrhc.`event_credit_grant_landing_page`,
			ckrhc.`event_jin_jian_landing_page_cost`,
			ckrhc.`event_jin_jian_landing_page`,
			ckrhc.`form_action_ratio`,
			ckrhc.`form_cost`,
			ckrhc.`form_count`,
			ckrhc.`event_order_paid_cost`,
			ckrhc.`event_order_paid_purchase_amount`,
			ckrhc.`event_order_paid`,
			ckrhc.`event_credit_grant_app_ratio`,
			ckrhc.`event_credit_grant_app_cost`,
			ckrhc.`event_credit_grant_app`,
			ckrhc.`event_pay_purchase_amount_first_day`,
			ckrhc.`event_jin_jian_app_cost`,
			ckrhc.`event_jin_jian_app`,
			ckrhc.`event_register_cost`,
			ckrhc.`event_register`,
			ckrhc.`event_pay_roi`,
			ckrhc.`event_pay_purchase_amount`,
			ckrhc.`event_pay`,
			ckrhc.`event_pay_first_day_roi`,
			ckrhc.`event_pay_first_day`,
			ckrhc.`photo_click_cost`,
			ckrhc.`action_cost`,
			ckrhc.`share`,
			ckrhc.`comment`,
			ckrhc.`photo_like`,
			ckrhc.`follow`,
			ckrhc.`report`,
			ckrhc.`block`,
			ckrhc.`negative`,
			ckrhc.`activation`,
			ckrhc.`submit`,
			ckrhc.`cancel_follow`,
			ckrhc.`download_started`,
			ckrhc.`download_completed`,
			ckrhc.`create_time`,
			ckrhc.`update_time`,
			ckvg.signature,
			ckvg.url
		FROM
			(
				SELECT
					*
				FROM
					ctop_kuaishou_report_hourly_creative
				WHERE
					account_id = #{accountId} and stat_date = #{date}) ckrhc,
					ctop_kuaishou_creative ckr,
					ctop_user_allocation cua,
					ctop_kuaishou_campaign ckc,
					ctop_kuaishou_group ckg,
					ctop_kuaishou_video_get ckvg
				WHERE
					ckrhc.creative_id = ckr.creative_id
				AND ckr.account_id = cua.account_id
				AND ckr.campaign_id = ckc.campaign_id
				AND ckr.unit_id = ckg.unit_id
				AND ckr.photo_id = ckvg.photo_id
				AND ckr.account_id = ckvg.account_id
			) t
    </insert>

	<delete id="deleteHourlyReport">
		delete from ctop_kuaishou_report_hourly_creative where stat_date &lt;= #{date}
	</delete>
</mapper>
