<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * Class System_Config
 * @package App
 */

class BaseConfig extends Model
{
    //
    protected $table = "base_config";
    protected $guarded = [];
    static $groups = [
        'ali_config'=>'阿里云',
        'wechat_config'=>'微信',
        'system_config'=>'系统设置',
        'put_config'=>'投放设置',
        'fetch_config'=>'清运设置',
        'cash_config'=>'提现设置',
        'assistant_setting'=>'协管员提现设置',
    ];
    const Field_textarea = 0,
          Filed_richText = 1,
          Field_Json = 2,
          Field_Switch = 3,
          Field_Time = 4,
          Field_File = 5,
          Field_Checkbox = 6,
          Field_Json_Array = 7;

    private static $_fieldType = [
        self::Field_textarea =>'纯文本',
        self::Filed_richText =>'富文本',
        self::Field_Json =>'JSON',
        self::Field_Switch =>'开关',
        self::Field_Time =>'时间',
        self::Field_File =>'文件',
        self::Field_Checkbox =>'选择框',
        self::Field_Json_Array =>'数组JSON',
    ];

    protected static function getType(){
        return self::$_fieldType;
    }

    /**
     * @param String $group
     * @param String $key
     * @param String $default
     * @return array|String
     */
    public static function get(string $group, string $key = '', string $default = "")
    {
        $query = self::where('group', $group);
        if ($key) {
            $res = $query->where('key', $key)->first(['value'])->value ?? $default;
            return $res;
        } else {
            $res =  $query->get(['key', 'value']);
            $arr = [];
            foreach ($res as $index => $item) {
                $arr[$item['key']] = $item['value'];
            }
            return $arr;
        }
    }

    /**
     * @param String $group
     * @param String $key
     * @param String $value
     * @return int
     */
    public static function set(string $group, string $key, string $value)
    {
        return self::where([['group', $group], ['key', $key]])->update(['value' => $value]);
    }
}
