pitcher_panel.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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},{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)),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,
  46. round(sum(cost),2) cost,
  47. round(sum(first_order_amount),2) first_order_amount,
  48. round(if(cost=0,0,first_order_amount/cost),4) first_roi ,
  49. round(sum(order_amount),2) order_amount,
  50. round(if(cost=0,0,sum(reg_order_amount)/cost),4) today_roi,
  51. round(sum(total_cost),2) total_cost,
  52. round(sum(total_amount),2) total_amount,
  53. round(total_amount-total_cost,2) total_profit,
  54. round(if(total_cost=0,0,total_amount/total_cost),4) total_roi
  55. from dw_daily_channel where dt>='{start}' and dt<='{end}' """
  56. if pitcher != 'all':
  57. sql += f" and pitcher='{pitcher}' "
  58. sql += f" group by date,pitcher order by {order_by} {order} limit {page},{page_size} "
  59. print(sql)
  60. data = ck.execute(sql)
  61. total=ck.execute(f"select count(1) from ({sql}) a")[0][0]
  62. key=['date','pitcher','cost','first_order_amount','first_roi','order_amount','today_roi','total_cost','total_amount','total_profit','total_roi']
  63. return get_dict_list(key,data),total
  64. def get_pitcher_panel_overview(pitcher):
  65. sql=f"""select pitcher,
  66. toDecimal32(cost,2) cost,
  67. toDecimal32(amount,2) amount,
  68. toDecimal32(roi,4) roi,
  69. channel_count,on_channel_count,
  70. off_channel_count,
  71. toDecimal32(this_month_cost,2) this_month_cost,
  72. toDecimal32(this_month_amount,2) this_month_amount,
  73. toDecimal32(this_month_roi,4) this_month_roi,
  74. toDecimal32(last_month_cost,2) last_month_cost,
  75. toDecimal32(last_month_amount,2) last_month_amount,
  76. toDecimal32(last_month_roi,4) last_month_roi,
  77. toDecimal32(last_month_far_amount,2) last_month_far_amount,
  78. follow_user,
  79. toDecimal32(last_month_far_roi,2) last_month_far_roi
  80. from dm_pitcher_daily_page_total where dt='{du.get_n_days(-1)}'"""
  81. if pitcher != 'all':
  82. sql += f" and pitcher='{pitcher}' "
  83. print(sql)
  84. data=ck.execute(sql)
  85. print(data)
  86. key=['pitcher','cost','amount','roi','channel_count','on_channel_count','off_channel_count','this_month_cost','this_month_amount','this_month_roi',
  87. 'last_month_cost','last_month_amount','last_month_roi','last_month_far_amount','follow_user','last_month_far_roi']
  88. return get_dict_list(key,data)
  89. def get_channel_overview(channel,pitcher,start,end,page,page_size,order_by,order):
  90. sql="""select channel,toString(dt) date,
  91. view_count,click_count,
  92. round(if(view_count=0,0,click_count/view_count),4) click_rate,
  93. follow_user,
  94. round(if(click_count=0,0,follow_user/click_count),4) follow_rate,
  95. round(if(follow_user=0,0,cost/follow_user),2) follow_per_cost,
  96. round(if(click_count=0,0,first_order_count/click_count),4) order_rate,
  97. round(if(first_order_user=0,0,cost/first_order_user),2) order_per_cost,
  98. round(cost,2) cost,
  99. first_order_count,first_order_user,
  100. round(first_order_amount,2) first_order_amount,order_count,order_user,
  101. round(order_amount,2) order_amount,
  102. round(order_amount-first_order_amount,2) old_order_amount,
  103. round(if(first_order_user=0,0,first_order_amount/first_order_user),2) first_amount_per_user,
  104. round(if(follow_user=0,0,first_order_amount/follow_user),2) amount_per_follow,
  105. round(if(first_order_user=0,0,cost/first_order_user),2) first_cost_per_user,
  106. round(if(follow_user=0,0,first_order_user/follow_user),4) new_user_order_rate,
  107. round(reg_order_amount,2) reg_user_amount,
  108. round(total_cost,2) total_cost,
  109. round(total_amount,2) total_amount,
  110. round(if(cost=0,0,first_order_amount/cost),4) day_roi,
  111. round(if(cost=0,0,reg_order_amount/cost),4) all_roi,
  112. 0 avg_new_order_rate,
  113. 0 old_user_once_order_rate from dw_daily_channel where 1=1 """
  114. if channel!='':
  115. sql+=f" and channel='{channel}'"
  116. if start!='':
  117. sql+=f" and start>='{start}'"
  118. if end!='':
  119. sql+=f" and end<='{end}'"
  120. if pitcher!='':
  121. sql+=f" and pitcher='{pitcher}'"
  122. total = ck.execute(f"select count(1) from ({sql}) a")[0][0]
  123. sql += f" order by {order_by} {order} limit {page},{page_size} "
  124. print(sql)
  125. key=['channel','date','view_count','click_count','click_rate','follow_user','follow_rate','follow_per_cost','order_rate','order_per_cost','cost',
  126. 'first_order_count','first_order_user','first_order_amount','order_count','order_user','order_amount','old_order_amount','first_amount_per_user',
  127. 'new_user_order_rate','reg_user_amount','total_cost','total_amount','day_roi','all_roi','avg_new_order_rate','old_user_once_order_rate']
  128. return get_dict_list(key,get_round(ck.execute(sql))),total
  129. def get_channel_again_order_trend(channel,date,pitcher):
  130. if get_channel_belong_pitcher(channel)!=pitcher:
  131. return []
  132. sql=f"""select toString(dt) date,
  133. channel,book,
  134. round(cost,2) cost,
  135. round(reg_order_amount,2) reg_amount,
  136. round(if(cost=0,0,reg_order_amount/cost),4) roi,
  137. follow_user new_follow,
  138. round(if(follow_user=0,0,cost/follow_user),2) new_follow_per_cost,
  139. reg_order_count reg_count,reg_order_user reg_user,
  140. round(if(reg_order_user=0,0,cost/reg_order_user),2) cost_per_user,
  141. round(if(reg_order_user=0,0,reg_order_amount/reg_order_user),2) avg_order_amount,
  142. round(if(reg_order_user=0,0,reg_order_user_again/reg_order_user),4) avg_again_order_rate,
  143. order_count
  144. from dw_daily_channel where channel='{channel}' and dt='{date}'
  145. """
  146. print(sql)
  147. data=ck.execute(sql)
  148. print(data)
  149. key1=['date','channel','book','cost','reg_amount','roi','new_follow','new_follow_per_cost',
  150. 'reg_count','reg_user','cost_per_user','avg_order_amount','avg_again_order_rate','order_count']
  151. json1=get_dict_list(key1,get_round(data))[0]
  152. # print(json1)
  153. sql2=f"""select
  154. dateDiff(day,toDate('{date}'),date)+1 ddf,
  155. count(1) c1,
  156. sum(if(count>1,1,0)) c2,
  157. sum(if(count>2,1,0)) c3,
  158. sum(if(count>3,1,0)) c4,
  159. sum(if(count>4,1,0)) c5,
  160. sum(if(count>5,1,0)) c6
  161. from (
  162. select count(1) count,date
  163. from order where channel='{channel}' and date>='{date}' and date<=addDays(toDate('{date}'),6)
  164. and formatDateTime(reg_time,'%Y-%m-%d')='{date}' group by user_id,date
  165. ) a group by date"""
  166. df=ck.execute(sql2)
  167. # 补全
  168. xx=[i[0] for i in df]
  169. for i in range(1,8):
  170. if i not in xx:
  171. df.append((i,0,0,0,0,0,0))
  172. # 排序
  173. import operator
  174. df.sort(key=operator.itemgetter(0))
  175. reg_user=json1["reg_user"]
  176. li=[]
  177. for i in range(1,6):
  178. print(i)
  179. d = {}
  180. d["user_order_count"] = i
  181. d1={}
  182. d1["origin"]=df[0][i]
  183. d1["new"]=0
  184. d1["move"]=df[0][i+1]
  185. d1["now"]=d1["origin"]+d1['new']-d1["move"]
  186. d1["follow_order_rate"]=round(d1["now"]/reg_user,2) if reg_user!=0 else 0
  187. d['d1'] = d1
  188. d2={}
  189. d2["origin"] = d1["now"]
  190. d2["new"] = df[1][i]
  191. d2["move"] = df[1][i+1]
  192. d2["now"] = d2["origin"] +d2['new']- d2["move"]
  193. d2["follow_order_rate"] = round(d2["now"] / reg_user, 2) if reg_user!=0 else 0
  194. d['d2'] = d2
  195. d3={}
  196. d3["origin"] = d2["now"]
  197. d3["new"] = df[2][i]
  198. d3["move"] =df[2][i+1]
  199. d3["now"] = d3["origin"] +d3['new']- d3["move"]
  200. d3["follow_order_rate"] = round(d3["now"] / reg_user, 2) if reg_user!=0 else 0
  201. d['d3'] = d3
  202. d4={}
  203. d4["origin"] = d3["now"]
  204. d4["new"] = df[3][i]
  205. d4["move"] = df[3][i+1]
  206. d4["now"] = d4["origin"] +d4['new']- d4["move"]
  207. d4["follow_order_rate"] = round(d4["now"] / reg_user, 2) if reg_user!=0 else 0
  208. d['d4'] = d4
  209. d5={}
  210. d5["origin"] = d4["now"]
  211. d5["new"] = df[4][i]
  212. d5["move"] = df[4][i+1]
  213. d5["now"] = d5["origin"] +d5['new']- d5["move"]
  214. d5["follow_order_rate"] = round(d4["now"] / reg_user, 2) if reg_user!=0 else 0
  215. d['d5'] = d5
  216. d6 = {}
  217. d6["origin"] = d5["now"]
  218. d6["new"] = df[5][i]
  219. d6["move"] = df[5][i+1]
  220. d6["now"] = d6["origin"] +d6['new']- d6["move"]
  221. d6["follow_order_rate"] = round(d6["now"] / reg_user, 2) if reg_user!=0 else 0
  222. d['d6'] = d6
  223. d7 = {}
  224. d7["origin"] = d6["now"]
  225. d7["new"] = df[6][i]
  226. d7["move"] = df[6][i+1]
  227. d7["now"] = d7["origin"] +d7['new']- d7["move"]
  228. d7["follow_order_rate"] = round(d7["now"] / reg_user, 2) if reg_user!=0 else 0
  229. d['d7'] = d7
  230. li.append(d)
  231. json1['data']=li
  232. print([json1])
  233. return [json1]
  234. def get_channel_active(channel,pitcher,start,end,page,page_size,order_by,order):
  235. if get_channel_belong_pitcher(channel)!=pitcher:
  236. return [],0
  237. 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,
  238. order_user_per_cost,day7_avg_act_rate,day7_avg_act_per_cost,day30_avg_act_rate,ay30_avg_act_cost,
  239. act_per_cost,
  240. 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))),
  241. concat(toString(reg_order_user2),',',toString(if(reg_order_user2=0,0,cost/reg_order_user2)),',',toString(if(order_user=0,0,reg_order_user2/order_user))),
  242. concat(toString(reg_order_user3),',',toString(if(reg_order_user3=0,0,cost/reg_order_user3)),',',toString(if(order_user=0,0,reg_order_user3/order_user))),
  243. concat(toString(reg_order_user4),',',toString(if(reg_order_user4=0,0, cost/reg_order_user4)),',',toString(if(order_user=0,0,reg_order_user4/order_user))),
  244. concat(toString(reg_order_user5),',',toString(if(reg_order_user5=0,0 ,cost/reg_order_user5)),',',toString(if(order_user=0,0,reg_order_user5/order_user))),
  245. concat(toString(reg_order_user6),',',toString(if(reg_order_user6=0,0,cost/reg_order_user6)),',',toString(if(order_user=0,0,reg_order_user6/order_user))),
  246. concat(toString(reg_order_user7),',',toString(if(reg_order_user7=0,0,cost/reg_order_user7)),',',toString(if(order_user=0,0,reg_order_user7/order_user))),
  247. concat(toString(reg_order_user8),',',toString(if(reg_order_user8=0,0,cost/reg_order_user8)),',',toString(if(order_user=0,0,reg_order_user8/order_user))),
  248. concat(toString(reg_order_user9),',',toString(if(reg_order_user9=0,0,cost/reg_order_user9)),',',toString(if(order_user=0,0,reg_order_user9/order_user))),
  249. concat(toString(reg_order_user10),',',toString(if(reg_order_user10=0,0,cost/reg_order_user10)),',',toString(if(order_user=0,0,reg_order_user10/order_user))),
  250. concat(toString(reg_order_user11),',',toString(if(reg_order_user11=0,0,cost/reg_order_user11)),',',toString(if(order_user=0,0,reg_order_user11/order_user))),
  251. concat(toString(reg_order_user12),',',toString(if(reg_order_user12=0,0,cost/reg_order_user12)),',',toString(if(order_user=0,0,reg_order_user12/order_user))),
  252. concat(toString(reg_order_user13),',',toString(if(reg_order_user13=0,0,cost/reg_order_user13)),',',toString(if(order_user=0,0,reg_order_user13/order_user))),
  253. concat(toString(reg_order_user14),',',toString(if(reg_order_user14=0,0,cost/reg_order_user14)),',',toString(if(order_user=0,0,reg_order_user14/order_user))),
  254. concat(toString(reg_order_user15),',',toString(if(reg_order_user15=0,0,cost/reg_order_user15)),',',toString(if(order_user=0,0,reg_order_user15/order_user))),
  255. concat(toString(reg_order_user16),',',toString(if(reg_order_user16=0,0,cost/reg_order_user16)),',',toString(if(order_user=0,0,reg_order_user16/order_user))),
  256. concat(toString(reg_order_user17),',',toString(if(reg_order_user17=0,0,cost/reg_order_user17)),',',toString(if(order_user=0,0,reg_order_user17/order_user))),
  257. concat(toString(reg_order_user18),',',toString(if(reg_order_user18=0,0,cost/reg_order_user18)),',',toString(if(order_user=0,0,reg_order_user18/order_user))),
  258. concat(toString(reg_order_user19),',',toString(if(reg_order_user19=0,0,cost/reg_order_user19)),',',toString(if(order_user=0,0,reg_order_user19/order_user))),
  259. concat(toString(reg_order_user20),',',toString(if(reg_order_user20=0,0,cost/reg_order_user20)),',',toString(if(order_user=0,0,reg_order_user20/order_user))),
  260. concat(toString(reg_order_user21),',',toString(if(reg_order_user21=0,0,cost/reg_order_user21)),',',toString(if(order_user=0,0,reg_order_user21/order_user))),
  261. concat(toString(reg_order_user22),',',toString(if(reg_order_user22=0,0,cost/reg_order_user22)),',',toString(if(order_user=0,0,reg_order_user22/order_user))),
  262. concat(toString(reg_order_user23),',',toString(if(reg_order_user23=0,0,cost/reg_order_user23)),',',toString(if(order_user=0,0,reg_order_user23/order_user))),
  263. concat(toString(reg_order_user24),',',toString(if(reg_order_user24=0,0,cost/reg_order_user24)),',',toString(if(order_user=0,0,reg_order_user24/order_user))),
  264. concat(toString(reg_order_user25),',',toString(if(reg_order_user25=0,0,cost/reg_order_user25)),',',toString(if(order_user=0,0,reg_order_user25/order_user))),
  265. concat(toString(reg_order_user26),',',toString(if(reg_order_user26=0,0,cost/reg_order_user26)),',',toString(if(order_user=0,0,reg_order_user26/order_user))),
  266. concat(toString(reg_order_user27),',',toString(if(reg_order_user27=0,0,cost/reg_order_user27)),',',toString(if(order_user=0,0,reg_order_user27/order_user))),
  267. concat(toString(reg_order_user28),',',toString(if(reg_order_user28=0,0,cost/reg_order_user28)),',',toString(if(order_user=0,0,reg_order_user28/order_user))),
  268. concat(toString(reg_order_user29),',',toString(if(reg_order_user29=0,0,cost/reg_order_user29)),',',toString(if(order_user=0,0,reg_order_user29/order_user))),
  269. concat(toString(reg_order_user30),',',toString(if(reg_order_user30=0,0,cost/reg_order_user30)),',',toString(if(order_user=0,0,reg_order_user30/order_user)))
  270. from (
  271. select dt,book,cost,
  272. reg_order_amount reg_amount,
  273. if(cost=0,0,reg_order_amount/cost) roi,
  274. follow_user new_follow_user,
  275. if(follow_user=0,0,cost/follow_user) new_follow_per_cost,
  276. reg_order_user order_user,
  277. reg_order_count order_count,
  278. if(reg_order_user=0,0,cost/reg_order_user) order_user_per_cost,
  279. if(follow_user=0,0,reg_order_user7/follow_user) day7_avg_act_rate ,
  280. if(reg_order_user7=0,0,cost/reg_order_user7) day7_avg_act_per_cost,
  281. if(follow_user=0,0,reg_order_user30/follow_user) day30_avg_act_rate,
  282. if(reg_order_user30=0,0,cost/reg_order_user30) ay30_avg_act_cost,
  283. if(reg_order_user=0,0,cost/reg_order_user) act_per_cost
  284. from dw_daily_channel where channel='{channel}' and dt>='{start}' and dt<='{end}') a
  285. left outer join (
  286. select toDate(formatDateTime(reg_time,'%Y-%m-%d')) dt,
  287. count(distinct if(subtractDays(date, 1)>=reg_time,NULL,user_id)) reg_order_user1,
  288. count(distinct if(subtractDays(date, 2)>=reg_time,NULL,user_id)) reg_order_user2,
  289. count(distinct if(subtractDays(date, 3)>=reg_time,NULL,user_id)) reg_order_user3,
  290. count(distinct if(subtractDays(date, 4)>=reg_time,NULL,user_id)) reg_order_user4,
  291. count(distinct if(subtractDays(date, 5)>=reg_time,NULL,user_id)) reg_order_user5,
  292. count(distinct if(subtractDays(date, 6)>=reg_time,NULL,user_id)) reg_order_user6,
  293. count(distinct if(subtractDays(date, 7)>=reg_time,NULL,user_id)) reg_order_user7,
  294. count(distinct if(subtractDays(date, 8)>=reg_time,NULL,user_id)) reg_order_user8,
  295. count(distinct if(subtractDays(date, 9)>=reg_time,NULL,user_id)) reg_order_user9,
  296. count(distinct if(subtractDays(date, 10)>=reg_time,NULL,user_id)) reg_order_user10,
  297. count(distinct if(subtractDays(date, 11)>=reg_time,NULL,user_id)) reg_order_user11,
  298. count(distinct if(subtractDays(date, 12)>=reg_time,NULL,user_id)) reg_order_user12,
  299. count(distinct if(subtractDays(date, 13)>=reg_time,NULL,user_id)) reg_order_user13,
  300. count(distinct if(subtractDays(date, 14)>=reg_time,NULL,user_id)) reg_order_user14,
  301. count(distinct if(subtractDays(date, 15)>=reg_time,NULL,user_id)) reg_order_user15,
  302. count(distinct if(subtractDays(date, 16)>=reg_time,NULL,user_id)) reg_order_user16,
  303. count(distinct if(subtractDays(date, 17)>=reg_time,NULL,user_id)) reg_order_user17,
  304. count(distinct if(subtractDays(date, 18)>=reg_time,NULL,user_id)) reg_order_user18,
  305. count(distinct if(subtractDays(date, 19)>=reg_time,NULL,user_id)) reg_order_user19,
  306. count(distinct if(subtractDays(date, 20)>=reg_time,NULL,user_id)) reg_order_user20,
  307. count(distinct if(subtractDays(date, 21)>=reg_time,NULL,user_id)) reg_order_user21,
  308. count(distinct if(subtractDays(date, 22)>=reg_time,NULL,user_id)) reg_order_user22,
  309. count(distinct if(subtractDays(date, 23)>=reg_time,NULL,user_id)) reg_order_user23,
  310. count(distinct if(subtractDays(date, 24)>=reg_time,NULL,user_id)) reg_order_user24,
  311. count(distinct if(subtractDays(date, 25)>=reg_time,NULL,user_id)) reg_order_user25,
  312. count(distinct if(subtractDays(date, 26)>=reg_time,NULL,user_id)) reg_order_user26,
  313. count(distinct if(subtractDays(date, 27)>=reg_time,NULL,user_id)) reg_order_user27,
  314. count(distinct if(subtractDays(date, 28)>=reg_time,NULL,user_id)) reg_order_user28,
  315. count(distinct if(subtractDays(date, 29)>=reg_time,NULL,user_id)) reg_order_user29,
  316. count(distinct if(subtractDays(date, 30)>=reg_time,NULL,user_id)) reg_order_user30
  317. from order where channel='{channel}' and dt>='{start}' group by formatDateTime(reg_time,'%Y-%m-%d')) b on a.dt=b.dt
  318. """
  319. print(sql)
  320. total = ck.execute(f"select count(1) from ({sql}) a")[0][0]
  321. sql+=f" order by {order_by} {order} limit {page},{page_size} "
  322. data = ck.execute(sql)
  323. print(data)
  324. key=['date','channel','book','cost','reg_amount','roi','new_follow_user','new_follow_per_cost','order_user','order_count','order_user_per_cost',
  325. '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',
  326. 'd11','d12','d13','d14','d15','d16','d17','d18','d19','d20','d21','d22','d23','d24','d25','d26','d27','d28','d29','d30']
  327. x=get_dict_list(key,get_round(data))
  328. print(x)
  329. li=[]
  330. for i in x:
  331. di = {}
  332. for j in i:
  333. if j in ['d1','d2','d3','d4','d5','d6','d7','d8','d9','d10',
  334. 'd11','d12','d13','d14','d15','d16','d17','d18','d19','d20','d21','d22','d23','d24','d25','d26','d27','d28','d29','d30']:
  335. if i[j]:
  336. k=i[j].split(",")
  337. k[0]=round(float(k[0]),0)
  338. k[1]=round(float(k[1]),2)
  339. k[2]=round(float(k[2]),4)
  340. else:
  341. k=[0,0,0]
  342. di[j] = dict(zip(['act_user', 'act_cost_per_cost', 'act_rate'], k))
  343. else:
  344. di[j]=i[j]
  345. li.append(di)
  346. return li,total
  347. def get_channel_order_trend(channel,pitcher,start,end,page,page_size,order_by,order):
  348. if get_channel_belong_pitcher(channel)!=pitcher:
  349. return [],0
  350. sql=""
  351. total = ck.execute(f"select count(1) from ({sql}) a")[0][0]
  352. a=''
  353. return a,total
  354. def get_channel_summary(channel,pitcher,page,page_size,order_by,order):
  355. sql = """select a.channel,state,'朋友圈' location,toString(start) start,toString(end) end,total_cost,total_amount,
  356. profit,roi,follow_user,follow_per_cost,order_user,
  357. if(follow_user=0,0,order_user/follow_user) order_tran_rate,if(order_user=0,0,total_cost/order_user) order_tran_cost
  358. from
  359. (
  360. select channel,if(max(dt)>subtractDays(today(),10),'在投','停投') state,
  361. min(dt) start,max(dt) end
  362. from dw_daily_channel_cost where cost>0 {} group by channel) a
  363. left outer join(
  364. select channel,sum(follow_user) follow_user,
  365. if(follow_user=0,0,sum(cost)/follow_user) follow_per_cost
  366. from dw_daily_channel group by channel) b on a.channel=b.channel
  367. left outer join(
  368. select sum(reg_order_user) order_user,channel from dw_daily_channel where dt=subtractDays(today(),1) group by channel) c on a.channel=c.channel
  369. left outer join (
  370. select total_cost,
  371. total_amount,
  372. channel,
  373. total_amount-total_cost profit,
  374. if(total_cost=0,0,total_amount/total_cost) roi
  375. from dw_channel_daily_total where dt=subtractDays(today(),1)) d on a.channel=d.channel
  376. where 1=1 """
  377. if pitcher!='':
  378. sql=sql.format(f" and pitcher='{pitcher}'")
  379. else:
  380. sql=sql.format('')
  381. if channel!='':
  382. sql+=f" and channel='{channel}'"
  383. total = ck.execute(f"select count(1) from ({sql}) a")[0][0]
  384. sql+=f' order by {order_by} {order} limit {page},{page_size} '
  385. print(sql)
  386. key=['channel','state','location','start','end','total_cost','total_amount','profit','roi',
  387. 'follow_user','follow_per_cost','order_user','order_tran_rate','order_tran_cost']
  388. return get_dict_list(key,get_round(ck.execute(sql))),total
  389. if __name__ == '__main__':
  390. # a=get_channel_overview('','','',1,10,'date','desc')
  391. # a=get_channel_summary('','')
  392. a=get_channel_again_order_trend('龙鳞文学','2021-01-01','宋刚')
  393. # a=get_channel_active('玉龙书社','2020-09-15','2020-09-20',1,10,'date','desc')
  394. # print(a)
  395. pass