<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class SystemConfig extends Model
{
    protected $fillable = ['store_id', 'group', 'key', 'value', 'description'];

    /**
     * 获取配置值
     */
    public static function getValue(string $key, ?int $storeId = null, $default = null)
    {
        $config = self::where('key', $key)
            ->where('store_id', $storeId)
            ->first();

        return $config ? $config->value : $default;
    }

    /**
     * 设置配置值
     */
    public static function setValue(string $key, $value, ?int $storeId = null, string $group = 'general'): void
    {
        self::updateOrCreate(
            ['key' => $key, 'store_id' => $storeId],
            ['value' => $value, 'group' => $group]
        );
    }
}
