ActivityWorkController.php 2.49 KB
<?php

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

use App\Enums\ActivityWorkStatusEnum;
use App\Excel\ActivityWorkExcel;
use App\Helpers\OperationLog;
use App\Http\Service\ActivityService;
use App\Models\Activity;
use App\Models\Views\ActivityWork;
use Auth;
use Hikoon\LaravelApi\Facades\Response;
use Hikoon\LaravelApi\Support\ApiCode;
use Hikoon\LaravelApi\Support\ApiController;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class ActivityWorkController extends ApiController
{
    /**
     * @param \Illuminate\Http\Request          $request
     * @param \App\Http\Service\ActivityService $service
     * @return \Hikoon\LaravelApi\Facades\Response|\Symfony\Component\HttpFoundation\BinaryFileResponse
     */
    public function index(Request $request): Response|BinaryFileResponse
    {
        $pageSize  = $request->get('pageSize');
        $fetchType = $request->get('fetchType', 'page');
        $filter    = $request->except(['page', 'pageSize', 'fetchType']);

        $query = ActivityWork::filter($filter);

        return match ($fetchType) {
            'excel' => ActivityWorkExcel::withBuilder($query)->download(),
            default => $this->successWithData($query->paginate($pageSize))
        };
    }

    /**
     * @param \Illuminate\Http\Request          $request
     * @param \App\Models\Views\ActivityWork    $work
     * @param \App\Http\Service\ActivityService $service
     * @return \Hikoon\LaravelApi\Facades\Response
     * @throws \Hikoon\LaravelApi\Exceptions\ValidationException
     */
    public function changeStatus(Request $request, ActivityWork $work, ActivityService $service): Response
    {
        $attribute = $request->validate(
            ['status' => 'bail|required|in:1,2'],
            ['status.required' => '请选择操作类型', 'status.in' => '操作类型不在指定范围内']
        );

        $service->auditWork($work, ActivityWorkStatusEnum::from($attribute['status']));

        OperationLog::admin()->updateAction()->causer(Auth::user())->subject(new Activity(['id' => $work->getAttribute('activity_id')]))->content(
            '歌曲《%s》,%s,作品《%s-V%s版本》',
            $work->getAttribute('activity_name'),
            $attribute['status'] === ActivityWorkStatusEnum::REJECT->value ? '标记不合适' : '完成匹配',
            $work->getAttribute('user_nick_name'),
            $work->getAttribute('version')
        );

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