AppKuaishouAccountBaseInfoDsD.py 9.1 KB

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