HEX
Server: nginx/1.24.0
System: Linux prod-btpayments-io 6.14.0-1018-aws #18~24.04.1-Ubuntu SMP Mon Nov 24 19:46:27 UTC 2025 x86_64
User: ubuntu (1000)
PHP: 8.3.19
Disabled: NONE
Upload Files
File: /var/www/BtPayments/BtPayments-platform/app/Models/User.php
<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Models\Payment\Payment;
use App\Models\Withdraw\Withdraw;
use App\Models\Withdraw\WithdrawAccount;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements MustVerifyEmail
{
    use HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'password' => 'hashed',
        ];
    }

    /**
     * 关联商户
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function merchant(): HasOne
    {
        return $this->hasOne(Merchant::class);
    }

    /**
     * 关联支付
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function payments(): HasMany
    {
        return $this->hasMany(Payment::class);
    }

    /**
     * 关联提现
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function withdraws(): HasMany
    {
        return $this->hasMany(Withdraw::class);
    }

    /**
     * 关联提现帐户
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function withdrawAccounts(): HasMany
    {
        return $this->hasMany(WithdrawAccount::class);
    }

    /**
     * Attr: 用户当前余额
     *
     * @return int|mixed
     */
    public function getCurrentBalanceAttribute()
    {
        $disposableBalance = bcmul($this->payments()->sum('total_paid_amount_usdt'), 0.99, 8);

        // 提现总额(不包含 rejected 状态的提现)
        $totalWithdrawAmount = $this->withdraws()->where('status', '!=', Withdraw::STATUS_REJECTED)->sum('amount');

        return bcsub($disposableBalance, $totalWithdrawAmount, 8);
    }

    /**
     * Attr: 用户总收入
     *
     * @return int|mixed
     */
    public function getTotalIncomeUsdtAttribute()
    {
        return $this->payments()->sum('total_paid_amount_usdt');
    }

    /**
     * Attr: 用户总销售额
     *
     * @return int|mixed
     */
    public function getTotalSalesUsdtAttribute()
    {
        return $this->payments()->sum('order_amount');
    }

    /**
     * Attr: 用户订单数
     *
     * @return int|mixed
     */
    public function getOrdersCountAttribute()
    {
        return $this->payments()->count();
    }
}