ShowController.php 3.65 KB
<?php

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

use App\Enums\ActivityAuditStatusEnum;
use App\Enums\ActivityCreateFormEnum;
use App\Enums\ActivityWorkTypeEnum;
use App\Enums\UserScopeEnum;
use App\Helpers\ConfigHelper;
use App\Helpers\JsonResource;
use App\Models\Activity;
use App\Models\Pivots\UserActivityCollectionPivot;
use App\Models\Pivots\UserActivityViewPivot;
use App\Models\UserShare;
use App\Models\Views\ActivityWork;
use App\Support\Controller;
use App\Traits\UserTrait;
use Arr;
use Auth;
use Illuminate\Http\JsonResponse;

class ShowController extends Controller
{
    use UserTrait;

    /**
     * @param int $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function __invoke(int $id): JsonResponse
    {
        $this->user = Auth::user();

        $activity = tap($this->getActivity($id), static fn($activity) => ConfigHelper::formatActivityDictionary($activity));

        $isSystemUser = $this->isScopeEquals(UserScopeEnum::SYSTEM);

        $isProjectManager = $this->isProjectManager($activity->getAttribute('project_id'));

        $isDemoCreate = $activity->getAttribute('created_form') === ActivityCreateFormEnum::USER->value && Auth::id() === $activity->getAttribute('user_id');

        return JsonResource::success(JsonResource::SUCCESS, Arr::except($activity->toArray(), ['expand', 'project_id', 'user_id', 'link_arranger']) + [
                'is_collection'      => (int)UserActivityCollectionPivot::query()->where('activity_id', $activity->getKey())->where('user_id', Auth::id())->exists(),
                'is_can_share'       => (int)($isSystemUser || $this->isScopeEquals(UserScopeEnum::PROJECT) || $isDemoCreate),
                'is_can_download'    => (int)($isSystemUser || $isProjectManager || $isDemoCreate || $this->hasSingPermission()),
                'public_audio_count' => $this->getPublicAudioCount($activity->getKey()),
                'share_count'        => $this->getShareCount($activity->getKey()),
                'listen_count'       => $this->getListenCount($activity->getKey())
            ]);
    }

    /**
     * @param int $activityId
     * @return \App\Models\Activity
     */
    public function getActivity(int $activityId): Activity
    {
        return Activity::query()
            ->with(['tags:id,name', 'project:id,name,cover', 'linkArranger:id,nick_name', 'user:id,nick_name,avatar'])
            ->where('audit_status', ActivityAuditStatusEnum::SUCCESS)
            ->findOrFail($activityId, [
                'id', 'song_name', 'song_type', 'cover', 'is_lyric', 'lyric', 'clip_lyric', 'expand', 'user_id',
                'guide', 'guide_duration', 'guide_clip', 'guide_clip_duration', 'karaoke', 'karaoke_clip',
                'status', 'estimate_release_at', 'publish_at', 'sex', 'mark', 'speed', 'lang', 'project_id',
                'created_form'
            ]);
    }

    /**
     * @param int $activityId
     * @return int
     */
    public function getListenCount(int $activityId): int
    {
        return UserActivityViewPivot::query()->where('activity_id', $activityId)->whereHas('user')->distinct()->count('user_id');
    }

    /**
     * @param int $activityId
     * @return int
     */
    public function getShareCount(int $activityId): int
    {
        return UserShare::query()->where('subject_type', Activity::class)->where('subject_id', $activityId)->whereHas('toUser')->distinct()->count('to_id');
    }

    /**
     * @param int $activityId
     * @return int
     */
    public function getPublicAudioCount(int $activityId): int
    {
        return ActivityWork::query()->where('type', ActivityWorkTypeEnum::SUBMIT)->where('activity_id', $activityId)->where('is_hide', 0)->count();
    }
}