pitcher_panel.py 34 KB

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