AppKuaishouAccountBaseInfoDsD.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. 0 effect_num,
  74. 0 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. (
  90. SELECT
  91. cua.account_id id,
  92. cua.account_id,
  93. cp.id project_id,
  94. product.id product_id,
  95. product.product_name,
  96. ca.id advertiser_id,
  97. uc.company_id company_id,
  98. cua.auth_name account_name,
  99. cp.project_name project_name,
  100. ca.`name` advertiser_name,
  101. uc.company_name company_name,
  102. cp.sale_id sale_id,
  103. su.id user_id,
  104. su.realname user_name,
  105. su.leader_id,
  106. lea.realname leader_name,
  107. su.org_code org_code,
  108. dep.dep_id dept_id,
  109. part.depart_name dept_name
  110. FROM
  111. `jeecg-boot`.ctop_user_allocation cua
  112. LEFT JOIN `jeecg-boot`.ctop_project cp ON cua.project_id = cp.id
  113. LEFT JOIN `jeecg-boot`.ctop_advertiser ca ON ca.id = cua.advertiser_id
  114. LEFT JOIN `jeecg-boot`.sys_user su ON su.id = cua.user_id
  115. LEFT JOIN `jeecg-boot`.sys_user lea ON lea.id = su.leader_id
  116. LEFT JOIN `jeecg-boot`.user_company uc ON uc.user_id = cua.user_id
  117. LEFT JOIN `jeecg-boot`.ctop_product product ON product.id = cp.product_id
  118. LEFT JOIN `jeecg-boot`.sys_user_depart dep ON cua.user_id=dep.user_id
  119. LEFT JOIN `jeecg-boot`.sys_depart part ON dep.dep_id=part.id
  120. WHERE
  121. cua.account_id IS NOT NULL and cua.account_status=0 and cua.media_id=2
  122. ) bei
  123. ON t1.account_id = bei.account_id where bei.account_id is not null
  124. union all
  125. select
  126. {date_time} stat_date,
  127. t1.account_id,
  128. bei.account_name,
  129. bei.project_id,
  130. bei.project_name,
  131. bei.product_id,
  132. bei.product_name,
  133. bei.user_id,
  134. bei.user_name,
  135. bei.leader_id,
  136. bei.leader_name,
  137. bei.company_id,
  138. bei.company_name,
  139. bei.dept_id,
  140. bei.dept_name,
  141. t2.cam_num as all_campaign_num ,
  142. t2.cam_num new_campaign_num,
  143. t2.unit_num as all_ad_num,
  144. t2.unit_num new_ad_num,
  145. t3.day_num as all_video_num,
  146. t3.day_num new_video_num,
  147. 0 effect_num,
  148. 0 hot_num,
  149. 0 put_campaign_num,
  150. 0 put_unit_num,
  151. 0 trust_campaign_num,
  152. 0 trust_unit_num
  153. from
  154. (select distinct account_id,signature from `media_api`.kuaishou_video_list where date_format(stat_date,'%Y%m%d')={date_time}) t1
  155. left join
  156. (select * from `application`.`app_kuaishou_account_base_info_d` where stat_date= date_sub({date_time}, INTERVAL 1 DAY)) tt
  157. on t1.account_id=tt.account_id
  158. left join
  159. (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}
  160. group by advertiser_id) t2
  161. on t1.account_id=t2.account_id
  162. left join
  163. (select account_id,count(distinct signature) day_num from `media_api`.kuaishou_video_list
  164. where date_format(stat_date,'%Y%m%d')={date_time} group by account_id) t3
  165. on t1.account_id=t3.account_id
  166. left join
  167. (
  168. SELECT
  169. cua.account_id id,
  170. cua.account_id,
  171. cp.id project_id,
  172. product.id product_id,
  173. product.product_name,
  174. ca.id advertiser_id,
  175. uc.company_id company_id,
  176. cua.auth_name account_name,
  177. cp.project_name project_name,
  178. ca.`name` advertiser_name,
  179. uc.company_name company_name,
  180. cp.sale_id sale_id,
  181. su.id user_id,
  182. su.realname user_name,
  183. su.leader_id,
  184. lea.realname leader_name,
  185. su.org_code org_code,
  186. dep.dep_id dept_id,
  187. part.depart_name dept_name
  188. FROM
  189. `jeecg-boot`.ctop_user_allocation cua
  190. LEFT JOIN `jeecg-boot`.ctop_project cp ON cua.project_id = cp.id
  191. LEFT JOIN `jeecg-boot`.ctop_advertiser ca ON ca.id = cua.advertiser_id
  192. LEFT JOIN `jeecg-boot`.sys_user su ON su.id = cua.user_id
  193. LEFT JOIN `jeecg-boot`.sys_user lea ON lea.id = su.leader_id
  194. LEFT JOIN `jeecg-boot`.user_company uc ON uc.user_id = cua.user_id
  195. LEFT JOIN `jeecg-boot`.ctop_product product ON product.id = cp.product_id
  196. LEFT JOIN `jeecg-boot`.sys_user_depart dep ON cua.user_id=dep.user_id
  197. LEFT JOIN `jeecg-boot`.sys_depart part ON dep.dep_id=part.id
  198. WHERE
  199. cua.account_id IS NOT NULL and cua.account_status=0 and cua.media_id=2
  200. ) bei
  201. ON t1.account_id = bei.account_id where tt.account_id is null and bei.account_id is not null;
  202. """.format(date_time=self.date_time)
  203. TidbConn.upsert(conn=self.conn, sql_buff=sql)
  204. self.conn.commit()
  205. TidbConn.conn_close(conn=self.conn)
  206. return 0
  207. except Exception as e:
  208. logging.error(e)
  209. return -1
  210. if __name__ == '__main__':
  211. status = AppKuaishouAccountBaseInfoDsD().taskHandler()
  212. if status == 0:
  213. print("作业执行成功")
  214. exit(0)
  215. else:
  216. print("作业执行失败")
  217. exit(-1)