GameStatis.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\controller;
  4. use app\BaseController;
  5. use app\model\GameBetGameModel;
  6. use app\model\GameBetOrderModel;
  7. use app\model\GameStatisModel;
  8. use app\common\CommonUtils;
  9. use app\model\GameModel;
  10. use think\facade\Request;
  11. class GameStatis extends BaseController
  12. {
  13. /**
  14. * 获取游戏每日数据列表
  15. */
  16. public function Daily()
  17. {
  18. $userInfo = $this->request->userInfo;
  19. // 获取查询参数
  20. $page = Request::get('page', 1, 'intval');
  21. $limit = Request::get('limit', 10, 'intval');
  22. // 筛选条件
  23. $filters = [
  24. // 时间筛选
  25. 'start_time' => Request::get('start_time', date('Y-m-d', strtotime('-7 days')), 'trim'),
  26. 'end_time' => Request::get('end_time', date('Y-m-d'), 'trim'),
  27. 'game_id' => Request::get('game_id', '', 'trim'),
  28. ];
  29. try {
  30. // 获取游戏每日数据
  31. $result = GameStatisModel::getGameDailyList(
  32. $userInfo['merchant_id'],
  33. $page,
  34. $limit,
  35. $filters,
  36. );
  37. // 获取游戏信息信息
  38. $game_ids = array_unique(array_column($result['list'], 'game_id'));
  39. $tempGameList = GameModel::where([['merchant_id', '=', $userInfo['merchant_id']], ['game_id', 'in', $game_ids]])
  40. ->field(['game_id','title','image', 'game_platform'])
  41. ->select()
  42. ->toArray();
  43. $gameList = [];
  44. foreach ($tempGameList as $game) {
  45. $game_id = $game['game_id'];
  46. $gameList[$game_id] = $game;
  47. }
  48. // 格式化数据
  49. foreach ($result['list'] as &$item) {
  50. $game_id = $item['game_id'];
  51. $gameInfo = $gameList[$game_id] ?? [];
  52. if (!empty($gameInfo))
  53. {
  54. $gameImages = CommonUtils::getGameDefaultImage($gameInfo);
  55. }
  56. $item['game_image_url'] = $gameImages['image_url'] ?? '';
  57. $item['game_title'] = $gameInfo['title'] ?? '';
  58. $item['game_type_text'] = CommonUtils::getGameTypeConfig($gameInfo['game_platform']);
  59. $item['game_profit'] = CommonUtils::convertBalance($item['game_profit'], false);
  60. $item['bet_amount'] = CommonUtils::convertBalance($item['bet_amount'], false);
  61. $item['commission_amount'] = CommonUtils::convertBalance($item['game_profit'] * 0.08, false);
  62. $item['platform_fee'] = CommonUtils::convertBalance($item['bet_amount'] * 0.08, false);
  63. $item['buy_free_bet'] = intval($item['buy_free_bet_count'] ?? 0);
  64. }
  65. return json_success($result, '获取成功');
  66. } catch (\Exception $e) {
  67. return json_error([], '获取游戏每日数据失败:' . $e->getMessage());
  68. }
  69. }
  70. /**
  71. * 游戏输赢汇总
  72. */
  73. public function Summary()
  74. {
  75. $userInfo = $this->request->userInfo;
  76. // 获取查询参数
  77. $page = Request::get('page', 1, 'intval');
  78. $limit = Request::get('limit', 10, 'intval');
  79. // 筛选条件
  80. $filters = [
  81. 'start_time' => Request::get('start_time', date('Y-m-d'), 'trim'),
  82. 'end_time' => Request::get('end_time', date('Y-m-d'), 'trim'),
  83. 'game_id' => Request::get('game_id', '', 'trim'),
  84. ];
  85. try {
  86. // 获取游戏汇总数据
  87. $result = GameStatisModel::getGameSummary(
  88. $userInfo['merchant_id'],
  89. $page,
  90. $limit,
  91. $filters
  92. );
  93. // 获取游戏信息
  94. if (!empty($result['list'])) {
  95. $game_ids = array_unique(array_column($result['list'], 'game_id'));
  96. $tempGameList = GameModel::where([
  97. ['merchant_id', '=', $userInfo['merchant_id']],
  98. ['game_id', 'in', $game_ids]
  99. ])
  100. ->field(['game_id','title','image', 'game_platform', 'rtp'])
  101. ->select()
  102. ->toArray();
  103. $gameList = [];
  104. foreach ($tempGameList as $game) {
  105. $game_id = $game['game_id'];
  106. $gameList[$game_id] = $game;
  107. }
  108. // 格式化数据
  109. foreach ($result['list'] as &$item) {
  110. $game_id = $item['game_id'];
  111. $gameInfo = $gameList[$game_id] ?? [];
  112. // 游戏信息
  113. $item['game_title'] = $gameInfo['title'] ?? '未知游戏';
  114. $item['game_platform'] = CommonUtils::getGameTypeConfig($gameInfo['game_platform']);
  115. $item['rtp'] = $gameInfo['rtp'] ?? '';
  116. // 计算游戏RTP
  117. $item['game_rtp'] = calculateRTP($item['total_score'], $item['total_bet']);
  118. // 格式化数据
  119. $item['game_id'] = (int) $item['game_id'];
  120. $item['buy_game_amount'] = (int) $item['buy_game_amount'];
  121. $item['total_bet'] = CommonUtils::convertBalance($item['total_bet'], false);
  122. $item['total_win'] = CommonUtils::convertBalance($item['total_win'], false);
  123. $item['total_score'] = CommonUtils::convertBalance($item['total_score'], false);
  124. // 计算历史游戏RTP
  125. if (isset($result['history'][$game_id])) {
  126. $history = $result['history'][$game_id];
  127. $item['history_rtp'] = calculateRTP($history['total_score'], $history['total_bet']);
  128. } else {
  129. $item['history_rtp'] = 0;
  130. }
  131. }
  132. }
  133. // 计算汇总数据
  134. $summary = [
  135. 'total_bet' => CommonUtils::convertBalance($result['summary']['total_bet'], false),
  136. 'total_bet_count' => $result['summary']['total_bet_count'],
  137. 'total_win' => CommonUtils::convertBalance($result['summary']['total_win'], false),
  138. 'total_user_count' => $result['summary']['total_user_count'],
  139. ];
  140. return json_success([
  141. 'list' => $result['list'],
  142. 'total' => $result['total'],
  143. 'page' => $page,
  144. 'limit' => $limit,
  145. 'summary' => $summary
  146. ], '获取成功');
  147. } catch (\Exception $e) {
  148. echo $e->getTraceAsString();
  149. return json_error([], '获取游戏汇总数据失败:' . $e->getMessage());
  150. }
  151. }
  152. /**
  153. * 档位押注数据
  154. */
  155. public function BetLevel()
  156. {
  157. $userInfo = $this->request->userInfo;
  158. // 获取查询参数
  159. $page = Request::get('page', 1, 'intval');
  160. $limit = Request::get('limit', 10, 'intval');
  161. // 筛选条件
  162. $filters = [
  163. 'start_time' => Request::get('start_time', date('Y-m-d'), 'trim'),
  164. 'end_time' => Request::get('end_time', date('Y-m-d'), 'trim'),
  165. 'game_id' => Request::get('game_id', '', 'trim'),
  166. ];
  167. try {
  168. // 获取档位押注数据
  169. $result = GameStatisModel::getBetLevelData(
  170. $userInfo['merchant_id'],
  171. $page,
  172. $limit,
  173. $filters
  174. );
  175. // 获取游戏信息
  176. if (!empty($result['list'])) {
  177. $game_ids = array_unique(array_column($result['list'], 'game_id'));
  178. $tempGameList = GameModel::where([
  179. ['merchant_id', '=', $userInfo['merchant_id']],
  180. ['game_id', 'in', $game_ids]
  181. ])
  182. ->field(['game_id','title', 'game_platform'])
  183. ->select()
  184. ->toArray();
  185. $gameList = [];
  186. foreach ($tempGameList as $game) {
  187. $game_id = $game['game_id'];
  188. $gameList[$game_id] = $game;
  189. }
  190. // 格式化数据
  191. foreach ($result['list'] as &$item) {
  192. $game_id = $item['game_id'];
  193. $gameInfo = $gameList[$game_id] ?? [];
  194. // 游戏信息
  195. $item['game_title'] = $gameInfo['title'] ?? '未知游戏';
  196. $item['game_platform'] = CommonUtils::getGameTypeConfig($gameInfo['game_platform']);
  197. // 游戏RTP
  198. $item['game_rtp'] = calculateRTP($item['total_score'], $item['total_bet']);
  199. $item['bet'] = CommonUtils::convertBalance($item['bet'], false);
  200. $item['total_bet_rate'] = bcmul(bcdiv($item['total_bet'], $result['total_stats']['total_bet_all'], 4), "100", 2);
  201. $item['total_bet'] = CommonUtils::convertBalance($item['total_bet'], false);
  202. $item['bet_count_rate'] = bcmul(bcdiv((string)$item['bet_count'], (string)$result['total_stats']['total_count_all'], 4), "100", 2);
  203. $item['bet_count'] = ($item['bet_count']);
  204. // 游戏输赢
  205. $item['total_win'] = CommonUtils::convertBalance($item['total_win'], false);
  206. // 游戏返奖
  207. $item['total_score'] = CommonUtils::convertBalance($item['total_score'], false);
  208. }
  209. }
  210. return json_success([
  211. 'list' => $result['list'],
  212. 'total' => $result['total'],
  213. 'page' => $page,
  214. 'limit' => $limit
  215. ], '获取成功');
  216. } catch (\Exception $e) {
  217. return json_error([], '获取档位押注数据失败:' . $e->getMessage());
  218. }
  219. }
  220. }