GameWinLoseRankController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Facade\TableName;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\DB;
  6. class GameWinLoseRankController extends BaseController
  7. {
  8. /**
  9. * 游戏输赢排行榜列表页面
  10. */
  11. public function index(Request $request)
  12. {
  13. $gameId = $request->get('game_id', '');
  14. $date = $request->get('date', date('Y-m-d'));
  15. $type = $request->get('type', 'win'); // win(赢100) or lose(输100)
  16. // 获取游戏列表
  17. $games = config('games.gameName');
  18. // 获取输赢数据
  19. $rankData = [];
  20. if (!empty($gameId) && !empty($date)) {
  21. $rankData = $this->getWinLoseRank($gameId, $date, $type);
  22. }
  23. return view('admin.game_winlose_rank.index', compact('games', 'gameId', 'date', 'type', 'rankData'));
  24. }
  25. /**
  26. * 获取游戏输赢排行榜数据
  27. */
  28. private function getWinLoseRank($gameId, $date, $type)
  29. {
  30. // 获取游戏对应的服务器ID
  31. $serverIds = DB::connection('read')
  32. ->table(TableName::QPPlatformDB() . 'GameRoomInfo')
  33. ->where('GameID', $gameId)
  34. ->pluck('ServerID')
  35. ->toArray();
  36. if (empty($serverIds)) {
  37. return [];
  38. }
  39. // 计算月份表名
  40. $monthTable = 'YN_RecordScoreInfo_' . date('Ym', strtotime($date));
  41. // 构建查询条件
  42. $startTime = $date . ' 00:00:00';
  43. $endTime = $date . ' 23:59:59';
  44. // 根据类型设置排序和限制条件
  45. $orderBy = $type === 'win' ? 'total_change DESC' : 'total_change ASC';
  46. $havingCondition = $type === 'win' ? 'SUM(r.ChangeScore) >= 10000' : 'SUM(r.ChangeScore) <= -10000'; // 100元 = 10000分
  47. try {
  48. // 查询输赢数据并关联用户信息
  49. $query = DB::connection('read')
  50. ->table(TableName::QPTreasureDB() . $monthTable . ' as r')
  51. ->join(TableName::QPAccountsDB() . 'AccountsInfo as ai', 'r.UserID', '=', 'ai.UserID')
  52. ->whereIn('r.ServerID', $serverIds)
  53. ->whereBetween('r.UpdateTime', [$startTime, $endTime])
  54. ->select([
  55. 'r.UserID',
  56. 'ai.GameID',
  57. 'ai.NickName',
  58. 'ai.Channel',
  59. 'ai.RegisterDate',
  60. 'ai.LastLogonDate',
  61. DB::raw('SUM(r.ChangeScore) as total_change')
  62. ])
  63. ->groupBy([
  64. 'r.UserID',
  65. 'ai.GameID',
  66. 'ai.NickName',
  67. 'ai.Channel',
  68. 'ai.RegisterDate',
  69. 'ai.LastLogonDate'
  70. ])
  71. ->havingRaw($havingCondition)
  72. ->orderByRaw($orderBy)
  73. ->limit(100);
  74. $results = $query->get();
  75. // 转换数据格式
  76. $rankData = [];
  77. foreach ($results as $index => $item) {
  78. $rankData[] = [
  79. 'rank' => $index + 1,
  80. 'user_id' => $item->UserID,
  81. 'game_id' => $item->GameID,
  82. 'nick_name' => $item->NickName,
  83. 'channel' => $item->Channel,
  84. 'register_date' => $item->RegisterDate ? date('Y-m-d H:i:s', strtotime($item->RegisterDate)) : '',
  85. 'last_logon_date' => $item->LastLogonDate ? date('Y-m-d H:i:s', strtotime($item->LastLogonDate)) : '',
  86. 'total_amount' => number_format($item->total_change / 100, 2), // 转换为元
  87. 'raw_amount' => $item->total_change
  88. ];
  89. }
  90. return $rankData;
  91. } catch (\Exception $e) {
  92. // 记录错误日志
  93. \Log::error('GameWinLoseRank query error: ' . $e->getMessage());
  94. return [];
  95. }
  96. }
  97. }