GameOrderModel.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 GameOrderModel extends Model
  10. {
  11. protected $name = 'game_bet_order';
  12. protected $connection = 'fortue_tiger';
  13. protected $pk = 'id';
  14. // 状态常量
  15. const STATUS_PENDING = 0; // 待处理
  16. const STATUS_SUCCESS = 1; // 成功
  17. const STATUS_FAILED = 2; // 失败
  18. const STATUS_CANCELLED = 3; // 已取消
  19. // 动作类型常量
  20. const ACTION_TYPE_BET = 1; // 下注
  21. const ACTION_TYPE_SETTLE = 2; // 结算
  22. const ACTION_TYPE_CANCEL = 3; // 取消
  23. const ACTION_TYPE_CHECK = 4; // 检查
  24. const ACTION_TYPE_RESULT = 5; // 结果
  25. // 游戏类型常量
  26. const GAME_TYPE_SLOT = 1; // 老虎机
  27. const GAME_TYPE_TABLE = 2; // 桌面游戏
  28. const GAME_TYPE_LIVE = 3; // 真人游戏
  29. /**
  30. * 获取下注订单列表(参考apiAdminBetOrderList方法)
  31. */
  32. public static function getBetOrderList($appId, $page = 1, $limit = 20, $filters = [])
  33. {
  34. $where = [
  35. ['app_id', '=', $appId]
  36. ];
  37. // 时间筛选
  38. if (!empty($filters['start_time'])) {
  39. $startTime = strtotime($filters['start_time'] . ' 00:00:00');
  40. $where[] = ['create_time', '>=', $startTime];
  41. }
  42. if (!empty($filters['end_time'])) {
  43. $endTime = strtotime($filters['end_time'] . ' 23:59:59');
  44. $where[] = ['create_time', '<=', $endTime];
  45. }
  46. // 游戏筛选
  47. if (!empty($filters['game_id'])) {
  48. $where[] = ['game_id', '=', $filters['game_id']];
  49. }
  50. // 订单原因筛选(动作类型)
  51. if (!empty($filters['order_reason'])) {
  52. $where[] = ['action_type', '=', $filters['order_reason']];
  53. }
  54. // 平台ID筛选
  55. if (!empty($filters['uname'])) {
  56. $where[] = ['uname', '=', $filters['uname']];
  57. }
  58. // 平台昵称筛选
  59. if (!empty($filters['nickname'])) {
  60. $where[] = ['nickname', 'like', '%' . $filters['nickname'] . '%'];
  61. }
  62. // 玩家id筛选
  63. if (!empty($filters['player_id'])) {
  64. $where[] = ['user_id', 'like', '%' . $filters['player_id'] . '%'];
  65. }
  66. // 订单状态筛选
  67. if ($filters['order_status'] !== '') {
  68. $where[] = ['status', '=', $filters['order_status']];
  69. }
  70. // 订单编号筛选
  71. if (!empty($filters['third_order_id'])) {
  72. $where[] = ['third_order_id', 'like', '%' . $filters['third_order_id'] . '%'];
  73. }
  74. // 母单号筛选
  75. if (!empty($filters['third_gid'])) {
  76. $where[] = ['third_gid', 'like', '%' . $filters['third_gid'] . '%'];
  77. }
  78. // 牌局编号筛选
  79. if (!empty($filters['third_round_id'])) {
  80. $where[] = ['third_round_id', 'like', '%' . $filters['third_round_id'] . '%'];
  81. }
  82. $query = self::where($where);
  83. // 统计总数
  84. $total = $query->count();
  85. // 获取列表数据
  86. $list = $query->field([
  87. 'id', 'user_id', 'app_id', 'uname', 'nickname',
  88. 'third_gid', 'third_order_id', 'third_round_id', 'parent_id',
  89. 'game_id', 'game_type', 'status', 'message',
  90. 'amount', 'bet', 'prev_amount', 'next_amount',
  91. 'action_type', 'ip', 'err_desc', 'create_time',
  92. 'total_win_amount', 'total_amount'
  93. ])
  94. ->order('id', 'desc')
  95. ->page($page, $limit)
  96. ->select()
  97. ->toArray();
  98. return [
  99. 'list' => $list,
  100. 'total' => $total,
  101. 'page' => $page,
  102. 'limit' => $limit
  103. ];
  104. }
  105. /**
  106. * 获取订单详情
  107. */
  108. public static function getOrderDetail($id, $appId)
  109. {
  110. $order = self::where('id', $id)
  111. ->where('app_id', $appId)
  112. ->find();
  113. if (!$order) {
  114. return null;
  115. }
  116. $order = $order->toArray();
  117. $order = self::formatItemData($order);
  118. // 获取相关的子订单
  119. if (!empty($order['third_gid'])) {
  120. $childOrders = self::getChildOrders([$order['third_gid']], $appId);
  121. $order['children'] = $childOrders;
  122. }
  123. // 游戏名称
  124. $order['game_name'] = self::getGameName($order['game_id']);
  125. return $order;
  126. }
  127. /**
  128. * 获取订单统计信息
  129. */
  130. public static function getOrderStatistics($appId, $filters = [])
  131. {
  132. $where = [
  133. ['app_id', '=', $appId],
  134. ['status', '=', 1]
  135. ];
  136. // 时间筛选
  137. if (!empty($filters['start_time'])) {
  138. $startTime = strtotime($filters['start_time'] . ' 00:00:00');
  139. $where[] = ['create_time', '>=', $startTime];
  140. }
  141. if (!empty($filters['end_time'])) {
  142. $endTime = strtotime($filters['end_time'] . ' 23:59:59');
  143. $where[] = ['create_time', '<=', $endTime];
  144. }
  145. $statistics = self::where($where)
  146. ->field([
  147. 'COUNT(*) as total_orders',
  148. 'COUNT(DISTINCT uname) as total_players',
  149. 'SUM(CASE WHEN action_type = 1 THEN bet ELSE 0 END) as total_bet_amount',
  150. 'SUM(total_win_amount) as total_win_amount',
  151. 'SUM(CASE WHEN amount > 0 THEN amount ELSE 0 END) as total_profit',
  152. 'SUM(CASE WHEN amount < 0 THEN ABS(amount) ELSE 0 END) as total_loss',
  153. 'SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) as success_orders',
  154. 'SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) as failed_orders',
  155. 'SUM(CASE WHEN status = 3 THEN 1 ELSE 0 END) as cancelled_orders'
  156. ])
  157. ->find()
  158. ->toArray();
  159. // 格式化金额
  160. $statistics['total_bet_amount_yuan'] = self::convertBalance($statistics['total_bet_amount']);
  161. $statistics['total_win_amount_yuan'] = self::convertBalance($statistics['total_win_amount']);
  162. $statistics['total_profit_yuan'] = self::convertBalance($statistics['total_profit']);
  163. $statistics['total_loss_yuan'] = self::convertBalance($statistics['total_loss']);
  164. $net_amount = $statistics['total_bet_amount'] - $statistics['total_win_amount'];
  165. $statistics['net_amount_yuan'] = self::convertBalance($net_amount);
  166. // 计算成功率
  167. if ($statistics['total_orders'] > 0) {
  168. $statistics['success_rate'] = round(($statistics['success_orders'] / $statistics['total_orders']) * 100, 2);
  169. } else {
  170. $statistics['success_rate'] = 0;
  171. }
  172. return $statistics;
  173. }
  174. /**
  175. * 获取所有游戏ID列表
  176. */
  177. public static function getAllGameIds($appId)
  178. {
  179. return self::where('app_id', $appId)
  180. ->distinct(true)
  181. ->column('game_id');
  182. }
  183. /**
  184. * 获取状态文本
  185. */
  186. public static function getStatusText($status)
  187. {
  188. $statusMap = [
  189. self::STATUS_PENDING => '待处理',
  190. self::STATUS_SUCCESS => '成功',
  191. self::STATUS_FAILED => '失败',
  192. self::STATUS_CANCELLED => '已取消'
  193. ];
  194. return $statusMap[$status] ?? '未知';
  195. }
  196. /**
  197. * 获取动作类型文本
  198. */
  199. public static function getActionTypeText($actionType)
  200. {
  201. $actionTypeMap = [
  202. self::ACTION_TYPE_BET => '下注',
  203. self::ACTION_TYPE_SETTLE => '结算',
  204. self::ACTION_TYPE_CANCEL => '取消',
  205. self::ACTION_TYPE_CHECK => '检查',
  206. self::ACTION_TYPE_RESULT => '结果'
  207. ];
  208. return $actionTypeMap[$actionType] ?? '其他';
  209. }
  210. /**
  211. * 获取游戏名称
  212. */
  213. private static function getGameName($gameId)
  214. {
  215. // 这里可以根据实际情况配置游戏ID对应的名称
  216. $gameNames = [
  217. 'tiger' => '老虎机',
  218. 'fortue_tiger' => '财富老虎',
  219. 'dragon_tiger' => '龙虎斗',
  220. 'baccarat' => '百家乐',
  221. // 可以继续添加其他游戏
  222. ];
  223. return $gameNames[$gameId] ?? $gameId;
  224. }
  225. }