OrderHandler.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. from handlers.HandlerBase import BaseHandler
  2. import time, json
  3. from model.DataBaseUtils import *
  4. from model.CommonUtils import *
  5. from model import UserAuthUtils
  6. from model.UserAuthUtils import super_auth, get_auth_game_info
  7. from model.DateUtils import DateUtils
  8. from datetime import datetime, timedelta
  9. class OrderInfoGame(BaseHandler):
  10. def get(self):
  11. if not self._au:
  12. self.write_fail(msg='auth error')
  13. else:
  14. db = MysqlUtils()
  15. sql = f'''
  16. select min(id),name from db_mp.h_game
  17. group by name
  18. '''
  19. data = db.quchen_text.getData_json(sql)
  20. self.write_json(data=data)
  21. class OrderInfoAgent(BaseHandler):
  22. def post(self):
  23. if not self._au:
  24. self.write_fail(msg='auth error')
  25. else:
  26. args = self.get_args()
  27. user_id = args.get('user_id')
  28. db = MysqlUtils()
  29. if user_id in super_auth():
  30. op = ''
  31. else:
  32. if len(UserAuthUtils.get_auth_channel(user_id)) == 0:
  33. return None, None, None
  34. elif len(UserAuthUtils.get_auth_channel(user_id)) == 1:
  35. op = f" and b.wx_name = '{UserAuthUtils.get_auth_channel(user_id)[0]}'"
  36. else:
  37. op = f" and b.wx_name in {str(UserAuthUtils.get_auth_channel(user_id))}"
  38. sql = f'''
  39. select row_number() over() as id,
  40. d.user_nicename as agent_name
  41. from quchen_text.advertiser_vx a
  42. left join db_mp.mp_mp_conf b on a.name =b.wx_name
  43. left join db_mp.mp_conf_agent c on b.id = c.advertiser_conf_id
  44. left join db_mp.h_user d on c.agent_id = d.id
  45. where d.user_nicename is not null
  46. {op}
  47. group by d.user_nicename
  48. '''
  49. data = db.quchen_text.getData_json(sql)
  50. data.insert(0, {'id': -1, 'agent_name': '自然流量渠道'})
  51. data.insert(0, {'id': -2, 'agent_name': '客服渠道'})
  52. self.write_json(data=data)
  53. class OrderInfoNobody(BaseHandler):
  54. def post(self):
  55. if not self._au:
  56. self.write_fail(msg='auth error')
  57. else:
  58. args = self.get_args()
  59. user_id = args.get('user_id')
  60. start = args.get("start", self.thisday)
  61. end = args.get("end", self.thisday)
  62. game = args.get('game')
  63. page = args.get("page")
  64. page_size = args.get("page_size")
  65. db = MysqlUtils()
  66. op = ''
  67. is_over = False
  68. if user_id in super_auth():
  69. op = ''
  70. else:
  71. if len(UserAuthUtils.get_auth_game_name(user_id)) == 0:
  72. self.write_json(None)
  73. is_over = True
  74. else:
  75. game_name_list = UserAuthUtils.get_auth_game_name(user_id)
  76. game_name_list = [_[0] for _ in game_name_list if _[0]]
  77. game_str = "'" + "','".join(game_name_list) + "'"
  78. op = f" and name in ({game_str}) "
  79. if not is_over:
  80. op1 = f''' and dt >= '{start}' and dt <= '{end}' '''
  81. op2 = ''
  82. if game:
  83. game_str = "'" + "','".join(game) + "'"
  84. op2 = f" and name in ({game_str}) "
  85. limit_start = (page - 1) * page_size
  86. sql = f'''
  87. select dt,name game,
  88. sum(if(agent='自然流量',sum_amount,0)) as nobody_order,
  89. sum(if(agent='买量流量',sum_amount,0)) as body_order,
  90. sum(if(agent='当日总量',sum_amount,0)) as all_order
  91. from
  92. (select sum(amount) sum_amount,toDate(FROM_UNIXTIME(a.create_time)) dt,
  93. b.name ,if(a.agent_id in ('sdk_one_0','sdk_two_0'),'自然流量','买量流量') as agent
  94. from GameDataSum.h_pay_sum a
  95. left join GameDataSum.h_game_sum b on a.app_id = b.id
  96. where a.status = 2 {op}
  97. group by toDate(FROM_UNIXTIME(a.create_time)) ,b.name,
  98. if(a.agent_id in ('sdk_one_0','sdk_two_0') ,'自然流量','买量流量')
  99. union all
  100. select sum(amount) sum_amount,toDate(FROM_UNIXTIME(a.create_time)) dt,
  101. b.name ,'当日总量' as agent
  102. from GameDataSum.h_pay_sum a
  103. left join GameDataSum.h_game_sum b on a.app_id = b.id
  104. where a.status = 2 {op}
  105. group by b.name ,toDate(FROM_UNIXTIME(a.create_time))) a
  106. where 1=1 {op1} {op2}
  107. group by dt,name
  108. order by dt desc
  109. limit {limit_start},{page_size}
  110. '''
  111. print(sql)
  112. ck = CkUtils()
  113. data_res = ck.execute(sql)
  114. data_res_dict = []
  115. for _ in data_res:
  116. tmp_dict = {'dt': _[0].strftime('%Y-%m-%d'),
  117. 'game': _[1],
  118. 'nobody_order': float(_[2]),
  119. 'body_order': float(_[3]),
  120. 'all_order': float(_[4])
  121. }
  122. data_res_dict.append(tmp_dict)
  123. data = data_res_dict
  124. if args.get("download"):
  125. self.write_download(str(int(time.time())), data)
  126. else:
  127. self.write_json(data=data)
  128. class OrderInfo(BaseHandler):
  129. def post(self):
  130. if not self._au:
  131. self.write_fail(msg='auth error')
  132. else:
  133. args = self.get_args()
  134. user_id = args.get('user_id')
  135. # 渠道表
  136. channel = args.get("channel")
  137. pitcher = args.get("pitcher")
  138. # role 表
  139. role_name = args.get("role_name")
  140. start = args.get("start")
  141. if args.get("end"):
  142. tmp_end = args.get("end")
  143. tmp_end = datetime.strptime(tmp_end, '%Y-%m-%d')
  144. tmp_end = tmp_end + timedelta(days=1)
  145. end = tmp_end.strftime('%Y-%m-%d')
  146. else:
  147. end = None
  148. user_start = args.get("user_start")
  149. if args.get("user_end"):
  150. tmp_end = args.get("user_end")
  151. tmp_end = datetime.strptime(tmp_end, '%Y-%m-%d')
  152. tmp_end = tmp_end + timedelta(days=1)
  153. user_end = tmp_end.strftime('%Y-%m-%d')
  154. else:
  155. user_end = None
  156. if not start and not user_start:
  157. start = self.thisday
  158. # start end 转timestamp
  159. start = None if not start else int(datetime.strptime(start, '%Y-%m-%d').timestamp())
  160. end = None if not end else int(datetime.strptime(end, '%Y-%m-%d').timestamp())
  161. user_start = None if not user_start else int(datetime.strptime(user_start, '%Y-%m-%d').timestamp())
  162. user_end = None if not user_end else int(datetime.strptime(user_end, '%Y-%m-%d').timestamp())
  163. order_id = args.get('order_id')
  164. member_id = args.get('member_id')
  165. status = (args.get('status'))
  166. game = (args.get('game'))
  167. agent_name = (args.get('agent_name'))
  168. page = args.get("page", 1)
  169. page_size = args.get("page_size", 20)
  170. order_by = args.get("order_by", 'order_date')
  171. order_ = args.get("order", 'desc')
  172. data, total, total_data = self.get_order_info(user_id, channel, pitcher, role_name, start, end,
  173. user_end, user_start, order_id, member_id, status,
  174. game, agent_name,
  175. page, page_size, order_, order_by)
  176. if args.get("download"):
  177. data, total, total_data = self.get_order_info(user_id, channel, pitcher, role_name, start, end,
  178. user_end, user_start, order_id, member_id, status,
  179. game, agent_name,
  180. 1, 100000000, order_, order_by)
  181. self.write_download(str(int(time.time())), data)
  182. else:
  183. self.write_json(data=data, total=total, total_data=total_data)
  184. def get_order_info(self, user_id, channel, pitcher, role_name, start, end,
  185. user_end, user_start, order_id, member_id, status,
  186. game, agent_name,
  187. page, page_size, order_, order_by):
  188. db = MysqlUtils()
  189. if user_id in super_auth():
  190. op_channel = ''
  191. op_game = ''
  192. else:
  193. op_game = ''
  194. op_base = f' and if( h.start_date is not null,d.create_time >= toUnixTimestamp(toDateTime(start_date)),1) '
  195. # 限制为自己游戏的1.自然流量,2.客服流量
  196. if agent_name and ('自然流量渠道' in agent_name or '客服渠道' in agent_name):
  197. game_ids = get_auth_game_info(user_id)
  198. print(game_ids)
  199. op_game = ''
  200. for game_id, game_name, game_timestamp in game_ids:
  201. op_game = op_game + f'''or (e.id = {game_id} and a.create_time >UNIX_TIMESTAMP('{game_timestamp}') ) '''
  202. op_game = ' and ' + '(' + op_game[2:] + ')' + op_base
  203. # 限制为自己公众号开始时候的数据
  204. if len(UserAuthUtils.get_auth_channel(user_id)) == 0:
  205. return None, None, None
  206. elif len(UserAuthUtils.get_auth_channel(user_id)) == 1:
  207. op_channel = f''' and c.wx_name = '{UserAuthUtils.get_auth_channel(user_id)[0]}'
  208. ''' + op_base
  209. else:
  210. op_channel = f'''
  211. and c.wx_name in {str(UserAuthUtils.get_auth_channel(user_id))}
  212. ''' + op_base
  213. op = op_game if agent_name and ('自然流量渠道' in agent_name or '客服渠道' in agent_name) else op_channel
  214. op1 = f" where pitcher ='{pitcher}' " if pitcher else ''
  215. op2 = f''' where wx_name in ({"'" + "','".join(channel) + "'"})''' if channel else ''
  216. op3 = f" and create_time >{start} " if start else ''
  217. op4 = f" and create_time <{end} " if end else ''
  218. op5 = f" where role_name ='{role_name}' " if role_name else ''
  219. op6 = f" and create_time >{user_start} " if user_start else ''
  220. op7 = f" and create_time <{user_end} " if user_end else ''
  221. op8 = f" and order_id like '_{order_id}'" if order_id else ''
  222. op9 = f" and mem_id like '_{member_id}'" if member_id else ''
  223. op10 = f" and status={status} " if status else ''
  224. op11 = f''' where name in ({"'" + "','".join(game) + "'"}) ''' if game else ''
  225. op12_pay = ''
  226. op12_user = ''
  227. # 有自然流量和客服渠道时,其他渠道进行规避
  228. if agent_name and '自然流量渠道' in agent_name and '客服渠道' in agent_name:
  229. op12_pay = f" and agent_id in (12,0) "
  230. elif agent_name and '自然流量渠道' in agent_name:
  231. op12_pay = f" and agent_id=0 "
  232. elif agent_name and '客服渠道' in agent_name:
  233. op12_pay = f" and agent_id=12 "
  234. else:
  235. op12_user = f''' where user_nicename in ({"'" + "','".join(agent_name) + "'"}) ''' if agent_name else ''
  236. # 选项:order_date:订单时间,user_date:用户时间,amount:金额,game:游戏名字,payway:支付方式,agent_name:渠道
  237. if order_by == 'agent_name':
  238. order_by = 'q.user_nicename'
  239. elif order_by == 'user_date':
  240. order_by = 'd.create_time'
  241. elif order_by == 'amount':
  242. order_by = 'amount'
  243. elif order_by == 'game':
  244. order_by = 'e.name'
  245. elif order_by == 'payway':
  246. order_by = 'a.payway'
  247. else:
  248. order_by = 'a.create_time'
  249. op15 = f" order by {order_by} {order_}" if order_by and order_ else ''
  250. sql = f"""select
  251. rowNumberInAllBlocks() as id,
  252. a.order_id order_id,
  253. h.pitcher pitcher,
  254. if(a.agent_id like '%_21','客服渠道',if(a.agent_id like '%_0','自然流量',q.user_nicename)) as agent_name,
  255. if(a.agent_id like '%_21','客服渠道',if(a.agent_id like '%_0','自然流量',c.wx_name)) as channel,
  256. FROM_UNIXTIME(a.create_time) order_time,
  257. FROM_UNIXTIME(d.create_time) user_create_time,
  258. d.id as mem_id,
  259. e.name as game ,
  260. a.amount amount,
  261. a.real_amount real_amount,
  262. g.server_name server_name,
  263. f.os as system_os,
  264. g.role_name role_name,
  265. a.payway payway,a.status status ,a.mg_mem_id as user_id
  266. from (
  267. select * from GameDataSum.h_pay_sum
  268. where 1=1 {op3} {op4} {op8} {op9} {op10} {op12_pay}
  269. ) a
  270. left join
  271. (select b.agent_id as agent_id,b.advertiser_conf_id from
  272. (select agent_id,max(update_time) as update_time
  273. from GameDataSum.mp_conf_agent_sum mmc
  274. group by agent_id ) a
  275. left join GameDataSum.mp_conf_agent_sum b on a.agent_id=b.agent_id
  276. and a.update_time = b.update_time) b on a.agent_id =b.agent_id
  277. left join
  278. (select id,wx_name from GameDataSum.mp_mp_conf_sum
  279. {op2} ) c on b.advertiser_conf_id = c.id
  280. left join (select id,create_time from GameDataSum.h_member_sum
  281. where 1=1 {op6} {op7} ) d on a.mem_id = d.id
  282. left join ( select * from GameDataSum.h_game_sum {op11} ) e on a.app_id =e.id
  283. left join GameDataSum.h_pay_ext_sum f on a.id = f.pay_id
  284. left join ( select * from GameDataSum.h_mg_role_sum {op5} ) g
  285. on g.role_id = f.role_id and g.server_id = f.server_id
  286. and g.app_id = a.app_id and g.mg_mem_id =a.mg_mem_id
  287. left join (select name,pitcher ,start_date
  288. from CostSourceData.advertiser_vx {op1}
  289. ) h on c.wx_name = h.name
  290. left join (select * from GameDataSum.h_user_sum {op12_user} ) q on a.agent_id =q.id
  291. where 1=1 {op}
  292. {op15}
  293. """
  294. limit_start = (page - 1) * page_size
  295. limit_sql = sql + '\n' + f''' limit {limit_start},{page_size} '''
  296. print(limit_sql)
  297. sumsql = f""" select '' as id,'' as order_id ,'' as pitcher,'' as agent_name, '' as channel,
  298. '' as order_time,'' as user_create_time,'' as user_id,
  299. '' as game,sum(amount) as amount ,sum(real_amount) as real_amount ,'' as server_name,
  300. '' as mem_id , '' as role_name,'' as payway ,'' as status ,'' as system_os
  301. from ({sql}) a
  302. """
  303. totalsql = f'''
  304. select count(1) from ({sql}) a
  305. '''
  306. print(sumsql)
  307. print(totalsql)
  308. ck = CkUtils()
  309. data_res = ck.execute(limit_sql)
  310. data_res_dict = []
  311. data_sum_res = ck.execute(sumsql)
  312. data_sum_res_dict = []
  313. data_total = ck.execute(totalsql)[0][0]
  314. for _ in data_res:
  315. tmp_dict = {'id': _[0],
  316. 'order_id': _[1],
  317. 'pitcher': _[2],
  318. 'agent_name': _[3],
  319. 'channel': _[4],
  320. 'order_time': datetime.strftime(_[5], '%Y-%m-%d %H:%M:%S'),
  321. 'user_create_time': datetime.strftime(_[6], '%Y-%m-%d %H:%M:%S'),
  322. 'mem_id': _[7],
  323. 'game': _[8],
  324. 'amount': float(_[9]),
  325. 'real_amount': float(_[10]),
  326. 'server_name': _[11],
  327. 'system_os': _[12],
  328. 'role_name': _[13],
  329. 'payway': _[14],
  330. 'status': _[15],
  331. 'user_id': _[16],
  332. }
  333. data_res_dict.append(tmp_dict)
  334. for _ in data_sum_res:
  335. tmp_dict = {'id': _[0],
  336. 'order_id': _[1],
  337. 'pitcher': _[2],
  338. 'agent_name': _[3],
  339. 'channel': _[4],
  340. 'order_time': '',
  341. 'user_create_time': '',
  342. 'mem_id': _[7],
  343. 'game': _[8],
  344. 'amount': float(_[9]),
  345. 'real_amount': float(_[10]),
  346. 'server_name': _[11],
  347. 'system_os': _[12],
  348. 'role_name': _[13],
  349. 'payway': _[14],
  350. 'status': _[15],
  351. 'user_id': _[16],
  352. }
  353. data_sum_res_dict.append(tmp_dict)
  354. data, total, total_data = data_res_dict, data_total, data_sum_res_dict
  355. return data, total, total_data
  356. class OrderH5Info(BaseHandler):
  357. def post(self):
  358. if not self._au:
  359. self.write_fail(msg='auth error')
  360. else:
  361. args = self.get_args()
  362. user_id = args.get('user_id')
  363. # role 表
  364. start = args.get("start", self.thisday)
  365. end = args.get("end")
  366. data = self.get_order_info(user_id, start, end)
  367. if args.get("download"):
  368. data = self.get_order_info_download(user_id, start, end)
  369. self.write_download(str(int(time.time())), data)
  370. else:
  371. self.write_json(data=data)
  372. def get_order_info(self, user_id, start, end):
  373. ck = CkUtils()
  374. du = DateUtils()
  375. if user_id not in super_auth():
  376. return []
  377. # todo: 定时跑数据汇总也还没有写好,写好
  378. # start end 转变为Unix_time
  379. end = du.getLastDays(end,-1) if end else None #end 往后面一天
  380. start = None if not start else int(datetime.strptime(start, '%Y-%m-%d').timestamp())
  381. end = None if not end else int(datetime.strptime(end, '%Y-%m-%d').timestamp())
  382. op1 = f" and create_time>='{start}' " if start else ''
  383. op2 = f" and create_time<='{end}' " if end else ''
  384. sql = f'''
  385. select a.dt,a.h5_game_name,
  386. b.sum_amount as sum_amount,
  387. b.natural_amount as natural_amount,
  388. b.amount_rate as amount_rate,
  389. b.human_count as human_count,
  390. b.natural_human_count as natural_human_count,
  391. b.human_count_rate as human_count_rate,
  392. a.channel_game_name as channel_game_name,
  393. a.is_natural as is_natural,
  394. toFloat64(a.sum_amount) as channel_amount,
  395. toFloat64(a.human_counts) as channel_human_count
  396. from
  397. (select toDate(FROM_UNIXTIME(a.create_time)) dt,a.app_id as app_id ,
  398. min(b.name) as h5_game_name,
  399. min(c.app_id) as c_app_id,d.name as channel_game_name,
  400. if(min(c.agent_id) in ('sdk_one_0','sdk_two_0'),1,0) as is_natural ,
  401. sum(amount) as sum_amount,count(DISTINCT(a.mem_id)) as human_counts
  402. from (select * from GameDataSum.h_pay_sum where 1=1 {op1} {op2} ) a
  403. LEFT join GameDataSum.h_game_sum b on a.app_id = b.id
  404. left join (select * from GameDataSum.h_member_sum where 1=1 {op1} {op2} ) c on a.mem_id = c.id
  405. LEFT join GameDataSum.h_game_sum d on c.app_id = d.id
  406. where a.app_id in (select id from GameDataSum.h_game_sum
  407. where classify =5)
  408. and a.app_id != c.app_id
  409. and a.status = 2
  410. group by toDate(FROM_UNIXTIME(a.create_time)),a.app_id ,
  411. c.app_id ,d.name,if(c.agent_id in ('sdk_one_0','sdk_two_0'),1,0) ) as a
  412. left join
  413. (select toDate(FROM_UNIXTIME(a.create_time)) as dt,a.app_id ,
  414. sum(a.amount) sum_amount,
  415. sum(if(a.app_id=c.app_id,a.amount,0)) natural_amount,
  416. count(DISTINCT(a.mem_id)) as human_count,
  417. count(DISTINCT(if(a.app_id=c.app_id,a.mem_id,null))) natural_human_count,
  418. ROUND(if(sum(a.amount)>0,sum(if(a.app_id=c.app_id,a.amount,0)) /sum(a.amount),0) ,2) amount_rate,
  419. ROUND( if(count(DISTINCT(a.mem_id))>0,count(DISTINCT(if(a.app_id=c.app_id,a.mem_id,null)))/count(DISTINCT(a.mem_id)),0),2) human_count_rate
  420. from (select * from GameDataSum.h_pay_sum where 1=1 {op1} {op2} ) a
  421. left join (select * from GameDataSum.h_member_sum where 1=1 {op1} {op2} ) c on a.mem_id = c.id
  422. where a.app_id in (select id from GameDataSum.h_game_sum
  423. where classify =5)
  424. and a.status=2
  425. group by toDate(FROM_UNIXTIME(a.create_time)),a.app_id ) b
  426. on a.dt=b.dt and a.app_id = b.app_id
  427. order by a.dt desc,a.app_id,c_app_id
  428. '''
  429. print(sql)
  430. df_source = ck.execute(sql)
  431. game_info = {}
  432. for _ in df_source:
  433. _ = list(_)
  434. _[0] = datetime.strftime(_[0], '%Y-%m-%d')
  435. _[2] = float(_[2])
  436. _[3] = float(_[3])
  437. _[4] = float(_[4])
  438. _[7] = float(_[7])
  439. _[10] = float(_[10])
  440. h_game = (_[0], _[1], _[2], _[3], _[4], _[5], _[6], _[7])
  441. if h_game in game_info.keys():
  442. game_info[h_game].append(_)
  443. else:
  444. game_info[h_game] = [_]
  445. res_game_info = []
  446. for k, v in game_info.items():
  447. print(k, v)
  448. channel_game_list_natural = []
  449. channel_game_list_unnatural = []
  450. for _ in v:
  451. if _[9]:
  452. channel_game_list_natural.append({'channel_game_name': _[8],
  453. 'channel_game_amount': _[10],
  454. 'channel_game_human_count': _[11]})
  455. else:
  456. channel_game_list_unnatural.append({'channel_game_name': _[8],
  457. 'channel_game_amount': _[10],
  458. 'channel_game_human_count': _[11]})
  459. if len(channel_game_list_natural) != len(channel_game_list_unnatural):
  460. game_name_natural = set()
  461. game_name_unnatural = set()
  462. for _ in channel_game_list_natural:
  463. game_name_natural.add(_['channel_game_name'])
  464. for _ in channel_game_list_unnatural:
  465. game_name_unnatural.add(_['channel_game_name'])
  466. for channel_game_name in game_name_unnatural - game_name_natural:
  467. channel_game_list_natural.append({'channel_game_name': channel_game_name,
  468. 'channel_game_amount': 0,
  469. 'channel_game_human_count': 0})
  470. for channel_game_name in game_name_natural - game_name_unnatural:
  471. channel_game_list_unnatural.append({'channel_game_name': channel_game_name,
  472. 'channel_game_amount': 0,
  473. 'channel_game_human_count': 0})
  474. channel_game_list_unnatural.sort(key=lambda x: str(x['channel_game_name']))
  475. channel_game_list_natural.sort(key=lambda x: str(x['channel_game_name']))
  476. tmp = {'dt': k[0], 'game_name': k[1], 'sum_amount': k[2], 'natural_amount': k[3],
  477. 'natural_amount_rate': k[4], 'human_count': k[5], 'natural_human_count': k[6],
  478. 'natural_human_count_rate': k[7], 'channel_game_natural_list': channel_game_list_natural,
  479. 'channel_game_unnatural_list': channel_game_list_unnatural
  480. }
  481. res_game_info.append(tmp)
  482. return res_game_info
  483. def get_order_info_download(self, user_id, start, end):
  484. if user_id not in super_auth():
  485. return []
  486. db = MysqlUtils()
  487. op1 = f"and date(FROM_UNIXTIME(a.create_time))>='{start}' " if start else ''
  488. op2 = f"and date(FROM_UNIXTIME(a.create_time))<='{end}' " if end else ''
  489. sql = f'''
  490. select DATE_FORMAT(a.dt,'%Y-%m-%d') 日期,
  491. a.h5_game_name 游戏名称H5,
  492. b.sum_amount as 今日总充值,
  493. b.natural_amount as H5自然量充值,
  494. b.amount_rate as 充值额比例,
  495. b.human_count as 今日总充值人数,
  496. b.natural_human_count H5自然量人数,
  497. b.human_count_rate as 用户比例,
  498. a.channel_game_name as 游戏名称小程序,
  499. a.is_natural as 小程序的量是否为自然量,
  500. a.sum_amount as 小程序导入用户充值,
  501. a.human_counts as 小程序导入人数
  502. from
  503. (select date(FROM_UNIXTIME(a.create_time)) dt,a.app_id as app_id ,
  504. b.name as h5_game_name,
  505. c.app_id as c_app_id,d.name as channel_game_name,if(c.agent_id=0,1,0) as is_natural ,
  506. sum(amount) as sum_amount,count(DISTINCT(a.mem_id)) as human_counts
  507. from db_mp.h_pay a
  508. LEFT join db_mp.h_game b on a.app_id = b.id
  509. left join db_mp.h_member c on a.mem_id = c.id
  510. LEFT join db_mp.h_game d on c.app_id = d.id
  511. where a.app_id in (select id from db_mp.h_game
  512. where classify =5)
  513. and a.app_id != c.app_id
  514. and a.status = 2
  515. {op1} {op2}
  516. group by date(FROM_UNIXTIME(a.create_time)),a.app_id ,
  517. c.app_id ,d.name,if(c.agent_id=0,1,0) ) as a
  518. left join
  519. (select date(FROM_UNIXTIME(a.create_time)) as dt,a.app_id ,
  520. sum(a.amount) sum_amount,
  521. sum(if(a.app_id=c.app_id,a.amount,0)) natural_amount,
  522. count(DISTINCT(a.mem_id)) as human_count,
  523. count(DISTINCT(if(a.app_id=c.app_id,a.mem_id,null))) natural_human_count,
  524. ROUND(if(sum(a.amount)>0,sum(if(a.app_id=c.app_id,a.amount,0)) /sum(a.amount),0) ,2) amount_rate,
  525. ROUND( if(count(DISTINCT(a.mem_id))>0,count(DISTINCT(if(a.app_id=c.app_id,a.mem_id,null)))/count(DISTINCT(a.mem_id)),0),2) human_count_rate
  526. from db_mp.h_pay a
  527. left join db_mp.h_member c on a.mem_id = c.id
  528. where a.app_id in (select id from db_mp.h_game
  529. where classify =5)
  530. {op1} {op2}
  531. and a.status=2
  532. group by date(FROM_UNIXTIME(a.create_time)),a.app_id ) b
  533. on a.dt=b.dt and a.app_id = b.app_id
  534. order by a.dt desc,a.app_id,c_app_id
  535. '''
  536. data = db.dm.getData_json(sql)
  537. return data
  538. class OrderH5InfoSpecial(BaseHandler):
  539. def post(self):
  540. if not self._au:
  541. self.write_fail(msg='auth error')
  542. else:
  543. args = self.get_args()
  544. user_id = args.get('user_id')
  545. # role 表
  546. start = args.get("start", self.thisday)
  547. end = args.get("end")
  548. data = []
  549. if args.get("download"):
  550. data = self.get_order_info_download(user_id, start, end)
  551. self.write_download(str(int(time.time())), data)
  552. else:
  553. self.write_json(data=data)
  554. def get_order_info_download(self, user_id, start, end):
  555. if user_id not in super_auth():
  556. return []
  557. db = MysqlUtils()
  558. op1 = f"and CONVERT(from_unixtime(a.create_time),char(10))>='{start}' " if start else ''
  559. op2 = f"and CONVERT(from_unixtime(a.create_time),char(10))<='{end}' " if end else ''
  560. sql = f'''
  561. select row_number() over() as id,a.order_id ,h.pitcher,
  562. if(a.agent_id =21,'客服渠道',if(a.agent_id=0,'自然流量',q.user_nicename)) as agent_name,
  563. if(a.agent_id =21,'客服渠道',if(a.agent_id=0,'自然流量',c.wx_name)) as channel,
  564. if(a.create_time is not null ,CONVERT(from_unixtime(a.create_time),char(20)),null) order_time,
  565. if(d.create_time is not null,CONVERT(from_unixtime(d.create_time),char(20)),null) user_create_time,
  566. e2.name as source_game,
  567. d.id as mem_id,e.name as game ,a.amount ,a.real_amount ,g.server_name ,f.os as system_os,
  568. CONVERT (g.role_name USING utf8) as role_name,a.payway ,a.status status ,a.mg_mem_id as user_id
  569. from db_mp.h_pay a
  570. left join
  571. (select b.agent_id as agent_id,b.advertiser_conf_id from
  572. (select agent_id,max(update_time) as update_time
  573. from db_mp.mp_conf_agent mmc
  574. group by agent_id ) a
  575. left join db_mp.mp_conf_agent b on a.agent_id=b.agent_id
  576. and a.update_time = b.update_time) b on a.agent_id =b.agent_id
  577. left join db_mp.mp_mp_conf c on b.advertiser_conf_id = c.id
  578. left join db_mp.h_member d on a.mem_id = d.id
  579. left join db_mp.h_game e on a.app_id =e.id
  580. left join db_mp.h_game e2 on d.app_id =e2.id
  581. left join db_mp.h_pay_ext f on a.id = f.pay_id
  582. left join db_mp.h_mg_role g
  583. on g.role_id = f.role_id and g.server_id = f.server_id
  584. and g.app_id = a.app_id and g.mg_mem_id =a.mg_mem_id
  585. left join quchen_text.advertiser_vx h on c.wx_name = h.name
  586. left join db_mp.h_user q on a.agent_id =q.id
  587. where
  588. a.app_id not in(6,13,23)
  589. and a.agent_id =0
  590. and d.app_id in(6,13,23)
  591. {op1} {op2}
  592. and a.status =2
  593. '''
  594. data = db.dm.getData_json(sql)
  595. return data