<?php
// +----------------------------------------------------------------------
// | onezan-admin 快速开发的管理平台
// +----------------------------------------------------------------------
// | 官方网址：https://www.onezan.cn
// +----------------------------------------------------------------------
// | onezan团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: 543885307@qq.com
// +----------------------------------------------------------------------
namespace Applications\sample\modules\settings\repository;

use app\repository\system\ConfigRepository;

/**
 * Settings Repository
 * @package Applications\sample\modules\settings\repository
 */
class SettingsRepository
{
    /**
     * @var ConfigRepository
     */
    private $configRepository;

    public function __construct(\think\App $app)
    {
        $this->configRepository = $app->make(ConfigRepository::class);
    }

    /**
     * 获取应用设置
     * @return array
     */
    public function get(): array
    {
        return [
            'sample_base' => array_merge([
                'app_name' => '示例应用',
                'app_slogan' => '框架功能最佳实践示例',
                'service_phone' => '',
                'copyright_text' => '',
                'notice' => '',
            ], $this->configRepository->getJson('sample_base', [])),
            'sample_article' => array_merge([
                'default_price' => 0,
                'page_size' => 10,
                'require_cover' => true,
                'guest_preview' => false,
                'enable_audio' => true,
            ], $this->configRepository->getJson('sample_article', [])),
        ];
    }

    /**
     * 保存应用设置
     * @param array $payload
     * @return void
     */
    public function save(array $payload): void
    {
        $this->configRepository->setJson('sample_base', (array)($payload['sample_base'] ?? []), '示例应用基础设置');
        $this->configRepository->setJson('sample_article', (array)($payload['sample_article'] ?? []), '示例应用文章设置');
    }
}
