CollectionActivityController.php
3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?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()
]);
}
}