OrderHandler.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. order_id = args.get('order_id')
  148. member_id = args.get('member_id')
  149. status = (args.get('status'))
  150. game = (args.get('game'))
  151. agent_name = (args.get('agent_name'))
  152. page = args.get("page", 1)
  153. page_size = args.get("page_size", 20)
  154. order_by = args.get("order_by", 'order_date')
  155. order_ = args.get("order", 'desc')
  156. data, total, total_data = self.get_order_info(user_id, channel, pitcher, role_name, start, end,
  157. user_end, user_start, order_id, member_id, status,
  158. game, agent_name,
  159. page, page_size, order_, order_by)
  160. if args.get("download"):
  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. 1, 100000000, order_, order_by)
  165. self.write_download(str(int(time.time())), data)
  166. else:
  167. self.write_json(data=data, total=total, total_data=total_data)
  168. def get_order_info(self, user_id, channel, pitcher, role_name, start, end,
  169. user_end, user_start, order_id, member_id, status,
  170. game, agent_name,
  171. page, page_size, order_, order_by):
  172. db = MysqlUtils()
  173. if user_id in super_auth():
  174. op_channel = ''
  175. op_game = ''
  176. else:
  177. op_game = ''
  178. op_base = f' and if( h.start_date is not null,d.create_time >= UNIX_TIMESTAMP( h.start_date),1) '
  179. # 限制为自己游戏的1.自然流量,2.客服流量
  180. if agent_name and ('自然流量渠道' in agent_name or '客服渠道' in agent_name):
  181. game_ids = get_auth_game_info(user_id)
  182. print(game_ids)
  183. op_game = ''
  184. for game_id, game_name, game_timestamp in game_ids:
  185. op_game = op_game + f'''or (e.id = {game_id} and a.create_time >UNIX_TIMESTAMP('{game_timestamp}') ) '''
  186. op_game = ' and ' + '(' + op_game[2:] + ')' + op_base
  187. # 限制为自己公众号开始时候的数据
  188. if len(UserAuthUtils.get_auth_channel(user_id)) == 0:
  189. return None, None, None
  190. elif len(UserAuthUtils.get_auth_channel(user_id)) == 1:
  191. op_channel = f''' and c.wx_name = '{UserAuthUtils.get_auth_channel(user_id)[0]}'
  192. ''' + op_base
  193. else:
  194. op_channel = f'''
  195. and c.wx_name in {str(UserAuthUtils.get_auth_channel(user_id))}
  196. ''' + op_base
  197. op = op_game if agent_name and ('自然流量渠道' in agent_name or '客服渠道' in agent_name) else op_channel
  198. op1 = f" and h.pitcher ='{pitcher}' " if pitcher else ''
  199. op2 = f''' and c.wx_name in ({"'" + "','".join(channel) + "'"})''' if channel else ''
  200. op3 = f" and from_unixtime(a.create_time) >'{start}' " if start else ''
  201. op4 = f" and from_unixtime(a.create_time) <'{end}' " if end else ''
  202. op5 = f" and CONVERT (g.role_name USING utf8)='{role_name}' " if role_name else ''
  203. op6 = f" and from_unixtime(d.create_time) >'{user_start}' " if user_start else ''
  204. op7 = f" and from_unixtime(d.create_time) <'{user_end}' " if user_end else ''
  205. op8 = f" and a.order_id='{order_id}'" if order_id else ''
  206. op9 = f" and a.mem_id='{member_id}'" if member_id else ''
  207. op10 = f" and a.status={status} " if status else ''
  208. op11 = f''' and e.name in ({"'" + "','".join(game) + "'"}) ''' if game else ''
  209. # 有自然流量和客服渠道时,其他渠道进行规避
  210. if agent_name and '自然流量渠道' in agent_name and '客服渠道' in agent_name:
  211. op12 = f" and a.agent_id in (12,0) "
  212. elif agent_name and '自然流量渠道' in agent_name:
  213. op12 = f" and a.agent_id=0 "
  214. elif agent_name and '客服渠道' in agent_name:
  215. op12 = f" and a.agent_id=12 "
  216. else:
  217. op12 = f''' and q.user_nicename in ({"'" + "','".join(agent_name) + "'"}) ''' if agent_name else ''
  218. # 选项:order_date:订单时间,user_date:用户时间,amount:金额,game:游戏名字,payway:支付方式,agent_name:渠道
  219. if order_by == 'agent_name':
  220. order_by = 'q.user_nicename'
  221. elif order_by == 'user_date':
  222. order_by = 'd.create_time'
  223. elif order_by == 'amount':
  224. order_by = 'amount'
  225. elif order_by == 'game':
  226. order_by = 'e.name'
  227. elif order_by == 'payway':
  228. order_by = 'a.payway'
  229. else:
  230. order_by = 'a.create_time'
  231. op15 = f" order by {order_by} {order_}" if order_by and order_ else ''
  232. sql = f"""select row_number() over() as id,a.order_id ,h.pitcher,
  233. if(a.agent_id =21,'客服渠道',if(a.agent_id=0,'自然流量',q.user_nicename)) as agent_name,
  234. if(a.agent_id =21,'客服渠道',if(a.agent_id=0,'自然流量',c.wx_name)) as channel,
  235. if(a.create_time is not null ,CONVERT(from_unixtime(a.create_time),char(20)),null) order_time,
  236. if(d.create_time is not null,CONVERT(from_unixtime(d.create_time),char(20)),null) user_create_time,
  237. d.id as mem_id,e.name as game ,a.amount ,a.real_amount ,g.server_name ,f.os as system_os,
  238. CONVERT (g.role_name USING utf8) as role_name,a.payway ,a.status status ,a.mg_mem_id as user_id
  239. from db_mp.h_pay a
  240. left join
  241. (select b.agent_id as agent_id,b.advertiser_conf_id from
  242. (select agent_id,max(update_time) as update_time
  243. from db_mp.mp_conf_agent mmc
  244. group by agent_id ) a
  245. left join db_mp.mp_conf_agent b on a.agent_id=b.agent_id
  246. and a.update_time = b.update_time) b on a.agent_id =b.agent_id
  247. left join db_mp.mp_mp_conf c on b.advertiser_conf_id = c.id
  248. left join db_mp.h_member d on a.mem_id = d.id
  249. left join db_mp.h_game e on a.app_id =e.id
  250. left join db_mp.h_pay_ext f on a.id = f.pay_id
  251. left join db_mp.h_mg_role g
  252. on g.role_id = f.role_id and g.server_id = f.server_id
  253. and g.app_id = a.app_id and g.mg_mem_id =a.mg_mem_id
  254. left join quchen_text.advertiser_vx h on c.wx_name = h.name
  255. left join db_mp.h_user q on a.agent_id =q.id
  256. where 1=1 {op} {op1} {op2} {op3} {op4} {op5}
  257. {op6} {op7} {op8} {op9} {op10}
  258. {op11} {op12}
  259. {op15}
  260. """
  261. print(sql)
  262. sumsql = f""" select '' as id,'' as order_id ,'' as pitcher,'' as agent_name, '' as channel,
  263. '' as order_time,'' as user_create_time,'' as user_id,
  264. '' as game,sum(amount) as amount ,sum(real_amount) as real_amount ,'' as server_name,
  265. '' as mem_id , '' as role_name,'' as payway ,'' as status ,'' as system_os
  266. from ({sql}) a
  267. """
  268. # print(sumsql)
  269. data, total, total_data = getLimitSumData(db.dm, sql, sumsql, page, page_size)
  270. print(len(data))
  271. return data, total, total_data
  272. def get_game_pitcher(self, user_id):
  273. sql = '''
  274. SELECT d.name as game_name,d.id
  275. FROM quchen_text.advertiser_vx a
  276. left join db_mp.mp_mp_conf b on a.name =b.wx_name
  277. left join db_mp.mp_conf_agent c on c.advertiser_conf_id = b.id
  278. left join db_mp.h_game d on c.app_id = d.id
  279. where pitcher ='金康'
  280. group by id
  281. '''
  282. class OrderH5Info(BaseHandler):
  283. def post(self):
  284. if not self._au:
  285. self.write_fail(msg='auth error')
  286. else:
  287. args = self.get_args()
  288. user_id = args.get('user_id')
  289. # role 表
  290. start = args.get("start", self.thisday)
  291. end = args.get("end")
  292. data = self.get_order_info(user_id, start, end)
  293. if args.get("download"):
  294. self.write_download(str(int(time.time())), data)
  295. else:
  296. self.write_json(data=data)
  297. def get_order_info(self, user_id, start, end):
  298. if user_id not in super_auth():
  299. return []
  300. db = MysqlUtils()
  301. op1 = f"and date(FROM_UNIXTIME(a.create_time))>='{start}' " if start else ''
  302. op2 = f"and date(FROM_UNIXTIME(a.create_time))<='{end}' " if end else ''
  303. sql = f'''
  304. select DATE_FORMAT(a.dt,'%Y-%m-%d'),a.h5_game_name,
  305. b.sum_amount as sum_amount,
  306. b.natural_amount as natural_amount,
  307. b.amount_rate as amount_rate,
  308. b.human_count as human_count,
  309. b.natural_human_count as natural_human_count,
  310. b.human_count_rate as human_count_rate,
  311. a.channel_game_name as channel_game_name,
  312. a.is_natural as is_natural,
  313. a.sum_amount as channel_amount,
  314. a.human_counts as channel_human_count
  315. from
  316. (select date(FROM_UNIXTIME(a.create_time)) dt,a.app_id as app_id ,
  317. b.name as h5_game_name,
  318. c.app_id as c_app_id,d.name as channel_game_name,if(c.agent_id=0,1,0) as is_natural ,
  319. sum(amount) as sum_amount,count(DISTINCT(a.mem_id)) as human_counts
  320. from db_mp.h_pay a
  321. LEFT join db_mp.h_game b on a.app_id = b.id
  322. left join db_mp.h_member c on a.mem_id = c.id
  323. LEFT join db_mp.h_game d on c.app_id = d.id
  324. where a.app_id in (select id from db_mp.h_game
  325. where classify =5)
  326. and a.app_id != c.app_id
  327. and a.status = 2
  328. {op1} {op2}
  329. group by date(FROM_UNIXTIME(a.create_time)),a.app_id ,
  330. c.app_id ,d.name,if(c.agent_id=0,1,0) ) as a
  331. left join
  332. (select date(FROM_UNIXTIME(a.create_time)) as dt,a.app_id ,
  333. sum(a.amount) sum_amount,
  334. sum(if(a.app_id=c.app_id,a.amount,0)) natural_amount,
  335. count(DISTINCT(a.mem_id)) as human_count,
  336. count(DISTINCT(if(a.app_id=c.app_id,a.mem_id,0))) natural_human_count,
  337. ROUND(if(sum(a.amount)>0,sum(if(a.app_id=c.app_id,a.amount,0)) /sum(a.amount),0) ,2) amount_rate,
  338. ROUND( if(count(DISTINCT(a.mem_id))>0,count(DISTINCT(if(a.app_id=c.app_id,a.mem_id,0)))/count(DISTINCT(a.mem_id)),0),2) human_count_rate
  339. from db_mp.h_pay a
  340. left join db_mp.h_member c on a.mem_id = c.id
  341. where a.app_id in (select id from db_mp.h_game
  342. where classify =5)
  343. {op1} {op2}
  344. and a.status=2
  345. group by date(FROM_UNIXTIME(a.create_time)),a.app_id ) b
  346. on a.dt=b.dt and a.app_id = b.app_id
  347. order by a.dt,a.app_id,c_app_id
  348. '''
  349. df_source = db.dm.get_data_list(sql)
  350. game_info = {}
  351. for _ in df_source:
  352. print(_)
  353. _[2] = float(_[2])
  354. _[3] = float(_[3])
  355. _[4] = float(_[4])
  356. _[7] = float(_[7])
  357. _[10] = float(_[10])
  358. h_game = (_[0], _[1], _[2], _[3], _[4], _[5], _[6], _[7])
  359. if h_game in game_info.keys():
  360. game_info[h_game].append(_)
  361. else:
  362. game_info[h_game] = [_]
  363. res_game_info = []
  364. for k, v in game_info.items():
  365. print(k, v)
  366. channel_game_list_natural = []
  367. channel_game_list_unnatural = []
  368. for _ in v:
  369. if _[9]:
  370. channel_game_list_natural.append({'channel_game_name': _[8],
  371. 'channel_game_amount': _[10],
  372. 'channel_game_human_count': _[11]})
  373. else:
  374. channel_game_list_unnatural.append({'channel_game_name': _[8],
  375. 'channel_game_amount': _[10],
  376. 'channel_game_human_count': _[11]})
  377. if len(channel_game_list_natural) != len(channel_game_list_unnatural):
  378. game_name_natural = set()
  379. game_name_unnatural = set()
  380. for _ in channel_game_list_natural:
  381. game_name_natural.add(_['channel_game_name'])
  382. for _ in channel_game_list_unnatural:
  383. game_name_unnatural.add(_['channel_game_name'])
  384. for channel_game_name in game_name_unnatural - game_name_natural:
  385. channel_game_list_natural.append({'channel_game_name': channel_game_name,
  386. 'channel_game_amount': 0,
  387. 'channel_game_human_count': 0})
  388. for channel_game_name in game_name_natural - game_name_unnatural:
  389. channel_game_list_unnatural.append({'channel_game_name': channel_game_name,
  390. 'channel_game_amount': 0,
  391. 'channel_game_human_count': 0})
  392. channel_game_list_unnatural.sort(key=lambda x: str(x['channel_game_name']))
  393. channel_game_list_natural.sort(key=lambda x: str(x['channel_game_name']))
  394. tmp = {'dt': k[0], 'game_name': k[1], 'sum_amount': k[2], 'natural_amount': k[3],
  395. 'natural_amount_rate': k[4], 'human_count': k[5], 'natural_human_count': k[6],
  396. 'natural_human_count_rate': k[7], 'channel_game_natural_list': channel_game_list_natural,
  397. 'channel_game_unnatural_list': channel_game_list_unnatural
  398. }
  399. res_game_info.append(tmp)
  400. return res_game_info