<?php
declare (strict_types = 1);

namespace app\model;

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

/**
 * 转账记录模型
 */
class TransferLogModel extends Model
{
    protected $name = 'merchants_user_balance_transfer_logs';
    protected $connection = 'fortue_tiger';
    protected $pk = 'id';
    
    
    // 状态常量
    const STATUS = [
        0 => '失败',
        1 => '成功',
        2 => '等待处理',
    ];
    
    /**
     * 获取转账记录列表
     */
    public static function getTransferLogs($merchantId, $page = 1, $limit = 20, $filters = [])
    {
        $where = [
            ['app_id', '=', $merchantId]
        ];
        
        // 时间筛选
        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];
        }
        
        // 平台ID筛选
        if (!empty($filters['uname'])) {
            $where[] = ['uname', '=', $filters['uname']];
        }

        // 平台昵称筛选
        if (!empty($filters['nickname'])) {
            $where[] = ['uname', 'like', '%' . $filters['nickname'] . '%'];
        }        
        
        // 玩家id筛选
        if (!empty($filters['player_id'])) {
            $where[] = ['user_id', 'like', '%' . $filters['player_id'] . '%'];
        }
        
        // 状态筛选
        if ($filters['status'] !== '') {
            $where[] = ['status', '=', $filters['status']];
        }
        
        // 转账流水号筛选
        if (!empty($filters['tx_id'])) {
            $where[] = ['tx_id', 'like', '%' . $filters['tx_id'] . '%'];
        }
        
        $query = self::where($where);
        
        // 统计总数
        $total = $query->count();
        
        // 获取列表数据
        $list = $query->field([
                'id', 'user_id', 'uname', 'tx_id', 'ptx_id', 'amount',
                'prev_amount', 'next_amount', 'status', 'ip', 
                'message', 'create_time'
            ])
            ->order('create_time', 'desc')
            ->page($page, $limit)
            ->select()
            ->toArray();
        
        // 获取用户信息
        $userIds = array_unique(array_column($list, 'user_id'));
        $userMap = [];
        if (!empty($userIds)) {
            // 从玩家表获取玩家信息
            $users = MerchantsUserModel::whereIn('user_id', $userIds)->field('user_id, nickname')->select()->toArray();
            foreach ($users as $user) {
                $userMap[$user['user_id']] = $user['nickname'];
            }
        }
        
        // 格式化数据
        foreach ($list as &$item) {            
            // 格式化金额（分转元）
            $item['amount'] = bcdiv($item['amount'], "10000", 2);
            $item['prev_amount'] = bcdiv($item['prev_amount'], "10000", 2);
            $item['next_amount'] = bcdiv($item['next_amount'], "10000", 2);
            // 状态文本
            $item['status_text'] = self::getStatusText($item['status']);
            // 平台昵称
            $item['nickname'] = $userMap[$item['user_id']] ?? '';
        }
        
        return [
            'list' => $list,
            'total' => $total,
            'page' => $page,
            'limit' => $limit
        ];
    }
        
    /**
     * 获取状态文本
     */
    public static function getStatusText($status)
    {        
        return self::STATUS[$status] ?? '未知';
    }
}