OrderHandler.py 35 KB

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