pitcher_panel.py 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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_pitcher_panel_channel(pitcher, channel, start, end, page, page_size, order_by, order):
  21. db = MysqlUtils()
  22. op1 = f" and pitcher='{pitcher}'" if pitcher else ''
  23. op2 = f" and channel='{channel}'" if channel else ''
  24. sql = f"""select channel,stage,platform,book,
  25. dt as date,cost,first_order_amount,
  26. if(cost=0,0,first_order_amount/cost) first_roi,
  27. first_order_user,first_order_count,
  28. round(if(first_order_user=0,0,cost/first_order_user),2) first_per_cost,
  29. view_count,click_count,follow_user,
  30. round(if(click_count=0,0,follow_user/click_count),4) follow_rate,
  31. round(if(follow_user=0,0,cost/follow_user),2) follow_per_cost,
  32. total_cost,
  33. round(if(total_cost=0,0,total_amount/total_cost),4) back_rate
  34. from dw_channel_daily where dt>='{start}' and dt<='{end}' {op1} {op2}
  35. order by {order_by} {order}
  36. """
  37. return getLimitData(db.quchen_text, sql, page, page_size)
  38. def get_pitcher_panel_daily(pitcher, start, end, page, page_size, order_by, order):
  39. db = MysqlUtils()
  40. op1 = f" and pitcher='{pitcher}'" if pitcher else ''
  41. op2 = f" and dt>='{start}' " if start else ''
  42. op3 = f" and dt<='{end}' " if end else ''
  43. op4 = f" order by {order_by} {order}" if order_by and order else ''
  44. sql = f"""
  45. select dt date,
  46. pitcher,cost,
  47. first_order_amount,
  48. reg_amount,
  49. round(if(cost=0,0,first_order_amount/cost),4) first_roi,
  50. amount order_amount,
  51. round(if(cost=0,0,reg_amount/cost),4) today_roi,
  52. reg_amount-cost profit
  53. from dw_pitcher_trend where 1=1 {op1} {op2} {op3} {op4}
  54. """
  55. print(sql)
  56. sumSql = f"""
  57. select '总计' date,sum(cost) cost,
  58. sum(first_order_amount) first_order_amount,sum(reg_amount) reg_amount,
  59. round(if(sum(cost)=0,0,sum(first_order_amount)/sum(cost)),4) first_roi,
  60. sum(order_amount) order_amount,
  61. round(if(sum(cost)=0,0,sum(reg_amount)/sum(cost)),4) today_roi,
  62. sum(profit) profit
  63. from ({sql}) b """
  64. print(sumSql)
  65. return getLimitSumData(db.dm, sql, sumSql, page, page_size)
  66. def get_pitcher_panel_overview(pitcher):
  67. db = MysqlUtils()
  68. op1 = f" and pitcher='{pitcher}'" if pitcher else ''
  69. sql = f"""select pitcher,
  70. total_cost cost,
  71. total_amount amount,
  72. if(total_cost=0,0,total_amount/total_cost) roi,
  73. channel_count,
  74. on_channel_count,
  75. off_channel_count,
  76. this_month_cost this_month_cost,
  77. this_month_amount this_month_amount,
  78. this_month_roi this_month_roi,
  79. last_month_cost last_month_cost,
  80. last_month_amount last_month_amount,
  81. last_month_roi last_month_roi,
  82. last_month_far_amount last_month_far_amount,
  83. follow_user,
  84. last_month_far_roi last_month_far_roi
  85. from dm_pitcher_daily_overview a
  86. left join (
  87. select sum(cost) total_cost,sum(reg_amount) total_amount,pitcher pitcher2 from dw_pitcher_trend group by pitcher) b on pitcher=pitcher2
  88. where 1=1 {op1}
  89. """
  90. print(sql)
  91. return db.dm.getData_json(sql)
  92. def get_channel_overview(user_id, channel, pitcher, stage, book, start, end, page, page_size, order_by, order):
  93. db = MysqlUtils()
  94. if user_id in super_auth():
  95. op = ''
  96. else:
  97. if len(UserAuthUtils.get_auth_channel(user_id)) == 0:
  98. return None, None, None
  99. elif len(UserAuthUtils.get_auth_channel(user_id)) == 1:
  100. op = f" and channel = '{UserAuthUtils.get_auth_channel(user_id)[0]}'"
  101. else:
  102. op = f" and channel in {str(UserAuthUtils.get_auth_channel(user_id))}"
  103. op1 = f" and pitcher ='{pitcher}' " if pitcher else ''
  104. op2 = f" and channel='{channel}'" if channel else ''
  105. op3 = f" and dt>='{start}' " if start else ''
  106. op4 = f" and dt<='{end}' " if end else ''
  107. op5 = f" order by {order_by} {order}" if order_by and order else ''
  108. op6 = f" and stage='{stage}'" if stage else ''
  109. op7 = f" and book='{book}'" if book else ''
  110. sql = f"""select channel,dt date,reg_num,
  111. round(if(reg_num=0,0,cost/reg_num),2) reg_per_cost,
  112. create_user_num,
  113. round(if(create_user_num=0,0,cost/create_user_num),2) create_user_per_cost,
  114. view_count,click_count,
  115. round(if(view_count=0,0,click_count/view_count),4) click_rate,
  116. follow_user,
  117. round(if(click_count=0,0,follow_user/click_count),4) follow_rate,
  118. round(if(follow_user=0,0,cost/follow_user),2) follow_per_cost,
  119. round(if(click_count=0,0,first_order_count/click_count),4) order_rate,
  120. round(if(first_order_user=0,0,cost/first_order_user),2) order_per_cost,
  121. cost,
  122. first_order_count,first_order_user,
  123. first_order_amount,
  124. order_count,order_user,
  125. order_amount,
  126. game_user_sum,
  127. third_stay_rate,
  128. second_stay_rate,
  129. today_active_user_rate,
  130. order_amount-first_order_amount old_order_amount,
  131. round(if(first_order_user=0,0,first_order_amount/first_order_user),2) first_amount_per_user,
  132. round(if(follow_user=0,0,first_order_amount/follow_user),2) amount_per_follow,
  133. round(if(first_order_user=0,0,cost/reg_order_user),2) first_cost_per_user,
  134. round(if(follow_user=0,0,first_order_user/follow_user),4) new_user_order_rate,
  135. reg_order_amount reg_user_amount,
  136. round(if(cost=0,0,first_order_amount/cost),4) day_roi,
  137. round(reg_order_amount/cost,4) roi,
  138. reg_order_user_again,
  139. reg_order_user,
  140. round(if(reg_order_user=0,0,reg_order_user_again/reg_order_user),4) old_user_once_order_rate,
  141. pitcher,stage,book,type,
  142. round(cost/first_order_user,2) first_order_user_per_cost,
  143. round(reg_order_amount/reg_order_user,2) user_per_amount,
  144. round(follow_user/click_count,4) click_follow_rate,
  145. round(reg_order_user/follow_user,4) follow_order_rate
  146. from dw_channel where 1=1 {op} {op1} {op2} {op3} {op4} {op6} {op7} {op5} """
  147. sumsql = f"""select '总计' date,
  148. sum(reg_num) reg_num,
  149. sum(game_user_sum) game_user_sum,
  150. round(if(sum(reg_num)=0,0,sum(reg_num*third_stay_rate)/sum(reg_num)),2) third_stay_rate,
  151. round(if(sum(reg_num)=0,0,sum(reg_num*second_stay_rate)/sum(reg_num)),2) second_stay_rate,
  152. round(if(sum(game_user_sum)=0,0,sum(game_user_sum*today_active_user_rate)/sum(game_user_sum)),2) today_active_user_rate,
  153. round(if(sum(reg_num)=0,0,sum(cost)/sum(reg_num)),2) reg_per_cost,
  154. sum(create_user_num) create_user_num,
  155. round(if(sum(create_user_num)=0,0,sum(cost)/sum(create_user_num)),2) create_user_per_cost,
  156. sum(view_count) view_count,
  157. sum(click_count) click_count,
  158. round(sum(click_count)/sum(view_count),4) click_rate,
  159. sum(follow_user) follow_user,
  160. round(sum(follow_user)/sum(click_count),4) follow_rate,
  161. round(sum(cost)/sum(follow_user),2) follow_per_cost,
  162. round(sum(first_order_count)/sum(click_count),4) order_rate,
  163. round(sum(cost)/sum(first_order_user),2) order_per_cost,
  164. sum(cost) cost,
  165. sum(first_order_count) first_order_count,
  166. sum(first_order_user) first_order_user,
  167. sum(first_order_amount) first_order_amount,
  168. sum(order_count) order_count,
  169. sum(order_user) order_user,
  170. sum(order_amount) order_amount,
  171. sum(old_order_amount) old_order_amount,
  172. round(sum(first_order_amount)/sum(first_order_user),2) first_amount_per_user,
  173. round(sum(first_order_amount)/sum(follow_user),2) amount_per_follow,
  174. round(sum(cost)/sum(first_order_user),2) first_cost_per_user,
  175. round(sum(first_order_user)/sum(follow_user),4) new_user_order_rate,
  176. sum(reg_user_amount) reg_user_amount,
  177. round(sum(first_order_amount)/sum(cost),4) day_roi,
  178. round(sum(reg_user_amount)/sum(cost),4) roi,
  179. round(sum(reg_order_user_again)/sum(reg_order_user),4) old_user_once_order_rate,
  180. round(sum(cost)/sum(first_order_user),2) first_order_user_per_cost,
  181. round(sum(reg_user_amount)/sum(reg_order_user),2) user_per_amount,
  182. round(sum(follow_user)/sum(click_count),4) click_follow_rate,
  183. round(sum(reg_order_user)/sum(follow_user),4) follow_order_rate
  184. from ({sql}) a
  185. """
  186. return getLimitSumData(db.dm, sql, sumsql, page, page_size)
  187. def get_channel_again_order_trend(channel, date):
  188. ck = CkUtils()
  189. db = MysqlUtils()
  190. sql = f"""select dt date,
  191. channel,book,pitcher,stage,
  192. cost,
  193. reg_order_amount reg_amount,
  194. round(if(cost=0,0,reg_order_amount/cost),4) roi,
  195. follow_user new_follow,
  196. round(if(follow_user=0,0,cost/follow_user),2) new_follow_per_cost,
  197. reg_order_count reg_count,reg_order_user reg_user,
  198. round(if(reg_order_user=0,0,cost/reg_order_user),2) cost_per_user,
  199. round(if(reg_order_user=0,0,reg_order_amount/reg_order_user),2) avg_order_amount,
  200. round(if(reg_order_user=0,0,reg_order_user_again/reg_order_user),4) avg_again_order_rate,
  201. if(reg_order_user=0,0,order_count/reg_order_user) order_count
  202. from dw_channel where channel='{channel}' and dt='{date}'
  203. """
  204. # print(sql)
  205. j = db.dm.getData_json(sql)
  206. if len(j) == 0:
  207. return None
  208. else:
  209. json1 = j[0]
  210. # print(json1)
  211. # print(json1)
  212. sql2 = f"""select
  213. 1 as ddf,count(1) c1,sum(if(count>1,1,0)) c2,sum(if(count>2,1,0)) c3,
  214. sum(if(count>3,1,0)) c4,sum(if(count>4,1,0)) c5,sum(if(count>5,1,0)) c6
  215. from (select count(1) count
  216. from order where channel='{channel}' and date='{date}'and status=2
  217. and formatDateTime(reg_time,'%Y-%m-%d')='{date}' group by user_id,date) a
  218. union all
  219. select
  220. 2 as ddf,count(1) c1,sum(if(count>1,1,0)) c2,sum(if(count>2,1,0)) c3,
  221. sum(if(count>3,1,0)) c4,sum(if(count>4,1,0)) c5,sum(if(count>5,1,0)) c6
  222. from (
  223. select count(1) count
  224. from order where channel='{channel}' and date>='{date}' and date<=addDays(toDate('{date}'),1) and status=2
  225. and formatDateTime(reg_time,'%Y-%m-%d')='{date}' group by user_id) a
  226. union all
  227. select
  228. 3 as ddf,count(1) c1,sum(if(count>1,1,0)) c2,sum(if(count>2,1,0)) c3,
  229. sum(if(count>3,1,0)) c4,sum(if(count>4,1,0)) c5,sum(if(count>5,1,0)) c6
  230. from (
  231. select count(1) count
  232. from order where channel='{channel}' and date>='{date}' and date<=addDays(toDate('{date}'),2) and status=2
  233. and formatDateTime(reg_time,'%Y-%m-%d')='{date}' group by user_id) a
  234. union all
  235. select
  236. 4 as ddf,count(1) c1,sum(if(count>1,1,0)) c2,sum(if(count>2,1,0)) c3,
  237. sum(if(count>3,1,0)) c4,sum(if(count>4,1,0)) c5,sum(if(count>5,1,0)) c6
  238. from (
  239. select count(1) count
  240. from order where channel='{channel}' and date>='{date}' and date<=addDays(toDate('{date}'),3) and status=2
  241. and formatDateTime(reg_time,'%Y-%m-%d')='{date}' group by user_id) a
  242. union all
  243. select
  244. 5 as ddf,count(1) c1,sum(if(count>1,1,0)) c2,sum(if(count>2,1,0)) c3,
  245. sum(if(count>3,1,0)) c4,sum(if(count>4,1,0)) c5,sum(if(count>5,1,0)) c6
  246. from (
  247. select count(1) count
  248. from order where channel='{channel}' and date>='{date}' and date<=addDays(toDate('{date}'),4) and status=2
  249. and formatDateTime(reg_time,'%Y-%m-%d')='{date}' group by user_id) a
  250. union all
  251. select
  252. 6 as ddf,count(1) c1,sum(if(count>1,1,0)) c2,sum(if(count>2,1,0)) c3,
  253. sum(if(count>3,1,0)) c4,sum(if(count>4,1,0)) c5,sum(if(count>5,1,0)) c6
  254. from (
  255. select count(1) count
  256. from order where channel='{channel}' and date>='{date}' and date<=addDays(toDate('{date}'),5) and status=2
  257. and formatDateTime(reg_time,'%Y-%m-%d')='{date}' group by user_id) a
  258. union all
  259. select
  260. 7 as ddf,count(1) c1,sum(if(count>1,1,0)) c2,sum(if(count>2,1,0)) c3,
  261. sum(if(count>3,1,0)) c4,sum(if(count>4,1,0)) c5,sum(if(count>5,1,0)) c6
  262. from (
  263. select count(1) count
  264. from order where channel='{channel}' and date>='{date}' and date<=addDays(toDate('{date}'),6) and status=2
  265. and formatDateTime(reg_time,'%Y-%m-%d')='{date}' group by user_id) a"""
  266. df = ck.execute(sql2)
  267. print(df)
  268. # 补全
  269. # xx=[i[0] for i in df]
  270. # for i in range(1,8):
  271. # if i not in xx:
  272. # df.append((i,0,0,0,0,0,0))
  273. # 排序
  274. import operator
  275. df.sort(key=operator.itemgetter(0))
  276. print(df)
  277. reg_user = json1["reg_user"]
  278. li = []
  279. for i in range(1, 6):
  280. print(i)
  281. d = {}
  282. d["user_order_count"] = i
  283. d1 = {}
  284. d1["origin"] = df[0][i]
  285. d1["new"] = 0
  286. d1["move"] = df[0][i + 1]
  287. d1["now"] = d1["origin"] + d1['new'] - d1["move"]
  288. d1["follow_order_rate"] = round(d1["now"] / df[0][1], 2) if df[0][i] != 0 else 0
  289. d['d1'] = d1
  290. d2 = {}
  291. d2["origin"] = d1["now"]
  292. d2["new"] = df[1][i] - df[0][i]
  293. d2["move"] = df[1][i + 1] - df[0][i + 1]
  294. d2["now"] = df[1][i] - df[1][i + 1]
  295. d2["follow_order_rate"] = round(d2["now"] / df[1][1], 2) if df[1][i] != 0 else 0
  296. d['d2'] = d2
  297. d3 = {}
  298. d3["origin"] = d2["now"]
  299. d3["new"] = df[2][i] - df[1][i]
  300. d3["move"] = df[2][i + 1] - df[1][i + 1]
  301. d3["now"] = df[2][i] - df[2][i + 1]
  302. d3["follow_order_rate"] = round(d3["now"] / df[2][1], 2) if df[2][i] != 0 else 0
  303. d['d3'] = d3
  304. d4 = {}
  305. d4["origin"] = d3["now"]
  306. d4["new"] = df[3][i] - df[2][i]
  307. d4["move"] = df[3][i + 1] - df[2][i + 1]
  308. d4["now"] = df[3][i] - df[3][i + 1]
  309. d4["follow_order_rate"] = round(d4["now"] / df[3][1], 2) if df[3][i] != 0 else 0
  310. d['d4'] = d4
  311. d5 = {}
  312. d5["origin"] = d4["now"]
  313. d5["new"] = df[4][i] - df[3][i]
  314. d5["move"] = df[4][i + 1] - df[3][i + 1]
  315. d5["now"] = df[4][i] - df[4][i + 1]
  316. d5["follow_order_rate"] = round(d4["now"] / df[4][1], 2) if df[3][i] != 0 else 0
  317. d['d5'] = d5
  318. d6 = {}
  319. d6["origin"] = d5["now"]
  320. d6["new"] = df[5][i] - df[4][i]
  321. d6["move"] = df[5][i + 1] - df[4][i + 1]
  322. d6["now"] = df[5][i] - df[5][i + 1]
  323. d6["follow_order_rate"] = round(d6["now"] / df[5][1], 2) if df[5][i] != 0 else 0
  324. d['d6'] = d6
  325. d7 = {}
  326. d7["origin"] = d6["now"]
  327. d7["new"] = df[6][i] - df[5][i]
  328. d7["move"] = df[6][i + 1] - df[5][i + 1]
  329. d7["now"] = df[6][i] - df[6][i + 1]
  330. d7["follow_order_rate"] = round(d7["now"] / df[6][1], 2) if df[6][i] != 0 else 0
  331. d['d7'] = d7
  332. li.append(d)
  333. print(li)
  334. json1['data'] = li
  335. # print([json1])
  336. return [json1]
  337. def get_channel_active(user_id, channel, pitcher, start, end, page, page_size, order_by, order):
  338. db = MysqlUtils()
  339. if user_id in super_auth():
  340. op = ''
  341. else:
  342. if len(UserAuthUtils.get_auth_channel(user_id)) == 0:
  343. return None, None, None
  344. elif len(UserAuthUtils.get_auth_channel(user_id)) == 1:
  345. op = f" and channel = '{UserAuthUtils.get_auth_channel(user_id)[0]}'"
  346. else:
  347. op = f" and channel in {str(UserAuthUtils.get_auth_channel(user_id))}"
  348. op1 = f" and pitcher ='{pitcher}' " if pitcher else ''
  349. op2 = f" and channel='{channel}'" if channel else ''
  350. op3 = f" and dt>='{start}' " if start else ''
  351. op4 = f" and dt<='{end}' " if end else ''
  352. op5 = f" order by {order_by} {order}" if order_by and order else ''
  353. sql = f"""
  354. select channel,dt date,book,stage,pitcher,cost,
  355. reg_order_amount reg_amount,
  356. dc7,dc30,
  357. round(reg_order_amount/cost,4) roi,
  358. follow_user new_follow_user,
  359. round(cost/follow_user,2) new_follow_per_cost,
  360. reg_order_user order_user,
  361. reg_order_count order_count,
  362. round(cost/reg_order_user,2) order_user_per_cost,
  363. round(dc7/follow_user,4) day7_avg_act_rate,
  364. round(cost/dc7,2) day7_avg_act_per_cost,
  365. round(dc30/follow_user,4) day30_avg_act_rate,
  366. round(cost/dc30,2) day30_avg_act_cost,
  367. round(cost/reg_order_user,2) act_per_cost,
  368. concat(dc1,',',cost/dc1,',',dc1/reg_order_user) d1,
  369. concat(dc2-dc1,',',cost/dc2,',',(dc2-dc1)/reg_order_user) d2,
  370. concat(dc3-dc2,',',cost/dc3,',',(dc3-dc2)/reg_order_user) d3,
  371. concat(dc4-dc3,',',cost/dc4,',',(dc4-dc3)/reg_order_user) d4,
  372. concat(dc5-dc4,',',cost/dc5,',',(dc5-dc4)/reg_order_user) d5,
  373. concat(dc6-dc5,',',cost/dc6,',',(dc6-dc5)/reg_order_user) d6,
  374. concat(dc7-dc6,',',cost/dc7,',',(dc7-dc6)/reg_order_user) d7,
  375. concat(dc8-dc7,',',cost/dc8,',',(dc8-dc7)/reg_order_user) d8,
  376. concat(dc9-dc8,',',cost/dc9,',',(dc9-dc8)/reg_order_user) d9,
  377. concat(dc10-dc9,',',cost/dc10,',',(dc10-dc9)/reg_order_user) d10,
  378. concat(dc11-dc10,',',cost/dc11,',',(dc11-dc10)/reg_order_user) d11,
  379. concat(dc12-dc11,',',cost/dc12,',',(dc12-dc11)/reg_order_user) d12,
  380. concat(dc13-dc12,',',cost/dc13,',',(dc13-dc12)/reg_order_user) d13,
  381. concat(dc14-dc13,',',cost/dc14,',',(dc14-dc13)/reg_order_user) d14,
  382. concat(dc15-dc14,',',cost/dc15,',',(dc15-dc14)/reg_order_user) d15,
  383. concat(dc16-dc15,',',cost/dc16,',',(dc16-dc15)/reg_order_user) d16,
  384. concat(dc17-dc16,',',cost/dc17,',',(dc17-dc16)/reg_order_user) d17,
  385. concat(dc18-dc17,',',cost/dc18,',',(dc18-dc17)/reg_order_user) d18,
  386. concat(dc19-dc18,',',cost/dc19,',',(dc19-dc18)/reg_order_user) d19,
  387. concat(dc20-dc19,',',cost/dc20,',',(dc20-dc19)/reg_order_user) d20,
  388. concat(dc21-dc20,',',cost/dc21,',',(dc21-dc20)/reg_order_user) d21,
  389. concat(dc22-dc21,',',cost/dc22,',',(dc22-dc21)/reg_order_user) d22,
  390. concat(dc23-dc22,',',cost/dc23,',',(dc23-dc22)/reg_order_user) d23,
  391. concat(dc24-dc23,',',cost/dc24,',',(dc24-dc23)/reg_order_user) d24,
  392. concat(dc25-dc24,',',cost/dc25,',',(dc25-dc24)/reg_order_user) d25,
  393. concat(dc26-dc25,',',cost/dc26,',',(dc26-dc25)/reg_order_user) d26,
  394. concat(dc27-dc26,',',cost/dc27,',',(dc27-dc26)/reg_order_user) d27,
  395. concat(dc28-dc27,',',cost/dc28,',',(dc28-dc27)/reg_order_user) d28,
  396. concat(dc29-dc28,',',cost/dc29,',',(dc29-dc28)/reg_order_user) d29,
  397. concat(dc30-dc29,',',cost/dc30,',',(dc30-dc29)/reg_order_user) d30
  398. from (select * from dw_channel where cost>0 {op} {op1} {op2} {op3} {op4}) a
  399. left join dw_channel_user_daily b using(dt,channel) {op5}
  400. """
  401. data, total = getLimitData(db.dm, sql, page, page_size)
  402. def parse(str):
  403. li = str.split(',')
  404. li[0] = round(float(li[0]), 0)
  405. li[1] = round(float(li[1]), 2)
  406. li[2] = round(float(li[2]), 4)
  407. return dict(zip(['act_user', 'act_cost_per_cost', 'act_rate'], li))
  408. for i in data:
  409. for x in i:
  410. if x in ['d1', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8', 'd9', 'd10',
  411. 'd11', 'd12', 'd13', 'd14', 'd15', 'd16', 'd17', 'd18', 'd19', 'd20', 'd21', 'd22', 'd23', 'd24',
  412. 'd25', 'd26', 'd27', 'd28', 'd29', 'd30']:
  413. i[x] = parse(i[x]) if i[x] is not None else {}
  414. return data, total
  415. def get_channel_order_trend(user_id, channel, pitcher, start, end, page, page_size, order_by, order):
  416. db = MysqlUtils()
  417. if user_id in super_auth():
  418. op = ''
  419. else:
  420. if len(UserAuthUtils.get_auth_channel(user_id)) == 0:
  421. return None, None, None
  422. elif len(UserAuthUtils.get_auth_channel(user_id)) == 1:
  423. op = f" and channel = '{UserAuthUtils.get_auth_channel(user_id)[0]}'"
  424. else:
  425. op = f" and channel in {str(UserAuthUtils.get_auth_channel(user_id))}"
  426. op1 = f" and pitcher ='{pitcher}' " if pitcher else ''
  427. if not channel:
  428. op2 = ''
  429. elif type(channel) is str:
  430. op2 = f" and channel='{channel}'" if channel else ''
  431. elif len(channel) > 1:
  432. op2 = f" and channel in {str(tuple(channel))}"
  433. else:
  434. op2 = f" and channel='{channel[0]}'" if channel else ''
  435. op3 = f" and dt>='{start}' " if start else ''
  436. op4 = f" and dt<='{end}' " if end else ''
  437. op5 = f" order by {order_by} {order}" if order_by and order else ''
  438. base_sql_multi_channel = f'''
  439. ( select a.dt,a.create_user_num,a.reg_num,group_concat(a.channel) channel ,group_concat(a.pitcher ) pitcher ,
  440. group_concat(a.stage ) stage ,group_concat(a.platform) platform ,
  441. group_concat(a.book) book, group_concat(a.type) type,sum(a.order_count ) order_count ,
  442. sum(a.order_user ) order_user ,sum(a.order_amount ) order_amount ,
  443. sum(a.reg_order_amount) reg_order_amount,
  444. sum(a.first_order_amount ) first_order_amount ,sum(a.view_count) view_count,
  445. sum(a.click_count) click_count ,sum(a.follow_user) follow_user ,
  446. sum(game_user_sum) game_user_sum,
  447. round(if(sum(reg_num)=0,0,sum(reg_num*third_stay_rate)/sum(reg_num)),2) third_stay_rate,
  448. round(if(sum(reg_num)=0,0,sum(reg_num*second_stay_rate)/sum(reg_num)),2) second_stay_rate,
  449. round(if(sum(game_user_sum)=0,0,sum(game_user_sum*today_active_user_rate)/sum(game_user_sum)),2) today_active_user_rate,
  450. sum(a.cost) cost,sum(a.reg_order_count) reg_order_count,
  451. sum(a.reg_order_user) reg_order_user ,sum(a.web_view_count) web_view_count ,
  452. sum(a.platform_view_count) platform_view_count ,sum(a.web_order_count ) web_order_count ,
  453. sum(a.reg_order_user_again) reg_order_user_again ,sum(a.reg_order_user_again3 ) reg_order_user_again3 ,
  454. sum(a.reg_order_user_again4) reg_order_user_again4,sum(a.reg_order_user_again5) reg_order_user_again5,
  455. sum(a.reg_order_user_again6) reg_order_user_again6,sum(b.da1) as da1,
  456. sum(b.da2) as da2,
  457. sum(b.da3) as da3,
  458. sum(b.da4) as da4,
  459. sum(b.da5) as da5,
  460. sum(b.da6) as da6,
  461. sum(b.da7) as da7,
  462. sum(b.da8) as da8,
  463. sum(b.da9) as da9,
  464. sum(b.da10) as da10,
  465. sum(b.da11) as da11,
  466. sum(b.da12) as da12,
  467. sum(b.da13) as da13,
  468. sum(b.da14) as da14,
  469. sum(b.da15) as da15,
  470. sum(b.da16) as da16,
  471. sum(b.da17) as da17,
  472. sum(b.da18) as da18,
  473. sum(b.da19) as da19,
  474. sum(b.da20) as da20,
  475. sum(b.da21) as da21,
  476. sum(b.da22) as da22,
  477. sum(b.da23) as da23,
  478. sum(b.da24) as da24,
  479. sum(b.da25) as da25,
  480. sum(b.da26) as da26,
  481. sum(b.da27) as da27,
  482. sum(b.da28) as da28,
  483. sum(b.da29) as da29,
  484. sum(b.da30) as da30,
  485. sum(b.da31) as da31,
  486. sum(b.da32) as da32,
  487. sum(b.da33) as da33,
  488. sum(b.da34) as da34,
  489. sum(b.da35) as da35,
  490. sum(b.da36) as da36,
  491. sum(b.da37) as da37,
  492. sum(b.da38) as da38,
  493. sum(b.da39) as da39,
  494. sum(b.da40) as da40,
  495. sum(b.da41) as da41,
  496. sum(b.da42) as da42,
  497. sum(b.da43) as da43,
  498. sum(b.da44) as da44,
  499. sum(b.da45) as da45,
  500. sum(b.da46) as da46,
  501. sum(b.da47) as da47,
  502. sum(b.da48) as da48,
  503. sum(b.da49) as da49,
  504. sum(b.da50) as da50,
  505. sum(b.da51) as da51,
  506. sum(b.da52) as da52,
  507. sum(b.da53) as da53,
  508. sum(b.da54) as da54,
  509. sum(b.da55) as da55,
  510. sum(b.da56) as da56,
  511. sum(b.da57) as da57,
  512. sum(b.da58) as da58,
  513. sum(b.da59) as da59,
  514. sum(b.da60) as da60,
  515. sum(b.dm3) as dm3,
  516. sum(b.dm4) as dm4,
  517. sum(b.dm5) as dm5,
  518. sum(c.node) as node,
  519. group_concat(annual_mult) as annual_mult
  520. from
  521. ( select * from dw_channel where cost+reg_order_amount>0
  522. {op} {op1} {op2} {op3} {op4} ) a
  523. left join dw_channel_amount_daily b on a.channel=b.channel
  524. and a.dt=b.dt
  525. left join src_book_info c on a.dt=c.dt and a.book=c.book
  526. and a.type=c.type and a.platform=c.platform
  527. group by a.dt ) a
  528. '''
  529. base_sql_singel_channel = f'''
  530. ( select * from dw_channel where cost+reg_order_amount>0 {op} {op1} {op2} {op3} {op4} ) a
  531. left join dw_channel_amount_daily b on a.channel=b.channel and a.dt=b.dt
  532. 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
  533. '''
  534. base_sql = base_sql_multi_channel if channel and len(channel) > 1 else base_sql_singel_channel
  535. sql = f"""select row_number() over() as id,
  536. stage,pitcher,a.channel,a.dt date,a.book,cost,
  537. a.type,
  538. a.reg_num,
  539. round(if(a.reg_num=0,0,cost/a.reg_num),2) reg_per_cost,
  540. a.create_user_num,
  541. round(if( a.create_user_num=0,0,cost/ a.create_user_num),2) create_user_per_cost,
  542. node/100 require_roi,round(100/node,2) require_mult,
  543. round(first_order_amount*100/node-cost,2) expect_profit,
  544. annual_mult,
  545. round(first_order_amount*annual_mult -cost,2) annual_expect_profit,
  546. first_order_amount first_amount,
  547. reg_order_amount reg_amount,
  548. reg_order_amount-cost profit,
  549. follow_user new_follow_user,
  550. game_user_sum,
  551. third_stay_rate,
  552. second_stay_rate,
  553. today_active_user_rate,
  554. round(reg_order_amount/cost,4) roi,
  555. round(first_order_amount/cost,4) first_roi,
  556. round(cost/follow_user,2) new_follow_per_cost,
  557. reg_order_user order_user,
  558. reg_order_count order_count,
  559. round(cost/reg_order_user ,2) order_user_per_cost,
  560. concat(da1,',',da1/cost,',', 0,',',1) d1,
  561. concat(da2-da1,',',da2/cost,',', (da2-da1)/cost,',',if(da1=0,1,da2/da1) ) d2,
  562. concat(da3-da2,',',da3/cost,',', (da3-da2)/cost,',',if(da1=0,1,da3/da1)) d3,
  563. concat(da4-da3,',',da4/cost,',', (da4-da3)/cost,',',if(da1=0,1,da4/da1)) d4,
  564. concat(da5-da4,',',da5/cost,',', (da5-da4)/cost,',',if(da1=0,1,da5/da1)) d5,
  565. concat(da6-da5,',',da6/cost,',', (da6-da5)/cost,',',if(da1=0,1,da6/da1)) d6,
  566. concat(da7-da6,',',da7/cost,',', (da7-da6)/cost,',',if(da1=0,1,da7/da1)) d7,
  567. concat(da8-da7,',',da8/cost,',', (da8-da7)/cost,',',if(da1=0,1,da8/da1)) d8,
  568. concat(da9-da8,',',da9/cost,',', (da9-da8)/cost,',',if(da1=0,1,da9/da1)) d9,
  569. concat(da10-da9 ,',' ,da10/cost ,',', (da10-da9)/cost ,',' ,if(da1=0,1,da10/da1)) d10,
  570. concat(da11-da10 ,',' ,da11/cost ,',', (da11-da10)/cost ,',' ,if(da1=0,1,da11/da1)) d11,
  571. concat(da12-da11 ,',' ,da12/cost ,',', (da12-da11)/cost ,',' ,if(da1=0,1,da12/da1)) d12,
  572. concat(da13-da12 ,',' ,da13/cost ,',', (da13-da12)/cost ,',' ,if(da1=0,1,da13/da1)) d13,
  573. concat(da14-da13 ,',' ,da14/cost ,',', (da14-da13)/cost ,',' ,if(da1=0,1,da14/da1)) d14,
  574. concat(da15-da14 ,',' ,da15/cost ,',', (da15-da14)/cost ,',' ,if(da1=0,1,da15/da1)) d15,
  575. concat(da16-da15 ,',' ,da16/cost ,',', (da16-da15)/cost ,',' ,if(da1=0,1,da16/da1)) d16,
  576. concat(da17-da16 ,',' ,da17/cost ,',', (da17-da16)/cost ,',' ,if(da1=0,1,da17/da1)) d17,
  577. concat(da18-da17 ,',' ,da18/cost ,',', (da18-da17)/cost ,',' ,if(da1=0,1,da18/da1)) d18,
  578. concat(da19-da18 ,',' ,da19/cost ,',', (da19-da18)/cost ,',' ,if(da1=0,1,da19/da1)) d19,
  579. concat(da20-da19 ,',' ,da20/cost ,',', (da20-da19)/cost ,',' ,if(da1=0,1,da20/da1)) d20,
  580. concat(da21-da20 ,',' ,da21/cost ,',', (da21-da20)/cost ,',' ,if(da1=0,1,da21/da1)) d21,
  581. concat(da22-da21 ,',' ,da22/cost ,',', (da22-da21)/cost ,',' ,if(da1=0,1,da22/da1)) d22,
  582. concat(da23-da22 ,',' ,da23/cost ,',', (da23-da22)/cost ,',' ,if(da1=0,1,da23/da1)) d23,
  583. concat(da24-da23 ,',' ,da24/cost ,',', (da24-da23)/cost ,',' ,if(da1=0,1,da24/da1)) d24,
  584. concat(da25-da24 ,',' ,da25/cost ,',', (da25-da24)/cost ,',' ,if(da1=0,1,da25/da1)) d25,
  585. concat(da26-da25 ,',' ,da26/cost ,',', (da26-da25)/cost ,',' ,if(da1=0,1,da26/da1)) d26,
  586. concat(da27-da26 ,',' ,da27/cost ,',', (da27-da26)/cost ,',' ,if(da1=0,1,da27/da1)) d27,
  587. concat(da28-da27 ,',' ,da28/cost ,',', (da28-da27)/cost ,',' ,if(da1=0,1,da28/da1)) d28,
  588. concat(da29-da28 ,',' ,da29/cost ,',', (da29-da28)/cost ,',' ,if(da1=0,1,da29/da1)) d29,
  589. concat(da30-da29 ,',' ,da30/cost ,',', (da30-da29)/cost ,',' ,if(da1=0,1,da30/da1)) d30,
  590. concat(da31-da30 ,',' ,da31/cost ,',', (da31-da30)/cost ,',' ,if(da1=0,1,da31/da1)) d31,
  591. concat(da32-da31 ,',' ,da32/cost ,',', (da32-da31)/cost ,',' ,if(da1=0,1,da32/da1)) d32,
  592. concat(da33-da32 ,',' ,da33/cost ,',', (da33-da32)/cost ,',' ,if(da1=0,1,da33/da1)) d33,
  593. concat(da34-da33 ,',' ,da34/cost ,',', (da34-da33)/cost ,',' ,if(da1=0,1,da34/da1)) d34,
  594. concat(da35-da34 ,',' ,da35/cost ,',', (da35-da34)/cost ,',' ,if(da1=0,1,da35/da1)) d35,
  595. concat(da36-da35 ,',' ,da36/cost ,',', (da36-da35)/cost ,',' ,if(da1=0,1,da36/da1)) d36,
  596. concat(da37-da36 ,',' ,da37/cost ,',', (da37-da36)/cost ,',' ,if(da1=0,1,da37/da1)) d37,
  597. concat(da38-da37 ,',' ,da38/cost ,',', (da38-da37)/cost ,',' ,if(da1=0,1,da38/da1)) d38,
  598. concat(da39-da38 ,',' ,da39/cost ,',', (da39-da38)/cost ,',' ,if(da1=0,1,da39/da1)) d39,
  599. concat(da40-da39 ,',' ,da40/cost ,',', (da40-da39)/cost ,',' ,if(da1=0,1,da40/da1)) d40,
  600. concat(da41-da40 ,',' ,da41/cost ,',', (da41-da40)/cost ,',' ,if(da1=0,1,da41/da1)) d41,
  601. concat(da42-da41 ,',' ,da42/cost ,',', (da42-da41)/cost ,',' ,if(da1=0,1,da42/da1)) d42,
  602. concat(da43-da42 ,',' ,da43/cost ,',', (da43-da42)/cost ,',' ,if(da1=0,1,da43/da1)) d43,
  603. concat(da44-da43 ,',' ,da44/cost ,',', (da44-da43)/cost ,',' ,if(da1=0,1,da44/da1)) d44,
  604. concat(da45-da44 ,',' ,da45/cost ,',', (da45-da44)/cost ,',' ,if(da1=0,1,da45/da1)) d45,
  605. concat(da46-da45 ,',' ,da46/cost ,',', (da46-da45)/cost ,',' ,if(da1=0,1,da46/da1)) d46,
  606. concat(da47-da46 ,',' ,da47/cost ,',', (da47-da46)/cost ,',' ,if(da1=0,1,da47/da1)) d47,
  607. concat(da48-da47 ,',' ,da48/cost ,',', (da48-da47)/cost ,',' ,if(da1=0,1,da48/da1)) d48,
  608. concat(da49-da48 ,',' ,da49/cost ,',', (da49-da48)/cost ,',' ,if(da1=0,1,da49/da1)) d49,
  609. concat(da50-da49 ,',' ,da50/cost ,',', (da50-da49)/cost ,',' ,if(da1=0,1,da50/da1)) d50,
  610. concat(da51-da50 ,',' ,da51/cost ,',', (da51-da50)/cost ,',' ,if(da1=0,1,da51/da1)) d51,
  611. concat(da52-da51 ,',' ,da52/cost ,',', (da52-da51)/cost ,',' ,if(da1=0,1,da52/da1)) d52,
  612. concat(da53-da52 ,',' ,da53/cost ,',', (da53-da52)/cost ,',' ,if(da1=0,1,da53/da1)) d53,
  613. concat(da54-da53 ,',' ,da54/cost ,',', (da54-da53)/cost ,',' ,if(da1=0,1,da54/da1)) d54,
  614. concat(da55-da54 ,',' ,da55/cost ,',', (da55-da54)/cost ,',' ,if(da1=0,1,da55/da1)) d55,
  615. concat(da56-da55 ,',' ,da56/cost ,',', (da56-da55)/cost ,',' ,if(da1=0,1,da56/da1)) d56,
  616. concat(da57-da56 ,',' ,da57/cost ,',', (da57-da56)/cost ,',' ,if(da1=0,1,da57/da1)) d57,
  617. concat(da58-da57 ,',' ,da58/cost ,',', (da58-da57)/cost ,',' ,if(da1=0,1,da58/da1)) d58,
  618. concat(da59-da58 ,',' ,da59/cost ,',', (da59-da58)/cost ,',' ,if(da1=0,1,da59/da1)) d59,
  619. concat(da60-da59 ,',' ,da60/cost ,',', (da60-da59)/cost ,',' ,if(da1=0,1,da60/da1)) d60,
  620. concat(dm3-da60 ,',' ,dm3/cost ,',', (dm3-da60)/cost ,',' ,if(da1=0,1,dm3/da1)) m3,
  621. concat(dm4-dm3 ,',' ,dm4/cost ,',', (dm4-dm3)/cost ,',' ,if(da1=0,1,dm4/da1)) m4,
  622. concat(dm5-dm4 ,',' ,dm5/cost ,',', (dm5-dm4)/cost ,',' ,if(da1=0,1,dm5/da1)) m5,
  623. da1,da2,da3,da4,da5,da6,da7,da8,da9,da10,da11,da12,da13,da14,da15,da16,da17,da18,da19,da20,
  624. da21,da22,da23,da24,da25,da26,da27,da28,da29,da30,da31,da32,da33,da34,da35,da36,da37,da38,da39,
  625. da40,da41,da42,da43,da44,da45,da46,da47,da48,da49,da50,da51,da52,da53,da54,da55,da56,da57,da58,
  626. da59,da60,dm3,dm4,dm5
  627. from {base_sql}
  628. {op5}
  629. """
  630. sumsql = f"""select concat(date_format(min(date),'%Y/%m/%d'),'~',date_format(max(date),'%Y/%m/%d')) date,
  631. sum(create_user_num) create_user_num,
  632. round(if( sum(create_user_num)=0,0,sum(cost)/ sum(create_user_num)),2) create_user_per_cost,
  633. sum(reg_num) reg_num,
  634. round(if(sum(reg_num)=0,0,sum(cost)/sum(reg_num)),2) reg_per_cost,
  635. sum(cost) cost,
  636. sum(reg_amount) reg_amount,
  637. sum(first_amount) first_amount,
  638. sum(profit) profit,
  639. sum(new_follow_user) new_follow_user,
  640. sum(game_user_sum) game_user_sum,
  641. round(if(sum(reg_num)=0,0,sum(reg_num*third_stay_rate)/sum(reg_num)),2) third_stay_rate,
  642. round(if(sum(reg_num)=0,0,sum(reg_num*second_stay_rate)/sum(reg_num)),2) second_stay_rate,
  643. round(if(sum(game_user_sum)=0,0,sum(game_user_sum*today_active_user_rate)/sum(game_user_sum)),2) today_active_user_rate,
  644. round(sum(reg_amount)/sum(cost),4) roi,
  645. round(sum(first_amount)/sum(cost),4) first_roi,
  646. round(sum(cost)/sum(new_follow_user),2) new_follow_per_cost,
  647. sum(order_user) order_user,
  648. sum(order_count) order_count,
  649. round(sum(cost)/sum(order_user),2) order_user_per_cost,
  650. round(avg(require_roi),4) require_roi,
  651. round(avg(require_mult),2) require_mult,
  652. round(sum(expect_profit),2) expect_profit,
  653. round(sum(annual_expect_profit),2) annual_expect_profit,
  654. concat(sum(da1),',',sum(da1)/sum(cost),',', 0,',',1) d1,
  655. concat(sum(da2)-sum(if (da2,da1,0)),',',sum(da2)/sum(if (da2,cost,0)),',', (sum(da2)-sum(if (da2,da1,0)))/sum(if (da2,cost,0)),',',if(sum(if (da2,da1,0))=0,1,sum(da2)/sum(if (da2,da1,0))) ) d2,
  656. concat(sum(da3)-sum(if (da3,da2,0)),',',sum(da3)/sum(if (da3,cost,0)),',', (sum(da3)-sum(if (da3,da2,0)))/sum(if (da3,cost,0)),',',if(sum(if (da3,da1,0))=0,1,sum(da3)/sum(if (da3,da1,0))) ) d3,
  657. concat(sum(da4)-sum(if (da4,da3,0)),',',sum(da4)/sum(if (da4,cost,0)),',', (sum(da4)-sum(if (da4,da3,0)))/sum(if (da4,cost,0)),',',if(sum(if (da4,da1,0))=0,1,sum(da4)/sum(if (da4,da1,0))) ) d4,
  658. concat(sum(da5)-sum(if (da5,da4,0)),',',sum(da5)/sum(if (da5,cost,0)),',', (sum(da5)-sum(if (da5,da4,0)))/sum(if (da5,cost,0)),',',if(sum(if (da5,da1,0))=0,1,sum(da5)/sum(if (da5,da1,0))) ) d5,
  659. concat(sum(da6)-sum(if (da6,da5,0)),',',sum(da6)/sum(if (da6,cost,0)),',', (sum(da6)-sum(if (da6,da5,0)))/sum(if (da6,cost,0)),',',if(sum(if (da6,da1,0))=0,1,sum(da6)/sum(if (da6,da1,0))) ) d6,
  660. concat(sum(da7)-sum(if (da7,da6,0)),',',sum(da7)/sum(if (da7,cost,0)),',', (sum(da7)-sum(if (da7,da6,0)))/sum(if (da7,cost,0)),',',if(sum(if (da7,da1,0))=0,1,sum(da7)/sum(if (da7,da1,0))) ) d7,
  661. concat(sum(da8)-sum(if (da8,da7,0)),',',sum(da8)/sum(if (da8,cost,0)),',', (sum(da8)-sum(if (da8,da7,0)))/sum(if (da8,cost,0)),',',if(sum(if (da8,da1,0))=0,1,sum(da8)/sum(if (da8,da1,0))) ) d8,
  662. concat(sum(da9)-sum(if (da9,da8,0)),',',sum(da9)/sum(if (da9,cost,0)),',', (sum(da9)-sum(if (da9,da8,0)))/sum(if (da9,cost,0)),',',if(sum(if (da9,da1,0))=0,1,sum(da9)/sum(if (da9,da1,0))) ) d9,
  663. concat(sum(da10)-sum(if (da10,da9,0)),',',sum(da10)/sum(if (da10,cost,0)),',', (sum(da10)-sum(if (da10,da9,0)))/sum(if (da10,cost,0)),',',if(sum(if (da10,da1,0))=0,1,sum(da10)/sum(if (da10,da1,0))) ) d10,
  664. concat(sum(da11)-sum(if (da11,da10,0)),',',sum(da11)/sum(if (da11,cost,0)),',', (sum(da11)-sum(if (da11,da10,0)))/sum(if (da11,cost,0)),',',if(sum(if (da11,da1,0))=0,1,sum(da11)/sum(if (da11,da1,0))) ) d11,
  665. concat(sum(da12)-sum(if (da12,da11,0)),',',sum(da12)/sum(if (da12,cost,0)),',', (sum(da12)-sum(if (da12,da11,0)))/sum(if (da12,cost,0)),',',if(sum(if (da12,da1,0))=0,1,sum(da12)/sum(if (da12,da1,0))) ) d12,
  666. concat(sum(da13)-sum(if (da13,da12,0)),',',sum(da13)/sum(if (da13,cost,0)),',', (sum(da13)-sum(if (da13,da12,0)))/sum(if (da13,cost,0)),',',if(sum(if (da13,da1,0))=0,1,sum(da13)/sum(if (da13,da1,0))) ) d13,
  667. concat(sum(da14)-sum(if (da14,da13,0)),',',sum(da14)/sum(if (da14,cost,0)),',', (sum(da14)-sum(if (da14,da13,0)))/sum(if (da14,cost,0)),',',if(sum(if (da14,da1,0))=0,1,sum(da14)/sum(if (da14,da1,0))) ) d14,
  668. concat(sum(da15)-sum(if (da15,da14,0)),',',sum(da15)/sum(if (da15,cost,0)),',', (sum(da15)-sum(if (da15,da14,0)))/sum(if (da15,cost,0)),',',if(sum(if (da15,da1,0))=0,1,sum(da15)/sum(if (da15,da1,0))) ) d15,
  669. concat(sum(da16)-sum(if (da16,da15,0)),',',sum(da16)/sum(if (da16,cost,0)),',', (sum(da16)-sum(if (da16,da15,0)))/sum(if (da16,cost,0)),',',if(sum(if (da16,da1,0))=0,1,sum(da16)/sum(if (da16,da1,0))) ) d16,
  670. concat(sum(da17)-sum(if (da17,da16,0)),',',sum(da17)/sum(if (da17,cost,0)),',', (sum(da17)-sum(if (da17,da16,0)))/sum(if (da17,cost,0)),',',if(sum(if (da17,da1,0))=0,1,sum(da17)/sum(if (da17,da1,0))) ) d17,
  671. concat(sum(da18)-sum(if (da18,da17,0)),',',sum(da18)/sum(if (da18,cost,0)),',', (sum(da18)-sum(if (da18,da17,0)))/sum(if (da18,cost,0)),',',if(sum(if (da18,da1,0))=0,1,sum(da18)/sum(if (da18,da1,0))) ) d18,
  672. concat(sum(da19)-sum(if (da19,da18,0)),',',sum(da19)/sum(if (da19,cost,0)),',', (sum(da19)-sum(if (da19,da18,0)))/sum(if (da19,cost,0)),',',if(sum(if (da19,da1,0))=0,1,sum(da19)/sum(if (da19,da1,0))) ) d19,
  673. concat(sum(da20)-sum(if (da20,da19,0)),',',sum(da20)/sum(if (da20,cost,0)),',', (sum(da20)-sum(if (da20,da19,0)))/sum(if (da20,cost,0)),',',if(sum(if (da20,da1,0))=0,1,sum(da20)/sum(if (da20,da1,0))) ) d20,
  674. concat(sum(da21)-sum(if (da21,da20,0)),',',sum(da21)/sum(if (da21,cost,0)),',', (sum(da21)-sum(if (da21,da20,0)))/sum(if (da21,cost,0)),',',if(sum(if (da21,da1,0))=0,1,sum(da21)/sum(if (da21,da1,0))) ) d21,
  675. concat(sum(da22)-sum(if (da22,da21,0)),',',sum(da22)/sum(if (da22,cost,0)),',', (sum(da22)-sum(if (da22,da21,0)))/sum(if (da22,cost,0)),',',if(sum(if (da22,da1,0))=0,1,sum(da22)/sum(if (da22,da1,0))) ) d22,
  676. concat(sum(da23)-sum(if (da23,da22,0)),',',sum(da23)/sum(if (da23,cost,0)),',', (sum(da23)-sum(if (da23,da22,0)))/sum(if (da23,cost,0)),',',if(sum(if (da23,da1,0))=0,1,sum(da23)/sum(if (da23,da1,0))) ) d23,
  677. concat(sum(da24)-sum(if (da24,da23,0)),',',sum(da24)/sum(if (da24,cost,0)),',', (sum(da24)-sum(if (da24,da23,0)))/sum(if (da24,cost,0)),',',if(sum(if (da24,da1,0))=0,1,sum(da24)/sum(if (da24,da1,0))) ) d24,
  678. concat(sum(da25)-sum(if (da25,da24,0)),',',sum(da25)/sum(if (da25,cost,0)),',', (sum(da25)-sum(if (da25,da24,0)))/sum(if (da25,cost,0)),',',if(sum(if (da25,da1,0))=0,1,sum(da25)/sum(if (da25,da1,0))) ) d25,
  679. concat(sum(da26)-sum(if (da26,da25,0)),',',sum(da26)/sum(if (da26,cost,0)),',', (sum(da26)-sum(if (da26,da25,0)))/sum(if (da26,cost,0)),',',if(sum(if (da26,da1,0))=0,1,sum(da26)/sum(if (da26,da1,0))) ) d26,
  680. concat(sum(da27)-sum(if (da27,da26,0)),',',sum(da27)/sum(if (da27,cost,0)),',', (sum(da27)-sum(if (da27,da26,0)))/sum(if (da27,cost,0)),',',if(sum(if (da27,da1,0))=0,1,sum(da27)/sum(if (da27,da1,0))) ) d27,
  681. concat(sum(da28)-sum(if (da28,da27,0)),',',sum(da28)/sum(if (da28,cost,0)),',', (sum(da28)-sum(if (da28,da27,0)))/sum(if (da28,cost,0)),',',if(sum(if (da28,da1,0))=0,1,sum(da28)/sum(if (da28,da1,0))) ) d28,
  682. concat(sum(da29)-sum(if (da29,da28,0)),',',sum(da29)/sum(if (da29,cost,0)),',', (sum(da29)-sum(if (da29,da28,0)))/sum(if (da29,cost,0)),',',if(sum(if (da29,da1,0))=0,1,sum(da29)/sum(if (da29,da1,0))) ) d29,
  683. concat(sum(da30)-sum(if (da30,da29,0)),',',sum(da30)/sum(if (da30,cost,0)),',', (sum(da30)-sum(if (da30,da29,0)))/sum(if (da30,cost,0)),',',if(sum(if (da30,da1,0))=0,1,sum(da30)/sum(if (da30,da1,0))) ) d30,
  684. concat(sum(da31)-sum(if (da31,da30,0)),',',sum(da31)/sum(if (da31,cost,0)),',', (sum(da31)-sum(if (da31,da30,0)))/sum(if (da31,cost,0)),',',if(sum(if (da31,da1,0))=0,1,sum(da31)/sum(if (da31,da1,0))) ) d31,
  685. concat(sum(da32)-sum(if (da32,da31,0)),',',sum(da32)/sum(if (da32,cost,0)),',', (sum(da32)-sum(if (da32,da31,0)))/sum(if (da32,cost,0)),',',if(sum(if (da32,da1,0))=0,1,sum(da32)/sum(if (da32,da1,0))) ) d32,
  686. concat(sum(da33)-sum(if (da33,da32,0)),',',sum(da33)/sum(if (da33,cost,0)),',', (sum(da33)-sum(if (da33,da32,0)))/sum(if (da33,cost,0)),',',if(sum(if (da33,da1,0))=0,1,sum(da33)/sum(if (da33,da1,0))) ) d33,
  687. concat(sum(da34)-sum(if (da34,da33,0)),',',sum(da34)/sum(if (da34,cost,0)),',', (sum(da34)-sum(if (da34,da33,0)))/sum(if (da34,cost,0)),',',if(sum(if (da34,da1,0))=0,1,sum(da34)/sum(if (da34,da1,0))) ) d34,
  688. concat(sum(da35)-sum(if (da35,da34,0)),',',sum(da35)/sum(if (da35,cost,0)),',', (sum(da35)-sum(if (da35,da34,0)))/sum(if (da35,cost,0)),',',if(sum(if (da35,da1,0))=0,1,sum(da35)/sum(if (da35,da1,0))) ) d35,
  689. concat(sum(da36)-sum(if (da36,da35,0)),',',sum(da36)/sum(if (da36,cost,0)),',', (sum(da36)-sum(if (da36,da35,0)))/sum(if (da36,cost,0)),',',if(sum(if (da36,da1,0))=0,1,sum(da36)/sum(if (da36,da1,0))) ) d36,
  690. concat(sum(da37)-sum(if (da37,da36,0)),',',sum(da37)/sum(if (da37,cost,0)),',', (sum(da37)-sum(if (da37,da36,0)))/sum(if (da37,cost,0)),',',if(sum(if (da37,da1,0))=0,1,sum(da37)/sum(if (da37,da1,0))) ) d37,
  691. concat(sum(da38)-sum(if (da38,da37,0)),',',sum(da38)/sum(if (da38,cost,0)),',', (sum(da38)-sum(if (da38,da37,0)))/sum(if (da38,cost,0)),',',if(sum(if (da38,da1,0))=0,1,sum(da38)/sum(if (da38,da1,0))) ) d38,
  692. concat(sum(da39)-sum(if (da39,da38,0)),',',sum(da39)/sum(if (da39,cost,0)),',', (sum(da39)-sum(if (da39,da38,0)))/sum(if (da39,cost,0)),',',if(sum(if (da39,da1,0))=0,1,sum(da39)/sum(if (da39,da1,0))) ) d39,
  693. concat(sum(da40)-sum(if (da40,da39,0)),',',sum(da40)/sum(if (da40,cost,0)),',', (sum(da40)-sum(if (da40,da39,0)))/sum(if (da40,cost,0)),',',if(sum(if (da40,da1,0))=0,1,sum(da40)/sum(if (da40,da1,0))) ) d40,
  694. concat(sum(da41)-sum(if (da41,da40,0)),',',sum(da41)/sum(if (da41,cost,0)),',', (sum(da41)-sum(if (da41,da40,0)))/sum(if (da41,cost,0)),',',if(sum(if (da41,da1,0))=0,1,sum(da41)/sum(if (da41,da1,0))) ) d41,
  695. concat(sum(da42)-sum(if (da42,da41,0)),',',sum(da42)/sum(if (da42,cost,0)),',', (sum(da42)-sum(if (da42,da41,0)))/sum(if (da42,cost,0)),',',if(sum(if (da42,da1,0))=0,1,sum(da42)/sum(if (da42,da1,0))) ) d42,
  696. concat(sum(da43)-sum(if (da43,da42,0)),',',sum(da43)/sum(if (da43,cost,0)),',', (sum(da43)-sum(if (da43,da42,0)))/sum(if (da43,cost,0)),',',if(sum(if (da43,da1,0))=0,1,sum(da43)/sum(if (da43,da1,0))) ) d43,
  697. concat(sum(da44)-sum(if (da44,da43,0)),',',sum(da44)/sum(if (da44,cost,0)),',', (sum(da44)-sum(if (da44,da43,0)))/sum(if (da44,cost,0)),',',if(sum(if (da44,da1,0))=0,1,sum(da44)/sum(if (da44,da1,0))) ) d44,
  698. concat(sum(da45)-sum(if (da45,da44,0)),',',sum(da45)/sum(if (da45,cost,0)),',', (sum(da45)-sum(if (da45,da44,0)))/sum(if (da45,cost,0)),',',if(sum(if (da45,da1,0))=0,1,sum(da45)/sum(if (da45,da1,0))) ) d45,
  699. concat(sum(da46)-sum(if (da46,da45,0)),',',sum(da46)/sum(if (da46,cost,0)),',', (sum(da46)-sum(if (da46,da45,0)))/sum(if (da46,cost,0)),',',if(sum(if (da46,da1,0))=0,1,sum(da46)/sum(if (da46,da1,0))) ) d46,
  700. concat(sum(da47)-sum(if (da47,da46,0)),',',sum(da47)/sum(if (da47,cost,0)),',', (sum(da47)-sum(if (da47,da46,0)))/sum(if (da47,cost,0)),',',if(sum(if (da47,da1,0))=0,1,sum(da47)/sum(if (da47,da1,0))) ) d47,
  701. concat(sum(da48)-sum(if (da48,da47,0)),',',sum(da48)/sum(if (da48,cost,0)),',', (sum(da48)-sum(if (da48,da47,0)))/sum(if (da48,cost,0)),',',if(sum(if (da48,da1,0))=0,1,sum(da48)/sum(if (da48,da1,0))) ) d48,
  702. concat(sum(da49)-sum(if (da49,da48,0)),',',sum(da49)/sum(if (da49,cost,0)),',', (sum(da49)-sum(if (da49,da48,0)))/sum(if (da49,cost,0)),',',if(sum(if (da49,da1,0))=0,1,sum(da49)/sum(if (da49,da1,0))) ) d49,
  703. concat(sum(da50)-sum(if (da50,da49,0)),',',sum(da50)/sum(if (da50,cost,0)),',', (sum(da50)-sum(if (da50,da49,0)))/sum(if (da50,cost,0)),',',if(sum(if (da50,da1,0))=0,1,sum(da50)/sum(if (da50,da1,0))) ) d50,
  704. concat(sum(da51)-sum(if (da51,da50,0)),',',sum(da51)/sum(if (da51,cost,0)),',', (sum(da51)-sum(if (da51,da50,0)))/sum(if (da51,cost,0)),',',if(sum(if (da51,da1,0))=0,1,sum(da51)/sum(if (da51,da1,0))) ) d51,
  705. concat(sum(da52)-sum(if (da52,da51,0)),',',sum(da52)/sum(if (da52,cost,0)),',', (sum(da52)-sum(if (da52,da51,0)))/sum(if (da52,cost,0)),',',if(sum(if (da52,da1,0))=0,1,sum(da52)/sum(if (da52,da1,0))) ) d52,
  706. concat(sum(da53)-sum(if (da53,da52,0)),',',sum(da53)/sum(if (da53,cost,0)),',', (sum(da53)-sum(if (da53,da52,0)))/sum(if (da53,cost,0)),',',if(sum(if (da53,da1,0))=0,1,sum(da53)/sum(if (da53,da1,0))) ) d53,
  707. concat(sum(da54)-sum(if (da54,da53,0)),',',sum(da54)/sum(if (da54,cost,0)),',', (sum(da54)-sum(if (da54,da53,0)))/sum(if (da54,cost,0)),',',if(sum(if (da54,da1,0))=0,1,sum(da54)/sum(if (da54,da1,0))) ) d54,
  708. concat(sum(da55)-sum(if (da55,da54,0)),',',sum(da55)/sum(if (da55,cost,0)),',', (sum(da55)-sum(if (da55,da54,0)))/sum(if (da55,cost,0)),',',if(sum(if (da55,da1,0))=0,1,sum(da55)/sum(if (da55,da1,0))) ) d55,
  709. concat(sum(da56)-sum(if (da56,da55,0)),',',sum(da56)/sum(if (da56,cost,0)),',', (sum(da56)-sum(if (da56,da55,0)))/sum(if (da56,cost,0)),',',if(sum(if (da56,da1,0))=0,1,sum(da56)/sum(if (da56,da1,0))) ) d56,
  710. concat(sum(da57)-sum(if (da57,da56,0)),',',sum(da57)/sum(if (da57,cost,0)),',', (sum(da57)-sum(if (da57,da56,0)))/sum(if (da57,cost,0)),',',if(sum(if (da57,da1,0))=0,1,sum(da57)/sum(if (da57,da1,0))) ) d57,
  711. concat(sum(da58)-sum(if (da58,da57,0)),',',sum(da58)/sum(if (da58,cost,0)),',', (sum(da58)-sum(if (da58,da57,0)))/sum(if (da58,cost,0)),',',if(sum(if (da58,da1,0))=0,1,sum(da58)/sum(if (da58,da1,0))) ) d58,
  712. concat(sum(da59)-sum(if (da59,da58,0)),',',sum(da59)/sum(if (da59,cost,0)),',', (sum(da59)-sum(if (da59,da58,0)))/sum(if (da59,cost,0)),',',if(sum(if (da59,da1,0))=0,1,sum(da59)/sum(if (da59,da1,0))) ) d59,
  713. concat(sum(da60)-sum(if (da60,da59,0)),',',sum(da60)/sum(if (da60,cost,0)),',', (sum(da60)-sum(if (da60,da59,0)))/sum(if (da60,cost,0)),',',if(sum(if (da60,da1,0))=0,1,sum(da60)/sum(if (da60,da1,0))) ) d60,
  714. concat(sum(dm3)-sum(if (dm3,da60,0)) ,',' ,sum(dm3)/sum(if(dm3,cost,0)) ,',', (sum(dm3)-sum(if (dm3,da60,0)))/sum(if(dm3,cost,0)) ,',' ,if(sum(if(dm3,da1,0))=0,1,sum(dm3)/sum(if(dm3,da1,0))) ) m3,
  715. concat(sum(dm4)-sum(if (dm4,dm3,0)),',',sum(dm4)/sum(if (dm4,cost,0)),',', (sum(dm4)-sum(if (dm4,dm3,0)))/sum(if (dm4,cost,0)),',',if(sum(if (dm4,da1,0))=0,1,sum(dm4)/sum(if (dm4,da1,0))) ) m4,
  716. concat(sum(dm5)-sum(if (dm5,dm4,0)),',',sum(dm5)/sum(if (dm5,cost,0)),',', (sum(dm5)-sum(if (dm5,dm4,0)))/sum(if (dm5,cost,0)),',',if(sum(if (dm5,da1,0))=0,1,sum(dm5)/sum(if (dm5,da1,0))) ) m5
  717. from ({sql}) a
  718. """
  719. print(sumsql)
  720. data, total, total_data = getLimitSumData(db.dm, sql, sumsql, page, page_size)
  721. def parse(key_str):
  722. if type(key_str) is not str:
  723. key_str = key_str.decode('utf-8')
  724. li = key_str.split(',')
  725. li[0] = round(float(li[0]), 2)
  726. li[1] = round(float(li[1]), 4)
  727. li[2] = round(float(li[2]), 4)
  728. li[3] = round(float(li[3]), 2)
  729. return dict(zip(['order', 'roi', 'add', 'mult'], li))
  730. for x in total_data.keys():
  731. if x in ['d1', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8', 'd9', 'd10', 'd11', 'd12', 'd13', 'd14', 'd15', 'd16',
  732. 'd17', 'd18',
  733. 'd19', 'd20', 'd21', 'd22', 'd23', 'd24', 'd25', 'd26', 'd27', 'd28', 'd29', 'd30', 'd31', 'd32',
  734. 'd33', 'd34', 'd35', 'd36', 'd37', 'd38',
  735. 'd39', 'd40', 'd41', 'd42', 'd43', 'd44', 'd45', 'd46', 'd47', 'd48', 'd49', 'd50', 'd51', 'd52',
  736. 'd53', 'd54', 'd55', 'd56', 'd57', 'd58', 'd59',
  737. 'd60', 'm3', 'm4', 'm5']:
  738. total_data[x] = parse(total_data[x]) if total_data[x] is not None else {}
  739. for i in data:
  740. for x in i:
  741. if x in ['d1', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8', 'd9', 'd10', 'd11', 'd12', 'd13', 'd14', 'd15',
  742. 'd16', 'd17', 'd18',
  743. 'd19', 'd20', 'd21', 'd22', 'd23', 'd24', 'd25', 'd26', 'd27', 'd28', 'd29', 'd30', 'd31', 'd32',
  744. 'd33', 'd34', 'd35', 'd36', 'd37', 'd38',
  745. 'd39', 'd40', 'd41', 'd42', 'd43', 'd44', 'd45', 'd46', 'd47', 'd48', 'd49', 'd50', 'd51', 'd52',
  746. 'd53', 'd54', 'd55', 'd56', 'd57', 'd58', 'd59',
  747. 'd60', 'm3', 'm4', 'm5']:
  748. i[x] = parse(i[x]) if i[x] is not None else {}
  749. # print(data)
  750. return data, total, total_data
  751. def get_channel_summary(user_id, channel, pitcher, page, page_size, order_by, order, state, location, start, end):
  752. db = MysqlUtils()
  753. if user_id in super_auth(): # 超级数据权限
  754. op = ''
  755. else:
  756. channel_li = UserAuthUtils.get_auth_channel(user_id)
  757. print(channel_li.__len__())
  758. if len(channel_li) == 0:
  759. return None, None, None
  760. elif len(channel_li) == 1:
  761. op = f" and a.channel = '{channel_li[0]}'"
  762. else:
  763. op = f" and a.channel in {tuple(channel_li)}"
  764. op1 = f" and a.channel='{channel}'" if channel else ''
  765. op2 = f" and pitcher='{pitcher}'" if pitcher else ''
  766. op5 = f" and state='{state}'" if state else ''
  767. op6 = f" and a.dt>='{start}'" if start else ''
  768. op7 = f" and a.dt<='{end}'" if end else ''
  769. sql = f"""SELECT channel,
  770. if(end>date_sub(now(),interval 10 day),'在投','停投') state,
  771. start,end,
  772. total_cost,total_amount,
  773. total_amount-total_cost profit,
  774. if(total_cost=0,0,round(total_amount/total_cost,4)) roi,
  775. follow_user,
  776. if(follow_user=0,0,round(total_cost/follow_user,2)) follow_per_cost,
  777. order_user,
  778. if(follow_user=0,0,round(order_user/follow_user,4)) order_tran_rate,
  779. if(order_user=0,0,round(total_cost/order_user,2)) order_tran_cost,
  780. td_amount,yd_amount,byd_amount
  781. FROM
  782. (select
  783. channel,
  784. min(if(cost>0,dt,null)) start,
  785. max(if(cost>0,dt,null)) end,
  786. sum(cost) total_cost,
  787. sum(reg_order_amount) total_amount,
  788. sum(follow_user) follow_user,
  789. sum(reg_order_user) order_user,
  790. sum(ba1) td_amount,sum(ba2) yd_amount,sum(ba3) byd_amount
  791. from dw_channel a
  792. left join dw_channel_amount_daily_reverse b using (dt,channel)
  793. left join quchen_text.advertiser_vx c on a.channel = c.name
  794. left join quchen_text.advertiser_vx d on b.channel = d.name
  795. where 1=1 and a.dt>=c.start_date and b.dt>=d.start_date
  796. {op} {op1} {op2} {op6} {op7} GROUP BY a.channel) x
  797. having 1=1 {op5} ORDER BY {order_by} {order}
  798. """
  799. print(sql)
  800. sumsql = f"""select '总计' channel,
  801. sum(total_cost) total_cost,
  802. sum(total_amount) total_amount,sum(profit) profit,
  803. round(sum(total_amount)/sum(total_cost),4) roi,
  804. sum(follow_user) follow_user,
  805. sum(order_user) order_user,
  806. round(sum(total_cost)/sum(follow_user),2) follow_per_cost,
  807. round(sum(order_user)/sum(follow_user),4) order_tran_rate,
  808. round(sum(total_cost)/sum(order_user),2) order_tran_cost ,
  809. sum(td_amount) td_amount,sum(yd_amount) yd_amount,sum(byd_amount) byd_amount
  810. from ({sql}) a
  811. """
  812. return getLimitSumData(db.dm, sql, sumsql, page, page_size)
  813. def get_pitcher_channel_summary(user_id, channel, pitcher, page, page_size, order_by, order, state, location, start,
  814. end):
  815. """投手投放号"""
  816. db = MysqlUtils()
  817. if user_id in super_auth(): # 超级数据权限
  818. op = ''
  819. else:
  820. user_name_li = UserAuthUtils.get_auth_user(user_id)
  821. if len(user_name_li) == 1:
  822. op = f" and pitcher='{user_name_li[0]}'"
  823. else:
  824. op = f" and pitcher in {tuple(user_name_li)}"
  825. op1 = f" and a.channel='{channel}'" if channel else ''
  826. op2 = f" and pitcher='{pitcher}'" if pitcher else ''
  827. op4 = f" and location='{location}' " if location else ''
  828. op5 = f" and state='{state}'" if state else ''
  829. op6 = f" and a.dt>='{start}'" if start else ''
  830. op7 = f" and a.dt<='{end}'" if end else ''
  831. sql = f"""SELECT channel,
  832. if(end>date_sub(now(),interval 10 day),'在投','停投') state,
  833. location,start,end,total_cost,total_amount,
  834. total_amount-total_cost profit,
  835. if(total_cost=0,0,round(total_amount/total_cost,4)) roi,
  836. follow_user,
  837. if(follow_user=0,0,round(total_cost/follow_user,2)) follow_per_cost,
  838. order_user,
  839. if(follow_user=0,0,round(order_user/follow_user,4)) order_tran_rate,
  840. if(order_user=0,0,round(total_cost/order_user,2)) order_tran_cost,
  841. pitcher,stage,td_amount,yd_amount,byd_amount
  842. FROM
  843. (select
  844. channel,pitcher,stage,
  845. type location,
  846. min(if(cost>0,dt,null)) start,
  847. max(if(cost>0,dt,null)) end,
  848. sum(cost) total_cost,
  849. sum(reg_order_amount) total_amount,
  850. sum(follow_user) follow_user,
  851. sum(reg_order_user) order_user,
  852. sum(ba1) td_amount,sum(ba2) yd_amount,sum(ba3) byd_amount
  853. from dw_channel a
  854. left join dw_channel_amount_daily_reverse b using (dt,channel)
  855. where 1=1 {op} {op1} {op2} {op6} {op7} GROUP BY a.channel,type,pitcher,stage) x
  856. having 1=1 {op4} {op5} ORDER BY {order_by} {order}
  857. """
  858. print(sql)
  859. sumsql = f"""select '总计' channel,
  860. sum(total_cost) total_cost,
  861. sum(total_amount) total_amount,sum(profit) profit,
  862. round(sum(total_amount)/sum(total_cost),4) roi,
  863. sum(follow_user) follow_user,
  864. sum(order_user) order_user,
  865. round(sum(total_cost)/sum(follow_user),2) follow_per_cost,
  866. round(sum(order_user)/sum(follow_user),4) order_tran_rate,
  867. round(sum(total_cost)/sum(order_user),2) order_tran_cost ,
  868. sum(td_amount) td_amount,sum(yd_amount) yd_amount,sum(byd_amount) byd_amount
  869. from ({sql}) a
  870. """
  871. return getLimitSumData(db.dm, sql, sumsql, page, page_size)
  872. def get_pitcher_trend(pitcher, start=None, end=None, page=None, page_size=None, order_by=None, order=None):
  873. db = MysqlUtils()
  874. op1 = f" and pitcher='{pitcher}'"
  875. op2 = f" and dt>='{start}' " if start else ''
  876. op3 = f" and dt<='{end}' " if end else ''
  877. op4 = f" order by {order_by} {order}" if order_by and order else ''
  878. sql = f"""select dt,pitcher,
  879. reg_num,create_user_num,
  880. cost,
  881. reg_amount,
  882. d1 first_amount,
  883. round(d1/cost,4) first_roi,
  884. round(reg_amount/cost,4) roi,
  885. d7 reg_amount7,
  886. round(d7/cost,4) roi7,
  887. d30 reg_amount30,
  888. round(d30/cost,4) roi30,
  889. reg_amount-cost profit,
  890. inva_cost expect_profit,
  891. annual_expect_profit,
  892. CONCAT(d1,",",0,',',round(d1/cost,4)) d1,
  893. CONCAT(d2-d1,",",round((d2-d1)/cost,4),',',round(d2/cost,4)) d2,
  894. CONCAT(d3-d2,",",round((d3-d2)/cost,4),',',round(d3/cost,4)) d3,
  895. CONCAT(d4-d3,",",round((d4-d3)/cost,4),',',round(d4/cost,4)) d4,
  896. CONCAT(d5-d4,",",round((d5-d4)/cost,4),',',round(d5/cost,4)) d5,
  897. CONCAT(d6-d5,",",round((d6-d5)/cost,4),',',round(d6/cost,4)) d6,
  898. CONCAT(d7-d6,",",round((d7-d6)/cost,4),',',round(d7/cost,4)) d7,
  899. CONCAT(d8-d7,",",round((d8-d7)/cost,4),',',round(d8/cost,4)) d8,
  900. CONCAT(d9-d8,",",round((d9-d8)/cost,4),',',round(d9/cost,4)) d9,
  901. CONCAT(d10-d9,",",round((d10-d9)/cost,4),',',round(d10/cost,4)) d10,
  902. CONCAT(d11-d10,",",round((d11-d10)/cost,4),',',round(d11/cost,4)) d11,
  903. CONCAT(d12-d11,",",round((d12-d11)/cost,4),',',round(d12/cost,4)) d12,
  904. CONCAT(d13-d12,",",round((d13-d12)/cost,4),',',round(d13/cost,4)) d13,
  905. CONCAT(d14-d13,",",round((d14-d13)/cost,4),',',round(d14/cost,4)) d14,
  906. CONCAT(d15-d14,",",round((d15-d14)/cost,4),',',round(d15/cost,4)) d15,
  907. CONCAT(d16-d15,",",round((d16-d15)/cost,4),',',round(d16/cost,4)) d16,
  908. CONCAT(d17-d16,",",round((d17-d16)/cost,4),',',round(d17/cost,4)) d17,
  909. CONCAT(d18-d17,",",round((d18-d17)/cost,4),',',round(d18/cost,4)) d18,
  910. CONCAT(d19-d18,",",round((d19-d18)/cost,4),',',round(d19/cost,4)) d19,
  911. CONCAT(d20-d19,",",round((d20-d19)/cost,4),',',round(d20/cost,4)) d20,
  912. CONCAT(d21-d20,",",round((d21-d20)/cost,4),',',round(d21/cost,4)) d21,
  913. CONCAT(d22-d21,",",round((d22-d21)/cost,4),',',round(d22/cost,4)) d22,
  914. CONCAT(d23-d22,",",round((d23-d22)/cost,4),',',round(d23/cost,4)) d23,
  915. CONCAT(d24-d23,",",round((d24-d23)/cost,4),',',round(d24/cost,4)) d24,
  916. CONCAT(d25-d24,",",round((d25-d24)/cost,4),',',round(d25/cost,4)) d25,
  917. CONCAT(d26-d25,",",round((d26-d25)/cost,4),',',round(d26/cost,4)) d26,
  918. CONCAT(d27-d26,",",round((d27-d26)/cost,4),',',round(d27/cost,4)) d27,
  919. CONCAT(d28-d27,",",round((d28-d27)/cost,4),',',round(d28/cost,4)) d28,
  920. CONCAT(d29-d28,",",round((d29-d28)/cost,4),',',round(d29/cost,4)) d29,
  921. CONCAT(d30-d29,",",round((d30-d29)/cost,4),',',round(d30/cost,4)) d30,
  922. d1 as da1,d2 as da2,d3 as da3,d4 as da4,d5 as da5,d6 as da6,d7 as da7,d8 as da8,d9 as da9,d10 as da10,
  923. d11 as da11,d12 as da12,d13 as da13,d14 as da14,d15 as da15,d16 as da16,d17 as da17,d18 as da18,d19 as da19,
  924. d20 as da20,d21 as da21,d22 as da22,d23 as da23,d24 as da24,d25 as da25,d26 as da26,d27 as da27,d28 as da28,
  925. d29 as da29,d30 as da30
  926. from dw_pitcher_trend where 1=1 {op1} {op2} {op3} {op4}
  927. """
  928. sumSql = f"""select concat(date_format(min(dt),'%Y/%m/%d'),'~',date_format(max(dt),'%Y/%m/%d')) dt,sum(cost) cost,
  929. sum(reg_num) reg_num,
  930. sum(create_user_num) create_user_num,
  931. sum(reg_amount) reg_amount,
  932. round(sum(first_amount)/sum(cost),4) first_roi,
  933. round(sum(reg_amount)/sum(cost),4) roi,
  934. sum(profit) profit,
  935. sum(first_amount) first_amount,
  936. round(sum(reg_amount7)/sum(cost),4) roi7,
  937. sum(reg_amount30) reg_amount30,
  938. round(sum(reg_amount30)/sum(cost),4) roi30,
  939. sum(expect_profit) expect_profit,
  940. sum(annual_expect_profit) annual_expect_profit,
  941. concat(sum(da1),',',sum(da1)/sum(cost),',', 0,',',1) d1,
  942. concat(sum(da2)-sum(if (da2,da1,0)),',',sum(da2)/sum(if (da2,cost,0)),',', (sum(da2)-sum(if (da2,da1,0)))/sum(if (da2,cost,0)),',',if(sum(if (da2,da1,0))=0,1,sum(da2)/sum(if (da2,da1,0))) ) d2,
  943. concat(sum(da3)-sum(if (da3,da2,0)),',',sum(da3)/sum(if (da3,cost,0)),',', (sum(da3)-sum(if (da3,da2,0)))/sum(if (da3,cost,0)),',',if(sum(if (da3,da1,0))=0,1,sum(da3)/sum(if (da3,da1,0))) ) d3,
  944. concat(sum(da4)-sum(if (da4,da3,0)),',',sum(da4)/sum(if (da4,cost,0)),',', (sum(da4)-sum(if (da4,da3,0)))/sum(if (da4,cost,0)),',',if(sum(if (da4,da1,0))=0,1,sum(da4)/sum(if (da4,da1,0))) ) d4,
  945. concat(sum(da5)-sum(if (da5,da4,0)),',',sum(da5)/sum(if (da5,cost,0)),',', (sum(da5)-sum(if (da5,da4,0)))/sum(if (da5,cost,0)),',',if(sum(if (da5,da1,0))=0,1,sum(da5)/sum(if (da5,da1,0))) ) d5,
  946. concat(sum(da6)-sum(if (da6,da5,0)),',',sum(da6)/sum(if (da6,cost,0)),',', (sum(da6)-sum(if (da6,da5,0)))/sum(if (da6,cost,0)),',',if(sum(if (da6,da1,0))=0,1,sum(da6)/sum(if (da6,da1,0))) ) d6,
  947. concat(sum(da7)-sum(if (da7,da6,0)),',',sum(da7)/sum(if (da7,cost,0)),',', (sum(da7)-sum(if (da7,da6,0)))/sum(if (da7,cost,0)),',',if(sum(if (da7,da1,0))=0,1,sum(da7)/sum(if (da7,da1,0))) ) d7,
  948. concat(sum(da8)-sum(if (da8,da7,0)),',',sum(da8)/sum(if (da8,cost,0)),',', (sum(da8)-sum(if (da8,da7,0)))/sum(if (da8,cost,0)),',',if(sum(if (da8,da1,0))=0,1,sum(da8)/sum(if (da8,da1,0))) ) d8,
  949. concat(sum(da9)-sum(if (da9,da8,0)),',',sum(da9)/sum(if (da9,cost,0)),',', (sum(da9)-sum(if (da9,da8,0)))/sum(if (da9,cost,0)),',',if(sum(if (da9,da1,0))=0,1,sum(da9)/sum(if (da9,da1,0))) ) d9,
  950. concat(sum(da10)-sum(if (da10,da9,0)),',',sum(da10)/sum(if (da10,cost,0)),',', (sum(da10)-sum(if (da10,da9,0)))/sum(if (da10,cost,0)),',',if(sum(if (da10,da1,0))=0,1,sum(da10)/sum(if (da10,da1,0))) ) d10,
  951. concat(sum(da11)-sum(if (da11,da10,0)),',',sum(da11)/sum(if (da11,cost,0)),',', (sum(da11)-sum(if (da11,da10,0)))/sum(if (da11,cost,0)),',',if(sum(if (da11,da1,0))=0,1,sum(da11)/sum(if (da11,da1,0))) ) d11,
  952. concat(sum(da12)-sum(if (da12,da11,0)),',',sum(da12)/sum(if (da12,cost,0)),',', (sum(da12)-sum(if (da12,da11,0)))/sum(if (da12,cost,0)),',',if(sum(if (da12,da1,0))=0,1,sum(da12)/sum(if (da12,da1,0))) ) d12,
  953. concat(sum(da13)-sum(if (da13,da12,0)),',',sum(da13)/sum(if (da13,cost,0)),',', (sum(da13)-sum(if (da13,da12,0)))/sum(if (da13,cost,0)),',',if(sum(if (da13,da1,0))=0,1,sum(da13)/sum(if (da13,da1,0))) ) d13,
  954. concat(sum(da14)-sum(if (da14,da13,0)),',',sum(da14)/sum(if (da14,cost,0)),',', (sum(da14)-sum(if (da14,da13,0)))/sum(if (da14,cost,0)),',',if(sum(if (da14,da1,0))=0,1,sum(da14)/sum(if (da14,da1,0))) ) d14,
  955. concat(sum(da15)-sum(if (da15,da14,0)),',',sum(da15)/sum(if (da15,cost,0)),',', (sum(da15)-sum(if (da15,da14,0)))/sum(if (da15,cost,0)),',',if(sum(if (da15,da1,0))=0,1,sum(da15)/sum(if (da15,da1,0))) ) d15,
  956. concat(sum(da16)-sum(if (da16,da15,0)),',',sum(da16)/sum(if (da16,cost,0)),',', (sum(da16)-sum(if (da16,da15,0)))/sum(if (da16,cost,0)),',',if(sum(if (da16,da1,0))=0,1,sum(da16)/sum(if (da16,da1,0))) ) d16,
  957. concat(sum(da17)-sum(if (da17,da16,0)),',',sum(da17)/sum(if (da17,cost,0)),',', (sum(da17)-sum(if (da17,da16,0)))/sum(if (da17,cost,0)),',',if(sum(if (da17,da1,0))=0,1,sum(da17)/sum(if (da17,da1,0))) ) d17,
  958. concat(sum(da18)-sum(if (da18,da17,0)),',',sum(da18)/sum(if (da18,cost,0)),',', (sum(da18)-sum(if (da18,da17,0)))/sum(if (da18,cost,0)),',',if(sum(if (da18,da1,0))=0,1,sum(da18)/sum(if (da18,da1,0))) ) d18,
  959. concat(sum(da19)-sum(if (da19,da18,0)),',',sum(da19)/sum(if (da19,cost,0)),',', (sum(da19)-sum(if (da19,da18,0)))/sum(if (da19,cost,0)),',',if(sum(if (da19,da1,0))=0,1,sum(da19)/sum(if (da19,da1,0))) ) d19,
  960. concat(sum(da20)-sum(if (da20,da19,0)),',',sum(da20)/sum(if (da20,cost,0)),',', (sum(da20)-sum(if (da20,da19,0)))/sum(if (da20,cost,0)),',',if(sum(if (da20,da1,0))=0,1,sum(da20)/sum(if (da20,da1,0))) ) d20,
  961. concat(sum(da21)-sum(if (da21,da20,0)),',',sum(da21)/sum(if (da21,cost,0)),',', (sum(da21)-sum(if (da21,da20,0)))/sum(if (da21,cost,0)),',',if(sum(if (da21,da1,0))=0,1,sum(da21)/sum(if (da21,da1,0))) ) d21,
  962. concat(sum(da22)-sum(if (da22,da21,0)),',',sum(da22)/sum(if (da22,cost,0)),',', (sum(da22)-sum(if (da22,da21,0)))/sum(if (da22,cost,0)),',',if(sum(if (da22,da1,0))=0,1,sum(da22)/sum(if (da22,da1,0))) ) d22,
  963. concat(sum(da23)-sum(if (da23,da22,0)),',',sum(da23)/sum(if (da23,cost,0)),',', (sum(da23)-sum(if (da23,da22,0)))/sum(if (da23,cost,0)),',',if(sum(if (da23,da1,0))=0,1,sum(da23)/sum(if (da23,da1,0))) ) d23,
  964. concat(sum(da24)-sum(if (da24,da23,0)),',',sum(da24)/sum(if (da24,cost,0)),',', (sum(da24)-sum(if (da24,da23,0)))/sum(if (da24,cost,0)),',',if(sum(if (da24,da1,0))=0,1,sum(da24)/sum(if (da24,da1,0))) ) d24,
  965. concat(sum(da25)-sum(if (da25,da24,0)),',',sum(da25)/sum(if (da25,cost,0)),',', (sum(da25)-sum(if (da25,da24,0)))/sum(if (da25,cost,0)),',',if(sum(if (da25,da1,0))=0,1,sum(da25)/sum(if (da25,da1,0))) ) d25,
  966. concat(sum(da26)-sum(if (da26,da25,0)),',',sum(da26)/sum(if (da26,cost,0)),',', (sum(da26)-sum(if (da26,da25,0)))/sum(if (da26,cost,0)),',',if(sum(if (da26,da1,0))=0,1,sum(da26)/sum(if (da26,da1,0))) ) d26,
  967. concat(sum(da27)-sum(if (da27,da26,0)),',',sum(da27)/sum(if (da27,cost,0)),',', (sum(da27)-sum(if (da27,da26,0)))/sum(if (da27,cost,0)),',',if(sum(if (da27,da1,0))=0,1,sum(da27)/sum(if (da27,da1,0))) ) d27,
  968. concat(sum(da28)-sum(if (da28,da27,0)),',',sum(da28)/sum(if (da28,cost,0)),',', (sum(da28)-sum(if (da28,da27,0)))/sum(if (da28,cost,0)),',',if(sum(if (da28,da1,0))=0,1,sum(da28)/sum(if (da28,da1,0))) ) d28,
  969. concat(sum(da29)-sum(if (da29,da28,0)),',',sum(da29)/sum(if (da29,cost,0)),',', (sum(da29)-sum(if (da29,da28,0)))/sum(if (da29,cost,0)),',',if(sum(if (da29,da1,0))=0,1,sum(da29)/sum(if (da29,da1,0))) ) d29,
  970. concat(sum(da30)-sum(if (da30,da29,0)),',',sum(da30)/sum(if (da30,cost,0)),',', (sum(da30)-sum(if (da30,da29,0)))/sum(if (da30,cost,0)),',',if(sum(if (da30,da1,0))=0,1,sum(da30)/sum(if (da30,da1,0))) ) d30
  971. from ({sql}) a
  972. """
  973. data, total, total_data = getLimitSumData(db.dm, sql, sumSql, page, page_size)
  974. print(data)
  975. def parse(key_str, is_total=False):
  976. if type(key_str) is not str:
  977. key_str = key_str.decode('utf-8')
  978. li = key_str.split(',')
  979. li[0] = round(float(li[0]), 2)
  980. li[1] = round(float(li[1]), 4)
  981. li[2] = round(float(li[2]), 4)
  982. if is_total:
  983. return dict(zip(['amount', 'roi', 'add'], li))
  984. else:
  985. return dict(zip(['amount', 'add', 'roi'], li))
  986. for k, v in total_data.items():
  987. if k in ['d1', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8', 'd9', 'd10',
  988. 'd11', 'd12', 'd13', 'd14', 'd15', 'd16', 'd17', 'd18', 'd19', 'd20', 'd21', 'd22', 'd23', 'd24',
  989. 'd25', 'd26', 'd27', 'd28', 'd29', 'd30']:
  990. total_data[k] = parse(v, is_total=True) if v else {}
  991. for i in data:
  992. for k, v in i.items():
  993. if k in ['d1', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8', 'd9', 'd10',
  994. 'd11', 'd12', 'd13', 'd14', 'd15', 'd16', 'd17', 'd18', 'd19', 'd20', 'd21', 'd22', 'd23', 'd24',
  995. 'd25', 'd26', 'd27', 'd28', 'd29', 'd30']:
  996. i[k] = parse(v) if v else {}
  997. return data, total, total_data
  998. if __name__ == '__main__':
  999. get_pitcher_trend(pitcher="陈凯")