pitcher_panel.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. from model.DateUtils import DateUtils
  2. from model.DataBaseUtils import *
  3. from model.log import logger
  4. from model.CommonUtils import *
  5. du = DateUtils()
  6. ck = CkUtils()
  7. db = MysqlUtils()
  8. log = logger()
  9. def get_pitcher_panel_channel(pitcher,channel,start,end,page,page_size,order_by,order):
  10. sql=f"""select channel,stage,platform,book,
  11. formatDateTime(dt,'%Y-%m-%d') as date,
  12. cost,
  13. first_order_amount,
  14. toDecimal32(if(cost=0,0,first_order_amount/cost),4) first_roi,
  15. first_order_user,first_order_count,
  16. toDecimal32(if(first_order_user=0,0,cost/first_order_user),2) first_per_cost,
  17. view_count,click_count,follow_user,
  18. toDecimal32(if(click_count=0,0,follow_user/click_count),4) follow_rate,
  19. toDecimal32(if(follow_user=0,0,cost/follow_user),2) follow_per_cost,
  20. total_cost,
  21. toDecimal32(if(total_cost=0,0,total_amount/total_cost),2) back_rate
  22. from dw_daily_channel where dt>='{start}' and dt<='{end}' """
  23. if pitcher!='all':
  24. sql += f" and pitcher='{pitcher}' "
  25. if channel!='':
  26. sql += f" and channel='{channel}' "
  27. sql += f" order by {order_by} {order} limit {page},{page_size} "
  28. print(sql)
  29. data=ck.execute(sql)
  30. key=['channel','stage','platform','book','date','cost','first_order_amount','first_roi','first_order_user',
  31. 'first_order_count','first_per_cost','view_count','click_count','follow_user','follow_rate','follow_per_cost',
  32. 'total_cost','back_rate']
  33. return get_dict_list(key, data)
  34. def get_pitcher_panel_daily(pitcher, start, end, page, page_size, order_by, order):
  35. sql=f"""
  36. select formatDateTime(dt,'%Y-%m-%d') date ,pitcher,
  37. toDecimal32(sum(cost),2) cost,
  38. toDecimal32(sum(first_order_amount),2) first_order_amount,
  39. toDecimal32(if(cost=0,0,first_order_amount/cost),4) first_roi ,
  40. toDecimal32(sum(order_amount),2) order_amount,
  41. toDecimal32(if(cost=0,0,sum(reg_order_amount)/cost),4) today_roi,
  42. toDecimal32(sum(total_cost),2) total_cost,
  43. toDecimal32(sum(total_amount),2) total_amount,
  44. toDecimal32(total_amount-total_cost,2) total_profit,
  45. toDecimal32(if(total_cost=0,0,total_amount/total_cost),4) total_roi
  46. from dw_daily_channel where dt>='{start}' and dt<='{end}' """
  47. if pitcher != 'all':
  48. sql += f" and pitcher='{pitcher}' "
  49. sql += f" group by date,pitcher order by {order_by} {order} limit {page},{page_size} "
  50. print(sql)
  51. data = ck.execute(sql)
  52. key=['date','pitcher','cost','first_order_amount','first_roi','order_amount','today_roi','total_cost','total_amount','total_profit','total_roi']
  53. return get_dict_list(key,data)
  54. def get_pitcher_panel_overview(pitcher):
  55. sql=f"""select pitcher,
  56. toDecimal32(cost,2) cost,
  57. toDecimal32(amount,2) amount,
  58. toDecimal32(roi,4) roi,
  59. channel_count,on_channel_count,
  60. off_channel_count,
  61. toDecimal32(this_month_cost,2) this_month_cost,
  62. toDecimal32(this_month_amount,2) this_month_amount,
  63. toDecimal32(this_month_roi,4) this_month_roi,
  64. toDecimal32(last_month_cost,2) last_month_cost,
  65. toDecimal32(last_month_amount,2) last_month_amount,
  66. toDecimal32(last_month_roi,4) last_month_roi,
  67. toDecimal32(last_month_far_amount,2) last_month_far_amount,
  68. follow_user
  69. from dm_pitcher_daily_page_total where dt='{du.get_n_days(-1)}'"""
  70. if pitcher != 'all':
  71. sql += f" and pitcher='{pitcher}' "
  72. print(sql)
  73. data=ck.execute(sql)
  74. print(data)
  75. key=['pitcher','cost','amount','roi','channel_count','on_channel_count','off_channel_count','this_month_cost','this_month_amount','this_month_roi',
  76. 'last_month_cost','last_month_amount','last_month_roi','last_month_far_amount','follow_user']
  77. return get_dict_list(key,data)
  78. def get_channel_overview(channel,start,end,page,page_size,order_by,order):
  79. sql="""select channel,toString(dt) date,
  80. view_count,click_count,
  81. round(if(view_count=0,0,click_count/view_count),4) click_rate,
  82. follow_user,
  83. round(if(click_count=0,0,follow_user/click_count),4) follow_rate,
  84. round(if(follow_user=0,0,cost/follow_user),2) follow_per_cost,
  85. round(if(click_count=0,0,first_order_count/click_count),4) order_rate,
  86. round(if(first_order_user=0,0,cost/first_order_user),2) order_per_cost,
  87. round(cost,2) cost,
  88. first_order_count,first_order_user,
  89. round(first_order_amount,2) first_order_amount,order_count,order_user,
  90. round(order_amount,2) order_amount,
  91. round(order_amount-first_order_amount,2) old_order_amount,
  92. round(if(first_order_user=0,0,first_order_amount/first_order_user),2) first_amount_per_user,
  93. round(if(follow_user=0,0,first_order_amount/follow_user),2) amount_per_follow,
  94. round(if(first_order_user=0,0,cost/first_order_user),2) first_cost_per_user,
  95. round(if(follow_user=0,0,first_order_user/follow_user),4) new_user_order_rate,
  96. round(reg_order_amount,2) reg_user_amount,
  97. round(total_cost,2) total_cost,
  98. round(total_amount,2) total_amount,
  99. round(if(cost=0,0,first_order_amount/cost),4) day_roi,
  100. round(if(cost=0,0,reg_order_amount/cost),4) all_roi,
  101. 0 avg_new_order_rate,
  102. 0 old_user_once_order_rate from dw_daily_channel where 1=1 """
  103. if channel!='':
  104. sql+=f" and channel='{channel}'"
  105. if start!='':
  106. sql+=f" and start>='{start}'"
  107. if end!='':
  108. sql+=f" and end<='{end}'"
  109. sql += f" order by {order_by} {order} limit {page},{page_size} "
  110. print(sql)
  111. return ck.execute(sql)
  112. def get_channel_again_order_trend(channel,date):
  113. a=[{'date':'2020-12-01',
  114. 'channel':'宝珠书屋',
  115. 'book':'熊出没',
  116. 'cost':23.23,
  117. 'reg_amount':23421.11,
  118. 'roi':0.23,
  119. 'new_follow':23,
  120. 'new_follow_per_cost':234.11,
  121. 'reg_user':111,
  122. 'reg_count':123,
  123. 'cost_per_user':123,
  124. 'avg_again_order_rate':0.24,
  125. 'avg_order_amount':1231.11,
  126. 'order_count':11,
  127. 'data':[{'user_order_count' :1,
  128. 'd1':{'origin':12,
  129. 'new':1231,
  130. 'move':12,
  131. 'now':2134,
  132. 'follow_order_rate':0.12},
  133. 'd2': {'origin': 12,
  134. 'new': 1231,
  135. 'move': 12,
  136. 'now': 2134,
  137. 'follow_order_rate': 0.12},
  138. 'd3': {'origin': 12,
  139. 'new': 1231,
  140. 'move': 12,
  141. 'now': 2134,
  142. 'follow_order_rate': 0.12},
  143. 'd4': {'origin': 12,
  144. 'new': 1231,
  145. 'move': 12,
  146. 'now': 2134,
  147. 'follow_order_rate': 0.12},
  148. 'd5': {'origin': 12,
  149. 'new': 1231,
  150. 'move': 12,
  151. 'now': 2134,
  152. 'follow_order_rate': 0.12},
  153. 'd6': {'origin': 12,
  154. 'new': 1231,
  155. 'move': 12,
  156. 'now': 2134,
  157. 'follow_order_rate': 0.12},
  158. 'd7': {'origin': 12,
  159. 'new': 1231,
  160. 'move': 12,
  161. 'now': 2134,
  162. 'follow_order_rate': 0.12}
  163. }
  164. ]
  165. }]
  166. return a
  167. def get_channel_active(channel,start,end):
  168. a=[{
  169. 'date':'2020-12-01',
  170. 'channel':'宝珠书屋',
  171. 'book':'魏汝稳',
  172. 'cost':234.11,
  173. 'reg_amount':123.11,
  174. 'roi':0.45,
  175. 'new_follow_user':11,
  176. 'new_follow_per_cost':234.11,
  177. 'order_user':222,
  178. 'order_count':12312,
  179. 'order_user_per_cost':123.11,
  180. 'day7_avg_act_rate':0.14,
  181. 'day7_avg_act_per_cost':5.23,
  182. 'day30_avg_act_rate':0.9334,
  183. 'day30_avg_act_rate':5.23,
  184. 'act_per_cost':43.11,
  185. 'd1':{
  186. 'act_user':12,
  187. 'act_cost_per_cost':23.11,
  188. 'act_rate':0.4512},
  189. 'd2': {
  190. 'act_user': 12,
  191. 'act_cost_per_cost': 23.11,
  192. 'act_rate': 0.4512},
  193. 'd3': {
  194. 'act_user': 12,
  195. 'act_cost_per_cost': 23.11,
  196. 'act_rate': 0.4512}
  197. }]
  198. return a
  199. def get_channel_order_trend(channel,start,end):
  200. a=[{
  201. 'date': '2020-12-01',
  202. 'channel': '宝珠书屋',
  203. 'book': '魏汝稳',
  204. 'cost': 234.11,
  205. 'reg_amount': 123.11,
  206. 'roi': 0.45,
  207. 'new_follow_user': 11,
  208. 'new_follow_per_cost': 234.11,
  209. 'order_user': 222,
  210. 'order_count': 12312,
  211. 'order_user_per_cost': 123.11,
  212. 'd1':{
  213. 'order':232.11,
  214. 'roi':0.2341,
  215. 'add':0.4511,
  216. 'mult':1.12},
  217. 'd2': {
  218. 'order': 232.11,
  219. 'roi': 0.2341,
  220. 'add': 0.4511,
  221. 'mult': 1.12},
  222. 'd3': {
  223. 'order': 232.11,
  224. 'roi': 0.2341,
  225. 'add': 0.4511,
  226. 'mult': 1.12},
  227. }]
  228. return a
  229. def get_channel_summary(channel,pitcher):
  230. sql = """select a.channel,state,'朋友圈' location,toString(start),toString(end),total_cost,total_amount,
  231. profit,roi,follow_user,follow_per_cost,order_user,
  232. if(follow_user=0,0,order_user/follow_user) order_tran_rate,if(order_user=0,0,total_cost/order_user) order_tran_cost
  233. from
  234. (
  235. select channel,if(max(dt)>subtractDays(today(),10),'在投','停投') state,
  236. min(dt) start,max(dt) end
  237. from dw_daily_channel_cost where cost>0 {} group by channel) a
  238. left outer join(
  239. select channel,sum(follow_user) follow_user,
  240. if(follow_user=0,0,sum(cost)/follow_user) follow_per_cost
  241. from dw_daily_channel group by channel) b on a.channel=b.channel
  242. left outer join(
  243. select sum(reg_order_user) order_user,channel from dw_daily_channel where dt=subtractDays(today(),1) group by channel) c on a.channel=c.channel
  244. left outer join (
  245. select total_cost,
  246. total_amount,
  247. channel,
  248. total_amount-total_cost profit,
  249. if(total_cost=0,0,total_amount/total_cost) roi
  250. from dw_channel_daily_total where dt=subtractDays(today(),1)) d on a.channel=d.channel
  251. where 1=1 """
  252. if pitcher!='':
  253. sql=sql.format(f" and pitcher='{pitcher}'")
  254. else:
  255. sql=sql.format('')
  256. if channel!='':
  257. sql+=f" and channel='{channel}'"
  258. return ck.execute(sql)
  259. if __name__ == '__main__':
  260. # a=get_channel_overview('','','',1,10,'date','desc')
  261. a=get_channel_summary('','')
  262. print(a)