pitcher_panel.py 48 KB

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