pitcher_panel.py 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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_order_amount,
  49. reg_amount,
  50. round(if(cost=0,0,first_order_amount/cost),4) first_roi,
  51. amount order_amount,
  52. round(if(cost=0,0,reg_amount/cost),4) today_roi,
  53. reg_amount-cost profit
  54. from dw_pitcher_trend where 1=1 {op1} {op2} {op3} {op4}
  55. """
  56. print(sql)
  57. sumSql=f"""
  58. select '总计' date,sum(cost) cost,
  59. sum(first_order_amount) first_order_amount,sum(reg_amount) reg_amount,
  60. round(if(sum(cost)=0,0,sum(first_order_amount)/sum(cost)),4) first_roi,
  61. sum(order_amount) order_amount,
  62. round(if(sum(cost)=0,0,sum(reg_amount)/sum(cost)),4) today_roi,
  63. sum(profit) profit
  64. from ({sql}) b """
  65. print(sumSql)
  66. return getLimitSumData(db.dm,sql,sumSql,page,page_size)
  67. def get_pitcher_panel_overview(pitcher):
  68. db=MysqlUtils()
  69. op1= f" and pitcher='{pitcher}'" if pitcher else ''
  70. sql=f"""select pitcher,
  71. total_cost cost,
  72. total_amount amount,
  73. if(total_cost=0,0,total_amount/total_cost) roi,
  74. channel_count,
  75. on_channel_count,
  76. off_channel_count,
  77. this_month_cost this_month_cost,
  78. this_month_amount this_month_amount,
  79. this_month_roi this_month_roi,
  80. last_month_cost last_month_cost,
  81. last_month_amount last_month_amount,
  82. last_month_roi last_month_roi,
  83. last_month_far_amount last_month_far_amount,
  84. follow_user,
  85. last_month_far_roi last_month_far_roi
  86. from dm_pitcher_daily_overview a
  87. left join (
  88. select sum(cost) total_cost,sum(reg_amount) total_amount,pitcher pitcher2 from dw_pitcher_trend group by pitcher) b on pitcher=pitcher2
  89. where 1=1 {op1}
  90. """
  91. print(sql)
  92. return db.dm.getData_json(sql)
  93. def get_channel_overview(channel,pitcher,start,end,page,page_size,order_by,order):
  94. db=MysqlUtils()
  95. if pitcher:
  96. op1 = f" and pitcher='{pitcher}'" if get_channels_from_user(
  97. pitcher).__len__() == 0 else f" and channel in {get_channels_from_user(pitcher)}"
  98. else:
  99. op1 = ''
  100. op2 = f" and channel='{channel}'" if channel else ''
  101. op3 = f" and dt>='{start}' " if start else ''
  102. op4 = f" and dt<='{end}' " if end else ''
  103. op5 = f" order by {order_by} {order}" if order_by and order else ''
  104. sql=f"""select channel,dt date,
  105. view_count,click_count,
  106. round(if(view_count=0,0,click_count/view_count),4) click_rate,
  107. follow_user,
  108. round(if(click_count=0,0,follow_user/click_count),4) follow_rate,
  109. round(if(follow_user=0,0,cost/follow_user),2) follow_per_cost,
  110. round(if(click_count=0,0,first_order_count/click_count),4) order_rate,
  111. round(if(first_order_user=0,0,cost/first_order_user),2) order_per_cost,
  112. cost,
  113. first_order_count,first_order_user,
  114. first_order_amount,
  115. order_count,order_user,
  116. order_amount,
  117. order_amount-first_order_amount old_order_amount,
  118. round(if(first_order_user=0,0,first_order_amount/first_order_user),2) first_amount_per_user,
  119. round(if(follow_user=0,0,first_order_amount/follow_user),2) amount_per_follow,
  120. round(if(first_order_user=0,0,cost/first_order_user),2) first_cost_per_user,
  121. round(if(follow_user=0,0,first_order_user/follow_user),4) new_user_order_rate,
  122. reg_order_amount reg_user_amount,
  123. round(if(cost=0,0,first_order_amount/cost),4) day_roi,
  124. round(reg_order_amount/cost,4) roi,
  125. reg_order_user_again,
  126. reg_order_user,
  127. round(if(reg_order_user=0,0,reg_order_user_again/reg_order_user),4) old_user_once_order_rate,
  128. pitcher,stage,book,
  129. round(cost/first_order_user,2) first_order_user_per_cost,
  130. round(reg_order_amount/reg_order_user,2) user_per_amount,
  131. round(follow_user/click_count,4) click_follow_rate,
  132. round(reg_order_user/follow_user,4) follow_order_rate
  133. from dw_channel_daily where 1=1 {op1} {op2} {op3} {op4} {op5} """
  134. print(sql)
  135. sumsql=f"""select '总计' date,
  136. sum(view_count) view_count,
  137. sum(click_count) click_count,
  138. round(sum(click_count)/sum(view_count),4) click_rate,
  139. sum(follow_user) follow_user,
  140. round(sum(follow_user)/sum(click_count),4) follow_rate,
  141. round(sum(cost)/sum(follow_user),2) follow_per_cost,
  142. round(sum(first_order_count)/sum(click_count),4) order_rate,
  143. round(sum(cost)/sum(first_order_user),2) order_per_cost,
  144. sum(cost) cost,
  145. sum(first_order_count) first_order_count,
  146. sum(first_order_user) first_order_user,
  147. sum(first_order_amount) first_order_amount,
  148. sum(order_count) order_count,
  149. sum(order_user) order_user,
  150. sum(order_amount) order_amount,
  151. sum(old_order_amount) old_order_amount,
  152. round(sum(first_order_amount)/sum(first_order_user),2) first_amount_per_user,
  153. round(sum(first_order_amount)/sum(follow_user),2) amount_per_follow,
  154. round(sum(cost)/sum(first_order_user),2) first_cost_per_user,
  155. round(sum(first_order_user)/sum(follow_user),4) new_user_order_rate,
  156. sum(reg_user_amount) reg_user_amount,
  157. round(sum(first_order_amount)/sum(cost),4) day_roi,
  158. round(sum(reg_user_amount)/sum(cost),4) roi,
  159. round(sum(reg_order_user_again)/sum(reg_order_user),4) old_user_once_order_rate,
  160. round(sum(cost)/sum(first_order_user),2) first_order_user_per_cost,
  161. round(sum(reg_user_amount)/sum(reg_order_user),2) user_per_amount,
  162. round(sum(follow_user)/sum(click_count),4) click_follow_rate,
  163. round(sum(reg_order_user)/sum(follow_user),4) follow_order_rate
  164. from ({sql}) a
  165. """
  166. return getLimitSumData(db.dm,sql,sumsql,page,page_size)
  167. def get_channel_again_order_trend(channel,date,pitcher):
  168. ck = CkUtils()
  169. # if get_channel_belong_pitcher(channel)!=pitcher:
  170. # return []
  171. sql=f"""select toString(dt) date,
  172. channel,book,pitcher,stage,
  173. toDecimal32(cost,2),
  174. toDecimal32(reg_order_amount,2),
  175. toDecimal32(if(cost=0,0,reg_order_amount/cost),4) roi,
  176. follow_user new_follow,
  177. toDecimal32(if(follow_user=0,0,cost/follow_user),2) new_follow_per_cost,
  178. reg_order_count reg_count,reg_order_user reg_user,
  179. toDecimal32(if(reg_order_user=0,0,cost/reg_order_user),2) cost_per_user,
  180. toDecimal32(if(reg_order_user=0,0,reg_order_amount/reg_order_user),2) avg_order_amount,
  181. toDecimal32(if(reg_order_user=0,0,reg_order_user_again/reg_order_user),4) avg_again_order_rate,
  182. if(reg_order_user=0,0,order_count/reg_order_user) order_count
  183. from dw_daily_channel where channel='{channel}' and dt='{date}'
  184. """
  185. # print(sql)
  186. data=ck.execute(sql)
  187. # print(data)
  188. key1=['date','channel','book','pitcher','stage','cost','reg_amount','roi','new_follow','new_follow_per_cost',
  189. 'reg_count','reg_user','cost_per_user','avg_order_amount','avg_again_order_rate','order_count']
  190. json1=get_dict_list(key1,get_round(data))[0]
  191. # print(json1)
  192. # print(json1)
  193. sql2=f"""select
  194. 1 as ddf,count(1) c1,sum(if(count>1,1,0)) c2,sum(if(count>2,1,0)) c3,
  195. sum(if(count>3,1,0)) c4,sum(if(count>4,1,0)) c5,sum(if(count>5,1,0)) c6
  196. from (select count(1) count
  197. from order where channel='{channel}' and date='{date}'
  198. and formatDateTime(reg_time,'%Y-%m-%d')='{date}' group by user_id,date) a
  199. union all
  200. select
  201. 2 as ddf,count(1) c1,sum(if(count>1,1,0)) c2,sum(if(count>2,1,0)) c3,
  202. sum(if(count>3,1,0)) c4,sum(if(count>4,1,0)) c5,sum(if(count>5,1,0)) c6
  203. from (
  204. select count(1) count
  205. from order where channel='{channel}' and date>='{date}' and date<=addDays(toDate('{date}'),1)
  206. and formatDateTime(reg_time,'%Y-%m-%d')='{date}' group by user_id) a
  207. union all
  208. select
  209. 3 as ddf,count(1) c1,sum(if(count>1,1,0)) c2,sum(if(count>2,1,0)) c3,
  210. sum(if(count>3,1,0)) c4,sum(if(count>4,1,0)) c5,sum(if(count>5,1,0)) c6
  211. from (
  212. select count(1) count
  213. from order where channel='{channel}' and date>='{date}' and date<=addDays(toDate('{date}'),2)
  214. and formatDateTime(reg_time,'%Y-%m-%d')='{date}' group by user_id) a
  215. union all
  216. select
  217. 4 as ddf,count(1) c1,sum(if(count>1,1,0)) c2,sum(if(count>2,1,0)) c3,
  218. sum(if(count>3,1,0)) c4,sum(if(count>4,1,0)) c5,sum(if(count>5,1,0)) c6
  219. from (
  220. select count(1) count
  221. from order where channel='{channel}' and date>='{date}' and date<=addDays(toDate('{date}'),3)
  222. and formatDateTime(reg_time,'%Y-%m-%d')='{date}' group by user_id) a
  223. union all
  224. select
  225. 5 as ddf,count(1) c1,sum(if(count>1,1,0)) c2,sum(if(count>2,1,0)) c3,
  226. sum(if(count>3,1,0)) c4,sum(if(count>4,1,0)) c5,sum(if(count>5,1,0)) c6
  227. from (
  228. select count(1) count
  229. from order where channel='{channel}' and date>='{date}' and date<=addDays(toDate('{date}'),4)
  230. and formatDateTime(reg_time,'%Y-%m-%d')='{date}' group by user_id) a
  231. union all
  232. select
  233. 6 as ddf,count(1) c1,sum(if(count>1,1,0)) c2,sum(if(count>2,1,0)) c3,
  234. sum(if(count>3,1,0)) c4,sum(if(count>4,1,0)) c5,sum(if(count>5,1,0)) c6
  235. from (
  236. select count(1) count
  237. from order where channel='{channel}' and date>='{date}' and date<=addDays(toDate('{date}'),5)
  238. and formatDateTime(reg_time,'%Y-%m-%d')='{date}' group by user_id) a
  239. union all
  240. select
  241. 7 as ddf,count(1) c1,sum(if(count>1,1,0)) c2,sum(if(count>2,1,0)) c3,
  242. sum(if(count>3,1,0)) c4,sum(if(count>4,1,0)) c5,sum(if(count>5,1,0)) c6
  243. from (
  244. select count(1) count
  245. from order where channel='{channel}' and date>='{date}' and date<=addDays(toDate('{date}'),6)
  246. and formatDateTime(reg_time,'%Y-%m-%d')='{date}' group by user_id) a"""
  247. df=ck.execute(sql2)
  248. print(df)
  249. # 补全
  250. # xx=[i[0] for i in df]
  251. # for i in range(1,8):
  252. # if i not in xx:
  253. # df.append((i,0,0,0,0,0,0))
  254. # 排序
  255. import operator
  256. df.sort(key=operator.itemgetter(0))
  257. print(df)
  258. reg_user=json1["reg_user"]
  259. li=[]
  260. for i in range(1,6):
  261. print(i)
  262. d = {}
  263. d["user_order_count"] = i
  264. d1={}
  265. d1["origin"]=df[0][i]
  266. d1["new"]=0
  267. d1["move"]=df[0][i+1]
  268. d1["now"]=d1["origin"]+d1['new']-d1["move"]
  269. d1["follow_order_rate"]=round(d1["now"]/df[0][1],2) if df[0][i]!=0 else 0
  270. d['d1'] = d1
  271. d2={}
  272. d2["origin"] = d1["now"]
  273. d2["new"] = df[1][i]-df[0][i]
  274. d2["move"] = df[1][i+1]-df[0][i+1]
  275. d2["now"] = df[1][i]-df[1][i+1]
  276. d2["follow_order_rate"] = round(d2["now"] /df[1][1], 2) if df[1][i]!=0 else 0
  277. d['d2'] = d2
  278. d3={}
  279. d3["origin"] = d2["now"]
  280. d3["new"] = df[2][i]-df[1][i]
  281. d3["move"] =df[2][i+1]-df[1][i+1]
  282. d3["now"] = df[2][i]-df[2][i+1]
  283. d3["follow_order_rate"] = round(d3["now"] / df[2][1], 2) if df[2][i]!=0 else 0
  284. d['d3'] = d3
  285. d4={}
  286. d4["origin"] = d3["now"]
  287. d4["new"] = df[3][i]-df[2][i]
  288. d4["move"] = df[3][i+1]-df[2][i+1]
  289. d4["now"] = df[3][i]-df[3][i+1]
  290. d4["follow_order_rate"] = round(d4["now"] / df[3][1], 2) if df[3][i]!=0 else 0
  291. d['d4'] = d4
  292. d5={}
  293. d5["origin"] = d4["now"]
  294. d5["new"] = df[4][i]-df[3][i]
  295. d5["move"] = df[4][i+1]-df[3][i+1]
  296. d5["now"] = df[4][i]-df[4][i+1]
  297. d5["follow_order_rate"] = round(d4["now"] / df[4][1], 2) if df[3][i]!=0 else 0
  298. d['d5'] = d5
  299. d6 = {}
  300. d6["origin"] = d5["now"]
  301. d6["new"] = df[5][i]-df[4][i]
  302. d6["move"] = df[5][i+1]-df[4][i+1]
  303. d6["now"] = df[5][i]-df[5][i+1]
  304. d6["follow_order_rate"] = round(d6["now"] / df[5][1], 2) if df[5][i]!=0 else 0
  305. d['d6'] = d6
  306. d7 = {}
  307. d7["origin"] = d6["now"]
  308. d7["new"] = df[6][i]-df[5][i]
  309. d7["move"] = df[6][i+1]-df[5][i+1]
  310. d7["now"] = df[6][i]-df[6][i+1]
  311. d7["follow_order_rate"] = round(d7["now"] / df[6][1], 2) if df[6][i]!=0 else 0
  312. d['d7'] = d7
  313. li.append(d)
  314. print(li)
  315. json1['data']=li
  316. # print([json1])
  317. return [json1]
  318. def get_channel_active(channel,pitcher,start,end,page,page_size,order_by,order):
  319. ck = CkUtils()
  320. channel_op=f" and channel='{channel}' " if channel!='' else ''
  321. if pitcher != '赞象 admin':
  322. if get_channels_from_user(pitcher).__len__() == 0:
  323. pitcher_op = f" and pitcher='{pitcher}'"
  324. else:
  325. pitcher_op = f" and channel in {get_channels_from_user(pitcher)}"
  326. 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,
  327. 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),
  328. toDecimal32(act_per_cost,2),
  329. 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))),
  330. 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))),
  331. 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))),
  332. 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))),
  333. 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))),
  334. 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))),
  335. 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))),
  336. 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))),
  337. 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))),
  338. 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))),
  339. 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))),
  340. 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))),
  341. 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))),
  342. 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))),
  343. 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))),
  344. 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))),
  345. 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))),
  346. 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))),
  347. 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))),
  348. 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))),
  349. 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))),
  350. 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))),
  351. 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))),
  352. 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))),
  353. 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))),
  354. 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))),
  355. 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))),
  356. 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))),
  357. 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))),
  358. 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)))
  359. from (
  360. select dt,book,cost,pitcher,channel,stage,
  361. reg_order_amount reg_amount,
  362. if(cost=0,0,reg_order_amount/cost) roi,
  363. follow_user new_follow_user,
  364. if(follow_user=0,0,cost/follow_user) new_follow_per_cost,
  365. reg_order_user order_user,
  366. reg_order_count order_count,
  367. if(reg_order_user=0,0,cost/reg_order_user) order_user_per_cost,
  368. if(follow_user=0,0,reg_order_user7/follow_user) day7_avg_act_rate ,
  369. if(reg_order_user7=0,0,cost/reg_order_user7) day7_avg_act_per_cost,
  370. if(follow_user=0,0,reg_order_user30/follow_user) day30_avg_act_rate,
  371. if(reg_order_user30=0,0,cost/reg_order_user30) ay30_avg_act_cost,
  372. if(reg_order_user=0,0,cost/reg_order_user) act_per_cost
  373. from dw_daily_channel where 1=1 {channel_op} and dt>='{start}' and dt<='{end}' {pitcher_op}) a
  374. left outer join (
  375. select toDate(formatDateTime(reg_time,'%Y-%m-%d')) dt,channel,
  376. count(distinct if(toDate(formatDateTime(reg_time,'%Y-%m-%d'))=date,user_id,NULL)) reg_order_user1,
  377. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),1)>=date,user_id,NULL)) reg_order_user2,
  378. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),2)>=date,user_id,NULL)) reg_order_user3,
  379. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),3)>=date,user_id,NULL)) reg_order_user4,
  380. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),4)>=date,user_id,NULL)) reg_order_user5,
  381. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),5)>=date,user_id,NULL)) reg_order_user6,
  382. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),6)>=date,user_id,NULL)) reg_order_user7,
  383. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),7)>=date,user_id,NULL)) reg_order_user8,
  384. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),8)>=date,user_id,NULL)) reg_order_user9,
  385. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),9)>=date,user_id,NULL)) reg_order_user10,
  386. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),10)>=date,user_id,NULL)) reg_order_user11,
  387. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),11)>=date,user_id,NULL)) reg_order_user12,
  388. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),12)>=date,user_id,NULL)) reg_order_user13,
  389. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),13)>=date,user_id,NULL)) reg_order_user14,
  390. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),14)>=date,user_id,NULL)) reg_order_user15,
  391. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),15)>=date,user_id,NULL)) reg_order_user16,
  392. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),16)>=date,user_id,NULL)) reg_order_user17,
  393. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),17)>=date,user_id,NULL)) reg_order_user18,
  394. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),18)>=date,user_id,NULL)) reg_order_user19,
  395. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),19)>=date,user_id,NULL)) reg_order_user20,
  396. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),20)>=date,user_id,NULL)) reg_order_user21,
  397. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),21)>=date,user_id,NULL)) reg_order_user22,
  398. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),22)>=date,user_id,NULL)) reg_order_user23,
  399. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),23)>=date,user_id,NULL)) reg_order_user24,
  400. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),24)>=date,user_id,NULL)) reg_order_user25,
  401. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),25)>=date,user_id,NULL)) reg_order_user26,
  402. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),26)>=date,user_id,NULL)) reg_order_user27,
  403. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),27)>=date,user_id,NULL)) reg_order_user28,
  404. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),28)>=date,user_id,NULL)) reg_order_user29,
  405. count(distinct if(addDays(toDate(formatDateTime(reg_time,'%Y-%m-%d')),29)>=date,user_id,NULL)) reg_order_user30
  406. 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
  407. """
  408. print(sql)
  409. total = ck.execute(f"select count(1) from ({sql}) a")[0][0]
  410. sql+=f" order by {order_by} {order} limit {(page-1)*page_size},{page_size} "
  411. data = ck.execute(sql)
  412. print(sql)
  413. print(data)
  414. 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',
  415. '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',
  416. 'd11','d12','d13','d14','d15','d16','d17','d18','d19','d20','d21','d22','d23','d24','d25','d26','d27','d28','d29','d30']
  417. x=get_dict_list(key,get_round(data))
  418. print(x)
  419. li=[]
  420. for i in x:
  421. di = {}
  422. for j in i:
  423. if j in ['d1','d2','d3','d4','d5','d6','d7','d8','d9','d10',
  424. 'd11','d12','d13','d14','d15','d16','d17','d18','d19','d20','d21','d22','d23','d24','d25','d26','d27','d28','d29','d30']:
  425. if i[j]:
  426. k=i[j].split(",")
  427. k[0]=round(float(k[0]),0)
  428. k[1]=round(float(k[1]),2)
  429. k[2]=round(float(k[2]),4)
  430. else:
  431. k=[0,0,0]
  432. di[j] = dict(zip(['act_user', 'act_cost_per_cost', 'act_rate'], k))
  433. else:
  434. di[j]=i[j]
  435. li.append(di)
  436. return li,total
  437. def get_channel_order_trend(channel,pitcher,start,end,page,page_size,order_by,order):
  438. db=MysqlUtils()
  439. if pitcher:
  440. op1 = f" and pitcher='{pitcher}'" if get_channels_from_user(
  441. pitcher).__len__() == 0 else f" and channel in {get_channels_from_user(pitcher)}"
  442. else:
  443. op1 = ''
  444. op2 = f" and channel='{channel}'" if channel else ''
  445. op3 = f" and dt>='{start}' " if start else ''
  446. op4 = f" and dt<='{end}' " if end else ''
  447. op5 = f" order by {order_by} {order}" if order_by and order else ''
  448. sql=f"""select stage,pitcher,a.channel,a.dt date,book,
  449. 0 require_roi,0 require_mult,
  450. -- round(r1*require_mult-cost,2) expect_profit,
  451. 0 expect_profit,
  452. cost,
  453. first_order_amount first_amount,
  454. reg_order_amount reg_amount,
  455. reg_order_amount-cost profit,
  456. follow_user new_follow_user,
  457. round(reg_order_amount/cost,4) roi,
  458. round(first_order_amount/cost) first_roi,
  459. round(cost/follow_user,2) new_follow_per_cost,
  460. reg_order_user order_user,
  461. reg_order_count order_count,
  462. round(cost/reg_order_user ,2) order_user_per_cost,
  463. concat(da1,',',da1/cost,',', 0,',',1) d1,
  464. concat(da2-da1,',',da2/cost,',', (da2-da1)/cost,',',da2/da1) d2,
  465. concat(da3-da2,',',da3/cost,',', (da3-da2)/cost,',',da3/da1) d3,
  466. concat(da4-da3,',',da4/cost,',', (da4-da3)/cost,',',da4/da1) d4,
  467. concat(da5-da4,',',da5/cost,',', (da5-da4)/cost,',',da5/da1) d5,
  468. concat(da6-da5,',',da6/cost,',', (da6-da5)/cost,',',da6/da1) d6,
  469. concat(da7-da6,',',da7/cost,',', (da7-da6)/cost,',',da7/da1) d7,
  470. concat(da8-da7,',',da8/cost,',', (da8-da7)/cost,',',da8/da1) d8,
  471. concat(da9-da8,',',da9/cost,',', (da9-da8)/cost,',',da9/da1) d9,
  472. concat(da10-da9 ,',' ,da10/cost ,',', (da10-da9)/cost ,',' ,da10/da1) d10,
  473. concat(da11-da10 ,',' ,da11/cost ,',', (da11-da10)/cost ,',' ,da11/da1) d11,
  474. concat(da12-da11 ,',' ,da12/cost ,',', (da12-da11)/cost ,',' ,da12/da1) d12,
  475. concat(da13-da12 ,',' ,da13/cost ,',', (da13-da12)/cost ,',' ,da13/da1) d13,
  476. concat(da14-da13 ,',' ,da14/cost ,',', (da14-da13)/cost ,',' ,da14/da1) d14,
  477. concat(da15-da14 ,',' ,da15/cost ,',', (da15-da14)/cost ,',' ,da15/da1) d15,
  478. concat(da16-da15 ,',' ,da16/cost ,',', (da16-da15)/cost ,',' ,da16/da1) d16,
  479. concat(da17-da16 ,',' ,da17/cost ,',', (da17-da16)/cost ,',' ,da17/da1) d17,
  480. concat(da18-da17 ,',' ,da18/cost ,',', (da18-da17)/cost ,',' ,da18/da1) d18,
  481. concat(da19-da18 ,',' ,da19/cost ,',', (da19-da18)/cost ,',' ,da19/da1) d19,
  482. concat(da20-da19 ,',' ,da20/cost ,',', (da20-da19)/cost ,',' ,da20/da1) d20,
  483. concat(da21-da20 ,',' ,da21/cost ,',', (da21-da20)/cost ,',' ,da21/da1) d21,
  484. concat(da22-da21 ,',' ,da22/cost ,',', (da22-da21)/cost ,',' ,da22/da1) d22,
  485. concat(da23-da22 ,',' ,da23/cost ,',', (da23-da22)/cost ,',' ,da23/da1) d23,
  486. concat(da24-da23 ,',' ,da24/cost ,',', (da24-da23)/cost ,',' ,da24/da1) d24,
  487. concat(da25-da24 ,',' ,da25/cost ,',', (da25-da24)/cost ,',' ,da25/da1) d25,
  488. concat(da26-da25 ,',' ,da26/cost ,',', (da26-da25)/cost ,',' ,da26/da1) d26,
  489. concat(da27-da26 ,',' ,da27/cost ,',', (da27-da26)/cost ,',' ,da27/da1) d27,
  490. concat(da28-da27 ,',' ,da28/cost ,',', (da28-da27)/cost ,',' ,da28/da1) d28,
  491. concat(da29-da28 ,',' ,da29/cost ,',', (da29-da28)/cost ,',' ,da29/da1) d29,
  492. concat(da30-da29 ,',' ,da30/cost ,',', (da30-da29)/cost ,',' ,da30/da1) d30,
  493. concat(da31-da30 ,',' ,da31/cost ,',', (da31-da30)/cost ,',' ,da31/da1) d31,
  494. concat(da32-da31 ,',' ,da32/cost ,',', (da32-da31)/cost ,',' ,da32/da1) d32,
  495. concat(da33-da32 ,',' ,da33/cost ,',', (da33-da32)/cost ,',' ,da33/da1) d33,
  496. concat(da34-da33 ,',' ,da34/cost ,',', (da34-da33)/cost ,',' ,da34/da1) d34,
  497. concat(da35-da34 ,',' ,da35/cost ,',', (da35-da34)/cost ,',' ,da35/da1) d35,
  498. concat(da36-da35 ,',' ,da36/cost ,',', (da36-da35)/cost ,',' ,da36/da1) d36,
  499. concat(da37-da36 ,',' ,da37/cost ,',', (da37-da36)/cost ,',' ,da37/da1) d37,
  500. concat(da38-da37 ,',' ,da38/cost ,',', (da38-da37)/cost ,',' ,da38/da1) d38,
  501. concat(da39-da38 ,',' ,da39/cost ,',', (da39-da38)/cost ,',' ,da39/da1) d39,
  502. concat(da40-da39 ,',' ,da40/cost ,',', (da40-da39)/cost ,',' ,da40/da1) d40,
  503. concat(da41-da40 ,',' ,da41/cost ,',', (da41-da40)/cost ,',' ,da41/da1) d41,
  504. concat(da42-da41 ,',' ,da42/cost ,',', (da42-da41)/cost ,',' ,da42/da1) d42,
  505. concat(da43-da42 ,',' ,da43/cost ,',', (da43-da42)/cost ,',' ,da43/da1) d43,
  506. concat(da44-da43 ,',' ,da44/cost ,',', (da44-da43)/cost ,',' ,da44/da1) d44,
  507. concat(da45-da44 ,',' ,da45/cost ,',', (da45-da44)/cost ,',' ,da45/da1) d45,
  508. concat(da46-da45 ,',' ,da46/cost ,',', (da46-da45)/cost ,',' ,da46/da1) d46,
  509. concat(da47-da46 ,',' ,da47/cost ,',', (da47-da46)/cost ,',' ,da47/da1) d47,
  510. concat(da48-da47 ,',' ,da48/cost ,',', (da48-da47)/cost ,',' ,da48/da1) d48,
  511. concat(da49-da48 ,',' ,da49/cost ,',', (da49-da48)/cost ,',' ,da49/da1) d49,
  512. concat(da50-da49 ,',' ,da50/cost ,',', (da50-da49)/cost ,',' ,da50/da1) d50,
  513. concat(da51-da50 ,',' ,da51/cost ,',', (da51-da50)/cost ,',' ,da51/da1) d51,
  514. concat(da52-da51 ,',' ,da52/cost ,',', (da52-da51)/cost ,',' ,da52/da1) d52,
  515. concat(da53-da52 ,',' ,da53/cost ,',', (da53-da52)/cost ,',' ,da53/da1) d53,
  516. concat(da54-da53 ,',' ,da54/cost ,',', (da54-da53)/cost ,',' ,da54/da1) d54,
  517. concat(da55-da54 ,',' ,da55/cost ,',', (da55-da54)/cost ,',' ,da55/da1) d55,
  518. concat(da56-da55 ,',' ,da56/cost ,',', (da56-da55)/cost ,',' ,da56/da1) d56,
  519. concat(da57-da56 ,',' ,da57/cost ,',', (da57-da56)/cost ,',' ,da57/da1) d57,
  520. concat(da58-da57 ,',' ,da58/cost ,',', (da58-da57)/cost ,',' ,da58/da1) d58,
  521. concat(da59-da58 ,',' ,da59/cost ,',', (da59-da58)/cost ,',' ,da59/da1) d59,
  522. concat(da60-da59 ,',' ,da60/cost ,',', (da60-da59)/cost ,',' ,da60/da1) d60,
  523. concat(dm3-da60 ,',' ,dm3/cost ,',', (dm3-da60)/cost ,',' ,dm3/da1) m3,
  524. concat(dm4-dm3 ,',' ,dm4/cost ,',', (dm4-dm3)/cost ,',' ,dm4/da1) m4,
  525. concat(dm5-dm4 ,',' ,dm5/cost ,',', (dm5-dm4)/cost ,',' ,dm5/da1) m5
  526. from ( select * from dw_channel where 1=1 {op1} {op2} {op3} {op4} ) a
  527. left join dw_channel_amount_daily b on a.channel=b.channel and a.dt=b.dt
  528. {op5}
  529. """
  530. sumsql=f"""select '总计' date,
  531. sum(cost) cost,
  532. sum(reg_amount) reg_amount,
  533. sum(first_amount) first_amount,
  534. sum(profit) profit,
  535. sum(new_follow_user) new_follow_user,
  536. sum(reg_amount)/sum(cost) roi,
  537. sum(first_amount)/sum(cost) first_roi,
  538. sum(cost)/sum(new_follow_user) new_follow_per_cost,
  539. sum(order_user) order_user,
  540. sum(order_count) order_count,
  541. sum(cost)/sum(order_count) order_user_per_cost,
  542. sum(expect_profit) expect_profit
  543. from ({sql}) a
  544. """
  545. data,total,total_data=getLimitSumData(db.dm,sql,sumsql,page,page_size)
  546. def parse(str):
  547. li=str.split(',')
  548. li[0]=round(float(li[0]),2)
  549. li[1]=round(float(li[1]),4)
  550. li[2]=round(float(li[2]),4)
  551. li[3]=round(float(li[3]),2)
  552. return dict(zip(['order','roi','add','mult'],li))
  553. for i in data:
  554. for x in i:
  555. if x in ['d1','d2','d3','d4','d5','d6','d7','d8','d9','d10','d11','d12','d13','d14','d15','d16','d17','d18',
  556. 'd19','d20','d21','d22','d23','d24','d25','d26','d27','d28','d29','d30','d31','d32','d33','d34','d35','d36','d37','d38',
  557. 'd39','d40','d41','d42','d43','d44','d45','d46','d47','d48','d49','d50','d51','d52','d53','d54','d55','d56','d57','d58','d59',
  558. 'd60','m3','m4','m5']:
  559. i[x]=parse(i[x]) if i[x] is not None else {}
  560. print(data)
  561. return data,total,total_data
  562. def get_channel_summary(channel,pitcher,page,page_size,order_by,order,state,location,start,end):
  563. db=MysqlUtils()
  564. op1=f" and channel='{channel}'" if channel else ''
  565. if pitcher:
  566. op2=f" and pitcher='{pitcher}'" if get_channels_from_user(pitcher).__len__()==0 else f" and channel in {get_channels_from_user(pitcher)}"
  567. else:
  568. op2=''
  569. op3=f" and channel='{channel}'" if channel else ''
  570. op4=f" and location='{location}' " if location else ''
  571. op5=f" and state='{state}'" if state else ''
  572. op6=f" and dt>='{start}'" if start else ''
  573. op7=f" and dt<='{end}'" if end else ''
  574. sql = f"""SELECT channel,
  575. if(end>date_sub(now(),interval 10 day),'在投','停投') state,
  576. location,start,end,total_cost,total_amount,
  577. total_amount-total_cost profit,
  578. if(total_cost=0,0,round(total_amount/total_cost,4)) roi,
  579. follow_user,
  580. if(follow_user=0,0,round(total_cost/follow_user,2)) follow_per_cost,
  581. order_user,
  582. if(follow_user=0,0,round(order_user/follow_user,4)) order_tran_rate,
  583. if(order_user=0,0,round(total_cost/order_user,2)) order_tran_cost,
  584. pitcher,stage
  585. FROM
  586. (select
  587. channel,pitcher,stage,
  588. case when type ='vx' then 'MP' when type ='qq' then 'GDT' end location,
  589. min(if(cost>0,dt,null)) start,
  590. max(if(cost>0,dt,null)) end,
  591. sum(cost) total_cost,
  592. sum(reg_order_amount) total_amount,
  593. sum(follow_user) follow_user,
  594. sum(reg_order_user) order_user
  595. from dw_channel_daily where 1=1 {op1} {op2} {op3} {op6} {op7} GROUP BY channel,type,pitcher,stage) a having 1=1 {op4} {op5} ORDER BY {order_by} {order}
  596. """
  597. print(sql)
  598. sumsql=f"""select '总计' channel,
  599. sum(total_cost) total_cost,
  600. sum(total_amount) total_amount,sum(profit) profit,
  601. round(sum(total_amount)/sum(total_cost),4) roi,
  602. sum(follow_user) follow_user,
  603. sum(order_user) order_user,
  604. round(sum(total_cost)/sum(follow_user),2) follow_per_cost,
  605. round(sum(order_user)/sum(follow_user),4) order_tran_rate,
  606. round(sum(total_cost)/sum(order_user),2) order_tran_cost
  607. from ({sql}) a
  608. """
  609. return getLimitSumData(db.dm,sql,sumsql,page,page_size)
  610. def get_pitcher_trend(pitcher,start=None,end=None,page=None,page_size=None,order_by=None,order=None):
  611. db=MysqlUtils()
  612. op1=f" and pitcher='{pitcher}'"
  613. op2 = f" and dt>='{start}' " if start else ''
  614. op3 = f" and dt<='{end}' " if end else ''
  615. op4 = f" order by {order_by} {order}" if order_by and order else ''
  616. sql=f"""select dt,pitcher,
  617. cost,
  618. reg_amount,
  619. d1 first_amount,
  620. round(d1/cost,4) first_roi,
  621. round(reg_amount/cost,4) roi,
  622. d7 reg_amount7,
  623. round(d7/cost,4) roi7,
  624. d30 reg_amount30,
  625. round(d30/cost,4) roi30,
  626. reg_amount-cost profit,
  627. inva_cost expect_profit,
  628. CONCAT(d1,",",0,',',round(d1/cost,4)) d1,
  629. CONCAT(d2-d1,",",round((d2-d1)/cost,4),',',round(d2/cost,4)) d2,
  630. CONCAT(d3-d2,",",round((d3-d2)/cost,4),',',round(d3/cost,4)) d3,
  631. CONCAT(d4-d3,",",round((d4-d3)/cost,4),',',round(d4/cost,4)) d4,
  632. CONCAT(d5-d4,",",round((d5-d4)/cost,4),',',round(d5/cost,4)) d5,
  633. CONCAT(d6-d5,",",round((d6-d5)/cost,4),',',round(d6/cost,4)) d6,
  634. CONCAT(d7-d6,",",round((d7-d6)/cost,4),',',round(d7/cost,4)) d7,
  635. CONCAT(d8-d7,",",round((d8-d7)/cost,4),',',round(d8/cost,4)) d8,
  636. CONCAT(d9-d8,",",round((d9-d8)/cost,4),',',round(d9/cost,4)) d9,
  637. CONCAT(d10-d9,",",round((d10-d9)/cost,4),',',round(d10/cost,4)) d10,
  638. CONCAT(d11-d10,",",round((d11-d10)/cost,4),',',round(d11/cost,4)) d11,
  639. CONCAT(d12-d11,",",round((d12-d11)/cost,4),',',round(d12/cost,4)) d12,
  640. CONCAT(d13-d12,",",round((d13-d12)/cost,4),',',round(d13/cost,4)) d13,
  641. CONCAT(d14-d13,",",round((d14-d13)/cost,4),',',round(d14/cost,4)) d14,
  642. CONCAT(d15-d14,",",round((d15-d14)/cost,4),',',round(d15/cost,4)) d15,
  643. CONCAT(d16-d15,",",round((d16-d15)/cost,4),',',round(d16/cost,4)) d16,
  644. CONCAT(d17-d16,",",round((d17-d16)/cost,4),',',round(d17/cost,4)) d17,
  645. CONCAT(d18-d17,",",round((d18-d17)/cost,4),',',round(d18/cost,4)) d18,
  646. CONCAT(d19-d18,",",round((d19-d18)/cost,4),',',round(d19/cost,4)) d19,
  647. CONCAT(d20-d19,",",round((d20-d19)/cost,4),',',round(d20/cost,4)) d20,
  648. CONCAT(d21-d20,",",round((d21-d20)/cost,4),',',round(d21/cost,4)) d21,
  649. CONCAT(d22-d21,",",round((d22-d21)/cost,4),',',round(d22/cost,4)) d22,
  650. CONCAT(d23-d22,",",round((d23-d22)/cost,4),',',round(d23/cost,4)) d23,
  651. CONCAT(d24-d23,",",round((d24-d23)/cost,4),',',round(d24/cost,4)) d24,
  652. CONCAT(d25-d24,",",round((d25-d24)/cost,4),',',round(d25/cost,4)) d25,
  653. CONCAT(d26-d25,",",round((d26-d25)/cost,4),',',round(d26/cost,4)) d26,
  654. CONCAT(d27-d26,",",round((d27-d26)/cost,4),',',round(d27/cost,4)) d27,
  655. CONCAT(d28-d27,",",round((d28-d27)/cost,4),',',round(d28/cost,4)) d28,
  656. CONCAT(d29-d28,",",round((d29-d28)/cost,4),',',round(d29/cost,4)) d29,
  657. CONCAT(d30-d29,",",round((d30-d29)/cost,4),',',round(d30/cost,4)) d30
  658. from dw_pitcher_trend where 1=1 {op1} {op2} {op3} {op4}
  659. """
  660. sumSql=f"""select '总计' dt,sum(cost) cost,
  661. sum(reg_amount) reg_amount,
  662. round(sum(first_amount)/sum(cost),4) first_roi,
  663. round(sum(reg_amount)/sum(cost),4) roi,
  664. sum(profit) profit,
  665. round(sum(reg_amount7)/sum(cost),4) roi7,
  666. sum(reg_amount30) reg_amount30,
  667. round(sum(reg_amount30)/sum(cost),4) roi30,
  668. sum(expect_profit) expect_profit
  669. from ({sql}) a
  670. """
  671. data,total,total_data=getLimitSumData(db.dm,sql,sumSql,page,page_size)
  672. print(data)
  673. def parse(str):
  674. li=str.split(',')
  675. li[0]=round(float(li[0]),2)
  676. li[1]=round(float(li[1]),4)
  677. li[2]=round(float(li[2]),4)
  678. return dict(zip(['amount','add','roi'],li))
  679. for i in data:
  680. for k,v in i.items():
  681. if k in ['d1','d2','d3','d4','d5','d6','d7','d8','d9','d10',
  682. 'd11','d12','d13','d14','d15','d16','d17','d18','d19','d20','d21','d22','d23','d24','d25','d26','d27','d28','d29','d30']:
  683. i[k]= parse(v) if i["cost"]!=0 else {}
  684. return data,total,total_data
  685. if __name__ == '__main__':
  686. get_pitcher_trend(pitcher="陈凯")