AppKuaishouAccountBaseInfoDsD.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # Author zhaohailiang
  2. # coding=utf-8
  3. # @Time : 2022/01/19 21:10 下午
  4. # @Site :
  5. # @File : AppKuaishouAccountBaseInfoDsD.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 AppKuaishouAccountBaseInfoDsD:
  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_kuaishou_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_unit_num,
  41. new_unit_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 * from (
  54. select
  55. {date_time} stat_date,
  56. t1.account_id,
  57. bei.account_name,
  58. bei.project_id,
  59. bei.project_name,
  60. bei.product_id,
  61. bei.product_name,
  62. bei.user_id,
  63. bei.user_name,
  64. bei.leader_id,
  65. bei.leader_name,
  66. bei.company_id,
  67. bei.company_name,
  68. bei.dept_id,
  69. bei.dept_name,
  70. t2.cam_num as all_campaign_num ,
  71. t2.cam_num new_campaign_num,
  72. t2.unit_num as all_ad_num,
  73. t2.unit_num new_ad_num,
  74. t3.day_num as all_video_num,
  75. t3.day_num new_video_num,
  76. ifnull(t4.new_eff_material,0) as effect_num,
  77. ifnull(t4.new_eff_material,0) as new_effect_num,
  78. ifnull(t5.new_hot_material,0) as hot_num,
  79. ifnull(t5.new_hot_material,0) as new_hot_num,
  80. 0 put_campaign_num,
  81. 0 put_unit_num,
  82. trush_cam_num,
  83. trush_unit_num
  84. from
  85. (select account_id from `jeecg-boot`.ctop_user_allocation where media_id=2)t1
  86. left join
  87. (select distinct account_id,signature from `media_api`.kuaishou_video_list where date_format(stat_date,'%Y%m%d')={date_time}) ty
  88. on t1.account_id=ty.account_id
  89. left join
  90. (select advertiser_id account_id,count(distinct campaign_id) cam_num ,count(unit_id) unit_num from `media_api`.kuaishou_ad_unit_list where date_format(create_time,'%Y%m%d')={date_time}
  91. and (unit_source!=1 or unit_source is null)
  92. group by advertiser_id) t2
  93. on t1.account_id=t2.account_id
  94. left join
  95. (select account_id,count(distinct signature) day_num from `media_api`.kuaishou_video_list
  96. where date_format(stat_date,'%Y%m%d')={date_time} group by account_id) t3
  97. on t1.account_id=t3.account_id
  98. left join
  99. (select t2.account_id,count(distinct t1.material_id) new_eff_material from
  100. (select material_id from application.app_kuaishou_material_info where effect_date={date_time}) t1
  101. inner join
  102. (select distinct account_id,signature from `media_api`.kuaishou_video_list where date_format(stat_date,'%Y%m%d')>=
  103. date_sub({date_time},INTERVAL 29 DAY)) t2
  104. on t1.material_id=t2.signature group by t2.account_id) t4
  105. on t1.account_id=t4.account_id
  106. left join
  107. (select t2.account_id,count(distinct t1.material_id) new_hot_material from
  108. (select material_id from application.app_kuaishou_material_info where faddish_date={date_time}) t1
  109. inner join
  110. (select distinct account_id,signature from `media_api`.kuaishou_video_list where date_format(stat_date,'%Y%m%d')>=
  111. date_sub({date_time},INTERVAL 29 DAY)) t2
  112. on t1.material_id=t2.signature group by t2.account_id) t5
  113. on t1.account_id=t5.account_id
  114. left join
  115. (
  116. select advertiser_id account_id,count(unit_id) trush_unit_num, count(distinct campaign_id) trush_cam_num from `media_api`.kuaishou_ad_unit_list where date_format(create_time,'%Y%m%d')={date_time}
  117. and unit_source=1
  118. group by advertiser_id
  119. )trush
  120. on t1.account_id=trush.account_id
  121. left join
  122. (
  123. SELECT
  124. cua.account_id id,
  125. cua.account_id,
  126. cp.id project_id,
  127. product.id product_id,
  128. product.product_name,
  129. ca.id advertiser_id,
  130. uc.company_id company_id,
  131. cua.auth_name account_name,
  132. cp.project_name project_name,
  133. ca.`name` advertiser_name,
  134. uc.company_name company_name,
  135. cp.sale_id sale_id,
  136. su.id user_id,
  137. su.realname user_name,
  138. su.leader_id,
  139. lea.realname leader_name,
  140. su.org_code org_code,
  141. dep.dep_id dept_id,
  142. part.depart_name dept_name
  143. FROM
  144. `jeecg-boot`.ctop_user_allocation cua
  145. LEFT JOIN `jeecg-boot`.ctop_project cp ON cua.project_id = cp.id
  146. LEFT JOIN `jeecg-boot`.ctop_advertiser ca ON ca.id = cua.advertiser_id
  147. LEFT JOIN `jeecg-boot`.sys_user su ON su.id = cua.user_id
  148. LEFT JOIN `jeecg-boot`.sys_user lea ON lea.id = su.leader_id
  149. LEFT JOIN `jeecg-boot`.user_company uc ON uc.user_id = cua.user_id
  150. LEFT JOIN `jeecg-boot`.ctop_product product ON product.id = cp.product_id
  151. LEFT JOIN `jeecg-boot`.sys_user_depart dep ON cua.user_id=dep.user_id
  152. LEFT JOIN `jeecg-boot`.sys_depart part ON dep.dep_id=part.id
  153. WHERE
  154. cua.account_id IS NOT NULL and cua.account_status=0 and cua.media_id=2
  155. ) bei
  156. ON t1.account_id = bei.account_id where bei.account_id is not null) m
  157. group by account_id;
  158. """.format(date_time=self.date_time)
  159. print(sql)
  160. TidbConn.upsert(conn=self.conn, sql_buff=sql)
  161. self.conn.commit()
  162. TidbConn.conn_close(conn=self.conn)
  163. return 0
  164. except Exception as e:
  165. logging.error(e)
  166. return -1
  167. if __name__ == '__main__':
  168. status = AppKuaishouAccountBaseInfoDsD().taskHandler()
  169. if status == 0:
  170. print("作业执行成功")
  171. exit(0)
  172. else:
  173. print("作业执行失败")
  174. exit(-1)