OrderHandler.py 29 KB

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