ActivityController.php 3.26 KB
<?php

namespace App\Http\Container\AppSection\Controllers\User;

use App\Enums\ActivityAuditStatusEnum;
use App\Enums\ActivityCreateFormEnum;
use App\Enums\ActivityStatusEnum;
use App\Helpers\JsonResource;
use App\Http\Request\BooleanStatusRequest;
use App\Models\Activity;
use App\Models\Pivots\UserActivityCollectionPivot;
use App\Support\Controller;
use Auth;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ActivityController extends Controller
{
    /**
     * @return array
     */
    public function getSelfActivityStatus(): array
    {
        return [
            ActivityStatusEnum::PROCESS->value,
            ActivityStatusEnum::UP->value,
            ActivityStatusEnum::MATCH->value,
            ActivityStatusEnum::FAIL->value,
            ActivityStatusEnum::SEND->value
        ];
    }

    /**
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request): JsonResponse
    {
        $size   = $request->get('size', 10);
        $filter = $request->mergeIfMissing([
            'user_id' => Auth::id(),
            'status'  => $request->integer('user_id') === Auth::id() ? $this->getSelfActivityStatus() : ActivityStatusEnum::UP->value
        ])->except('page', 'size');


        $data = Activity::filter($filter)
            ->select(['id', 'song_name', 'cover', 'user_id', 'status', 'publish_at'])
            ->selectRaw('if(u_top = 0, 0 , 1) as is_top')
            ->addSelect([
                'is_collection' => UserActivityCollectionPivot::selectRaw('IF(COUNT(*) = 0, 0, 1)')->where('user_id', Auth::id())->whereColumn('activity_id', 'activitys.id'),
            ])
            ->with(['tags:id,name', 'user:id,nick_name,avatar'])
            ->where('created_form', ActivityCreateFormEnum::USER)
            ->latest('u_top')->latest('publish_at')
            ->paginate($size);

        return JsonResource::success(JsonResource::SUCCESS, ['data' => $data->items(), 'count' => $data->total()]);
    }

    /**
     * @param \App\Http\Request\BooleanStatusRequest $request
     * @param int                                    $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function changeTop(BooleanStatusRequest $request, int $id): JsonResponse
    {
        $activity = Activity::query()
            ->where('user_id', Auth::id())
            ->where('created_form', ActivityCreateFormEnum::USER)
            ->find($id);

        if ($activity?->getAttribute('user_id') !== Auth::id()) {
            return JsonResource::fail(JsonResource::NO_PERMISSION);
        }

        $isTop = $request->integer('status') === 1;

        if (
            $isTop &&
            Activity::query()->where('user_id', Auth::id())->where('created_form', ActivityCreateFormEnum::USER)
                ->where('song_type', $activity->getAttribute('song_type'))
                ->where("audit_status", ActivityAuditStatusEnum::SUCCESS)
                ->where('status', ActivityStatusEnum::UP)
                ->where('u_top', '!=', 0)->count() >= 3

        ) {
            return JsonResource::fail('最多允许置顶3条内容');
        }

        $activity->update(['u_top' => $isTop ? now()->timestamp : 0]);

        return JsonResource::success(JsonResource::OPERATION_SUCCESS);
    }
}