ShareController.php 2.21 KB
<?php

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

use App\Enums\ActivityAuditStatusEnum;
use App\Enums\ActivityCreateFormEnum;
use App\Enums\ActivitySongTypeEnum;
use App\Enums\ActivityStatusEnum;
use App\Enums\UserScopeEnum;
use App\Helpers\JsonResource;
use App\Http\Service\UserService;
use App\Models\Activity;
use App\Support\Controller;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ShareController extends Controller
{
    protected UserScopeEnum $scope;


    /**
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function demo(Request $request): JsonResponse
    {
        $pageSize    = $request->integer('size', 15);
        $search      = $request->get('search');
        $filter      = $request->except(['page', 'size']);
        $this->scope = Auth::user()?->getAttribute('scope');

        $data = Activity::filter($filter)->with(['tags:id,name', 'project:id,name,cover', 'user:id,nick_name,avatar'])
            ->select(['id', 'song_name', 'song_type', 'sub_title', 'user_id', 'cover', 'project_id'])
            ->when($search, fn(Builder $query) => $query->where('song_name', 'like', "%$search%"))
            ->when($this->scope !== UserScopeEnum::SYSTEM, fn(Builder $query) => $query->where(function (Builder $query) {
                if (UserService::hasDemoPermission(Auth::user())) {
                    $query->where(['user_id' => Auth::id(), 'created_form' => ActivityCreateFormEnum::USER]);
                }

                $projectIds = $this->scope === UserScopeEnum::PROJECT ? UserService::getManageProjectIds(Auth::id()) : [];

                if (count($projectIds) !== 0) {
                    $query->orWhereIn('project_id', $projectIds);
                }
            }))
            ->where('song_type', ActivitySongTypeEnum::DEMO)
            ->where('audit_status', ActivityAuditStatusEnum::SUCCESS)
            ->where('status', ActivityStatusEnum::UP)
            ->orderByDesc('audit_at')->paginate($pageSize);


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