AppBytedanceAccountBaseInfoDsD.py 9.4 KB

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