<?php

namespace App\Models\User;

use App\Exceptions\AlertException;
use App\Models\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;

/**
 * App\Models\User\UserStateModel
 *
 * @property int $id
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property int $uid 用户
 * @property string $key key
 * @property string $value value
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\UserStateModel newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\UserStateModel newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\UserStateModel query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\UserStateModel whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\UserStateModel whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\UserStateModel whereKey($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\UserStateModel whereUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\UserStateModel whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\UserStateModel whereValue($value)
 * @mixin \Eloquent
 */
class UserStateModel extends Model
{
    protected $table = "kdgx_fpdx_user_states";

    protected $fillable = ['uid', 'key', 'value'];
    public static $keys = array(
        'pay_open_supvip' => 0,
        'popularity_sign_end_at' => 0,
        'algorithm' => null
    );
    /**
     * 获取
     * @param  int  $uid
     * @param  string  $key
     * @return mixed
     * @throws AlertException
     */
    public static function get(int $uid, string $key)
    {
        if (!in_array($key, self::$keys)) {
            throw new AlertException("错误的配置");
        }

        try {
            $model = self::where([['uid', $uid], ['key', $key]])->firstOrFail();
            return $model->value;
        } catch (ModelNotFoundException $exception) {
            return self::$keys[$key];
        }
    }

    /**
     * 设置
     * @param  int  $uid
     * @param  string  $key
     * @param string $value
     * @return bool
     * @throws AlertException
     */
    public static function set(int $uid, string $key, $value)
    {
        if (!in_array($key, self::$keys)) {
            throw new AlertException("错误的配置");
        }

        self::updateOrCreate(['uid' => $uid, 'key' => $key], [
            'uid' => $uid,
            'key' => $key,
            'value' => (string)$value
        ]);
        return true;
    }

    /**
     * 增加
     * @param  int  $uid
     * @param  string  $key
     * @param string $value
     * @param  bool  $now  是否以现在作为基准
     * @return bool
     * @throws AlertException
     */
    public static function inc(int $uid, string $key, $value, bool $now = false)
    {
        if (!in_array($key, self::$keys)) {
            throw new AlertException("错误的配置");
        }
        $state = self::where([['uid', $uid], ['key', $key]])->first();
        if (collect($state)->isEmpty()) {
            if ($now) {
                $v = time() + $value;
            } else {
                $v = self::$keys[$key] + $value;
            }
            self::create(['uid' => $uid, 'key' => $key, 'value' => $v]);
        } else {
            $v = intval($state->value) + intval($value);
            if ($now && intval($state->value) < time()) {
                $v = time() + $value;
            }
            $state->value = $v;
            $state->save();
        }
        return true;
    }
}
