AppBytedanceAccountBaseInfoDsD.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. # Author zhaohailiang
  2. # coding=utf-8
  3. # @Time : 2022/01/19 21:10 下午
  4. # @Site :
  5. # @File : AppBytedanceAccountBaseInfoDsD.py
  6. # @Software: PyCharm
  7. # @contact: zhaohailiang@c-top.com.cn
  8. # @Tel 18201631162
  9. import logging
  10. from Apietl.conn.TidbConn import TidbConn
  11. from Apietl.utlis.Utils import Utils
  12. class AppBytedanceAccountBaseInfoDsD:
  13. def __init__(self):
  14. self.conn = TidbConn.get_connection()
  15. self.parm = Utils.get_task_args()
  16. self.date_time = self.parm[0]
  17. def taskHandler(self):
  18. try:
  19. sql = """
  20. SET sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
  21. replace into `application`.`app_bytedance_account_base_info_d`
  22. (stat_date,
  23. account_id,
  24. account_name,
  25. project_id,
  26. project_name,
  27. product_id,
  28. product_name,
  29. user_id,
  30. user_name,
  31. leader_id,
  32. leader_name,
  33. company_id,
  34. company_name,
  35. dept_id,
  36. dept_name,
  37. all_campaign_num,
  38. new_campaign_num,
  39. all_ad_num,
  40. new_ad_num,
  41. all_video_num,
  42. new_video_num,
  43. effect_num,
  44. new_effect_num,
  45. hot_num,
  46. new_hot_num,
  47. put_campaign_num,
  48. put_unit_num,
  49. trust_campaign_num,
  50. trust_unit_num,
  51. new_cpd_project_num,
  52. promotion_num)
  53. select {date_time} stat_date,
  54. t1.account_id,
  55. bei.account_name,
  56. bei.project_id,
  57. bei.project_name,
  58. bei.product_id,
  59. bei.product_name,
  60. bei.user_id,
  61. bei.user_name,
  62. bei.leader_id,
  63. bei.leader_name,
  64. bei.company_id,
  65. bei.company_name,
  66. bei.dept_id,
  67. bei.dept_name,
  68. 0 as all_campaign_num,
  69. 0 new_campaign_num,
  70. 0 as all_ad_num,
  71. 0 new_ad_num,
  72. 0 as all_video_num,
  73. t3.day_num new_video_num,
  74. 0 effect_num,
  75. 0 as new_effect_num,
  76. sum_hot.new_hot_material as hot_num,
  77. ifnull(t5.new_hot_material, 0) as new_hot_num,
  78. 0 as put_campaign_num,
  79. 0 as put_unit_num,
  80. 0 as trust_campaign_num,
  81. 0 as trust_unit_num,
  82. project_num as new_cpd_project_num,
  83. promotion_num as promotion_num
  84. from (select * from `jeecg-boot`.ctop_user_allocation where media_id = 1) t1
  85. left join
  86. (select advertiser_id account_id, count(distinct signature) day_num
  87. from `media_api`.bytedance_file_video_get
  88. where date_format(create_time, '%Y%m%d') = {date_time}
  89. group by advertiser_id) t3
  90. on t1.account_id = t3.account_id
  91. left join(select advertiser_id, count(distinct project_id) as project_num
  92. from media_api.bytedance_cdp_project
  93. where date_format(project_create_time, '%Y%m%d') = {date_time}
  94. group by advertiser_id) pro
  95. on t1.account_id = pro.advertiser_id
  96. left join(select advertiser_id, count(distinct promotion_id) as promotion_num
  97. from media_api.bytedance_promotion_list
  98. where date_format(promotion_create_time, '%Y%m%d') = {date_time}
  99. group by advertiser_id) promotion
  100. on t1.account_id = promotion.advertiser_id
  101. left join
  102. (select t2.account_id, count(distinct t1.signature) new_hot_material
  103. from (select signature from application.hot_material where hot_date = {date_time} and media_id = 1) t1
  104. left join
  105. (select distinct advertiser_id account_id, signature
  106. from `media_api`.bytedance_file_video_get
  107. where date_format(create_time, '%Y%m%d') >=
  108. date_sub({date_time}, INTERVAL 29 DAY)) t2
  109. on t1.signature = t2.signature
  110. group by t2.account_id) t5
  111. on t1.account_id = t5.account_id
  112. left join
  113. (select t2.account_id, count(distinct t1.signature) new_hot_material
  114. from (select signature from application.hot_material where hot_date <= {date_time}) t1
  115. left join
  116. (select distinct advertiser_id account_id, signature
  117. from `media_api`.bytedance_file_video_get
  118. where date_format(create_time, '%Y%m%d') <= {date_time}) t2
  119. on t1.signature = t2.signature
  120. group by t2.account_id) sum_hot
  121. on t1.account_id = sum_hot.account_id
  122. left join
  123. (SELECT cua.account_id id,
  124. cua.account_id,
  125. cp.id project_id,
  126. product.id product_id,
  127. product.product_name,
  128. ca.id advertiser_id,
  129. uc.company_id company_id,
  130. cua.auth_name account_name,
  131. cp.project_name project_name,
  132. ca.`name` advertiser_name,
  133. uc.company_name company_name,
  134. cp.sale_id sale_id,
  135. su.id user_id,
  136. su.realname user_name,
  137. su.leader_id,
  138. lea.realname leader_name,
  139. su.org_code org_code,
  140. dep.dep_id dept_id,
  141. part.depart_name dept_name
  142. FROM `jeecg-boot`.ctop_user_allocation cua
  143. LEFT JOIN `jeecg-boot`.ctop_project cp ON cua.project_id = cp.id
  144. LEFT JOIN `jeecg-boot`.ctop_advertiser ca ON ca.id = cua.advertiser_id
  145. LEFT JOIN `jeecg-boot`.sys_user su ON su.id = cua.user_id
  146. LEFT JOIN `jeecg-boot`.sys_user lea ON lea.id = su.leader_id
  147. LEFT JOIN `jeecg-boot`.user_company uc ON uc.user_id = cua.user_id
  148. LEFT JOIN `jeecg-boot`.ctop_product product ON product.id = cp.product_id
  149. LEFT JOIN `jeecg-boot`.sys_user_depart dep ON cua.user_id = dep.user_id
  150. LEFT JOIN `jeecg-boot`.sys_depart part ON dep.dep_id = part.id
  151. WHERE cua.account_id IS NOT NULL
  152. and cua.account_status = 0
  153. and cua.media_id = 1) bei
  154. ON t1.account_id = bei.account_id
  155. where bei.account_id is not null
  156. """.format(date_time=self.date_time)
  157. print(sql)
  158. TidbConn.upsert(conn=self.conn, sql_buff=sql)
  159. self.conn.commit()
  160. TidbConn.conn_close(conn=self.conn)
  161. return 0
  162. except Exception as e:
  163. logging.error(e)
  164. return -1
  165. if __name__ == '__main__':
  166. status = AppBytedanceAccountBaseInfoDsD().taskHandler()
  167. if status == 0:
  168. print("作业执行成功")
  169. exit(0)
  170. else:
  171. print("作业执行失败")
  172. exit(-1)