<?php

namespace App\Models;

class BalanceLog extends BaseModel
{
    protected $table = 'balance_logs';
    protected $fillable = ['room_id', 'member_id', 'amount', 'before_balance', 'after_balance', 'change_type', 'remark', 'related_id','type','frozen_status'];
    protected $hidden = [ 'updated_at'];

    function member()
    {
        return $this->belongsTo(User::class, 'member_id', 'member_id')
            ->select(['id', 'member_id', 'username', 'first_name', 'admin_note', 'status', 'phone', 'visitor_id', 'register_ip']);
    }

    //用户累计消费总额
    public static function getTotalConsume($memberId){
        $amount = self::where('member_id', $memberId)->where('type',1)->where('change_type', 'like', '%投注%')->sum('amount');
        $refund = self::where('member_id', $memberId)->where('type',1)->where('change_type', 'like', '%彩退款')->sum('amount');
        $amount = abs($amount) - $refund;
        return $amount > 0 ? bcadd($amount, 0, 2) : 0;
    }

}