AppBytedanceAccountBaseInfoDsD.py 8.7 KB

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