pitcher_panel.py 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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_channel_belong_pitcher(channel):
  10. """根据公众号获取投手"""
  11. sql = f"select pitcher,channel from dw_daily_channel_cost where dt='{du.get_n_days(-1)}' and pitcher!='' and channel!=''"
  12. data=ck.execute(sql)
  13. di={}
  14. for i in data:
  15. di[i[1]]=i[0]
  16. return di.get(channel,'')
  17. def get_pitcher_panel_channel(pitcher,channel,start,end,page,page_size,order_by,order):
  18. sql=f"""select channel,stage,platform,book,
  19. formatDateTime(dt,'%Y-%m-%d') as date,
  20. cost,
  21. first_order_amount,
  22. toDecimal32(if(cost=0,0,first_order_amount/cost),4) first_roi,
  23. first_order_user,first_order_count,
  24. toDecimal32(if(first_order_user=0,0,cost/first_order_user),2) first_per_cost,
  25. view_count,click_count,follow_user,
  26. toDecimal32(if(click_count=0,0,follow_user/click_count),4) follow_rate,
  27. toDecimal32(if(follow_user=0,0,cost/follow_user),2) follow_per_cost,
  28. total_cost,
  29. toDecimal32(if(total_cost=0,0,total_amount/total_cost),2) back_rate
  30. from dw_daily_channel where dt>='{start}' and dt<='{end}' """
  31. if pitcher!='all':
  32. sql += f" and pitcher='{pitcher}' "
  33. if channel!='':
  34. sql += f" and channel='{channel}' "
  35. total = ck.execute(f"select count(1) from ({sql}) a")[0][0]
  36. sql += f" order by {order_by} {order} limit {(page-1)*page_size},{page_size} "
  37. print(sql)
  38. data=ck.execute(sql)
  39. key=['channel','stage','platform','book','date','cost','first_order_amount','first_roi','first_order_user',
  40. 'first_order_count','first_per_cost','view_count','click_count','follow_user','follow_rate','follow_per_cost',
  41. 'total_cost','back_rate']
  42. return get_dict_list(key,get_round(data,4)),total
  43. def get_pitcher_panel_daily(pitcher, start, end, page, page_size, order_by, order):
  44. sql=f"""
  45. select formatDateTime(dt,'%Y-%m-%d') date,'{pitcher}' pitcher,cost,first_order_amount,first_roi,
  46. order_amount,today_roi,total_amount,total_cost,total_amount-total_cost total_profit,
  47. total_amount/total_cost total_roi from
  48. (select dt,
  49. round(sum(cost),2) cost,
  50. round(sum(first_order_amount),2) first_order_amount,
  51. round(if(cost=0,0,first_order_amount/cost),4) first_roi ,
  52. round(sum(order_amount),2) order_amount,
  53. round(if(cost=0,0,sum(reg_order_amount)/cost),4) today_roi
  54. from dw_daily_channel
  55. where dt>='{start}' and dt<='{end}' and pitcher='{pitcher}'
  56. group by dt) a
  57. left outer join (
  58. select dt,total_amount,total_cost from dw_daily_pitcher where pitcher='{pitcher}'
  59. ) b on a.dt=b.dt
  60. """
  61. print(sql)
  62. total = ck.execute(f"select count(1) from ({sql}) a")[0][0]
  63. sql += f" order by {order_by} {order} limit {(page-1)*page_size},{page_size} "
  64. print(sql)
  65. data = ck.execute(sql)
  66. key=['date','pitcher','cost','first_order_amount','first_roi','order_amount','today_roi','total_amount','total_cost','total_profit','total_roi']
  67. return get_dict_list(key,data),total
  68. def get_pitcher_panel_overview(pitcher):
  69. sql=f"""select pitcher,
  70. cost cost,
  71. amount amount,
  72. roi roi,
  73. channel_count,on_channel_count,
  74. off_channel_count,
  75. this_month_cost this_month_cost,
  76. this_month_amount this_month_amount,
  77. this_month_roi this_month_roi,
  78. last_month_cost last_month_cost,
  79. last_month_amount last_month_amount,
  80. last_month_roi last_month_roi,
  81. last_month_far_amount last_month_far_amount,
  82. follow_user,
  83. last_month_far_roi last_month_far_roi
  84. from dm_pitcher_daily_page_total where dt='{du.get_n_days(-1)}'"""
  85. if pitcher != 'all':
  86. sql += f" and pitcher='{pitcher}' "
  87. print(sql)
  88. data=ck.execute(sql)
  89. print(data)
  90. key=['pitcher','cost','amount','roi','channel_count','on_channel_count','off_channel_count','this_month_cost','this_month_amount','this_month_roi',
  91. 'last_month_cost','last_month_amount','last_month_roi','last_month_far_amount','follow_user','last_month_far_roi']
  92. return get_dict_list(key,get_round(data))
  93. def get_channel_overview(channel,pitcher,start,end,page,page_size,order_by,order):
  94. sql="""select channel,toString(dt) date,
  95. view_count,click_count,
  96. round(if(view_count=0,0,click_count/view_count),4) click_rate,
  97. follow_user,
  98. round(if(click_count=0,0,follow_user/click_count),4) follow_rate,
  99. round(if(follow_user=0,0,cost/follow_user),2) follow_per_cost,
  100. round(if(click_count=0,0,first_order_count/click_count),4) order_rate,
  101. round(if(first_order_user=0,0,cost/first_order_user),2) order_per_cost,
  102. round(cost,2) cost,
  103. first_order_count,first_order_user,
  104. round(first_order_amount,2) first_order_amount,order_count,order_user,
  105. round(order_amount,2) order_amount,
  106. round(order_amount-first_order_amount,2) old_order_amount,
  107. round(if(first_order_user=0,0,first_order_amount/first_order_user),2) first_amount_per_user,
  108. round(if(follow_user=0,0,first_order_amount/follow_user),2) amount_per_follow,
  109. round(if(first_order_user=0,0,cost/first_order_user),2) first_cost_per_user,
  110. round(if(follow_user=0,0,first_order_user/follow_user),4) new_user_order_rate,
  111. round(reg_order_amount,2) reg_user_amount,
  112. round(total_cost,2) total_cost,
  113. round(total_amount,2) total_amount,
  114. round(if(cost=0,0,first_order_amount/cost),4) day_roi,
  115. round(if(cost=0,0,reg_order_amount/cost),4) all_roi,
  116. 0 avg_new_order_rate,
  117. if(reg_order_user=0,0,reg_order_user_again/reg_order_user) old_user_once_order_rate from dw_daily_channel where 1=1 """
  118. if channel!='':
  119. sql+=f" and channel='{channel}'"
  120. if start!='':
  121. sql+=f" and dt>='{start}'"
  122. if end!='':
  123. sql+=f" and dt<='{end}'"
  124. if pitcher!='':
  125. sql+=f" and pitcher='{pitcher}'"
  126. total = ck.execute(f"select count(1) from ({sql}) a")[0][0]
  127. sql += f" order by {order_by} {order} limit {(page-1)*page_size},{page_size} "
  128. print(sql)
  129. key=['channel','date','view_count','click_count','click_rate','follow_user','follow_rate','follow_per_cost','order_rate','order_per_cost','cost',
  130. 'first_order_count','first_order_user','first_order_amount','order_count','order_user','order_amount','old_order_amount','first_amount_per_user','amount_per_follow','first_cost_per_user',
  131. 'new_user_order_rate','reg_user_amount','total_cost','total_amount','day_roi','all_roi','avg_new_order_rate','old_user_once_order_rate']
  132. return get_dict_list(key,get_round(ck.execute(sql))),total
  133. def get_channel_again_order_trend(channel,date,pitcher):
  134. # if get_channel_belong_pitcher(channel)!=pitcher:
  135. # return []
  136. sql=f"""select toString(dt) date,
  137. channel,book,
  138. round(cost,2) cost,
  139. round(reg_order_amount,2) reg_amount,
  140. round(if(cost=0,0,reg_order_amount/cost),4) roi,
  141. follow_user new_follow,
  142. round(if(follow_user=0,0,cost/follow_user),2) new_follow_per_cost,
  143. reg_order_count reg_count,reg_order_user reg_user,
  144. round(if(reg_order_user=0,0,cost/reg_order_user),2) cost_per_user,
  145. round(if(reg_order_user=0,0,reg_order_amount/reg_order_user),2) avg_order_amount,
  146. round(if(reg_order_user=0,0,reg_order_user_again/reg_order_user),4) avg_again_order_rate,
  147. round(if(reg_order_user=0,0,order_count/reg_order_user),4) order_count
  148. from dw_daily_channel where channel='{channel}' and dt='{date}'
  149. """
  150. # print(sql)
  151. data=ck.execute(sql)
  152. # print(data)
  153. key1=['date','channel','book','cost','reg_amount','roi','new_follow','new_follow_per_cost',
  154. 'reg_count','reg_user','cost_per_user','avg_order_amount','avg_again_order_rate','order_count']
  155. json1=get_dict_list(key1,get_round(data))[0]
  156. # print(json1)
  157. sql2=f"""select
  158. 1 as ddf,count(1) c1,sum(if(count>1,1,0)) c2,sum(if(count>2,1,0)) c3,
  159. sum(if(count>3,1,0)) c4,sum(if(count>4,1,0)) c5,sum(if(count>5,1,0)) c6
  160. from (select count(1) count
  161. from order where channel='观云书海' and date='{date}'
  162. and formatDateTime(reg_time,'%Y-%m-%d')='{date}' group by user_id,date) a
  163. union all
  164. select
  165. 2 as ddf,count(1) c1,sum(if(count>1,1,0)) c2,sum(if(count>2,1,0)) c3,
  166. sum(if(count>3,1,0)) c4,sum(if(count>4,1,0)) c5,sum(if(count>5,1,0)) c6
  167. from (
  168. select count(1) count
  169. from order where channel='观云书海' and date>='{date}' and date<=addDays(toDate('{date}'),1)
  170. and formatDateTime(reg_time,'%Y-%m-%d')='{date}' group by user_id) a
  171. union all
  172. select
  173. 3 as ddf,count(1) c1,sum(if(count>1,1,0)) c2,sum(if(count>2,1,0)) c3,
  174. sum(if(count>3,1,0)) c4,sum(if(count>4,1,0)) c5,sum(if(count>5,1,0)) c6
  175. from (
  176. select count(1) count
  177. from order where channel='观云书海' and date>='{date}' and date<=addDays(toDate('{date}'),2)
  178. and formatDateTime(reg_time,'%Y-%m-%d')='{date}' group by user_id) a
  179. union all
  180. select
  181. 4 as ddf,count(1) c1,sum(if(count>1,1,0)) c2,sum(if(count>2,1,0)) c3,
  182. sum(if(count>3,1,0)) c4,sum(if(count>4,1,0)) c5,sum(if(count>5,1,0)) c6
  183. from (
  184. select count(1) count
  185. from order where channel='观云书海' and date>='{date}' and date<=addDays(toDate('{date}'),3)
  186. and formatDateTime(reg_time,'%Y-%m-%d')='{date}' group by user_id) a
  187. union all
  188. select
  189. 5 as ddf,count(1) c1,sum(if(count>1,1,0)) c2,sum(if(count>2,1,0)) c3,
  190. sum(if(count>3,1,0)) c4,sum(if(count>4,1,0)) c5,sum(if(count>5,1,0)) c6
  191. from (
  192. select count(1) count
  193. from order where channel='观云书海' and date>='{date}' and date<=addDays(toDate('{date}'),4)
  194. and formatDateTime(reg_time,'%Y-%m-%d')='{date}' group by user_id) a
  195. union all
  196. select
  197. 6 as ddf,count(1) c1,sum(if(count>1,1,0)) c2,sum(if(count>2,1,0)) c3,
  198. sum(if(count>3,1,0)) c4,sum(if(count>4,1,0)) c5,sum(if(count>5,1,0)) c6
  199. from (
  200. select count(1) count
  201. from order where channel='观云书海' and date>='{date}' and date<=addDays(toDate('{date}'),5)
  202. and formatDateTime(reg_time,'%Y-%m-%d')='{date}' group by user_id) a
  203. union all
  204. select
  205. 7 as ddf,count(1) c1,sum(if(count>1,1,0)) c2,sum(if(count>2,1,0)) c3,
  206. sum(if(count>3,1,0)) c4,sum(if(count>4,1,0)) c5,sum(if(count>5,1,0)) c6
  207. from (
  208. select count(1) count
  209. from order where channel='观云书海' and date>='{date}' and date<=addDays(toDate('{date}'),6)
  210. and formatDateTime(reg_time,'%Y-%m-%d')='{date}' group by user_id) a"""
  211. df=ck.execute(sql2)
  212. print(df)
  213. # 补全
  214. # xx=[i[0] for i in df]
  215. # for i in range(1,8):
  216. # if i not in xx:
  217. # df.append((i,0,0,0,0,0,0))
  218. # 排序
  219. import operator
  220. df.sort(key=operator.itemgetter(0))
  221. print(df)
  222. reg_user=json1["reg_user"]
  223. li=[]
  224. for i in range(1,6):
  225. print(i)
  226. d = {}
  227. d["user_order_count"] = i
  228. d1={}
  229. d1["origin"]=df[0][i]
  230. d1["new"]=0
  231. d1["move"]=df[0][i+1]
  232. d1["now"]=d1["origin"]+d1['new']-d1["move"]
  233. d1["follow_order_rate"]=round(d1["now"]/df[0][1],2) if df[0][i]!=0 else 0
  234. d['d1'] = d1
  235. d2={}
  236. d2["origin"] = d1["now"]
  237. d2["new"] = df[1][i]-df[0][i]
  238. d2["move"] = df[1][i+1]-df[0][i+1]
  239. d2["now"] = df[1][i]-df[1][i+1]
  240. d2["follow_order_rate"] = round(d2["now"] /df[1][1], 2) if df[1][i]!=0 else 0
  241. d['d2'] = d2
  242. d3={}
  243. d3["origin"] = d2["now"]
  244. d3["new"] = df[2][i]-df[1][i]
  245. d3["move"] =df[2][i+1]-df[1][i+1]
  246. d3["now"] = df[2][i]-df[2][i+1]
  247. d3["follow_order_rate"] = round(d3["now"] / df[2][1], 2) if df[2][i]!=0 else 0
  248. d['d3'] = d3
  249. d4={}
  250. d4["origin"] = d3["now"]
  251. d4["new"] = df[3][i]-df[2][i]
  252. d4["move"] = df[3][i+1]-df[2][i+1]
  253. d4["now"] = df[3][i]-df[3][i+1]
  254. d4["follow_order_rate"] = round(d4["now"] / df[3][1], 2) if df[3][i]!=0 else 0
  255. d['d4'] = d4
  256. d5={}
  257. d5["origin"] = d4["now"]
  258. d5["new"] = df[4][i]-df[3][i]
  259. d5["move"] = df[4][i+1]-df[3][i+1]
  260. d5["now"] = df[4][i]-df[4][i+1]
  261. d5["follow_order_rate"] = round(d4["now"] / df[4][1], 2) if df[3][i]!=0 else 0
  262. d['d5'] = d5
  263. d6 = {}
  264. d6["origin"] = d5["now"]
  265. d6["new"] = df[5][i]-df[4][i]
  266. d6["move"] = df[5][i+1]-df[4][i+1]
  267. d6["now"] = df[5][i]-df[5][i+1]
  268. d6["follow_order_rate"] = round(d6["now"] / df[5][1], 2) if df[5][i]!=0 else 0
  269. d['d6'] = d6
  270. d7 = {}
  271. d7["origin"] = d6["now"]
  272. d7["new"] = df[6][i]-df[5][i]
  273. d7["move"] = df[6][i+1]-df[5][i+1]
  274. d7["now"] = df[6][i]-df[6][i+1]
  275. d7["follow_order_rate"] = round(d7["now"] / df[6][1], 2) if df[6][i]!=0 else 0
  276. d['d7'] = d7
  277. li.append(d)
  278. print(li)
  279. json1['data']=li
  280. print([json1])
  281. return [json1]
  282. def get_channel_active(channel,pitcher,start,end,page,page_size,order_by,order):
  283. if get_channel_belong_pitcher(channel)!=pitcher:
  284. return [],0
  285. sql=f"""select formatDateTime(a.dt,'%Y-%m-%d') date, '{channel}' channel,book,cost,reg_amount,roi,new_follow_user,new_follow_per_cost,order_user,order_count,
  286. order_user_per_cost,day7_avg_act_rate,day7_avg_act_per_cost,day30_avg_act_rate,ay30_avg_act_cost,
  287. act_per_cost,
  288. concat(toString(reg_order_user1),',',toString(if(reg_order_user1=0,0,cost/reg_order_user1)),',',toString(if(order_user=0,0,reg_order_user1/order_user))),
  289. concat(toString(reg_order_user2-reg_order_user1),',',toString(if(reg_order_user2=0,0,cost/reg_order_user2)),',',toString(if(order_user=0,0,(reg_order_user2-reg_order_user1)/order_user))),
  290. concat(toString(reg_order_user3-reg_order_user2),',',toString(if(reg_order_user3=0,0,cost/reg_order_user3)),',',toString(if(order_user=0,0,(reg_order_user3-reg_order_user2)/order_user))),
  291. concat(toString(reg_order_user4-reg_order_user3),',',toString(if(reg_order_user4=0,0,cost/reg_order_user4)),',',toString(if(order_user=0,0,(reg_order_user4-reg_order_user3)/order_user))),
  292. concat(toString(reg_order_user5-reg_order_user4),',',toString(if(reg_order_user5=0,0,cost/reg_order_user5)),',',toString(if(order_user=0,0,(reg_order_user5-reg_order_user4)/order_user))),
  293. concat(toString(reg_order_user6-reg_order_user5),',',toString(if(reg_order_user6=0,0,cost/reg_order_user6)),',',toString(if(order_user=0,0,(reg_order_user6-reg_order_user5)/order_user))),
  294. concat(toString(reg_order_user7-reg_order_user6),',',toString(if(reg_order_user7=0,0,cost/reg_order_user7)),',',toString(if(order_user=0,0,(reg_order_user7-reg_order_user6)/order_user))),
  295. concat(toString(reg_order_user8-reg_order_user7),',',toString(if(reg_order_user8=0,0,cost/reg_order_user8)),',',toString(if(order_user=0,0,(reg_order_user8-reg_order_user7)/order_user))),
  296. concat(toString(reg_order_user9-reg_order_user8),',',toString(if(reg_order_user9=0,0,cost/reg_order_user9)),',',toString(if(order_user=0,0,(reg_order_user9-reg_order_user8)/order_user))),
  297. concat(toString(reg_order_user10-reg_order_user9),',',toString(if(reg_order_user10=0,0,cost/reg_order_user10)),',',toString(if(order_user=0,0,(reg_order_user10-reg_order_user9)/order_user))),
  298. concat(toString(reg_order_user11-reg_order_user10),',',toString(if(reg_order_user11=0,0,cost/reg_order_user11)),',',toString(if(order_user=0,0,(reg_order_user11-reg_order_user10)/order_user))),
  299. concat(toString(reg_order_user12-reg_order_user11),',',toString(if(reg_order_user12=0,0,cost/reg_order_user12)),',',toString(if(order_user=0,0,(reg_order_user12-reg_order_user11)/order_user))),
  300. concat(toString(reg_order_user13-reg_order_user12),',',toString(if(reg_order_user13=0,0,cost/reg_order_user13)),',',toString(if(order_user=0,0,(reg_order_user13-reg_order_user12)/order_user))),
  301. concat(toString(reg_order_user14-reg_order_user13),',',toString(if(reg_order_user14=0,0,cost/reg_order_user14)),',',toString(if(order_user=0,0,(reg_order_user14-reg_order_user13)/order_user))),
  302. concat(toString(reg_order_user15-reg_order_user14),',',toString(if(reg_order_user15=0,0,cost/reg_order_user15)),',',toString(if(order_user=0,0,(reg_order_user15-reg_order_user14)/order_user))),
  303. concat(toString(reg_order_user16-reg_order_user15),',',toString(if(reg_order_user16=0,0,cost/reg_order_user16)),',',toString(if(order_user=0,0,(reg_order_user16-reg_order_user15)/order_user))),
  304. concat(toString(reg_order_user17-reg_order_user16),',',toString(if(reg_order_user17=0,0,cost/reg_order_user17)),',',toString(if(order_user=0,0,(reg_order_user17-reg_order_user16)/order_user))),
  305. concat(toString(reg_order_user18-reg_order_user17),',',toString(if(reg_order_user18=0,0,cost/reg_order_user18)),',',toString(if(order_user=0,0,(reg_order_user18-reg_order_user17)/order_user))),
  306. concat(toString(reg_order_user19-reg_order_user18),',',toString(if(reg_order_user19=0,0,cost/reg_order_user19)),',',toString(if(order_user=0,0,(reg_order_user19-reg_order_user18)/order_user))),
  307. concat(toString(reg_order_user20-reg_order_user19),',',toString(if(reg_order_user20=0,0,cost/reg_order_user20)),',',toString(if(order_user=0,0,(reg_order_user20-reg_order_user19)/order_user))),
  308. concat(toString(reg_order_user21-reg_order_user20),',',toString(if(reg_order_user21=0,0,cost/reg_order_user21)),',',toString(if(order_user=0,0,(reg_order_user21-reg_order_user20)/order_user))),
  309. concat(toString(reg_order_user22-reg_order_user21),',',toString(if(reg_order_user22=0,0,cost/reg_order_user22)),',',toString(if(order_user=0,0,(reg_order_user22-reg_order_user21)/order_user))),
  310. concat(toString(reg_order_user23-reg_order_user22),',',toString(if(reg_order_user23=0,0,cost/reg_order_user23)),',',toString(if(order_user=0,0,(reg_order_user23-reg_order_user22)/order_user))),
  311. concat(toString(reg_order_user24-reg_order_user23),',',toString(if(reg_order_user24=0,0,cost/reg_order_user24)),',',toString(if(order_user=0,0,(reg_order_user24-reg_order_user23)/order_user))),
  312. concat(toString(reg_order_user25-reg_order_user24),',',toString(if(reg_order_user25=0,0,cost/reg_order_user25)),',',toString(if(order_user=0,0,(reg_order_user25-reg_order_user24)/order_user))),
  313. concat(toString(reg_order_user26-reg_order_user25),',',toString(if(reg_order_user26=0,0,cost/reg_order_user26)),',',toString(if(order_user=0,0,(reg_order_user26-reg_order_user25)/order_user))),
  314. concat(toString(reg_order_user27-reg_order_user26),',',toString(if(reg_order_user27=0,0,cost/reg_order_user27)),',',toString(if(order_user=0,0,(reg_order_user27-reg_order_user26)/order_user))),
  315. concat(toString(reg_order_user28-reg_order_user27),',',toString(if(reg_order_user28=0,0,cost/reg_order_user28)),',',toString(if(order_user=0,0,(reg_order_user28-reg_order_user27)/order_user))),
  316. concat(toString(reg_order_user29-reg_order_user28),',',toString(if(reg_order_user29=0,0,cost/reg_order_user29)),',',toString(if(order_user=0,0,(reg_order_user29-reg_order_user28)/order_user))),
  317. concat(toString(reg_order_user30-reg_order_user29),',',toString(if(reg_order_user30=0,0,cost/reg_order_user30)),',',toString(if(order_user=0,0,(reg_order_user30-reg_order_user29)/order_user)))
  318. from (
  319. select dt,book,cost,
  320. reg_order_amount reg_amount,
  321. if(cost=0,0,reg_order_amount/cost) roi,
  322. follow_user new_follow_user,
  323. if(follow_user=0,0,cost/follow_user) new_follow_per_cost,
  324. reg_order_user order_user,
  325. reg_order_count order_count,
  326. if(reg_order_user=0,0,cost/reg_order_user) order_user_per_cost,
  327. if(follow_user=0,0,reg_order_user7/follow_user) day7_avg_act_rate ,
  328. if(reg_order_user7=0,0,cost/reg_order_user7) day7_avg_act_per_cost,
  329. if(follow_user=0,0,reg_order_user30/follow_user) day30_avg_act_rate,
  330. if(reg_order_user30=0,0,cost/reg_order_user30) ay30_avg_act_cost,
  331. if(reg_order_user=0,0,cost/reg_order_user) act_per_cost
  332. from dw_daily_channel where channel='{channel}' and dt>='{start}' and dt<='{end}') a
  333. left outer join (
  334. select toDate(formatDateTime(reg_time,'%Y-%m-%d')) dt,
  335. count(distinct if(toDate(formatDateTime(reg_time,'%Y-%m-%d'))=date,user_id,NULL)) reg_order_user1,
  336. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),1)>=date,user_id,NULL)) reg_order_user2,
  337. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),2)>=date,user_id,NULL)) reg_order_user3,
  338. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),3)>=date,user_id,NULL)) reg_order_user4,
  339. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),4)>=date,user_id,NULL)) reg_order_user5,
  340. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),5)>=date,user_id,NULL)) reg_order_user6,
  341. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),6)>=date,user_id,NULL)) reg_order_user7,
  342. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),7)>=date,user_id,NULL)) reg_order_user8,
  343. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),8)>=date,user_id,NULL)) reg_order_user9,
  344. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),9)>=date,user_id,NULL)) reg_order_user10,
  345. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),10)>=date,user_id,NULL)) reg_order_user11,
  346. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),11)>=date,user_id,NULL)) reg_order_user12,
  347. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),12)>=date,user_id,NULL)) reg_order_user13,
  348. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),13)>=date,user_id,NULL)) reg_order_user14,
  349. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),14)>=date,user_id,NULL)) reg_order_user15,
  350. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),15)>=date,user_id,NULL)) reg_order_user16,
  351. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),16)>=date,user_id,NULL)) reg_order_user17,
  352. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),17)>=date,user_id,NULL)) reg_order_user18,
  353. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),18)>=date,user_id,NULL)) reg_order_user19,
  354. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),19)>=date,user_id,NULL)) reg_order_user20,
  355. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),20)>=date,user_id,NULL)) reg_order_user21,
  356. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),21)>=date,user_id,NULL)) reg_order_user22,
  357. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),22)>=date,user_id,NULL)) reg_order_user23,
  358. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),23)>=date,user_id,NULL)) reg_order_user24,
  359. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),24)>=date,user_id,NULL)) reg_order_user25,
  360. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),25)>=date,user_id,NULL)) reg_order_user26,
  361. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),26)>=date,user_id,NULL)) reg_order_user27,
  362. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),27)>=date,user_id,NULL)) reg_order_user28,
  363. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),28)>=date,user_id,NULL)) reg_order_user29,
  364. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),29)>=date,user_id,NULL)) reg_order_user30
  365. from order where channel='{channel}' and dt>='{start}' group by formatDateTime(reg_time,'%Y-%m-%d')) b on a.dt=b.dt
  366. """
  367. total = ck.execute(f"select count(1) from ({sql}) a")[0][0]
  368. sql+=f" order by {order_by} {order} limit {(page-1)*page_size},{page_size} "
  369. data = ck.execute(sql)
  370. print(sql)
  371. print(data)
  372. key=['date','channel','book','cost','reg_amount','roi','new_follow_user','new_follow_per_cost','order_user','order_count','order_user_per_cost',
  373. 'day7_avg_act_rate','day7_avg_act_per_cost','day30_avg_act_rate','day30_avg_act_cost','act_per_cost','d1','d2','d3','d4','d5','d6','d7','d8','d9','d10',
  374. 'd11','d12','d13','d14','d15','d16','d17','d18','d19','d20','d21','d22','d23','d24','d25','d26','d27','d28','d29','d30']
  375. x=get_dict_list(key,get_round(data))
  376. print(x)
  377. li=[]
  378. for i in x:
  379. di = {}
  380. for j in i:
  381. if j in ['d1','d2','d3','d4','d5','d6','d7','d8','d9','d10',
  382. 'd11','d12','d13','d14','d15','d16','d17','d18','d19','d20','d21','d22','d23','d24','d25','d26','d27','d28','d29','d30']:
  383. if i[j]:
  384. k=i[j].split(",")
  385. k[0]=round(float(k[0]),0)
  386. k[1]=round(float(k[1]),2)
  387. k[2]=round(float(k[2]),4)
  388. else:
  389. k=[0,0,0]
  390. di[j] = dict(zip(['act_user', 'act_cost_per_cost', 'act_rate'], k))
  391. else:
  392. di[j]=i[j]
  393. li.append(di)
  394. return li,total
  395. def get_channel_order_trend(channel,pitcher,start,end,page,page_size,order_by,order):
  396. if get_channel_belong_pitcher(channel)!=pitcher:
  397. return [],0
  398. sql=f"""select '{channel}' channel,toString(dt) date,book,cost,reg_order_amount,roi,new_follow_user,new_follow_per_cost,order_user,order_count,
  399. order_user_per_cost,
  400. concat(toString(r1),',',toString(r1/cost),',', toString(a1/cost),',',toString(b1)),
  401. concat(toString(a2),',',toString(r2/cost),',', toString(a2/cost),',',toString(b2)),
  402. concat(toString(a3),',',toString(r3/cost),',', toString(a3/cost),',',toString(b3)),
  403. concat(toString(a4),',',toString(r4/cost),',', toString(a4/cost),',',toString(b4)),
  404. concat(toString(a5),',',toString(r5/cost),',', toString(a5/cost),',',toString(b5)),
  405. concat(toString(a6),',',toString(r6/cost),',', toString(a6/cost),',',toString(b6)),
  406. concat(toString(a7),',',toString(r7/cost),',', toString(a7/cost),',',toString(b7)),
  407. concat(toString(a8),',',toString(r8/cost),',', toString(a8/cost),',',toString(b8)),
  408. concat(toString(a9),',',toString(r9/cost),',', toString(a9/cost),',',toString(b9)),
  409. concat(toString(a10),',',toString(r10/cost),',', toString(a10/cost),',',toString(b10)),
  410. concat(toString(a11),',',toString(r11/cost),',', toString(a11/cost),',',toString(b11)),
  411. concat(toString(a12),',',toString(r12/cost),',', toString(a12/cost),',',toString(b12)),
  412. concat(toString(a13),',',toString(r13/cost),',', toString(a13/cost),',',toString(b13)),
  413. concat(toString(a14),',',toString(r14/cost),',', toString(a14/cost),',',toString(b14)),
  414. concat(toString(a15),',',toString(r15/cost),',', toString(a15/cost),',',toString(b15)),
  415. concat(toString(a16),',',toString(r16/cost),',', toString(a16/cost),',',toString(b16)),
  416. concat(toString(a17),',',toString(r17/cost),',', toString(a17/cost),',',toString(b17)),
  417. concat(toString(a18),',',toString(r18/cost),',', toString(a18/cost),',',toString(b18)),
  418. concat(toString(a19),',',toString(r19/cost),',', toString(a19/cost),',',toString(b19)),
  419. concat(toString(a20),',',toString(r20/cost),',', toString(a20/cost),',',toString(b20)),
  420. concat(toString(a21),',',toString(r21/cost),',', toString(a21/cost),',',toString(b21)),
  421. concat(toString(a22),',',toString(r22/cost),',', toString(a22/cost),',',toString(b22)),
  422. concat(toString(a23),',',toString(r23/cost),',', toString(a23/cost),',',toString(b23)),
  423. concat(toString(a24),',',toString(r24/cost),',', toString(a24/cost),',',toString(b24)),
  424. concat(toString(a25),',',toString(r25/cost),',', toString(a25/cost),',',toString(b25)),
  425. concat(toString(a26),',',toString(r26/cost),',', toString(a26/cost),',',toString(b26)),
  426. concat(toString(a27),',',toString(r27/cost),',', toString(a27/cost),',',toString(b27)),
  427. concat(toString(a28),',',toString(r28/cost),',', toString(a28/cost),',',toString(b28)),
  428. concat(toString(a29),',',toString(r29/cost),',', toString(a29/cost),',',toString(b29)),
  429. concat(toString(a30),',',toString(r30/cost),',', toString(a30/cost),',',toString(b30)),
  430. concat(toString(a31),',',toString(r31/cost),',', toString(a31/cost),',',toString(b31)),
  431. concat(toString(a32),',',toString(r32/cost),',', toString(a32/cost),',',toString(b32)),
  432. concat(toString(a33),',',toString(r33/cost),',', toString(a33/cost),',',toString(b33)),
  433. concat(toString(a34),',',toString(r34/cost),',', toString(a34/cost),',',toString(b34)),
  434. concat(toString(a35),',',toString(r35/cost),',', toString(a35/cost),',',toString(b35)),
  435. concat(toString(a36),',',toString(r36/cost),',', toString(a36/cost),',',toString(b36)),
  436. concat(toString(a37),',',toString(r37/cost),',', toString(a37/cost),',',toString(b37)),
  437. concat(toString(a38),',',toString(r38/cost),',', toString(a38/cost),',',toString(b38)),
  438. concat(toString(a39),',',toString(r39/cost),',', toString(a39/cost),',',toString(b39)),
  439. concat(toString(a40),',',toString(r40/cost),',', toString(a40/cost),',',toString(b40)),
  440. concat(toString(a41),',',toString(r41/cost),',', toString(a41/cost),',',toString(b41)),
  441. concat(toString(a42),',',toString(r42/cost),',', toString(a42/cost),',',toString(b42)),
  442. concat(toString(a43),',',toString(r43/cost),',', toString(a43/cost),',',toString(b43)),
  443. concat(toString(a44),',',toString(r44/cost),',', toString(a44/cost),',',toString(b44)),
  444. concat(toString(a45),',',toString(r45/cost),',', toString(a45/cost),',',toString(b45)),
  445. concat(toString(a46),',',toString(r46/cost),',', toString(a46/cost),',',toString(b46)),
  446. concat(toString(a47),',',toString(r47/cost),',', toString(a47/cost),',',toString(b47)),
  447. concat(toString(a48),',',toString(r48/cost),',', toString(a48/cost),',',toString(b48)),
  448. concat(toString(a49),',',toString(r49/cost),',', toString(a49/cost),',',toString(b49)),
  449. concat(toString(a50),',',toString(r50/cost),',', toString(a50/cost),',',toString(b50)),
  450. concat(toString(a51),',',toString(r51/cost),',', toString(a51/cost),',',toString(b51)),
  451. concat(toString(a52),',',toString(r52/cost),',', toString(a52/cost),',',toString(b52)),
  452. concat(toString(a53),',',toString(r53/cost),',', toString(a53/cost),',',toString(b53)),
  453. concat(toString(a54),',',toString(r54/cost),',', toString(a54/cost),',',toString(b54)),
  454. concat(toString(a55),',',toString(r55/cost),',', toString(a55/cost),',',toString(b55)),
  455. concat(toString(a56),',',toString(r56/cost),',', toString(a56/cost),',',toString(b56)),
  456. concat(toString(a57),',',toString(r57/cost),',', toString(a57/cost),',',toString(b57)),
  457. concat(toString(a58),',',toString(r58/cost),',', toString(a58/cost),',',toString(b58)),
  458. concat(toString(a59),',',toString(r59/cost),',', toString(a59/cost),',',toString(b59)),
  459. concat(toString(a60),',',toString(r60/cost),',', toString(a60/cost),',',toString(b60)),
  460. concat(toString(a90),',',toString(r90/cost),',', toString(a90/cost),',',toString(b90)),
  461. concat(toString(a120),',',toString(r120/cost),',', toString(a120/cost),',',toString(b120)),
  462. concat(toString(a150),',',toString(r150/cost),',', toString(a150/cost),',',toString(b150))
  463. from
  464. (
  465. select dt,book,cost,reg_order_amount,
  466. if(cost=0,0,reg_order_amount/cost) roi,
  467. follow_user new_follow_user,
  468. if(follow_user=0,0,cost/follow_user) new_follow_per_cost,
  469. reg_order_user order_user, reg_order_count order_count,
  470. if(reg_order_user=0,0,cost/reg_order_user) order_user_per_cost
  471. from dw_daily_channel where dt>='{start}' and dt<='{end}' and channel='{channel}' and cost>0) a
  472. left outer join (
  473. select toDate(formatDateTime(reg_time,'%Y-%m-%d')) dt,
  474. sum(if(toDate(formatDateTime(reg_time,'%Y-%m-%d'))=date,amount,0)) as r1,
  475. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),1)>=date,amount,0)) as r2,
  476. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),2)>=date,amount,0)) as r3,
  477. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),3)>=date,amount,0)) as r4,
  478. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),4)>=date,amount,0)) as r5,
  479. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),5)>=date,amount,0)) as r6,
  480. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),6)>=date,amount,0)) as r7,
  481. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),7)>=date,amount,0)) as r8,
  482. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),8)>=date,amount,0)) as r9,
  483. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),9)>=date,amount,0)) as r10,
  484. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),10)>=date,amount,0)) as r11,
  485. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),11)>=date,amount,0)) as r12,
  486. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),12)>=date,amount,0)) as r13,
  487. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),13)>=date,amount,0)) as r14,
  488. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),14)>=date,amount,0)) as r15,
  489. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),15)>=date,amount,0)) as r16,
  490. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),16)>=date,amount,0)) as r17,
  491. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),17)>=date,amount,0)) as r18,
  492. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),18)>=date,amount,0)) as r19,
  493. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),19)>=date,amount,0)) as r20,
  494. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),20)>=date,amount,0)) as r21,
  495. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),21)>=date,amount,0)) as r22,
  496. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),22)>=date,amount,0)) as r23,
  497. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),23)>=date,amount,0)) as r24,
  498. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),24)>=date,amount,0)) as r25,
  499. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),25)>=date,amount,0)) as r26,
  500. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),26)>=date,amount,0)) as r27,
  501. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),27)>=date,amount,0)) as r28,
  502. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),28)>=date,amount,0)) as r29,
  503. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),29)>=date,amount,0)) as r30,
  504. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),30)>=date,amount,0)) as r31,
  505. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),31)>=date,amount,0)) as r32,
  506. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),32)>=date,amount,0)) as r33,
  507. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),33)>=date,amount,0)) as r34,
  508. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),34)>=date,amount,0)) as r35,
  509. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),35)>=date,amount,0)) as r36,
  510. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),36)>=date,amount,0)) as r37,
  511. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),37)>=date,amount,0)) as r38,
  512. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),38)>=date,amount,0)) as r39,
  513. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),39)>=date,amount,0)) as r40,
  514. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),40)>=date,amount,0)) as r41,
  515. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),41)>=date,amount,0)) as r42,
  516. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),42)>=date,amount,0)) as r43,
  517. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),43)>=date,amount,0)) as r44,
  518. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),44)>=date,amount,0)) as r45,
  519. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),45)>=date,amount,0)) as r46,
  520. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),46)>=date,amount,0)) as r47,
  521. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),47)>=date,amount,0)) as r48,
  522. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),48)>=date,amount,0)) as r49,
  523. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),49)>=date,amount,0)) as r50,
  524. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),50)>=date,amount,0)) as r51,
  525. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),51)>=date,amount,0)) as r52,
  526. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),52)>=date,amount,0)) as r53,
  527. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),53)>=date,amount,0)) as r54,
  528. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),54)>=date,amount,0)) as r55,
  529. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),55)>=date,amount,0)) as r56,
  530. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),56)>=date,amount,0)) as r57,
  531. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),57)>=date,amount,0)) as r58,
  532. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),58)>=date,amount,0)) as r59,
  533. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),59)>=date,amount,0)) as r60,
  534. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),89)>=date,amount,0)) as r90,
  535. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),119)>=date,amount,0)) as r120,
  536. sum(if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),149)>=date,amount,0)) as r150,
  537. 0 a1,1 b1,
  538. r2-r1 a2,if(r1=0,0,r2/r1) b2,
  539. r3-r2 a3,if(r1=0,0,r3/r1) b3,
  540. r4-r3 a4,if(r1=0,0,r4/r1) b4,
  541. r5-r4 a5,if(r1=0,0,r5/r1) b5,
  542. r6-r5 a6,if(r1=0,0,r6/r1) b6,
  543. r7-r6 a7,if(r1=0,0,r7/r1) b7,
  544. r8-r7 a8,if(r1=0,0,r8/r1) b8,
  545. r9-r8 a9,if(r1=0,0,r9/r1) b9,
  546. r10-r9 a10,if(r1=0,0,r10/r1) b10,
  547. r11-r10 a11,if(r1=0,0,r11/r1) b11,
  548. r12-r11 a12,if(r1=0,0,r12/r1) b12,
  549. r13-r12 a13,if(r1=0,0,r13/r1) b13,
  550. r14-r13 a14,if(r1=0,0,r14/r1) b14,
  551. r15-r14 a15,if(r1=0,0,r15/r1) b15,
  552. r16-r15 a16,if(r1=0,0,r16/r1) b16,
  553. r17-r16 a17,if(r1=0,0,r17/r1) b17,
  554. r18-r17 a18,if(r1=0,0,r18/r1) b18,
  555. r19-r18 a19,if(r1=0,0,r19/r1) b19,
  556. r20-r19 a20,if(r1=0,0,r20/r1) b20,
  557. r21-r20 a21,if(r1=0,0,r21/r1) b21,
  558. r22-r21 a22,if(r1=0,0,r22/r1) b22,
  559. r23-r22 a23,if(r1=0,0,r23/r1) b23,
  560. r24-r23 a24,if(r1=0,0,r24/r1) b24,
  561. r25-r24 a25,if(r1=0,0,r25/r1) b25,
  562. r26-r25 a26,if(r1=0,0,r26/r1) b26,
  563. r27-r26 a27,if(r1=0,0,r27/r1) b27,
  564. r28-r27 a28,if(r1=0,0,r28/r1) b28,
  565. r29-r28 a29,if(r1=0,0,r29/r1) b29,
  566. r30-r29 a30,if(r1=0,0,r30/r1) b30,
  567. r31-r30 a31,if(r1=0,0,r31/r1) b31,
  568. r32-r31 a32,if(r1=0,0,r32/r1) b32,
  569. r33-r32 a33,if(r1=0,0,r33/r1) b33,
  570. r34-r33 a34,if(r1=0,0,r34/r1) b34,
  571. r35-r34 a35,if(r1=0,0,r35/r1) b35,
  572. r36-r35 a36,if(r1=0,0,r36/r1) b36,
  573. r37-r36 a37,if(r1=0,0,r37/r1) b37,
  574. r38-r37 a38,if(r1=0,0,r38/r1) b38,
  575. r39-r38 a39,if(r1=0,0,r39/r1) b39,
  576. r40-r39 a40,if(r1=0,0,r40/r1) b40,
  577. r41-r40 a41,if(r1=0,0,r41/r1) b41,
  578. r42-r41 a42,if(r1=0,0,r42/r1) b42,
  579. r43-r42 a43,if(r1=0,0,r43/r1) b43,
  580. r44-r43 a44,if(r1=0,0,r44/r1) b44,
  581. r45-r44 a45,if(r1=0,0,r45/r1) b45,
  582. r46-r45 a46,if(r1=0,0,r46/r1) b46,
  583. r47-r46 a47,if(r1=0,0,r47/r1) b47,
  584. r48-r47 a48,if(r1=0,0,r48/r1) b48,
  585. r49-r48 a49,if(r1=0,0,r49/r1) b49,
  586. r50-r49 a50,if(r1=0,0,r50/r1) b50,
  587. r51-r50 a51,if(r1=0,0,r51/r1) b51,
  588. r52-r51 a52,if(r1=0,0,r52/r1) b52,
  589. r53-r52 a53,if(r1=0,0,r53/r1) b53,
  590. r54-r53 a54,if(r1=0,0,r54/r1) b54,
  591. r55-r54 a55,if(r1=0,0,r55/r1) b55,
  592. r56-r55 a56,if(r1=0,0,r56/r1) b56,
  593. r57-r56 a57,if(r1=0,0,r57/r1) b57,
  594. r58-r57 a58,if(r1=0,0,r58/r1) b58,
  595. r59-r58 a59,if(r1=0,0,r59/r1) b59,
  596. r60-r59 a60,if(r1=0,0,r60/r1) b60,
  597. r90-r60 a90,if(r1=0,0,r90/r1) b90,
  598. r120-r90 a120,if(r1=0,0,r120/r1) b120,
  599. r150-r120 a150,if(r1=0,0,r150/r1) b150
  600. from order where channel='{channel}' and dt>='{start}' group by formatDateTime(reg_time,'%Y-%m-%d')
  601. ) b on a.dt=b.dt
  602. """
  603. if start==end:
  604. total=1
  605. else:
  606. total = ck.execute(f"select count(1) from ({sql}) a")[0][0]
  607. sql += f" order by {order_by} {order} limit {(page-1)*page_size},{page_size} "
  608. data=get_round(ck.execute(sql))
  609. # print(data)
  610. def parse(str):
  611. li=str.split(',')
  612. li[0]=round(float(li[0]),2)
  613. li[1]=round(float(li[1]),4)
  614. li[2]=round(float(li[2]),4)
  615. li[3]=round(float(li[3]),2)
  616. return dict(zip(['order','roi','add','mult'],li))
  617. for i in data:
  618. for x,y in enumerate(i):
  619. if x>=11:
  620. i[x]=parse(y)
  621. print(data)
  622. key=['channel','date','book','cost','reg_amount','roi','new_follow_user','new_follow_per_cost','order_user','order_count',
  623. 'order_user_per_cost','d1','d2','d3','d4','d5','d6','d7','d8','d9','d10','d11','d12','d13','d14','d15','d16','d17','d18',
  624. 'd19','d20','d21','d22','d23','d24','d25','d26','d27','d28','d29','d30','d31','d32','d33','d34','d35','d36','d37','d38',
  625. 'd39','d40','d41','d42','d43','d44','d45','d46','d47','d48','d49','d50','d51','d52','d53','d54','d55','d56','d57','d58','d59',
  626. 'd60','m3','m4','m5']
  627. return get_dict_list(key,data),total
  628. def get_channel_summary(channel,pitcher,page,page_size,order_by,order,state):
  629. pitcher_op=f" and pitcher='{pitcher}'" if pitcher!='' else ''
  630. channel_op=f" and channel='{channel}'" if channel!='' else ''
  631. sql = f"""select a.channel channel,state,'朋友圈' location,toString(start) start,toString(end) end,total_cost,total_amount,
  632. profit,roi,follow_user,follow_per_cost,order_user,
  633. if(follow_user=0,0,order_user/follow_user) order_tran_rate,if(order_user=0,0,total_cost/order_user) order_tran_cost
  634. from
  635. (
  636. select channel,if(max(dt)>subtractDays(today(),10),'在投','停投') state,
  637. min(dt) start,max(dt) end
  638. from dw_daily_channel_cost where cost>0 {pitcher_op} {channel_op} group by channel) a
  639. left outer join(
  640. select channel,sum(follow_user) follow_user,
  641. if(follow_user=0,0,sum(cost)/follow_user) follow_per_cost
  642. from dw_daily_channel where 1=1 {pitcher_op} {channel_op} group by channel) b on a.channel=b.channel
  643. left outer join(
  644. select sum(reg_order_user) order_user,channel
  645. from dw_daily_channel where 1=1 {pitcher_op} {channel_op} group by channel) c on a.channel=c.channel
  646. left outer join (
  647. select total_cost,
  648. total_amount,
  649. channel,
  650. total_amount-total_cost profit,
  651. if(total_cost=0,0,total_amount/total_cost) roi
  652. from dw_channel_daily_total where dt=today() {channel_op}) d on a.channel=d.channel
  653. """
  654. print(sql)
  655. if channel!='':
  656. total=1
  657. else:
  658. sql+=f" having state='{state}' " if state!='' else ''
  659. total = ck.execute(f"select count(1) from ({sql}) a")[0][0]
  660. sql += f' order by {order_by} {order} limit {(page-1)*page_size},{page_size} '
  661. print(sql)
  662. key=['channel','state','location','start','end','total_cost','total_amount','profit','roi',
  663. 'follow_user','follow_per_cost','order_user','order_tran_rate','order_tran_cost']
  664. print(ck.execute(sql))
  665. return get_dict_list(key,get_round(ck.execute(sql))),total
  666. if __name__ == '__main__':
  667. # a=get_channel_overview('','','',1,10,'date','desc')
  668. # a=get_channel_summary('','')
  669. a=get_channel_again_order_trend('龙鳞文学','2021-01-01','宋刚')
  670. # a=get_channel_active('玉龙书社','2020-09-15','2020-09-20',1,10,'date','desc')
  671. # a,b =get_channel_order_trend("玉龙书社",'王俊杰','2020-12-01','2020-12-12',1,5,'date','desc')
  672. print(a)
  673. pass