<?php

namespace App\Game;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Redis;

class RePayConfig extends Model
{
    protected $connection = 'mysql';
    protected $table = 'webgame.RePayConfig';
    protected $primaryKey = 'id';
    public $timestamps = false;
    protected $guarded = []; // Allow mass assignment for all fields

    protected $fillable = [
        'PayTotal',
        'PayTimes',
        'WithdrawTotal',
        'LessThan',
        'Condition',
        'Price',
        'Amount',
        'Gift',
        'TimeLimit',
        'Status',
    ];

    public static function CacheDatas()
    {
        $key = 'RePayConfig';
        if (Redis::exists($key)) {
            $all = json_decode(Redis::get($key), true);
        }
        if (!isset($all) || empty($all) || !count($all)) {
            $all = self::all()->where('Status', 1);
            $all = json_decode(json_encode($all), true);
            Redis::setex($key, 600, json_encode($all));
        }
        return $all;
    }

    protected static function boot()
    {
        parent::boot();

        static::updated(function ($model) {
            Redis::del('RePayConfig');
        });

        static::deleted(function ($model) {
            Redis::del('RePayConfig');
        });
    }
}
