AppKuaishouAccountBaseInfoDsD.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. 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_unit_num,0)+ifnull(t2.unit_num,0)) as all_ad_num,
  70. t2.unit_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_kuaishou_account_base_info_d` where stat_date= date_sub({date_time}, INTERVAL 1 DAY)) t1
  80. left join
  81. (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}
  82. group by advertiser_id) t2
  83. on t1.account_id=t2.account_id
  84. left join
  85. (select account_id,count(distinct signature) day_num from `media_api`.kuaishou_video_list
  86. where date_format(stat_date,'%Y%m%d')={date_time} group by account_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 account_id,signature from `media_api`.kuaishou_video_list where date_format(stat_date,'%Y%m%d')<={date_time}) a
  91. left join (select distinct signature from application.effect_material where media_id=2) b
  92. on a.signature=b.signature
  93. left join (select distinct signature from application.hot_material where media_id=2) 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=2
  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.unit_num as all_ad_num,
  153. t2.unit_num new_ad_num,
  154. t3.day_num as all_video_num,
  155. t3.day_num new_video_num,
  156. 0 effect_num,
  157. 0 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 account_id,signature from `media_api`.kuaishou_video_list where date_format(stat_date,'%Y%m%d')={date_time}) t1
  164. left join
  165. (select * from `application`.`app_kuaishou_account_base_info_d` where stat_date= date_sub({date_time}, INTERVAL 1 DAY)) tt
  166. on t1.account_id=tt.account_id
  167. left join
  168. (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}
  169. group by advertiser_id) t2
  170. on t1.account_id=t2.account_id
  171. left join
  172. (select account_id,count(distinct signature) day_num from `media_api`.kuaishou_video_list
  173. where date_format(stat_date,'%Y%m%d')={date_time} group by account_id) t3
  174. on t1.account_id=t3.account_id
  175. left join
  176. (
  177. SELECT
  178. cua.account_id id,
  179. cua.account_id,
  180. cp.id project_id,
  181. product.id product_id,
  182. product.product_name,
  183. ca.id advertiser_id,
  184. uc.company_id company_id,
  185. cua.auth_name account_name,
  186. cp.project_name project_name,
  187. ca.`name` advertiser_name,
  188. uc.company_name company_name,
  189. cp.sale_id sale_id,
  190. su.id user_id,
  191. su.realname user_name,
  192. su.leader_id,
  193. lea.realname leader_name,
  194. su.org_code org_code,
  195. dep.dep_id dept_id,
  196. part.depart_name dept_name
  197. FROM
  198. `jeecg-boot`.ctop_user_allocation cua
  199. LEFT JOIN `jeecg-boot`.ctop_project cp ON cua.project_id = cp.id
  200. LEFT JOIN `jeecg-boot`.ctop_advertiser ca ON ca.id = cua.advertiser_id
  201. LEFT JOIN `jeecg-boot`.sys_user su ON su.id = cua.user_id
  202. LEFT JOIN `jeecg-boot`.sys_user lea ON lea.id = su.leader_id
  203. LEFT JOIN `jeecg-boot`.user_company uc ON uc.user_id = cua.user_id
  204. LEFT JOIN `jeecg-boot`.ctop_product product ON product.id = cp.product_id
  205. LEFT JOIN `jeecg-boot`.sys_user_depart dep ON cua.user_id=dep.user_id
  206. LEFT JOIN `jeecg-boot`.sys_depart part ON dep.dep_id=part.id
  207. WHERE
  208. cua.account_id IS NOT NULL and cua.account_status=0 and cua.media_id=2
  209. ) bei
  210. ON t1.account_id = bei.account_id where tt.account_id is null and bei.account_id is not null;
  211. """.format(date_time=self.date_time)
  212. TidbConn.upsert(conn=self.conn, sql_buff=sql)
  213. self.conn.commit()
  214. TidbConn.conn_close(conn=self.conn)
  215. return 0
  216. except Exception as e:
  217. logging.error(e)
  218. return -1
  219. if __name__ == '__main__':
  220. status = AppKuaishouAccountBaseInfoDsD().taskHandler()
  221. if status == 0:
  222. print("作业执行成功")
  223. exit(0)
  224. else:
  225. print("作业执行失败")
  226. exit(-1)