MerchantStatisModel.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. ['action_type', '=', 1],
  27. ];
  28. // 时间筛选
  29. if (!empty($filters['start_time'])) {
  30. $startTime = strtotime($filters['start_time'] . " 00:00:00");
  31. $where[] = ['create_time', '>=', $startTime];
  32. }
  33. if (!empty($filters['end_time'])) {
  34. $endTime = strtotime($filters['end_time']. " 23:59:59");
  35. $where[] = ['create_time', '<=', $endTime];
  36. }
  37. $query = self::where($where);
  38. // 统计总数
  39. $total = $query->count();
  40. // 获取列表数据
  41. $list = $query->field([
  42. "FROM_UNIXTIME(create_time, '%Y-%m-%d') as date", // 日期
  43. 'AVG(rtp) AS rtp', // 平均RTP
  44. 'COUNT(DISTINCT uname) as bet_users', // 投注用户数
  45. 'COUNT(id) as bet_count', // 注单数
  46. 'SUM(bet) as bet_amount', // 注单金额(下注分数)
  47. 'SUM(total_win_amount) as game_profit', // 游戏输赢(金额变化)
  48. 'COUNT(DISTINCT game_id) as game_count', // 游戏数量
  49. 'SUM(is_buy_game) as buy_free_bet_count'
  50. ])
  51. ->order('date', 'desc')
  52. ->group('date')
  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. /**
  71. * 获取商户汇总统计
  72. */
  73. public static function getMerchantHistory($merchantId, $filters = [])
  74. {
  75. $where = [
  76. ['app_id', '=', $merchantId],
  77. ];
  78. $getWhere = function(string $timeField) use ($where, $filters) {
  79. // 时间筛选
  80. if (!empty($filters['start_time'])) {
  81. $startTime = strtotime($filters['start_time'] . " 00:00:00");
  82. $where[] = [$timeField, '>=', $startTime];
  83. }
  84. if (!empty($filters['end_time'])) {
  85. $endTime = strtotime($filters['end_time'] . " 23:59:59");
  86. $where[] = [$timeField, '<=', $endTime];
  87. }
  88. return $where;
  89. };
  90. $tempWhere = $getWhere('create_time');
  91. $tempWhere[] = ['action_type', '=', 1];
  92. $query = self::where($tempWhere);
  93. // 获取列表数据
  94. $data = $query->field([
  95. 'AVG(rtp) AS rtp', // 平均RTP
  96. 'COUNT(DISTINCT uname) as bet_users', // 投注用户数
  97. 'COUNT(id) as bet_count', // 注单数
  98. 'SUM(bet) as bet_amount', // 注单金额(下注分数)
  99. 'SUM(total_win_amount) as game_profit', // 游戏输赢(金额变化)
  100. 'COUNT(DISTINCT game_id) as game_count', // 游戏数量
  101. 'SUM(is_buy_game) as buy_free_bet_count'
  102. ])
  103. ->find()
  104. ->toArray();
  105. // 获取登录用户、注册用户
  106. $data['login_users'] = MerchantsUserModel::where($getWhere('login_time'))->count();
  107. $data['register_users'] = MerchantsUserModel::where($getWhere('create_time'))->count();
  108. return $data;
  109. }
  110. }