<?php
declare (strict_types = 1);

namespace app\model;

use think\Model;
use think\facade\Db;
use app\model\GameModel;

/**
 * 游戏订单模型 - 基于tp_game_bet_order表
 */
class GameBetOrderModel extends Model
{
    protected $name = 'game_bet_order';
    protected $connection = 'fortue_tiger';
    protected $pk = 'id';
       
    /**
     * 获取下注订单列表（参考apiAdminBetOrderList方法）
     */
    public static function getBetOrderList($appId, $page = 1, $limit = 20, $filters = [])
    {
        $where = [
            ['app_id', '=', $appId]
        ];
        
        // 时间筛选
        if (!empty($filters['start_time'])) {
            $startTime = strtotime($filters['start_time']);
            $where[] = ['create_time', '>=', $startTime];
        }
        
        if (!empty($filters['end_time'])) {
            $endTime = strtotime($filters['end_time']);
            $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['uname'])) {
            $where[] = ['uname', '=', $filters['uname']];
        }

        // 平台昵称筛选
        if (!empty($filters['nickname'])) {
            $where[] = ['nickname', 'like', '%' . $filters['nickname'] . '%'];
        }        
        
        // 玩家id筛选
        if (!empty($filters['player_id'])) {
            $where[] = ['user_id', 'like', '%' . $filters['player_id'] . '%'];
        }
        
        // 订单状态筛选
        if ($filters['order_status'] !== '') {
            $where[] = ['status', '=', $filters['order_status']];
        }
                
        // 订单编号筛选
        if (!empty($filters['third_order_id'])) {
            $where[] = ['third_order_id', 'like', '%' . $filters['third_order_id'] . '%'];
        }
        
        // 母单号筛选
        if (!empty($filters['third_gid'])) {
            $where[] = ['third_gid', 'like', '%' . $filters['third_gid'] . '%'];
        }
        
        // 牌局编号筛选
        if (!empty($filters['third_round_id'])) {
            $where[] = ['third_round_id', 'like', '%' . $filters['third_round_id'] . '%'];
        }
        
        $query = self::where($where);
        
        // 统计总数
        $total = $query->count();
        
        // 获取列表数据
        $list = $query->field([
                'id', 'user_id', 'app_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', 'err_desc', 'create_time',
                'total_win_amount', 'total_amount'
            ])
            ->order('id', 'desc')
            ->page($page, $limit)
            ->select()
            ->toArray();
                
        return [
            'list' => $list,
            'total' => $total,
            'page' => $page,
            'limit' => $limit
        ];
    }
}