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

use app\service\admin\BaseAdminService;
use Applications\sample\modules\settings\repository\SettingsRepository;
use think\Request;

/**
 * Settings Service
 * @package Applications\sample\modules\settings\service\admin
 */
class SettingsService extends BaseAdminService
{
    /**
     * 获取应用设置
     * @return array
     */
    public function get(): array
    {
        return ['code' => 0, 'data' => $this->repository()->get()];
    }

    /**
     * 保存应用设置
     * @param Request $request
     * @return array
     */
    public function save(Request $request): array
    {
        $base = $request->post('sample_base');
        $article = $request->post('sample_article');

        $payload = [
            'sample_base' => is_array($base) ? $base : [],
            'sample_article' => is_array($article) ? $article : [],
        ];

        $this->repository()->save($payload);
        return ['code' => 0, 'msg' => '保存成功'];
    }

    /**
     * 获取设置仓库实例
     * @return SettingsRepository
     */
    private function repository(): SettingsRepository
    {
        return $this->app->make(SettingsRepository::class);
    }
}
