MerchantStatisModel.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model;
  4. use think\Model;
  5. use think\facade\Db;
  6. use app\model\MerchantsUserModel;
  7. /**
  8. * 商户数据统计
  9. */
  10. class MerchantStatisModel extends Model
  11. {
  12. protected $name = 'game_bet_game';
  13. // 连接到fortue_tiger数据库
  14. protected $connection = 'fortue_tiger';
  15. /**
  16. * 获取商户每日数据列表
  17. * @param int $merchantId 商户ID
  18. * @param int $page 页码
  19. * @param int $limit 每页数量
  20. * @param array $filters 过滤条件
  21. */
  22. public static function getMerchantDailyList($merchantId, $page = 1, $limit = 20, $filters = [])
  23. {
  24. $where = [
  25. ['app_id', '=', $merchantId]
  26. ];
  27. // 时间筛选
  28. if (!empty($filters['start_time'])) {
  29. $startTime = strtotime($filters['start_time']);
  30. $where[] = ['create_time', '>=', $startTime];
  31. }
  32. if (!empty($filters['end_time'])) {
  33. $endTime = strtotime($filters['end_time']);
  34. $where[] = ['create_time', '<=', $endTime];
  35. }
  36. $query = self::where($where);
  37. // 统计总数
  38. $total = $query->count();
  39. // 获取列表数据
  40. $list = $query->field([
  41. "FROM_UNIXTIME(create_time, '%Y-%m-%d') as date", // 日期
  42. 'AVG(rtp) AS rtp', // 平均RTP
  43. 'COUNT(DISTINCT uname) as bet_users', // 投注用户数
  44. 'COUNT(id) as bet_count', // 注单数
  45. 'SUM(bet) as bet_amount', // 注单金额(下注分数)
  46. 'SUM(total_win_amount) as game_profit', // 游戏输赢(金额变化)
  47. 'COUNT(DISTINCT game_id) as game_count', // 游戏数量
  48. ])
  49. ->order('date', 'desc')
  50. ->group('date')
  51. ->page($page, $limit)
  52. ->select()
  53. ->toArray();
  54. // 获取登录用户、注册用户
  55. $merchantsUserStatic = MerchantsUserModel::getPlayerStatistics($merchantId, $filters);
  56. foreach ($list as &$row)
  57. {
  58. $row['login_users'] = $merchantsUserStatic[$row['date']]['login_users'] ?? 0;
  59. $row['register_users'] = $merchantsUserStatic[$row['date']]['register_users'] ?? 0;
  60. }
  61. return [
  62. 'list' => $list,
  63. 'total' => $total,
  64. 'page' => $page,
  65. 'limit' => $limit
  66. ];
  67. }
  68. /**
  69. * 获取商户汇总统计
  70. */
  71. public static function getMerchantHistory($merchantId, $filters = [])
  72. {
  73. $where = [
  74. ['app_id', '=', $merchantId]
  75. ];
  76. $getWhere = function(string $timeField) use ($where, $filters) {
  77. // 时间筛选
  78. if (!empty($filters['start_time'])) {
  79. $startTime = strtotime($filters['start_time']);
  80. $where[] = [$timeField, '>=', $startTime];
  81. }
  82. if (!empty($filters['end_time'])) {
  83. $endTime = strtotime($filters['end_time']);
  84. $where[] = [$timeField, '<=', $endTime];
  85. }
  86. return $where;
  87. };
  88. $query = self::where($getWhere('create_time'));
  89. // 获取列表数据
  90. $data = $query->field([
  91. 'AVG(rtp) AS rtp', // 平均RTP
  92. 'COUNT(DISTINCT uname) as bet_users', // 投注用户数
  93. 'COUNT(id) as bet_count', // 注单数
  94. 'SUM(bet) as bet_amount', // 注单金额(下注分数)
  95. 'SUM(total_win_amount) as game_profit', // 游戏输赢(金额变化)
  96. 'COUNT(DISTINCT game_id) as game_count', // 游戏数量
  97. ])
  98. ->find()
  99. ->toArray();
  100. // 获取登录用户、注册用户
  101. $data['login_users'] = MerchantsUserModel::where($getWhere('login_time'))->count();
  102. $data['register_users'] = MerchantsUserModel::where($getWhere('create_time'))->count();
  103. return $data;
  104. }
  105. }