ReceptionActivityController.php
4.07 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
<?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()
]);
}
}