GameStatisModel.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model;
  4. use think\Model;
  5. use think\facade\Db;
  6. /**
  7. * 游戏数据统计
  8. */
  9. class GameStatisModel extends Model
  10. {
  11. protected $name = 'game_bet_game';
  12. // 连接到fortue_tiger数据库
  13. protected $connection = 'fortue_tiger';
  14. /**
  15. * 获取游戏每日数据列表
  16. */
  17. public static function getGameDailyList($merchantId, $page = 1, $limit = 20, $filters = [])
  18. {
  19. $where = [
  20. ['app_id', '=', $merchantId]
  21. ];
  22. // 时间筛选
  23. if (!empty($filters['start_time'])) {
  24. $startTime = strtotime($filters['start_time']);
  25. $where[] = ['create_time', '>=', $startTime];
  26. }
  27. if (!empty($filters['end_time'])) {
  28. $endTime = strtotime($filters['end_time']);
  29. $where[] = ['create_time', '<=', $endTime];
  30. }
  31. if (!empty($filters['game_id'])) {
  32. $gameId = is_array($filters['game_id']) ? $filters['game_id'] : explode(',', $filters['game_id']);
  33. $where[] = ['game_id', 'in', $gameId];
  34. }
  35. // 使用子查询获取每个用户的统计数据
  36. $subQuery = self::field([
  37. "FROM_UNIXTIME(create_time, '%Y-%m-%d') as date", // 日期
  38. 'game_id', //游戏id
  39. 'AVG(rtp) AS rtp', // 平均RTP
  40. 'COUNT(DISTINCT uname) as bet_users', // 投注用户数
  41. 'COUNT(id) as bet_count', // 注单数
  42. 'SUM(bet) as bet_amount', // 注单金额(下注分数)
  43. 'SUM(total_win_amount) as game_profit', // 游戏输赢(金额变化)
  44. ])
  45. ->where($where)
  46. ->group('date, game_id')
  47. ->buildSql();
  48. // 获取总数
  49. $total = self::table($subQuery . ' t')->count();
  50. // 获取排行榜数据
  51. $list = self::table($subQuery . ' t')
  52. ->order('date', 'desc')
  53. ->page($page, $limit)
  54. ->select()
  55. ->toArray();
  56. // 获取登录用户、注册用户
  57. $merchantsUserStatic = MerchantsUserModel::getPlayerStatistics($merchantId, $filters);
  58. foreach ($list as &$row)
  59. {
  60. $row['login_users'] = $merchantsUserStatic[$row['date']]['login_users'] ?? 0;
  61. $row['register_users'] = $merchantsUserStatic[$row['date']]['register_users'] ?? 0;
  62. }
  63. return [
  64. 'list' => $list,
  65. 'total' => $total,
  66. 'page' => $page,
  67. 'limit' => $limit
  68. ];
  69. }
  70. }