<?php

namespace App\Models;

use App\Services\UserFun;
use Illuminate\Database\Eloquent\Model;

class UserAccount extends Model
{
    // 显式指定表名
    protected $table = 'user_account';
    protected $guarded = ['id'];

    // 时间字段类型自动转为 Carbon 实例
    protected $casts = [
        'date' => 'datetime',
        'amount' => 'decimal:2',
        'balance' => 'decimal:2',
    ];

    //检查账号是否存在
    public static function CheckUserId($user){
        return UserAccount::where([
            'chat_id' => $user['chat_id'],
            'user_id' => $user['user_id'],
        ])->exists();
    }

    //更新账号资料
    public static function UpdateUser($user){
        UserAccount::where([
            'chat_id' => $user['chat_id'],
            'user_id' => $user['user_id'],
        ])->increment('balance', $user['amount']);
        $balance = UserAccount::where([
            'chat_id' => $user['chat_id'],
            'user_id' => $user['user_id'],
        ])->value('balance');
        return $balance;
    }

    //新建账号
    public static function CreatUser($user){
        $UserAccount=new UserAccount();
        $UserAccount->chat_id=$user['chat_id'];
        $UserAccount->user_id=$user['user_id'];
        $UserAccount->username=$user['username'];
        $UserAccount->stime=now();
        $UserAccount->balance=$user['amount'];
        $UserAccount->subjection=$user['token'];
        $UserAccount->save();
        return $user['amount'];
    }

}
