AppBytedanceAccountBaseInfoDsD.py 9.3 KB

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