ApplyController.php 3.52 KB
<?php

namespace App\Http\Container\ManageSection\Controllers\Audition;

use App\Enums\ActivityAuditStatusEnum;
use App\Enums\ActivitySongTypeEnum;
use App\Helpers\OperationLog;
use App\Http\Container\ManageSection\Filters\ActivityFilter;
use App\Http\Container\ManageSection\Requests\Audition\ApplyCreateRequest;
use App\Http\Container\ManageSection\Requests\Audition\ApplyUpdateRequest;
use App\Http\Service\ActivityService;
use App\Models\Activity;
use App\Support\Controller;
use App\Support\Model;
use Auth;
use Hikoon\LaravelApi\Facades\Response;
use Hikoon\LaravelApi\Support\ApiCode;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class ApplyController 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(['audit_status' => [0, 2]])->except(['page', 'pageSize']);
        return $this->successWithData(Activity::filter($filter, ActivityFilter::class)->paginate($pageSize));
    }

    /**
     * @param \App\Http\Container\ManageSection\Requests\Audition\ApplyCreateRequest $request
     * @param \App\Http\Service\ActivityService                                      $service
     * @return \Hikoon\LaravelApi\Facades\Response
     * @throws \Hikoon\LaravelApi\Exceptions\ValidationException
     */
    public function store(ApplyCreateRequest $request, ActivityService $service): Response
    {
        $attribute = array_merge($request->safe()->toArray(), ['user_id' => (int)Auth::id(), 'created_form' => 0]);

        if (Cache::lock('user-' . Auth::id() . '-apply', 5)->get()) {
            $activity = match ($request->integer('song_type')) {
                ActivitySongTypeEnum::SONG->value => $service->createActivityApply($attribute + ['lang' => [], 'is_push' => 0]),
                ActivitySongTypeEnum::DEMO->value => $service->createDemoApply($attribute + ['is_push' => 1])
            };
            return $this->successWithData($activity, ApiCode::CREATE_SUCCESS);
        }

        return $this->fail(ApiCode::REQUEST_LIMIT_ERROR);
    }

    /**
     * @param \App\Http\Container\ManageSection\Requests\Audition\ApplyCreateRequest $request
     * @param \App\Models\Activity                                                   $apply
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function update(ApplyUpdateRequest $request, Activity $apply): Response
    {
        $apply->update(Model::dot($request->safe()->merge(['audit_status' => 0, 'status' => 0])));

        OperationLog::manage()->subject($apply)->updateAction()
            ->content('《%s》,重新申请%s', $apply->getAttribute('song_name'), $apply->getChangeColumn()->except(['audit_status', 'status'])->format(',修改了:'));

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

    /**
     * @param \App\Models\Activity $apply
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function destroy(Activity $apply, ActivityService $service): Response
    {
        if ($apply->getAttribute('audit_status') !== ActivityAuditStatusEnum::FAIL->value) {
            return $this->fail(ApiCode::VALIDATION_ERROR, '只允许删除审核未通过');
        }

        $service->delete($apply);
        OperationLog::manage()->subject($apply)->deleteAction()->content('歌曲《%s》', $apply->getAttribute('song_name'));

        return $this->success(ApiCode::DELETE_SUCCESS);
    }
}