# Author renyupeng # coding=utf-8 # @Time : 2021/11/10 3:37 下午 # @Site : # @File : AppBytedanceCompanyUserReportBusi.py # @Software: PyCharm # @contact: renyupeng@c-top.com.cn # @Tel 1501435553 import logging from Apietl.conn.TidbConn import TidbConn from Apietl.utlis.Utils import Utils class AppBytedanceCompanyUserReportBusi: def __init__(self): self.conn = TidbConn.get_connection() self.parm = Utils.get_task_args() self.date_time = self.parm[0] self.day_count = self.parm[1] def taskHandler(self): try: sql = """ SET sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'; REPLACE INTO application.app_bytedance_company_user_report_busi_{day_count}d SELECT new.start_end_time , new.company_id , new.company_name , new.org_code , new.org_type , new.dep_id , new.dep_name , new.leader_id, new.leader_name, new.account_num, new.cost , ifnull(ratio.cost,0) last_cost, ifnull(round((new.cost-ratio.cost)/ratio.cost*100,3),0) cost_ratio, new.show , new.avg_show_cost, new.click, new.avg_click_cost , new.ctr, new.convert, new.convert_cost , new.convert_rate , new.deep_convert , new.deep_convert_cost, new.deep_convert_rate, new.attribution_convert, new.attribution_convert_cost , new.attribution_deep_convert , new.attribution_deep_convert_cost, new.download_start , new.download_start_cost, new.download_start_rate, new.download_finish, new.download_finish_cost , new.download_finish_rate , new.click_install, new.install_finish , new.install_finish_cost, new.install_finish_rate, new.active , new.active_cost, new.active_rate, new.register , new.active_register_cost , new.active_register_rate , new.next_day_open, new.next_day_open_cost , new.next_day_open_rate , new.attribution_next_day_open_cnt, new.attribution_next_day_open_cost , new.attribution_next_day_open_rate , new.game_addiction , new.game_addiction_cost, new.game_addiction_rate, new.pay_count, new.active_pay_cost, new.active_pay_rate, new.loan_completion, new.loan_completion_cost , new.loan_completion_rate , new.pre_loan_credit, new.pre_loan_credit_cost , new.loan_credit, new.loan_credit_cost , new.loan_credit_rate , new.in_app_uv, new.in_app_detail_uv , new.in_app_cart, new.in_app_pay , new.in_app_order , new.attribution_game_pay_7d_count, new.attribution_game_pay_7d_cost , new.attribution_active_pay_7d_per_count, new.game_pay_cost, new.game_pay_count , new.phone, new.form , new.map_search , new.button , new.view , new.download , new.qq , new.lottery, new.vote , new.message, new.redirect , new.shopping , new.consult, new.wechat , new.phone_confirm, new.phone_connect, new.consult_effective, new.coupon , new.coupon_single_page , new.redirect_to_shop , new.poi_collect, new.poi_address_click, new.luban_order_cnt, new.luban_order_stat_amount, new.luban_order_roi, new.luban_live_enter_cnt , new.live_watch_one_minute_count, new.luban_live_follow_cnt, new.live_fans_club_join_cnt, new.luban_live_comment_cnt , new.luban_live_share_cnt , new.luban_live_gift_cnt, new.luban_live_gift_amount , new.luban_live_slidecart_click_cnt , new.luban_live_click_product_cnt , new.luban_live_pay_order_count , new.luban_live_pay_order_stat_cost , new.live_component_click_count , new.live_component_click_cost, new.live_component_click_rate, new.wechat_login_count , new.attribution_wechat_login_30d_count , new.wechat_login_cost, new.attribution_wechat_login_30d_cost, new.wechat_first_pay_count , new.attribution_wechat_first_pay_30d_count , new.wechat_first_pay_cost, new.attribution_wechat_first_pay_30d_cost, new.wechat_first_pay_rate, new.attribution_wechat_first_pay_30d_rate, new.wechat_pay_amount, new.attribution_wechat_pay_30d_amount, new.attribution_wechat_pay_30d_roi , new.phone_effective, new.total_play , new.valid_play , new.valid_play_cost, new.valid_play_rate, new.play_25_feed_break , new.play_50_feed_break , new.play_75_feed_break , new.play_100_feed_break, new.average_play_time_per_play , new.play_over_rate , new.wifi_play_rate , new.wifi_play, new.play_duration_sum, new.advanced_creative_phone_click, new.advanced_creative_counsel_click, new.advanced_creative_form_click , new.advanced_creative_coupon_addition, new.advanced_creative_form_submit, new.card_show, new.share, new.comment, new.like , new.follow , new.home_visited , new.ies_challenge_click, new.ies_music_click, new.location_click , new.message_action , new.click_landing_page , new.click_shopwindow , new.click_website, new.click_download , new.click_call_dy, new.submit_certification_count , new.approval_count , new.first_rental_order_count , new.first_order_count, new.commute_first_pay_count from ( SELECT concat(date_format(date_sub({date_time}, INTERVAL ({day_count} -1 ) DAY),'%Y%m%d'),'-',{date_time}) start_end_time, ifnull(company_id,'-') company_id, company_name, org_code, org_type, dep_id, depart_name dep_name, ifnull(leader_id,'-') leader_id, trim(leader_name) leader_name, count(DISTINCT account_id) account_num, sum(cost) cost, sum(`show`) `show`, ifnull(round(sum(cost) / sum(`show`), 3),0) avg_show_cost, sum(click) click, ifnull(round(sum(cost) / sum(`click`), 3),0) avg_click_cost, ifnull(round(sum(click) / sum(`show`) * 100,3),0) ctr, sum(`convert`) `convert`, ifnull(round(sum(cost) / sum(`convert`), 3),0) convert_cost, ifnull(round(sum(`convert`) / sum(click) * 100,3),0) convert_rate, sum(deep_convert) deep_convert, ifnull(round(sum(cost) / sum(deep_convert),3),0) deep_convert_cost, ifnull(round(sum(deep_convert) / sum(`convert`) * 100,3),0) deep_convert_rate, sum(attribution_convert) attribution_convert, ifnull(round(sum(cost) / sum(attribution_convert),3),0) attribution_convert_cost, sum(attribution_deep_convert) attribution_deep_convert, ifnull(round(sum(cost) / sum(attribution_deep_convert),3),0) attribution_deep_convert_cost, sum(download_start) download_start, ifnull(round(sum(cost) / sum(download_start),3),0) download_start_cost, ifnull(round(sum(download_start) / sum(click),3),0) download_start_rate, sum(download_finish) download_finish, ifnull(round(sum(cost) / sum(download_finish),3),0) download_finish_cost, ifnull(round(sum(download_finish) / sum(download_start) * 100,3),0) download_finish_rate, sum(click_install) click_install, sum(install_finish) install_finish, ifnull(round(sum(cost) / sum(install_finish),3),0) install_finish_cost, ifnull(round(sum(install_finish) / sum(download_finish) * 100,3),0) install_finish_rate, sum(active) active, ifnull(round(sum(cost) / sum(active), 3),0) active_cost, ifnull(round(sum(active) / sum(click) * 100,3),0) active_rate, sum(register) register, ifnull(round(sum(cost) / sum(register), 3),0) active_register_cost, ifnull(round(sum(active_register_rate)/{day_count} * 100,3),0) active_register_rate, sum(next_day_open) next_day_open, ifnull(round(sum(cost) / sum(next_day_open),3),0) next_day_open_cost, ifnull(round(sum(next_day_open) / sum(active) * 100,3),0) next_day_open_rate, sum(attribution_next_day_open_cnt) attribution_next_day_open_cnt, ifnull(round(sum(cost) / sum(attribution_next_day_open_cnt),3),0) attribution_next_day_open_cost, ifnull(round(sum(attribution_next_day_open_cnt) / sum(active) * 100,3),0) attribution_next_day_open_rate, sum(game_addiction) game_addiction, ifnull(round(sum(cost) / sum(game_addiction),3),0) game_addiction_cost, ifnull(round(sum(game_addiction_rate)/{day_count} * 100,3),0) game_addiction_rate, sum(pay_count) pay_count, ifnull(round(sum(cost) / sum(pay_count), 3),0) active_pay_cost, ifnull(round(sum(pay_count) / sum(active) * 100,3),0) active_pay_rate, sum(loan_completion) loan_completion, ifnull(round(sum(cost) / sum(loan_completion),3),0) loan_completion_cost, ifnull(round(sum(loan_completion) / sum(register) * 100,3),0) loan_completion_rate, ifnull(round(sum(pre_loan_credit), 3),0) pre_loan_credit, ifnull(round(sum(cost) / sum(pre_loan_credit),3),0) pre_loan_credit_cost, sum(loan_credit) loan_credit, ifnull(round(sum(cost) / sum(loan_credit),3),0) loan_credit_cost, ifnull(round(sum(loan_credit) / sum(loan_completion) * 100,3),0) loan_credit_rate, sum(in_app_uv) in_app_uv, sum(in_app_detail_uv) in_app_detail_uv, sum(in_app_cart) in_app_cart, sum(in_app_pay) in_app_pay, sum(in_app_order) in_app_order, ifnull(round(sum(attribution_game_pay_7d_count)/{day_count},3),0) attribution_game_pay_7d_count, ifnull(round(sum(attribution_game_pay_7d_cost)/{day_count},3),0) attribution_game_pay_7d_cost, ifnull(round(sum(attribution_active_pay_7d_per_count)/{day_count},3),0) attribution_active_pay_7d_per_count, sum(game_pay_cost) game_pay_cost, sum(game_pay_count) game_pay_count, sum(phone) phone, sum(form) form, sum(map_search) map_search, sum(button) button, sum(`view`) `view`, sum(download) download, sum(qq) qq, sum(lottery) lottery, sum(vote) vote, sum(message) message, sum(redirect) redirect, sum(shopping) shopping, sum(consult) consult, sum(wechat) wechat, sum(phone_confirm) phone_confirm, sum(phone_connect) phone_connect, sum(consult_effective) consult_effective, sum(coupon) coupon, sum(coupon_single_page) coupon_single_page, sum(redirect_to_shop) redirect_to_shop, sum(poi_collect) poi_collect, sum(poi_address_click) poi_address_click, sum(luban_order_cnt) luban_order_cnt, sum(luban_order_stat_amount) luban_order_stat_amount, sum(luban_order_roi) luban_order_roi, sum(luban_live_enter_cnt) luban_live_enter_cnt, sum(live_watch_one_minute_count) live_watch_one_minute_count, sum(luban_live_follow_cnt) luban_live_follow_cnt, sum(live_fans_club_join_cnt) live_fans_club_join_cnt, sum(luban_live_comment_cnt) luban_live_comment_cnt, sum(luban_live_share_cnt) luban_live_share_cnt, sum(luban_live_gift_cnt) luban_live_gift_cnt, sum(luban_live_gift_amount) luban_live_gift_amount, sum(luban_live_slidecart_click_cnt) luban_live_slidecart_click_cnt, sum(luban_live_click_product_cnt) luban_live_click_product_cnt, sum(luban_live_pay_order_count) luban_live_pay_order_count, sum(luban_live_pay_order_stat_cost) luban_live_pay_order_stat_cost, sum(live_component_click_count) live_component_click_count, ifnull(round(sum(cost) / sum(live_component_click_count),3),0) live_component_click_cost, ifnull(round(sum(live_component_click_count)/{day_count} * 100,3),0) live_component_click_rate, sum(wechat_login_count) wechat_login_count, sum(attribution_wechat_login_30d_count) attribution_wechat_login_30d_count, ifnull(round(sum(cost) / sum(wechat_login_count),3),0) wechat_login_cost, ifnull(round(sum(cost) / sum(attribution_wechat_login_30d_count),3),0) attribution_wechat_login_30d_cost, sum(wechat_first_pay_count) wechat_first_pay_count, sum(attribution_wechat_first_pay_30d_count) attribution_wechat_first_pay_30d_count, ifnull(round(sum(cost) / sum(wechat_first_pay_count),3),0) wechat_first_pay_cost, ifnull(round(sum(cost) / sum(attribution_wechat_first_pay_30d_count),3),0) attribution_wechat_first_pay_30d_cost, ifnull(round(sum(wechat_first_pay_count) / sum(wechat_login_count) * 100,3),0) wechat_first_pay_rate, ifnull(round(sum(attribution_wechat_first_pay_30d_count) / sum(attribution_wechat_login_30d_count) * 100,3),0) attribution_wechat_first_pay_30d_rate, sum(wechat_pay_amount) wechat_pay_amount, sum(attribution_wechat_pay_30d_amount) attribution_wechat_pay_30d_amount, sum(attribution_wechat_pay_30d_roi) attribution_wechat_pay_30d_roi, sum(phone_effective) phone_effective, sum(total_play) total_play, sum(valid_play) valid_play, ifnull(round(sum(cost) / sum(valid_play),3),0) valid_play_cost, ifnull(round(sum(valid_play) / sum(`show`) * 100,3),0) valid_play_rate, sum(play_25_feed_break) play_25_feed_break, sum(play_50_feed_break) play_50_feed_break, sum(play_75_feed_break) play_75_feed_break, sum(play_100_feed_break) play_100_feed_break, ifnull(round(sum(average_play_time_per_play)/{day_count},3),0) average_play_time_per_play, ifnull(round(sum(play_over_rate)/{day_count} * 100,3),0) play_over_rate, ifnull(round(sum(wifi_play) / sum(total_play) * 100,3),0) wifi_play_rate, sum(wifi_play) wifi_play, ifnull(round(sum(play_duration_sum)/{day_count}, 3),0) play_duration_sum, sum(advanced_creative_phone_click) advanced_creative_phone_click, sum(advanced_creative_counsel_click) advanced_creative_counsel_click, sum(advanced_creative_form_click) advanced_creative_form_click, sum(advanced_creative_coupon_addition) advanced_creative_coupon_addition, sum(advanced_creative_form_submit) advanced_creative_form_submit, sum(card_show) card_show, sum(`share`) `share`, sum(`comment`) `comment`, sum(`like`) `like`, sum(follow) follow, sum(home_visited) home_visited, sum(ies_challenge_click) ies_challenge_click, sum(ies_music_click) ies_music_click, sum(location_click) location_click, sum(message_action) message_action, sum(click_landing_page) click_landing_page, sum(click_shopwindow) click_shopwindow, sum(click_website) click_website, sum(click_download) click_download, sum(click_call_dy) click_call_dy, sum(submit_certification_count) submit_certification_count, sum(approval_count) approval_count, sum(first_order_count) first_rental_order_count, sum(first_rental_order_count) first_order_count, sum(commute_first_pay_count) commute_first_pay_count FROM ( SELECT report.advertiser_id account_id, dep.company_id, CASE WHEN dep.company_id = 'd57fecdcf7a94d009736d9c850731582' THEN '北京汇创思拓数字科技有限公司' WHEN dep.company_id = '6608ed13c0dd42de93c790dbdf124234' THEN '广州汇创思拓数字科技有限公司' WHEN dep.company_id = '4b10089775c040119e139087517aed88' THEN '上海汇创思拓数字科技有限公司' ELSE dep.company_name END company_name, dep.org_code, dep.org_type, dep.dep_id, dep.depart_name, dep.user_id, dep.realname, dep.leader_id, dep.leader_name, report.cost, report.`show`, report.avg_show_cost, report.click, report.avg_click_cost, report.ctr, report.`convert`, report.convert_cost, report.convert_rate, report.deep_convert, report.deep_convert_cost, report.deep_convert_rate, report.attribution_convert, report.attribution_convert_cost, report.attribution_deep_convert, report.attribution_deep_convert_cost, report.download_start, report.download_start_cost, report.download_start_rate, report.download_finish, report.download_finish_cost, report.download_finish_rate, report.click_install, report.install_finish, report.install_finish_cost, report.install_finish_rate, report.active, report.active_cost, report.active_rate, report.register, report.active_register_cost, report.active_register_rate, report.next_day_open, report.next_day_open_cost, report.next_day_open_rate, report.attribution_next_day_open_cnt, report.attribution_next_day_open_cost, report.attribution_next_day_open_rate, report.game_addiction, report.game_addiction_cost, report.game_addiction_rate, report.pay_count, report.active_pay_cost, report.active_pay_rate, report.loan_completion, report.loan_completion_cost, report.loan_completion_rate, report.pre_loan_credit, report.pre_loan_credit_cost, report.loan_credit, report.loan_credit_cost, report.loan_credit_rate, report.in_app_uv, report.in_app_detail_uv, report.in_app_cart, report.in_app_pay, report.in_app_order, report.attribution_game_pay_7d_count, report.attribution_game_pay_7d_cost, report.attribution_active_pay_7d_per_count, report.game_pay_cost, report.game_pay_count, report.phone, report.form, report.map_search, report.button, report.`view`, report.download, report.qq, report.lottery, report.vote, report.message, report.redirect, report.shopping, report.consult, report.wechat, report.phone_confirm, report.phone_connect, report.consult_effective, report.coupon, report.coupon_single_page, report.redirect_to_shop, report.poi_collect, report.poi_address_click, report.luban_order_cnt, report.luban_order_stat_amount, report.luban_order_roi, report.luban_live_enter_cnt, report.live_watch_one_minute_count, report.luban_live_follow_cnt, report.live_fans_club_join_cnt, report.luban_live_comment_cnt, report.luban_live_share_cnt, report.luban_live_gift_cnt, report.luban_live_gift_amount, report.luban_live_slidecart_click_cnt, report.luban_live_click_product_cnt, report.luban_live_pay_order_count, report.luban_live_pay_order_stat_cost, report.live_component_click_count, report.live_component_click_cost, report.live_component_click_rate, report.wechat_login_count, report.attribution_wechat_login_30d_count, report.wechat_login_cost, report.attribution_wechat_login_30d_cost, report.wechat_first_pay_count, report.attribution_wechat_first_pay_30d_count, report.wechat_first_pay_cost, report.attribution_wechat_first_pay_30d_cost, report.wechat_first_pay_rate, report.attribution_wechat_first_pay_30d_rate, report.wechat_pay_amount, report.attribution_wechat_pay_30d_amount, report.attribution_wechat_pay_30d_roi, report.phone_effective, report.total_play, report.valid_play, report.valid_play_cost, report.valid_play_rate, report.play_25_feed_break, report.play_50_feed_break, report.play_75_feed_break, report.play_100_feed_break, report.average_play_time_per_play, report.play_over_rate, report.wifi_play_rate, report.wifi_play, report.play_duration_sum, report.advanced_creative_phone_click, report.advanced_creative_counsel_click, report.advanced_creative_form_click, report.advanced_creative_coupon_addition, report.advanced_creative_form_submit, report.card_show, report.`share`, report.`comment`, report.`like`, report.follow, report.home_visited, report.ies_challenge_click, report.ies_music_click, report.location_click, report.message_action, report.click_landing_page, report.click_shopwindow, report.click_website, report.click_download, report.click_call_dy, report.submit_certification_count, report.approval_count, report.first_rental_order_count, report.first_order_count, report.commute_first_pay_count FROM ( SELECT * FROM media_api.bytedance_advertiser_report_daily WHERE stat_datetime >= date_format( date_sub({date_time}, INTERVAL ({day_count} -1 ) DAY), '%Y%m%d' ) ) report LEFT JOIN ( SELECT cua.account_id, uc.company_id, uc.company_name, pa.org_code, pa.org_type, pa.id dep_id, pa.depart_name, cua.user_id, su.realname , cp.sale_id, su.leader_id, se.realname as leader_name FROM `jeecg-boot`.ctop_user_allocation cua LEFT JOIN `jeecg-boot`.ctop_project cp ON cua.project_id = cp.id LEFT JOIN `jeecg-boot`.user_company uc ON uc.user_id = cua.user_id LEFT JOIN `jeecg-boot`.sys_user su ON su.id = cua.user_id LEFT JOIN `jeecg-boot`.sys_user se ON se.id = su.leader_id LEFT JOIN `jeecg-boot`.sys_user_depart de ON su.leader_id = de.user_id LEFT JOIN `jeecg-boot`.sys_depart pa ON pa.id = de.dep_id ) dep ON report.advertiser_id = dep.account_id ) info GROUP BY company_id, leader_id) new LEFT JOIN (select ifnull(su.leader_id,'-') leader_id,sum(report.cost) cost from (select advertiser_id account_id,sum(cost) cost from media_api.bytedance_advertiser_report_daily where stat_datetime between date_format(date_sub(date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d'), INTERVAL {day_count}-1 DAY),'%Y%m%d') and date_format(date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d') group by advertiser_id) report LEFT JOIN `jeecg-boot`.ctop_user_allocation cua ON report.account_id = cua.account_id LEFT JOIN `jeecg-boot`.sys_user su ON su.id = cua.user_id group by su.leader_id) ratio ON new.leader_id=ratio.leader_id ; """.format(date_time=self.date_time, day_count=self.day_count) TidbConn.upsert(conn=self.conn, sql_buff=sql) self.conn.commit() TidbConn.conn_close(conn=self.conn) return 0 except Exception as e: logging.error(e) return -1 if __name__ == '__main__': status = AppBytedanceCompanyUserReportBusi().taskHandler() if status == 0: print("作业执行成功") exit(0) else: print("作业执行失败") exit(-1)