<?php

namespace App\Distributor\Repositories;

use App\Models\DistAdminUsersSetting as Model;
use Dcat\Admin\Admin;
use Dcat\Admin\Repositories\EloquentRepository;

class DistAdminUsersSetting extends EloquentRepository
{
    /**
     * Model.
     *
     * @var string
     */
    protected $eloquentClass = Model::class;

    //得到用户配置
    public static function getValue($key)
    {
        $userId = Admin::user()->id;
        $model = new Model();
        $setting = $model->where('user_id',$userId)->where('key',$key)->first();
        if($setting){
            return $setting->value;
        }else{
            $congif = config('dictionary.users_settings');
            return $congif[$key];
        }
    }

    /*
     * 设置用户配置
     */
    public static function setValue($key,$value)
    {
        if ($value == null || $value == ''){
            return false;
        }
        $congif = config('dictionary.users_settings');
        if (!isset($congif[$key])){
            return false;
        }
        $userId = Admin::user()->id;
        $model = new Model();
        $setting = $model->where('user_id',$userId)->where('key',$key)->first();
        if($setting){
            $setting->value = $value;
            $setting->save();
        }else{
            $model->user_id = $userId;
            $model->key = $key;
            $model->value = $value;
            $model->save();
        }
        return true;
    }

}
