CollectionActivityController.php 3.74 KB
<?php

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

use App\Enums\ActivityAuditStatusEnum;
use App\Enums\ActivityStatusEnum;
use App\Helpers\JsonResource;
use App\Models\Activity;
use App\Models\Pivots\UserActivityCollectionPivot;
use App\Models\User;
use App\Support\Controller;
use Arr;
use DB;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

/**
 * 成员收藏活动
 */
class CollectionActivityController extends Controller
{
    public function __construct()
    {
        $this->middleware('emptyToNull');
    }

    /**
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request): JsonResponse
    {
        $pageSize  = $request->integer('size', 20);
        $filter    = $request->mergeIfMissing(['song_type' => 1])->except('page', 'size');
        $memberIds = User::query()->where('business_id', Auth::id())->pluck('id')->toArray();

        $collectQuery = UserActivityCollectionPivot::query()
            ->selectRaw('activity_id,max(created_at) as last_collect_at')
            ->whereIn('user_id', $memberIds)
            ->groupBy('activity_id');

        $activities = Activity::filter($filter)
            ->with(['project:id,name,cover', 'tags:id,name', 'user:id,nick_name'])
            ->joinSub($collectQuery, 'collect', 'collect.activity_id', 'activitys.id')
            ->selectRaw('activitys.id,activitys.cover,activitys.song_name,activitys.user_id,activitys.project_id,collect.last_collect_at,activitys.status')
            ->where('activitys.audit_status', ActivityAuditStatusEnum::SUCCESS)
            ->where('activitys.status', ActivityStatusEnum::UP)
            ->orderByDesc('collect.last_collect_at')
            ->paginate($pageSize);

        $collectQuery = UserActivityCollectionPivot::query()
            ->select(['activity_id', 'user_id', 'created_at'])
            ->addSelect(DB::raw('row_number() over (partition by activity_id order by created_at desc) as num'))
            ->whereIn('user_id', $memberIds)
            ->whereIn('activity_id', $activities->modelKeys());

        $items = DB::query()
            ->fromSub($collectQuery, 'collect')
            ->joinSub(User::query()->select(['id', 'nick_name', 'avatar']), 'u', 'u.id', 'collect.user_id')
            ->selectRaw('collect.activity_id,u.*,created_at as collect_at')
            ->where('collect.num', '<=', 4)
            ->get()->mapToGroups(fn($item) => [$item->activity_id => Arr::except((array)$item, 'activity_id')]);

        foreach ($activities as $activity) {
            $activity->setAttribute('items', $items->get($activity->getKey(), []));
        }


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

    /**
     * @param \Illuminate\Http\Request $request
     * @param int                      $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(Request $request, int $id): JsonResponse
    {
        $pageSize  = $request->integer('size', 20);
        $memberIds = User::query()->where('business_id', Auth::id())->pluck('id')->toArray();

        $users = UserActivityCollectionPivot::query()
            ->selectRaw('u.*,created_at as collect_at')
            ->joinSub(User::query()->select(['id', 'nick_name', 'avatar']), 'u', 'u.id', 'user_id')
            ->whereIn('user_id', $memberIds)
            ->where('activity_id', $id)
            ->orderByDesc('created_at')
            ->paginate($pageSize);


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

    }
}