OrderHandler.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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. # todo:订单之后写个专门的clickhouse订单表,clickhouse 一小时同步一次
  190. if user_id in super_auth():
  191. op_channel = ''
  192. op_game = ''
  193. else:
  194. op_game = ''
  195. op_base = f' and if( h.start_date is not null,d.create_time >= toUnixTimestamp(toDateTime(start_date)),1) '
  196. # 限制为自己游戏的1.自然流量,2.客服流量
  197. if agent_name and ('自然流量渠道' in agent_name or '客服渠道' in agent_name):
  198. game_ids = get_auth_game_info(user_id)
  199. print(game_ids)
  200. op_game = ''
  201. for game_id, game_name, game_timestamp in game_ids:
  202. op_game = op_game + f'''or (e.id = {game_id} and a.create_time >UNIX_TIMESTAMP('{game_timestamp}') ) '''
  203. op_game = ' and ' + '(' + op_game[2:] + ')' + op_base
  204. # 限制为自己公众号开始时候的数据
  205. if len(UserAuthUtils.get_auth_channel(user_id)) == 0:
  206. return None, None, None
  207. elif len(UserAuthUtils.get_auth_channel(user_id)) == 1:
  208. op_channel = f''' and c.wx_name = '{UserAuthUtils.get_auth_channel(user_id)[0]}'
  209. ''' + op_base
  210. else:
  211. op_channel = f'''
  212. and c.wx_name in {str(UserAuthUtils.get_auth_channel(user_id))}
  213. ''' + op_base
  214. op = op_game if agent_name and ('自然流量渠道' in agent_name or '客服渠道' in agent_name) else op_channel
  215. op1 = f" where pitcher ='{pitcher}' " if pitcher else ''
  216. op2 = f''' where wx_name in ({"'" + "','".join(channel) + "'"})''' if channel else ''
  217. op3 = f" and create_time >{start} " if start else ''
  218. op4 = f" and create_time <{end} " if end else ''
  219. op5 = f" where role_name ='{role_name}' " if role_name else ''
  220. op6 = f" and create_time >{user_start} " if user_start else ''
  221. op7 = f" and create_time <{user_end} " if user_end else ''
  222. op8 = f" and order_id like '%_{order_id}'" if order_id else ''
  223. op9 = f" and mem_id like '%_{member_id}'" if member_id else ''
  224. op10 = f" and status={status} " if status else ''
  225. op11 = f''' where name in ({"'" + "','".join(game) + "'"}) ''' if game else ''
  226. op12_pay = ''
  227. op12_user = ''
  228. # 有自然流量和客服渠道时,其他渠道进行规避
  229. if agent_name and '自然流量渠道' in agent_name and '客服渠道' in agent_name:
  230. op12_pay = f" and agent_id in (12,0) "
  231. elif agent_name and '自然流量渠道' in agent_name:
  232. op12_pay = f" and agent_id=0 "
  233. elif agent_name and '客服渠道' in agent_name:
  234. op12_pay = f" and agent_id=12 "
  235. else:
  236. op12_user = f''' where user_nicename in ({"'" + "','".join(agent_name) + "'"}) ''' if agent_name else ''
  237. # 选项:order_date:订单时间,user_date:用户时间,amount:金额,game:游戏名字,payway:支付方式,agent_name:渠道
  238. if order_by == 'agent_name':
  239. order_by = 'q.user_nicename'
  240. elif order_by == 'user_date':
  241. order_by = 'd.create_time'
  242. elif order_by == 'amount':
  243. order_by = 'amount'
  244. elif order_by == 'game':
  245. order_by = 'e.name'
  246. elif order_by == 'payway':
  247. order_by = 'a.payway'
  248. else:
  249. order_by = 'a.create_time'
  250. op15 = f" order by {order_by} {order_}" if order_by and order_ else ''
  251. sql = f"""select
  252. rowNumberInAllBlocks() as id,
  253. a.order_id order_id,
  254. h.pitcher pitcher,
  255. if(a.agent_id like '%_21','客服渠道',if(a.agent_id like '%_0','自然流量',q.user_nicename)) as agent_name,
  256. if(a.agent_id like '%_21','客服渠道',if(a.agent_id like '%_0','自然流量',c.wx_name)) as channel,
  257. FROM_UNIXTIME(a.create_time) order_time,
  258. FROM_UNIXTIME(d.create_time) user_create_time,
  259. d.id as mem_id,
  260. e.name as game ,
  261. a.amount amount,
  262. a.real_amount real_amount,
  263. g.server_name server_name,
  264. f.os as system_os,
  265. g.role_name role_name,
  266. a.payway payway,a.status status ,a.mg_mem_id as user_id
  267. from (
  268. select * from GameDataSum.h_pay_sum
  269. where 1=1 {op3} {op4} {op8} {op9} {op10} {op12_pay}
  270. ) a
  271. left join
  272. (select b.agent_id as agent_id,b.advertiser_conf_id from
  273. (select agent_id,max(update_time) as update_time
  274. from GameDataSum.mp_conf_agent_sum mmc
  275. group by agent_id ) a
  276. left join GameDataSum.mp_conf_agent_sum b on a.agent_id=b.agent_id
  277. and a.update_time = b.update_time) b on a.agent_id =b.agent_id
  278. left join
  279. (select id,wx_name from GameDataSum.mp_mp_conf_sum
  280. {op2} ) c on b.advertiser_conf_id = c.id
  281. left join (select id,create_time from GameDataSum.h_member_sum
  282. where 1=1 {op6} {op7} ) d on a.mem_id = d.id
  283. left join ( select * from GameDataSum.h_game_sum {op11} ) e on a.app_id =e.id
  284. left join GameDataSum.h_pay_ext_sum f on a.id = f.pay_id
  285. left join ( select * from GameDataSum.h_mg_role_sum {op5} ) g
  286. on g.role_id = f.role_id and g.server_id = f.server_id
  287. and g.app_id = a.app_id and g.mg_mem_id =a.mg_mem_id
  288. left join (select name,pitcher ,start_date
  289. from CostSourceData.advertiser_vx {op1}
  290. ) h on c.wx_name = h.name
  291. left join (select * from GameDataSum.h_user_sum {op12_user} ) q on a.agent_id =q.id
  292. where 1=1 {op}
  293. {op15}
  294. """
  295. limit_start = (page - 1) * page_size
  296. limit_sql = sql + '\n' + f''' limit {limit_start},{page_size} '''
  297. print(limit_sql)
  298. sumsql = f""" select '' as id,'' as order_id ,'' as pitcher,'' as agent_name, '' as channel,
  299. '' as order_time,'' as user_create_time,'' as user_id,
  300. '' as game,sum(amount) as amount ,sum(real_amount) as real_amount ,'' as server_name,
  301. '' as mem_id , '' as role_name,'' as payway ,'' as status ,'' as system_os
  302. from ({sql}) a
  303. """
  304. totalsql = f'''
  305. select count(1) from ({sql}) a
  306. '''
  307. print(sumsql)
  308. print(totalsql)
  309. ck = CkUtils()
  310. data_res = ck.execute(limit_sql)
  311. data_res_dict = []
  312. data_sum_res = ck.execute(sumsql)
  313. data_sum_res_dict = []
  314. data_total = ck.execute(totalsql)[0][0]
  315. for _ in data_res:
  316. tmp_dict = {'id': _[0],
  317. 'order_id': _[1],
  318. 'pitcher': _[2],
  319. 'agent_name': _[3],
  320. 'channel': _[4],
  321. 'order_time': datetime.strftime(_[5], '%Y-%m-%d %H:%M:%S'),
  322. 'user_create_time': datetime.strftime(_[6], '%Y-%m-%d %H:%M:%S'),
  323. 'mem_id': _[7],
  324. 'game': _[8],
  325. 'amount': float(_[9]),
  326. 'real_amount': float(_[10]),
  327. 'server_name': _[11],
  328. 'system_os': _[12],
  329. 'role_name': _[13],
  330. 'payway': _[14],
  331. 'status': _[15],
  332. 'user_id': _[16],
  333. }
  334. data_res_dict.append(tmp_dict)
  335. for _ in data_sum_res:
  336. tmp_dict = {'id': _[0],
  337. 'order_id': _[1],
  338. 'pitcher': _[2],
  339. 'agent_name': _[3],
  340. 'channel': _[4],
  341. 'order_time': '',
  342. 'user_create_time': '',
  343. 'mem_id': _[7],
  344. 'game': _[8],
  345. 'amount': float(_[9]),
  346. 'real_amount': float(_[10]),
  347. 'server_name': _[11],
  348. 'system_os': _[12],
  349. 'role_name': _[13],
  350. 'payway': _[14],
  351. 'status': _[15],
  352. 'user_id': _[16],
  353. }
  354. data_sum_res_dict.append(tmp_dict)
  355. data, total, total_data = data_res_dict, data_total, data_sum_res_dict
  356. return data, total, total_data
  357. class OrderH5Info(BaseHandler):
  358. def post(self):
  359. if not self._au:
  360. self.write_fail(msg='auth error')
  361. else:
  362. args = self.get_args()
  363. user_id = args.get('user_id')
  364. # role 表
  365. start = args.get("start", self.thisday)
  366. end = args.get("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. data = self.get_order_info(user_id, start, end)
  372. self.write_json(data=data)
  373. def get_order_info(self, user_id, start, end):
  374. ck = CkUtils()
  375. du = DateUtils()
  376. if user_id not in super_auth():
  377. return []
  378. # todo: 定时跑数据汇总也还没有写好,写好
  379. # start end 转变为Unix_time
  380. end = du.getLastDays(end, -1) if end else None # end 往后面一天
  381. start = None if not start else int(datetime.strptime(start, '%Y-%m-%d').timestamp())
  382. end = None if not end else int(datetime.strptime(end, '%Y-%m-%d').timestamp())
  383. op1 = f" and create_time>={start} " if start else ''
  384. op2 = f" and create_time<={end} " if end else ''
  385. sql = f'''
  386. select a.dt,a.h5_game_name,
  387. b.sum_amount as sum_amount,
  388. b.natural_amount as natural_amount,
  389. b.amount_rate as amount_rate,
  390. b.human_count as human_count,
  391. b.natural_human_count as natural_human_count,
  392. b.human_count_rate as human_count_rate,
  393. a.channel_game_name as channel_game_name,
  394. a.is_natural as is_natural,
  395. toFloat64(a.sum_amount) as channel_amount,
  396. toFloat64(a.human_counts) as channel_human_count
  397. from
  398. (select toDate(FROM_UNIXTIME(a.create_time)) dt,a.app_id as app_id ,
  399. min(b.name) as h5_game_name,
  400. min(c.app_id) as c_app_id,d.name as channel_game_name,
  401. if(min(c.agent_id) in ('sdk_one_0','sdk_two_0'),1,0) as is_natural ,
  402. sum(amount) as sum_amount,count(DISTINCT(a.mem_id)) as human_counts
  403. from (select * from GameDataSum.h_pay_sum where 1=1 {op1} {op2} ) a
  404. LEFT join GameDataSum.h_game_sum b on a.app_id = b.id
  405. left join (select * from GameDataSum.h_member_sum where 1=1 {op1} {op2} ) c on a.mem_id = c.id
  406. LEFT join GameDataSum.h_game_sum d on c.app_id = d.id
  407. where a.app_id in (select id from GameDataSum.h_game_sum
  408. where classify =5)
  409. and a.app_id != c.app_id
  410. and a.status = 2
  411. group by toDate(FROM_UNIXTIME(a.create_time)),a.app_id ,
  412. c.app_id ,d.name,if(c.agent_id in ('sdk_one_0','sdk_two_0'),1,0) ) as a
  413. left join
  414. (select toDate(FROM_UNIXTIME(a.create_time)) as dt,a.app_id ,
  415. sum(a.amount) sum_amount,
  416. sum(if(a.app_id=c.app_id,a.amount,0)) natural_amount,
  417. count(DISTINCT(a.mem_id)) as human_count,
  418. count(DISTINCT(if(a.app_id=c.app_id,a.mem_id,null))) natural_human_count,
  419. ROUND(if(sum(a.amount)>0,sum(if(a.app_id=c.app_id,a.amount,0)) /sum(a.amount),0) ,2) amount_rate,
  420. 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
  421. from (select * from GameDataSum.h_pay_sum where 1=1 {op1} {op2} ) a
  422. left join (select * from GameDataSum.h_member_sum where 1=1 {op1} {op2} ) c on a.mem_id = c.id
  423. where a.app_id in (select id from GameDataSum.h_game_sum
  424. where classify =5)
  425. and a.status=2
  426. group by toDate(FROM_UNIXTIME(a.create_time)),a.app_id ) b
  427. on a.dt=b.dt and a.app_id = b.app_id
  428. order by a.dt desc,a.app_id,c_app_id
  429. '''
  430. print(sql)
  431. df_source = ck.execute(sql)
  432. game_info = {}
  433. for _ in df_source:
  434. _ = list(_)
  435. _[0] = datetime.strftime(_[0], '%Y-%m-%d')
  436. _[2] = float(_[2])
  437. _[3] = float(_[3])
  438. _[4] = float(_[4])
  439. _[7] = float(_[7])
  440. _[10] = float(_[10])
  441. h_game = (_[0], _[1], _[2], _[3], _[4], _[5], _[6], _[7])
  442. if h_game in game_info.keys():
  443. game_info[h_game].append(_)
  444. else:
  445. game_info[h_game] = [_]
  446. res_game_info = []
  447. for k, v in game_info.items():
  448. # print(k, v)
  449. channel_game_list_natural = []
  450. channel_game_list_unnatural = []
  451. for _ in v:
  452. if _[9]:
  453. channel_game_list_natural.append({'channel_game_name': _[8],
  454. 'channel_game_amount': _[10],
  455. 'channel_game_human_count': _[11]})
  456. else:
  457. channel_game_list_unnatural.append({'channel_game_name': _[8],
  458. 'channel_game_amount': _[10],
  459. 'channel_game_human_count': _[11]})
  460. if len(channel_game_list_natural) != len(channel_game_list_unnatural):
  461. game_name_natural = set()
  462. game_name_unnatural = set()
  463. for _ in channel_game_list_natural:
  464. game_name_natural.add(_['channel_game_name'])
  465. for _ in channel_game_list_unnatural:
  466. game_name_unnatural.add(_['channel_game_name'])
  467. for channel_game_name in game_name_unnatural - game_name_natural:
  468. channel_game_list_natural.append({'channel_game_name': channel_game_name,
  469. 'channel_game_amount': 0,
  470. 'channel_game_human_count': 0})
  471. for channel_game_name in game_name_natural - game_name_unnatural:
  472. channel_game_list_unnatural.append({'channel_game_name': channel_game_name,
  473. 'channel_game_amount': 0,
  474. 'channel_game_human_count': 0})
  475. channel_game_list_unnatural.sort(key=lambda x: str(x['channel_game_name']))
  476. channel_game_list_natural.sort(key=lambda x: str(x['channel_game_name']))
  477. tmp = {'dt': k[0], 'game_name': k[1], 'sum_amount': k[2], 'natural_amount': k[3],
  478. 'natural_amount_rate': k[4], 'human_count': k[5], 'natural_human_count': k[6],
  479. 'natural_human_count_rate': k[7], 'channel_game_natural_list': channel_game_list_natural,
  480. 'channel_game_unnatural_list': channel_game_list_unnatural
  481. }
  482. res_game_info.append(tmp)
  483. return res_game_info
  484. def get_order_info_download(self, user_id, start, end):
  485. if user_id not in super_auth():
  486. return []
  487. ck = CkUtils()
  488. du = DateUtils()
  489. # start end 转变为Unix_time
  490. end = du.getLastDays(end, -1) if end else None # end 往后面一天
  491. start = None if not start else int(datetime.strptime(start, '%Y-%m-%d').timestamp())
  492. end = None if not end else int(datetime.strptime(end, '%Y-%m-%d').timestamp())
  493. op1 = f" and create_time>={start} " if start else ''
  494. op2 = f" and create_time<={end} " if end else ''
  495. sql = f'''
  496. select a.dt dt, --日期
  497. a.h5_game_name h5_game_name, --游戏名称H5,
  498. b.sum_amount sum_amount_today,--as 今日总充值,
  499. b.natural_amount natural_amount,-- as H5自然量充值,
  500. b.amount_rate amount_rate, -- as 充值额比例,
  501. b.human_count human_count, -- as 今日总充值人数,
  502. b.natural_human_count natural_human_count, -- H5自然量人数,
  503. b.human_count_rate human_count_rate, -- as 用户比例,
  504. a.channel_game_name channel_game_name, -- as 游戏名称小程序,
  505. a.is_natural is_natural, -- as 小程序的量是否为自然量,
  506. a.sum_amount sum_amount_small_app, -- as 小程序导入用户充值,
  507. a.human_counts human_counts -- as 小程序导入人数
  508. from
  509. (select toDate(FROM_UNIXTIME(a.create_time)) dt,a.app_id as app_id ,
  510. min(b.name) as h5_game_name,
  511. min(c.app_id) as c_app_id,d.name as channel_game_name,
  512. if(min(c.agent_id) in ('sdk_one_0','sdk_two_0'),1,0) as is_natural ,
  513. sum(amount) as sum_amount,count(DISTINCT(a.mem_id)) as human_counts
  514. from (select * from GameDataSum.h_pay_sum where 1=1 {op1} {op2} ) a
  515. LEFT join GameDataSum.h_game_sum b on a.app_id = b.id
  516. left join (select * from GameDataSum.h_member_sum where 1=1 {op1} {op2} ) c on a.mem_id = c.id
  517. LEFT join GameDataSum.h_game_sum d on c.app_id = d.id
  518. where a.app_id in (select id from GameDataSum.h_game_sum
  519. where classify =5)
  520. and a.app_id != c.app_id
  521. and a.status = 2
  522. group by toDate(FROM_UNIXTIME(a.create_time)),a.app_id ,
  523. c.app_id ,d.name,if(c.agent_id in ('sdk_one_0','sdk_two_0'),1,0) ) as a
  524. left join
  525. (select toDate(FROM_UNIXTIME(a.create_time)) as dt,a.app_id ,
  526. sum(a.amount) sum_amount,
  527. sum(if(a.app_id=c.app_id,a.amount,0)) natural_amount,
  528. count(DISTINCT(a.mem_id)) as human_count,
  529. count(DISTINCT(if(a.app_id=c.app_id,a.mem_id,null))) natural_human_count,
  530. ROUND(if(sum(a.amount)>0,sum(if(a.app_id=c.app_id,a.amount,0)) /sum(a.amount),0) ,2) amount_rate,
  531. 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
  532. from (select * from GameDataSum.h_pay_sum where 1=1 {op1} {op2} ) a
  533. left join (select * from GameDataSum.h_member_sum where 1=1 {op1} {op2} ) c on a.mem_id = c.id
  534. where a.app_id in (select id from GameDataSum.h_game_sum
  535. where classify =5)
  536. and a.status=2
  537. group by toDate(FROM_UNIXTIME(a.create_time)),a.app_id ) b
  538. on a.dt=b.dt and a.app_id = b.app_id
  539. order by a.dt desc,a.app_id,c_app_id
  540. '''
  541. print(sql)
  542. data = ck.execute(sql)
  543. data_res = []
  544. for _ in data:
  545. tmp = {}
  546. tmp['日期'] = _[0]
  547. tmp['游戏名称H5'] = _[1]
  548. tmp['今日总充值'] = _[2]
  549. tmp['H5自然量充值'] = _[3]
  550. tmp['充值额比例'] = _[4]
  551. tmp['今日总充值人数'] = _[5]
  552. tmp['H5自然量人数'] = _[6]
  553. tmp['用户比例'] = _[7]
  554. tmp['游戏名称小程序'] = _[8]
  555. tmp['小程序的量是否为自然量'] = _[9]
  556. tmp['小程序导入用户充值'] = _[10]
  557. tmp['小程序导入人数'] = _[11]
  558. data_res.append(tmp)
  559. return data_res
  560. class OrderH5InfoSpecial(BaseHandler):
  561. def post(self):
  562. if not self._au:
  563. self.write_fail(msg='auth error')
  564. else:
  565. args = self.get_args()
  566. user_id = args.get('user_id')
  567. # role 表
  568. start = args.get("start", self.thisday)
  569. end = args.get("end")
  570. data = []
  571. if args.get("download"):
  572. data = self.get_order_info_download(user_id, start, end)
  573. self.write_download(str(int(time.time())), data)
  574. else:
  575. data = self.get_order_info_download(user_id, start, end)
  576. self.write_json(data=data)
  577. def get_order_info_download(self, user_id, start, end):
  578. if user_id not in super_auth():
  579. return []
  580. ck = CkUtils()
  581. du = DateUtils()
  582. end = du.getLastDays(end, -1) if end else None # end 往后面一天
  583. start = None if not start else int(datetime.strptime(start, '%Y-%m-%d').timestamp())
  584. end = None if not end else int(datetime.strptime(end, '%Y-%m-%d').timestamp())
  585. op1 = f" and create_time>={start} " if start else ''
  586. op2 = f" and create_time<={end} " if end else ''
  587. sql = f'''
  588. select a.order_id ,h.pitcher,
  589. if(a.agent_id in ('sdk_one_21','sdk_two_21'),
  590. '客服渠道',if(a.agent_id in ('sdk_one_0','sdk_two_0'),'自然流量',q.user_nicename)) as agent_name,
  591. if(a.agent_id in ('sdk_one_21','sdk_two_21'),
  592. '客服渠道',if(a.agent_id in ('sdk_one_0','sdk_two_0'),'自然流量',c.wx_name)) as channel,
  593. formatDateTime(FROM_UNIXTIME(a.create_time),'%Y-%m-%d %H:%M:%S') order_time,
  594. formatDateTime(FROM_UNIXTIME(d.create_time),'%Y-%m-%d %H:%M:%S') user_create_time,
  595. e2.name as source_game,
  596. d.id as mem_id,e.name as game ,a.amount ,a.real_amount ,g.server_name ,f.os as system_os,
  597. g.role_name as role_name,a.payway ,a.status status ,a.mg_mem_id as user_id
  598. from (select * from GameDataSum.h_pay_sum where 1=1 {op1} {op2} ) a
  599. left join
  600. (select b.agent_id as agent_id,b.advertiser_conf_id from
  601. (select agent_id,max(update_time) as update_time
  602. from GameDataSum.mp_conf_agent_sum mmc
  603. group by agent_id ) a
  604. left join GameDataSum.mp_conf_agent_sum b on a.agent_id=b.agent_id
  605. and a.update_time = b.update_time) b on a.agent_id =b.agent_id
  606. left join GameDataSum.mp_mp_conf_sum c on b.advertiser_conf_id = c.id
  607. left join (select * from GameDataSum.h_member_sum where 1=1 {op1} {op2} ) d
  608. on a.mem_id = d.id
  609. left join GameDataSum.h_game_sum e on a.app_id =e.id
  610. left join GameDataSum.h_game_sum e2 on d.app_id =e2.id
  611. left join GameDataSum.h_pay_ext_sum f on a.id = f.pay_id
  612. left join (select * from GameDataSum.h_mg_role_sum where 1=1 {op1} {op2} ) g
  613. on g.role_id = f.role_id and g.server_id = f.server_id
  614. and g.app_id = a.app_id and g.mg_mem_id =a.mg_mem_id
  615. left join CostSourceData.advertiser_vx h on c.wx_name = h.name
  616. left join GameDataSum.h_user_sum q on a.agent_id =q.id
  617. where
  618. a.app_id not in('sdk_one_6','sdk_one_13','sdk_one_23',
  619. 'sdk_two_6','sdk_two_13','sdk_two_23')
  620. and a.agent_id in( 'sdk_two_0','sdk_one_0')
  621. and d.app_id in('sdk_one_6','sdk_one_13','sdk_one_23',
  622. 'sdk_two_6','sdk_two_13','sdk_two_23')
  623. and a.status =2
  624. '''
  625. print(sql)
  626. data = ck.execute(sql)
  627. return data