GameRecordModel.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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_game表
  8. */
  9. class GameRecordModel extends Model
  10. {
  11. protected $name = 'game_bet_game';
  12. protected $connection = 'fortue_tiger';
  13. protected $pk = 'id';
  14. // 动作类型常量
  15. const ACTION_TYPE_BET = 1; // 下注
  16. const ACTION_TYPE_RESULT = 2; // 结算
  17. // 状态常量
  18. const STATUS_PENDING = 0; // 待处理
  19. const STATUS_SUCCESS = 1; // 成功
  20. const STATUS_FAILED = 2; // 失败
  21. // 游戏类型常量
  22. const GAME_TYPE_SLOT = 1; // 老虎机
  23. const GAME_TYPE_TABLE = 2; // 桌面游戏
  24. const GAME_TYPE_LIVE = 3; // 真人游戏
  25. /**
  26. * 获取游戏记录列表(参考apiAdminBetGameList方法)
  27. */
  28. public static function getBetGameList($appId, $page = 1, $limit = 20, $filters = [])
  29. {
  30. $wheres = [];
  31. $wheres[] = ['action_type', '=', 1]; // 只查询下注记录
  32. $wheres[] = ['app_id', '=', $appId];
  33. // 时间筛选
  34. if (!empty($filters['start_time'])) {
  35. $startTime = strtotime($filters['start_time'] . ' 00:00:00');
  36. $wheres[] = ['create_time', '>=', $startTime];
  37. }
  38. if (!empty($filters['end_time'])) {
  39. $endTime = strtotime($filters['end_time'] . ' 23:59:59');
  40. $wheres[] = ['create_time', '<=', $endTime];
  41. }
  42. // 游戏ID筛选
  43. if (!empty($filters['game_id'])) {
  44. $wheres[] = ['game_id', '=', $filters['game_id']];
  45. }
  46. // 牌局编号筛选
  47. if (!empty($filters['third_round_id'])) {
  48. $wheres[] = ['third_round_id', '=', $filters['third_round_id']];
  49. }
  50. // 用户ID筛选
  51. if (!empty($filters['player_id'])) {
  52. $wheres[] = ['user_id', '=', $filters['user_id']];
  53. }
  54. // 游戏玩法类型筛选
  55. if (!empty($filters['bet_game_play_type'])) {
  56. if ($filters['bet_game_play_type'] == 2) {
  57. $wheres[] = ['bet_game_play_type', 'in', [1, 2]];
  58. } else {
  59. $wheres[] = ['bet_game_play_type', '=', $filters['bet_game_play_type']];
  60. }
  61. }
  62. $query = self::where($wheres);
  63. // 统计总数
  64. $total = $query->count();
  65. // 获取列表数据(不包含result字段)
  66. $list = $query->withoutField('result')
  67. ->order('id', 'desc')
  68. ->page($page, $limit)
  69. ->select()
  70. ->toArray();
  71. if (empty($list)) {
  72. return [
  73. 'list' => [],
  74. 'total' => $total,
  75. 'page' => $page,
  76. 'limit' => $limit
  77. ];
  78. }
  79. return [
  80. 'list' => $list,
  81. 'total' => $total,
  82. 'page' => $page,
  83. 'limit' => $limit
  84. ];
  85. }
  86. /**
  87. * 获取子订单列表(action_type = 2的记录)
  88. */
  89. public static function getChildBetGameOrders($thirdGids, $appId)
  90. {
  91. if (empty($thirdGids)) {
  92. return [];
  93. }
  94. $childOrders = self::where([
  95. ['third_gid', 'in', $thirdGids],
  96. ['action_type', '=', 2],
  97. ['app_id', '=', $appId]
  98. ])
  99. ->withoutField('result')
  100. ->select()
  101. ->toArray();
  102. return $childOrders;
  103. }
  104. /**
  105. * 获取商户信息映射
  106. */
  107. private static function getMerchantsInfoMap($appIds)
  108. {
  109. if (empty($appIds)) {
  110. return [];
  111. }
  112. // 这里可以从商户表获取信息,暂时返回基本信息
  113. $map = [];
  114. foreach ($appIds as $appId) {
  115. $map[$appId] = [
  116. 'app_id' => $appId,
  117. 'nickname' => '商户' . $appId
  118. ];
  119. }
  120. return $map;
  121. }
  122. /**
  123. * 获取游戏记录详情
  124. */
  125. public static function getGameRecordDetail($id, $appId)
  126. {
  127. $record = self::where('id', $id)
  128. ->where('app_id', $appId)
  129. ->find();
  130. if (!$record) {
  131. return null;
  132. }
  133. $record = $record->toArray();
  134. // 获取商户信息
  135. $merchantsMap = self::getMerchantsInfoMap([$record['app_id']]);
  136. $record = self::formatBetGameData($record, $merchantsMap);
  137. // 获取相关的子订单
  138. if (!empty($record['third_gid'])) {
  139. $childOrders = self::getChildBetGameOrders([$record['third_gid']], $appId);
  140. $record['children'] = $childOrders;
  141. }
  142. return $record;
  143. }
  144. /**
  145. * 获取游戏统计信息
  146. */
  147. public static function getGameStatistics($appId, $filters = [])
  148. {
  149. $where = [
  150. ['app_id', '=', $appId],
  151. ['action_type', '=', 1]
  152. ];
  153. // 时间筛选
  154. if (!empty($filters['start_time'])) {
  155. $startTime = strtotime($filters['start_time'] . ' 00:00:00');
  156. $where[] = ['create_time', '>=', $startTime];
  157. }
  158. if (!empty($filters['end_time'])) {
  159. $endTime = strtotime($filters['end_time'] . ' 23:59:59');
  160. $where[] = ['create_time', '<=', $endTime];
  161. }
  162. $statistics = self::where($where)
  163. ->field([
  164. 'COUNT(*) as total_records',
  165. 'COUNT(DISTINCT uname) as total_players',
  166. 'SUM(bet) as total_bet_amount',
  167. 'SUM(total_win_amount) as total_win_amount',
  168. 'COUNT(DISTINCT game_id) as total_games'
  169. ])
  170. ->find()
  171. ->toArray();
  172. // 格式化金额
  173. $statistics['total_bet_amount_yuan'] = self::convertBalance($statistics['total_bet_amount']);
  174. $statistics['total_win_amount_yuan'] = self::convertBalance($statistics['total_win_amount']);
  175. $net_amount = $statistics['total_bet_amount'] - $statistics['total_win_amount'];
  176. $statistics['net_amount_yuan'] = self::convertBalance($net_amount);
  177. // 计算RTP
  178. if ($statistics['total_bet_amount'] > 0) {
  179. $statistics['rtp'] = round(($statistics['total_win_amount'] / $statistics['total_bet_amount']) * 100, 2);
  180. } else {
  181. $statistics['rtp'] = 0;
  182. }
  183. return $statistics;
  184. }
  185. /**
  186. * 获取所有游戏ID列表
  187. */
  188. public static function getAllGameIds($appId)
  189. {
  190. return self::where('app_id', $appId)
  191. ->where('action_type', 1)
  192. ->distinct(true)
  193. ->column('game_id');
  194. }
  195. /**
  196. * 获取所有游戏玩法列表
  197. */
  198. public static function getAllPlayMethods($appId)
  199. {
  200. return self::where('app_id', $appId)
  201. ->where('action_type', 1)
  202. ->distinct(true)
  203. ->column('bet_game_play_type');
  204. }
  205. }