AppBytedanceAccountBaseInfoDsD.py 12 KB

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