<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class AgentUserInfo extends Model
{
    /**
     * 数据库连接名
     */
    protected $connection = 'mysql';

    /**
     * 与模型关联的表名
     */
    protected $table = 'webgame.agent_user_info';

    /**
     * 可批量赋值的属性
     */
    protected $fillable = [
        'UserID',
        'referral_code',
        'level',
        'valid_referrals',
        'total_commission',
        'today_commission',
        'withdraw_rewards',
        'now_rewards',
        'total_rewards',
        'yesterday_commission'
    ];

//    /**
//     * 获取用户
//     */
//    public function accountsInfo()
//    {
//        return $this->belongsTo(AccountsInfo::class, 'UserID', 'UserID');
//    }

    /**
     * 获取当前级别
     */
    public function agentLevel()
    {
        return $this->belongsTo(AgentLevel::class, 'level', 'level');
    }

    /**
     * 获取所有邀请的用户记录
     */
    public function referrals()
    {
        return $this->hasMany(AgentUserRecord::class, 'SpreaderID', 'UserID');
    }

    /**
     * 获取有效的邀请用户记录
     */
    public function validReferrals()
    {
        return $this->hasMany(AgentUserRecord::class, 'SpreaderID', 'UserID')
                    ->where('is_valid', 1);
    }

    /**
     * 获取用户的佣金记录
     */
    public function commissions()
    {
        return $this->hasMany(AgentDepositCommission::class, 'SpreaderID', 'UserID');
    }
}
