MerchantDailyModel.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model;
  4. use think\Model;
  5. use think\facade\Db;
  6. /**
  7. * 商户每日数据统计模型 - 基于tp_game_bet_order表实时统计
  8. */
  9. class MerchantDailyModel extends Model
  10. {
  11. // 连接到fortue_tiger数据库
  12. protected $connection = 'fortue_tiger';
  13. /**
  14. * 获取商户每日数据列表
  15. * @param int $merchantId 商户ID
  16. * @param string $startDate 开始日期
  17. * @param string $endDate 结束日期
  18. * @param int $page 页码
  19. * @param int $limit 每页数量
  20. */
  21. public static function getMerchantDailyList($merchantId, $startDate, $endDate, $page = 1, $limit = 10)
  22. {
  23. $startTime = strtotime($startDate . ' 00:00:00');
  24. $endTime = strtotime($endDate . ' 23:59:59');
  25. // 生成日期数组
  26. $dates = [];
  27. $current = $startTime;
  28. while ($current <= $endTime) {
  29. $dates[] = date('Y-m-d', $current);
  30. $current += 86400;
  31. }
  32. // 按日期分组统计
  33. $offset = ($page - 1) * $limit;
  34. $paginatedDates = array_slice($dates, $offset, $limit);
  35. $list = [];
  36. foreach ($paginatedDates as $date) {
  37. $dayStart = strtotime($date . ' 00:00:00');
  38. $dayEnd = strtotime($date . ' 23:59:59');
  39. // 获取游戏下注统计数据
  40. $betStats = Db::connect('fortue_tiger')
  41. ->name('game_bet_order')
  42. ->where('app_id', $merchantId)
  43. ->where('create_time', '>=', $dayStart)
  44. ->where('create_time', '<=', $dayEnd)
  45. ->where('status', 1) // 只统计有效订单
  46. ->field([
  47. 'COUNT(DISTINCT uname) as bet_users', // 投注用户数
  48. 'COUNT(id) as bet_count', // 注单数
  49. 'SUM(bet) as bet_amount', // 注单金额(下注分数)
  50. 'SUM(amount) as game_profit', // 游戏输赢(金额变化)
  51. 'SUM(total_win_amount) as total_win', // 总赢得金额
  52. 'COUNT(DISTINCT game_id) as game_count' // 游戏数量
  53. ])
  54. ->find();
  55. // 计算RTP(Return to Player)
  56. $rtp = 0;
  57. if ($betStats['bet_amount'] > 0) {
  58. // RTP = (返还给玩家的金额 / 投注总额) * 100
  59. $rtp = round(($betStats['total_win'] / $betStats['bet_amount']) * 100, 2);
  60. }
  61. // 获取注册用户数
  62. $registerUsers = Db::connect('fortue_tiger')
  63. ->name('merchants_user')
  64. ->where('app_id', $merchantId)
  65. ->where('create_time', '>=', $dayStart)
  66. ->where('create_time', '<=', $dayEnd)
  67. ->count();
  68. // 获取登录用户数
  69. $loginUsers = Db::connect('fortue_tiger')
  70. ->name('merchants_user')
  71. ->where('app_id', $merchantId)
  72. ->where('login_time', '>=', $dayStart)
  73. ->where('login_time', '<=', $dayEnd)
  74. ->count();
  75. // 计算抽水额度(平台盈利 = 下注金额 - 赢得金额)
  76. $commission = ($betStats['bet_amount'] ?? 0) - ($betStats['total_win'] ?? 0);
  77. // 获取免费游戏购买次数(假设action_type中某个值代表免费游戏)
  78. $freeGameCount = Db::connect('fortue_tiger')
  79. ->name('game_bet_order')
  80. ->where('app_id', $merchantId)
  81. ->where('create_time', '>=', $dayStart)
  82. ->where('create_time', '<=', $dayEnd)
  83. ->where('action_type', 5) // 假设5代表免费游戏
  84. ->count();
  85. // 获取平台费用(假设从转账记录表获取)
  86. $platformFee = 0;
  87. if (Db::connect('fortue_tiger')->query("SHOW TABLES LIKE 'tp_transfer_log'")) {
  88. $platformFee = Db::connect('fortue_tiger')
  89. ->name('transfer_log')
  90. ->where('merchant_id', $merchantId)
  91. ->where('type', 'platform_fee')
  92. ->where('create_time', '>=', $dayStart)
  93. ->where('create_time', '<=', $dayEnd)
  94. ->sum('amount');
  95. }
  96. $list[] = [
  97. 'date' => $date,
  98. 'merchant_id' => $merchantId,
  99. 'game_profit' => abs($betStats['game_profit'] ?? 0) / 100, // 转换为元
  100. 'game_rtp' => $rtp,
  101. 'bet_amount' => ($betStats['bet_amount'] ?? 0) / 100, // 转换为元
  102. 'bet_count' => $betStats['bet_count'] ?? 0,
  103. 'bet_users' => $betStats['bet_users'] ?? 0,
  104. 'register_users' => $registerUsers,
  105. 'login_users' => $loginUsers,
  106. 'commission_amount' => $commission / 100, // 转换为元
  107. 'platform_fee' => $platformFee / 100, // 转换为元
  108. 'free_game_count' => $freeGameCount,
  109. 'game_count' => $betStats['game_count'] ?? 0
  110. ];
  111. }
  112. return [
  113. 'list' => $list,
  114. 'total' => count($dates),
  115. 'page' => $page,
  116. 'limit' => $limit
  117. ];
  118. }
  119. /**
  120. * 获取商户汇总统计
  121. */
  122. public static function getMerchantSummary($merchantId, $startDate, $endDate)
  123. {
  124. $startTime = strtotime($startDate . ' 00:00:00');
  125. $endTime = strtotime($endDate . ' 23:59:59');
  126. // 获取汇总统计数据
  127. $summary = Db::connect('fortue_tiger')
  128. ->name('game_bet_order')
  129. ->where('app_id', $merchantId)
  130. ->where('create_time', '>=', $startTime)
  131. ->where('create_time', '<=', $endTime)
  132. ->where('status', 1)
  133. ->field([
  134. 'COUNT(DISTINCT uname) as total_bet_users',
  135. 'COUNT(id) as total_bet_count',
  136. 'SUM(bet) as total_bet_amount',
  137. 'SUM(amount) as total_game_profit',
  138. 'SUM(total_win_amount) as total_win_amount',
  139. 'COUNT(DISTINCT game_id) as total_games',
  140. 'COUNT(DISTINCT DATE_FORMAT(FROM_UNIXTIME(create_time), "%Y-%m-%d")) as active_days'
  141. ])
  142. ->find();
  143. // 计算平均RTP
  144. $avgRtp = 0;
  145. if ($summary['total_bet_amount'] > 0) {
  146. $avgRtp = round(($summary['total_win_amount'] / $summary['total_bet_amount']) * 100, 2);
  147. }
  148. // 获取总注册用户
  149. $totalRegisterUsers = Db::connect('fortue_tiger')
  150. ->name('merchants_user')
  151. ->where('app_id', $merchantId)
  152. ->where('create_time', '>=', $startTime)
  153. ->where('create_time', '<=', $endTime)
  154. ->count();
  155. // 获取总登录用户
  156. $totalLoginUsers = Db::connect('fortue_tiger')
  157. ->name('merchants_user')
  158. ->where('app_id', $merchantId)
  159. ->where('login_time', '>=', $startTime)
  160. ->where('login_time', '<=', $endTime)
  161. ->count();
  162. return [
  163. 'total_bet_users' => $summary['total_bet_users'] ?? 0,
  164. 'total_bet_count' => $summary['total_bet_count'] ?? 0,
  165. 'total_bet_amount' => ($summary['total_bet_amount'] ?? 0) / 100,
  166. 'total_game_profit' => abs($summary['total_game_profit'] ?? 0) / 100,
  167. 'total_win_amount' => ($summary['total_win_amount'] ?? 0) / 100,
  168. 'avg_rtp' => $avgRtp,
  169. 'total_games' => $summary['total_games'] ?? 0,
  170. 'active_days' => $summary['active_days'] ?? 0,
  171. 'total_register_users' => $totalRegisterUsers,
  172. 'total_login_users' => $totalLoginUsers,
  173. 'total_commission' => (($summary['total_bet_amount'] ?? 0) - ($summary['total_win_amount'] ?? 0)) / 100
  174. ];
  175. }
  176. }