AppBytedanceAccountBaseInfoDsD.py 9.8 KB

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