| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- <?php
- declare (strict_types = 1);
- namespace app\model;
- use think\Model;
- use think\facade\Db;
- /**
- * 游戏订单模型
- */
- class GameOrderModel extends Model
- {
- protected $name = 'game_bet_order';
- protected $connection = 'fortue_tiger';
- protected $pk = 'id';
-
- // 状态常量
- const STATUS_PENDING = 0; // 待处理
- const STATUS_SUCCESS = 1; // 成功
- const STATUS_FAILED = 2; // 失败
- const STATUS_CANCELLED = 3; // 已取消
-
- // 动作类型常量
- const ACTION_TYPE_BET = 1; // 下注
- const ACTION_TYPE_SETTLE = 2; // 结算
- const ACTION_TYPE_CANCEL = 3; // 取消
-
- /**
- * 获取订单记录列表
- */
- public static function getOrderRecords($appId, $page = 1, $limit = 20, $filters = [])
- {
- $where = [
- ['app_id', '=', $appId]
- ];
-
- // 时间筛选
- if (!empty($filters['start_time'])) {
- $startTime = strtotime($filters['start_time'] . ' 00:00:00');
- $where[] = ['create_time', '>=', $startTime];
- }
-
- if (!empty($filters['end_time'])) {
- $endTime = strtotime($filters['end_time'] . ' 23:59:59');
- $where[] = ['create_time', '<=', $endTime];
- }
-
- // 游戏筛选
- if (!empty($filters['game_id'])) {
- $where[] = ['game_id', '=', $filters['game_id']];
- }
-
- // 订单原因筛选
- if (!empty($filters['order_reason'])) {
- $where[] = ['action_type', '=', $filters['order_reason']];
- }
-
- // 平台ID筛选
- if (!empty($filters['platform_id'])) {
- $where[] = ['uname', '=', $filters['platform_id']];
- }
-
- // 订单状态筛选
- if ($filters['order_status'] !== '') {
- $where[] = ['status', '=', $filters['order_status']];
- }
-
- // 订单编号筛选(支持母单号、子单号、牌局编号)
- if (!empty($filters['order_number'])) {
- $where[] = ['third_order_id', 'like', '%' . $filters['order_number'] . '%'];
- }
-
- // 母单号筛选
- if (!empty($filters['parent_order'])) {
- $where[] = ['third_gid', 'like', '%' . $filters['parent_order'] . '%'];
- }
-
- // 牌局编号筛选
- if (!empty($filters['round_number'])) {
- $where[] = ['third_round_id', 'like', '%' . $filters['round_number'] . '%'];
- }
-
- $query = self::where($where);
-
- // 统计总数
- $total = $query->count();
-
- // 获取列表数据
- $list = $query->field([
- 'id', 'user_id', 'uname', 'nickname', 'third_gid', 'third_order_id',
- 'third_round_id', 'parent_id', 'game_id', 'game_type', 'status',
- 'message', 'amount', 'bet', 'prev_amount', 'next_amount',
- 'action_type', 'ip', 'create_time', 'total_win_amount', 'total_amount'
- ])
- ->order('create_time', 'desc')
- ->page($page, $limit)
- ->select()
- ->toArray();
-
- // 格式化数据
- foreach ($list as &$item) {
- // 格式化金额(分转元)
- $item['amount'] = bcdiv((string)$item['amount'], '100', 2);
- $item['bet'] = bcdiv((string)$item['bet'], '100', 2);
- $item['prev_amount'] = bcdiv((string)$item['prev_amount'], '10000', 2);
- $item['next_amount'] = bcdiv((string)$item['next_amount'], '10000', 2);
- $item['total_win_amount'] = bcdiv((string)$item['total_win_amount'], '10000', 2);
- $item['total_amount'] = bcdiv((string)$item['total_amount'], '10000', 2);
-
- // 状态文本
- $item['status_text'] = self::getStatusText($item['status']);
-
- // 动作类型文本
- $item['action_type_text'] = self::getActionTypeText($item['action_type']);
-
- // 游戏名称
- $item['game_name'] = self::getGameName($item['game_id']);
- }
-
- return [
- 'list' => $list,
- 'total' => $total,
- 'page' => $page,
- 'limit' => $limit
- ];
- }
-
- /**
- * 获取订单详情
- */
- public static function getOrderDetail($id, $appId)
- {
- $order = self::where('id', $id)
- ->where('app_id', $appId)
- ->find();
-
- if (!$order) {
- return null;
- }
-
- $order = $order->toArray();
-
- // 格式化数据
- $order['create_time_text'] = date('Y-m-d H:i:s', $order['create_time']);
-
- // 格式化金额
- $order['amount_yuan'] = bcdiv((string)$order['amount'], '100', 2);
- $order['bet_yuan'] = bcdiv((string)$order['bet'], '100', 2);
- $order['prev_amount_yuan'] = bcdiv((string)$order['prev_amount'], '10000', 2);
- $order['next_amount_yuan'] = bcdiv((string)$order['next_amount'], '10000', 2);
- $order['total_win_amount_yuan'] = bcdiv((string)$order['total_win_amount'], '10000', 2);
- $order['total_amount_yuan'] = bcdiv((string)$order['total_amount'], '10000', 2);
-
- // 状态文本
- $order['status_text'] = self::getStatusText($order['status']);
-
- // 动作类型文本
- $order['action_type_text'] = self::getActionTypeText($order['action_type']);
-
- // 游戏名称
- $order['game_name'] = self::getGameName($order['game_id']);
-
- return $order;
- }
-
- /**
- * 获取订单统计信息
- */
- public static function getOrderStatistics($appId, $filters = [])
- {
- $where = [
- ['app_id', '=', $appId]
- ];
-
- // 时间筛选
- if (!empty($filters['start_time'])) {
- $startTime = strtotime($filters['start_time'] . ' 00:00:00');
- $where[] = ['create_time', '>=', $startTime];
- }
-
- if (!empty($filters['end_time'])) {
- $endTime = strtotime($filters['end_time'] . ' 23:59:59');
- $where[] = ['create_time', '<=', $endTime];
- }
-
- $statistics = self::where($where)
- ->field([
- 'COUNT(*) as total_orders',
- 'COUNT(DISTINCT uname) as total_players',
- 'SUM(bet) as total_bet_amount',
- 'SUM(CASE WHEN amount > 0 THEN amount ELSE 0 END) as total_win_amount',
- 'SUM(CASE WHEN amount < 0 THEN ABS(amount) ELSE 0 END) as total_loss_amount',
- 'SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) as success_orders',
- 'SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) as failed_orders',
- 'SUM(CASE WHEN status = 3 THEN 1 ELSE 0 END) as cancelled_orders'
- ])
- ->find()
- ->toArray();
-
- // 格式化金额
- $statistics['total_bet_amount_yuan'] = bcdiv((string)$statistics['total_bet_amount'], '100', 2);
- $statistics['total_win_amount_yuan'] = bcdiv((string)$statistics['total_win_amount'], '100', 2);
- $statistics['total_loss_amount_yuan'] = bcdiv((string)$statistics['total_loss_amount'], '100', 2);
- $net_amount = bcsub((string)$statistics['total_loss_amount'], (string)$statistics['total_win_amount']);
- $statistics['net_amount_yuan'] = bcdiv($net_amount, '100', 2);
-
- // 计算成功率
- if ($statistics['total_orders'] > 0) {
- $rate = bcdiv((string)$statistics['success_orders'], (string)$statistics['total_orders'], 4);
- $statistics['success_rate'] = bcmul($rate, '100', 2);
- } else {
- $statistics['success_rate'] = '0.00';
- }
-
- return $statistics;
- }
-
- /**
- * 获取所有游戏ID列表
- */
- public static function getAllGameIds($appId)
- {
- return self::where('app_id', $appId)
- ->distinct(true)
- ->column('game_id');
- }
-
- /**
- * 获取状态文本
- */
- public static function getStatusText($status)
- {
- $statusMap = [
- self::STATUS_PENDING => '待处理',
- self::STATUS_SUCCESS => '成功',
- self::STATUS_FAILED => '失败',
- self::STATUS_CANCELLED => '已取消'
- ];
-
- return $statusMap[$status] ?? '未知';
- }
-
- /**
- * 获取动作类型文本
- */
- public static function getActionTypeText($actionType)
- {
- $actionTypeMap = [
- self::ACTION_TYPE_BET => '下注',
- self::ACTION_TYPE_SETTLE => '结算',
- self::ACTION_TYPE_CANCEL => '取消'
- ];
-
- return $actionTypeMap[$actionType] ?? '其他';
- }
-
- /**
- * 获取游戏名称
- */
- private static function getGameName($gameId)
- {
- // 这里可以根据实际情况配置游戏ID对应的名称
- $gameNames = [
- 'tiger' => '老虎机',
- 'fortue_tiger' => '财富老虎',
- 'dragon_tiger' => '龙虎斗',
- 'baccarat' => '百家乐',
- // 可以继续添加其他游戏
- ];
-
- return $gameNames[$gameId] ?? $gameId;
- }
- }
|