AppBytedanceAccountBaseInfoDsD.py 11 KB

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