MerchantStatisModel.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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']);
  31. $where[] = ['create_time', '>=', $startTime];
  32. }
  33. if (!empty($filters['end_time'])) {
  34. $endTime = strtotime($filters['end_time']. " +1 day");
  35. $where[] = ['create_time', '<=', $endTime];
  36. }
  37. // 获取列表数据
  38. $subQuery = self::field([
  39. "FROM_UNIXTIME(create_time, '%Y-%m-%d') as date", // 日期
  40. 'COUNT(DISTINCT uname) as bet_users', // 投注用户数
  41. 'COUNT(id) as bet_count', // 注单数
  42. 'SUM(bet) as bet_amount', // 注单金额(下注分数)
  43. 'SUM(winning_score) as total_winning_score', // 总赢金额
  44. 'SUM(total_win_amount) as game_profit', // 游戏输赢(金额变化)
  45. 'COUNT(DISTINCT game_id) as game_count', // 游戏数量
  46. 'SUM(is_buy_game) as buy_free_bet_count'
  47. ])
  48. ->where($where)
  49. ->group('date')
  50. ->buildSql();
  51. // 统计总数
  52. $total = self::table($subQuery . ' t')->count();
  53. $list = self::table($subQuery . ' t')
  54. ->order('date', 'desc')
  55. ->page($page, $limit)
  56. ->select()
  57. ->toArray();
  58. // 获取登录用户、注册用户
  59. $merchantsUserStatic = MerchantsUserModel::getPlayerStatistics($merchantId, $filters);
  60. foreach ($list as &$row)
  61. {
  62. $row['rtp'] = calculateRTP($row['total_winning_score'], $row['bet_amount']);
  63. $row['login_users'] = $merchantsUserStatic['login'][$row['date']]['count'] ?? 0;
  64. $row['register_users'] = $merchantsUserStatic['register'][$row['date']]['count'] ?? 0;
  65. }
  66. return [
  67. 'list' => $list,
  68. 'total' => $total,
  69. 'page' => $page,
  70. 'limit' => $limit
  71. ];
  72. }
  73. /**
  74. * 获取商户汇总统计
  75. */
  76. public static function getMerchantHistory($merchantId, $filters = [])
  77. {
  78. $where = [
  79. ['app_id', '=', $merchantId],
  80. ];
  81. $getWhere = function(string $timeField) use ($where, $filters) {
  82. // 时间筛选
  83. if (!empty($filters['start_time'])) {
  84. $startTime = strtotime($filters['start_time']);
  85. $where[] = [$timeField, '>=', $startTime];
  86. }
  87. if (!empty($filters['end_time'])) {
  88. $endTime = strtotime($filters['end_time'] . " +1 day");
  89. $where[] = [$timeField, '<=', $endTime];
  90. }
  91. return $where;
  92. };
  93. $tempWhere = $getWhere('create_time');
  94. $tempWhere[] = ['action_type', '=', 1];
  95. $query = self::where($tempWhere);
  96. // 获取列表数据
  97. $data = $query->field([
  98. 'COUNT(DISTINCT uname) as bet_users', // 投注用户数
  99. 'COUNT(id) as bet_count', // 注单数
  100. 'SUM(bet) as bet_amount', // 注单金额(下注分数)
  101. 'SUM(total_win_amount) as game_profit', // 游戏输赢(金额变化)
  102. 'SUM(winning_score) as total_winning_score', // 游戏赢总额
  103. 'COUNT(DISTINCT game_id) as game_count', // 游戏数量
  104. 'SUM(is_buy_game) as buy_free_bet_count'
  105. ])
  106. ->find()
  107. ->toArray();
  108. return $data;
  109. }
  110. }