AppBytedanceAccountBaseInfoDsD.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. (
  23. stat_date,
  24. account_id,
  25. account_name,
  26. project_id,
  27. project_name,
  28. product_id,
  29. product_name,
  30. user_id,
  31. user_name,
  32. leader_id,
  33. leader_name,
  34. company_id,
  35. company_name,
  36. dept_id,
  37. dept_name,
  38. all_campaign_num,
  39. new_campaign_num,
  40. all_ad_num,
  41. new_ad_num,
  42. all_video_num,
  43. new_video_num,
  44. effect_num,
  45. new_effect_num,
  46. hot_num,
  47. new_hot_num,
  48. put_campaign_num,
  49. put_unit_num,
  50. trust_campaign_num,
  51. trust_unit_num,
  52. new_cpd_project_num
  53. )
  54. select {date_time} stat_date,
  55. t1.account_id,
  56. bei.account_name,
  57. bei.project_id,
  58. bei.project_name,
  59. bei.product_id,
  60. bei.product_name,
  61. bei.user_id,
  62. bei.user_name,
  63. bei.leader_id,
  64. bei.leader_name,
  65. bei.company_id,
  66. bei.company_name,
  67. bei.dept_id,
  68. bei.dept_name,
  69. sum_cam.cam_num as all_campaign_num,
  70. cam.campain_num new_campaign_num,
  71. sum_ad.ad_num as all_ad_num,
  72. t2.ad_num new_ad_num,
  73. video_num as all_video_num,
  74. t3.day_num new_video_num,
  75. sum_eff.new_eff_material effect_num,
  76. ifnull(t4.new_eff_material, 0) as new_effect_num,
  77. sum_hot.new_hot_material as hot_num,
  78. ifnull(t5.new_hot_material, 0) as new_hot_num,
  79. 0 as put_campaign_num,
  80. 0 as put_unit_num,
  81. 0 as trust_campaign_num,
  82. 0 as trust_unit_num,
  83. project_num as new_cpd_project_num
  84. from (select * from `jeecg-boot`.ctop_user_allocation where media_id = 1) t1
  85. left join
  86. (select account_id, count(id) ad_num
  87. from `media_api`.bytedance_ad_get
  88. where date_format(ad_create_time, '%Y%m%d') = {date_time}
  89. group by account_id) t2
  90. on t1.account_id = t2.account_id
  91. left join(
  92. select account_id, count(id) ad_num
  93. from `media_api`.bytedance_ad_get
  94. where date_format(ad_create_time, '%Y%m%d') <= {date_time}
  95. group by account_id
  96. ) sum_ad on t1.account_id = sum_ad.account_id
  97. left join (select advertiser_id, count(campaign_id) campain_num
  98. from media_api.bytedance_campaign_get
  99. where date_format(campaign_create_time, '%Y%m%d') = {date_time}
  100. group by advertiser_id) cam
  101. on t1.account_id = cam.advertiser_id
  102. left join(
  103. select advertiser_id, count(campaign_id) cam_num
  104. from `media_api`.bytedance_campaign_get
  105. where date_format(campaign_create_time, '%Y%m%d') <= {date_time}
  106. group by advertiser_id
  107. ) sum_cam on t1.account_id = sum_cam.advertiser_id
  108. left join
  109. (select advertiser_id account_id, count(distinct signature) day_num
  110. from `media_api`.bytedance_file_video_get
  111. where date_format(create_time, '%Y%m%d') = {date_time}
  112. group by advertiser_id) t3
  113. on t1.account_id = t3.account_id
  114. left join
  115. (select advertiser_id account_id, count(distinct signature) video_num
  116. from `media_api`.bytedance_file_video_get
  117. where date_format(create_time, '%Y%m%d') <= {date_time}
  118. group by advertiser_id) all_video
  119. on t1.account_id = all_video.account_id
  120. left join
  121. (select t2.account_id, count(distinct t1.signature) new_eff_material
  122. from (select signature from application.effect_material where effect_date = {date_time}) t1
  123. inner join
  124. (select distinct advertiser_id account_id, signature
  125. from `media_api`.bytedance_file_video_get
  126. where date_format(create_time, '%Y%m%d') >=
  127. date_sub({date_time}, INTERVAL 29 DAY)) t2
  128. on t1.signature = t2.signature
  129. group by t2.account_id) t4
  130. on t1.account_id = t4.account_id
  131. left join
  132. (select t2.account_id, count(distinct t1.signature) new_eff_material
  133. from (select signature from application.effect_material where effect_date <= {date_time}) t1
  134. inner join
  135. (select distinct advertiser_id account_id, signature
  136. from `media_api`.bytedance_file_video_get
  137. where date_format(create_time, '%Y%m%d') <= {date_time}) t2
  138. on t1.signature = t2.signature
  139. group by t2.account_id) sum_eff
  140. on t1.account_id = sum_eff.account_id
  141. left join(
  142. select advertiser_id,count(distinct project_id) as project_num from media_api.bytedance_cdp_project
  143. where date_format(project_create_time, '%Y%m%d') = {date_time}
  144. group by advertiser_id
  145. )pro
  146. on t1.account_id=pro.advertiser_id
  147. left join(
  148. select advertiser_id,count(distinct promotion_id) as project_num from media_api.bytedance_promotion_list
  149. where date_format(promotion_create_time, '%Y%m%d') = {date_time}
  150. group by advertiser_id
  151. )promotion
  152. on t1.account_id=promotion.advertiser_id
  153. left join
  154. (select t2.account_id, count(distinct t1.signature) new_hot_material
  155. from (select signature from application.hot_material where hot_date = {date_time} and media_id=1) t1
  156. inner join
  157. (select distinct advertiser_id account_id, signature
  158. from `media_api`.bytedance_file_video_get
  159. where date_format(create_time, '%Y%m%d') >=
  160. date_sub({date_time}, INTERVAL 29 DAY)) t2
  161. on t1.signature = t2.signature
  162. group by t2.account_id) t5
  163. on t1.account_id = t5.account_id
  164. left join
  165. (select t2.account_id, count(distinct t1.signature) new_hot_material
  166. from (select signature from application.hot_material where hot_date<= {date_time}) t1
  167. inner join
  168. (select distinct advertiser_id account_id, signature
  169. from `media_api`.bytedance_file_video_get
  170. where date_format(create_time, '%Y%m%d') <= {date_time}) t2
  171. on t1.signature = t2.signature
  172. group by t2.account_id) sum_hot
  173. on t1.account_id = sum_hot.account_id
  174. left join
  175. (
  176. SELECT cua.account_id id,
  177. cua.account_id,
  178. cp.id project_id,
  179. product.id product_id,
  180. product.product_name,
  181. ca.id advertiser_id,
  182. uc.company_id company_id,
  183. cua.auth_name account_name,
  184. cp.project_name project_name,
  185. ca.`name` advertiser_name,
  186. uc.company_name company_name,
  187. cp.sale_id sale_id,
  188. su.id user_id,
  189. su.realname user_name,
  190. su.leader_id,
  191. lea.realname leader_name,
  192. su.org_code org_code,
  193. dep.dep_id dept_id,
  194. part.depart_name dept_name
  195. FROM `jeecg-boot`.ctop_user_allocation cua
  196. LEFT JOIN `jeecg-boot`.ctop_project cp ON cua.project_id = cp.id
  197. LEFT JOIN `jeecg-boot`.ctop_advertiser ca ON ca.id = cua.advertiser_id
  198. LEFT JOIN `jeecg-boot`.sys_user su ON su.id = cua.user_id
  199. LEFT JOIN `jeecg-boot`.sys_user lea ON lea.id = su.leader_id
  200. LEFT JOIN `jeecg-boot`.user_company uc ON uc.user_id = cua.user_id
  201. LEFT JOIN `jeecg-boot`.ctop_product product ON product.id = cp.product_id
  202. LEFT JOIN `jeecg-boot`.sys_user_depart dep ON cua.user_id = dep.user_id
  203. LEFT JOIN `jeecg-boot`.sys_depart part ON dep.dep_id = part.id
  204. WHERE cua.account_id IS NOT NULL
  205. and cua.account_status = 0
  206. and cua.media_id = 1
  207. ) bei
  208. ON t1.account_id = bei.account_id
  209. where bei.account_id is not null
  210. """.format(date_time=self.date_time)
  211. print(sql)
  212. TidbConn.upsert(conn=self.conn, sql_buff=sql)
  213. self.conn.commit()
  214. TidbConn.conn_close(conn=self.conn)
  215. return 0
  216. except Exception as e:
  217. logging.error(e)
  218. return -1
  219. if __name__ == '__main__':
  220. status = AppBytedanceAccountBaseInfoDsD().taskHandler()
  221. if status == 0:
  222. print("作业执行成功")
  223. exit(0)
  224. else:
  225. print("作业执行失败")
  226. exit(-1)