ConfigController.php 3.73 KB
<?php

namespace App\Http\Container\AdminSection\Controllers\System;

use App\Helpers\OperationLog;
use App\Http\Container\AdminSection\Requests\System\ConfigCreateRequest;
use App\Http\Container\AdminSection\Requests\System\ConfigUpdateRequest;
use App\Http\Request\BooleanStatusRequest;
use App\Models\SystemConfig;
use App\Models\User;
use App\Support\Controller;
use Hikoon\LaravelApi\Facades\Response;
use Hikoon\LaravelApi\Support\ApiCode;
use Illuminate\Http\Request;

class ConfigController extends Controller
{
    /**
     * @param \Illuminate\Http\Request $request
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function index(Request $request): Response
    {
        $pageSize  = $request->get('pageSize', 20);
        $filter    = $request->mergeIfMissing(['parent_id' => 0])->except('page', 'pageSize', 'fetchType');
        $fetchType = $request->get('fetchType', 'page');
        $build     = SystemConfig::filter($filter);

        return match ($fetchType) {
            'all' => $this->successWithData($build->get()),
            default => $this->successWithData($build->paginate($pageSize))
        };
    }

    /**
     * @param \App\Http\Container\AdminSection\Requests\System\ConfigCreateRequest $request
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function store(ConfigCreateRequest $request): Response
    {
        $config = SystemConfig::query()->create($request->validated());
        OperationLog::admin()->createAction()->subject($config)->content('配置字典《%s》', $config->getAttribute('name'));
        return $this->successWithData($config, ApiCode::CREATE_SUCCESS);
    }

    /**
     * @param \App\Models\SystemConfig $config
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function show(SystemConfig $config): Response
    {
        $config->load('children');
        return $this->successWithData($config);
    }

    /**
     * @param \App\Http\Container\AdminSection\Requests\System\ConfigUpdateRequest $request
     * @param \App\Models\SystemConfig                                             $config
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function update(ConfigUpdateRequest $request, SystemConfig $config): Response
    {
        //如果是修改Demo标签
        if ($config->getAttribute('identifier') === 'CtGly-jsLjAu5yJvLrN7L') {
            $currentIds = array_filter(explode(',', $config->getAttribute('content')));
            $nextIds    = array_filter(explode(',', $request->get('content')));
            $diffIds    = array_filter($currentIds, static fn($value) => !in_array($value, $nextIds, false));
            if (count($diffIds) > 0) {
                User::query()->where('demo_type', 0)
                    ->whereHas('authTags', fn($query) => $query->whereIn('tag_id', $diffIds))
                    ->whereDoesntHave('authTags', fn($query) => $query->whereIn('tag_id', $nextIds))
                    ->update(['demo_type' => 1]);
            }
        }

        $config->update($request->validated());
        OperationLog::admin()->updateAction()->subject($config)->content('配置字典《%s》', $config->getAttribute('name'));

        return $this->successWithData($config, ApiCode::UPDATE_SUCCESS);
    }

    /**
     * @param \App\Http\Request\BooleanStatusRequest $request
     * @param \App\Models\SystemConfig               $config
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function changeStatus(BooleanStatusRequest $request, SystemConfig $config): Response
    {
        $config->update($request->validated());
        OperationLog::admin()->statusAction()->subject($config)->content('配置字典《%s》', $config->getAttribute('name'));
        return $this->success(ApiCode::UPDATE_SUCCESS);
    }
}