<?php

namespace App\Game;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;

class AgentUserRecord extends Model
{
    protected $table = 'webgame.AgentUserRecord';
    protected $connection = 'mysql';
    protected $fillable = [
        'GlobalUID', 'UserID', 'Date', 'Level', 'SubTotal', 'CreatedAt', 'UpdatedAt','HigherGUID','HigherID'
    ];
    public $timestamps = false;

    // 查找或创建当日的记录，并增加到 SubTotal
    public static function findOrCreateAndUpdate($globalUID, $userID, $level, $amount,$HigherGUID=null,$HigherID=0)
    {
        $date = Carbon::now()->format('Ymd'); // 获取当前日期，格式为 YYYYMMDD
        $record = self::where('GlobalUID', $globalUID)
            ->where('Date', $date)
            ->where('Level', $level);

        if ($record->exists()) {
            // 如果记录存在，增加 SubTotal
            $record->increment('SubTotal', $amount);
        } else {
            // 如果记录不存在，创建新记录
            self::create([
                'GlobalUID' => $globalUID,
                'UserID' => $userID,
                'Date' => $date,
                'Level' => $level,
                'SubTotal' => $amount,
                'HigherGUID' => $HigherGUID,
                'HigherID' => $HigherID
            ]);
        }

    }
}
