AppKuaishouAccountBaseInfoDsD.py 8.5 KB

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