ReceptionActivityController.php 4.07 KB
<?php

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

use App\Helpers\JsonResource;
use App\Models\Activity;
use App\Models\User;
use App\Models\UserShare;
use App\Support\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

/**
 * 成员收到分享活动
 */
class ReceptionActivityController 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', 15);
        $filter   = $request->except('page', 'size');


        $shareQuery = UserShare::query()
            ->selectRaw('subject_id,max(created_at) as last_share_at')
            ->where('subject_type', Activity::class)
            ->whereRelation('toUser', 'users.business_id', Auth::id())
            ->groupBy('subject_id');

        $activities = Activity::filter($filter)->with(['project:id,name,cover', 'tags:id,name', 'user:id,nick_name,avatar'])
            ->joinSub($shareQuery, 'share', 'share.subject_id', 'activitys.id')
            ->selectRaw('activitys.id,activitys.cover,activitys.song_name,activitys.user_id,activitys.project_id,share.last_share_at,activitys.status')
            ->where('activitys.audit_status', 1)
            ->orderByDesc('share.last_share_at')
            ->paginate($pageSize);

        $formQuery = User::query()->select(['id', 'nick_name', 'avatar']);
        $toQuery   = User::query()->select(['id', 'nick_name', 'avatar'])->where('business_id', Auth::id());

        $shareQuery = UserShare::query()
            ->selectRaw('user_share.id,user_share.subject_id,user_share.from_id,f.nick_name as from_nick_name,f.avatar as from_avatar,user_share.to_id,t.nick_name as to_nick_name,t.avatar as to_avatar,user_share.created_at,row_number() over ( partition by `user_share`.`subject_id` order by `user_share`.`created_at` desc) as num')
            ->joinSub($formQuery, 'f', 'f.id', 'user_share.from_id')
            ->joinSub($toQuery, 't', 't.id', 'user_share.to_id')
            ->where('user_share.subject_type', Activity::class)
            ->whereIn('user_share.subject_id', $activities->modelKeys());

        $items = UserShare::query()
            ->fromSub($shareQuery, 'l')
            ->select(['id', 'from_id', 'from_nick_name', 'from_avatar', 'to_id', 'to_nick_name', 'to_avatar', 'created_at', 'subject_id'])
            ->where('l.num', '<=', 4)
            ->get();

        foreach ($activities as $activity) {
            $activity->setAttribute('items', $items->where('subject_id', $activity->getKey())->values());
        }

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


    /**
     * 详情
     * @param \Illuminate\Http\Request $request
     * @param int                      $id
     * @return JsonResponse
     */
    public function show(Request $request, int $id): JsonResponse
    {
        $pageSize  = $request->integer('size', 15);
        $formQuery = User::query()->select(['id', 'nick_name', 'avatar']);
        $toQuery   = User::query()->select(['id', 'nick_name', 'avatar'])->where('business_id', Auth::id());
        $shareData = UserShare::query()
            ->selectRaw('user_share.id,user_share.from_id,f.nick_name as from_nick_name,f.avatar as from_avatar,user_share.to_id,t.nick_name as to_nick_name,t.avatar as to_avatar,user_share.created_at')
            ->joinSub($formQuery, 'f', 'f.id', 'user_share.from_id')
            ->joinSub($toQuery, 't', 't.id', 'user_share.to_id')
            ->where('user_share.subject_type', Activity::class)
            ->where('user_share.subject_id', $id)
            ->orderByDesc('created_at')
            ->paginate($pageSize);

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