ActivityController.php
10.6 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
namespace App\Http\Container\ManageSection\Controllers\Audition;
use App\Enums\ActivityAuditStatusEnum;
use App\Enums\ActivitySongTypeEnum;
use App\Enums\ActivityStatusEnum;
use App\Excel\ActivityDemoExcel;
use App\Excel\ActivityExcel;
use App\Excel\ActivitySongExcel;
use App\Helpers\OperationLog;
use App\Http\Container\AdminSection\Requests\Audition\ActivityChangeStatusRequest;
use App\Http\Container\ManageSection\Filters\ActivityFilter;
use App\Http\Container\ManageSection\Requests\Audition\ApplyCreateRequest;
use App\Http\Service\ActivityService;
use App\Jobs\ActivityChangeStatusJob;
use App\Jobs\ActivityMakeMediaJob;
use App\Models\Activity;
use App\Models\Pivots\UserActivityCollectionPivot;
use App\Models\Pivots\UserActivityViewPivot;
use App\Models\User;
use App\Models\Views\UserManageActivity;
use App\Support\Controller;
use App\Support\Model;
use Auth;
use Hikoon\LaravelApi\Facades\Response;
use Hikoon\LaravelApi\Support\ApiCode;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
class ActivityController extends Controller
{
/**
* @param \Illuminate\Http\Request $request
* @return \Hikoon\LaravelApi\Facades\Response|\Symfony\Component\HttpFoundation\BinaryFileResponse
*/
public function index(Request $request): Response|BinaryFileResponse
{
$pageSize = $request->get('pageSize', 20);
$fetchType = $request->get('fetchType', 'page');
$filter = $request->except(['page', 'pageSize', 'fetchType']);
$build = Activity::filter($filter, ActivityFilter::class)->where('audit_status', ActivityAuditStatusEnum::SUCCESS);
return match ($fetchType) {
'excel' => match ($request->integer('songType')) {
1 => ActivitySongExcel::withBuilder($build)->download(),
2 => ActivityDemoExcel::withBuilder($build)->download(),
default => ActivityExcel::withBuilder($build)->download(),
},
default => $this->successWithData($build->paginate($pageSize))
};
}
/**
* @param \App\Models\Activity $activity
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function show(Request $request, int $id): Response
{
$activity = Activity::filter($request->all(), ActivityFilter::class)->findOrFail($id);
return $this->successWithData($activity);
}
/**
* @param \App\Http\Container\ManageSection\Requests\Audition\ApplyCreateRequest $request
* @param \App\Models\Activity $activity
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function update(ApplyCreateRequest $request, Activity $activity): Response
{
if ($activity->getAttribute('status') === ActivityStatusEnum::PROCESS) {
return $this->fail(ApiCode::VALIDATION_ERROR, '音频处理中');
}
$activity->update(Model::dot($request->safe()));
OperationLog::manage()->subject($activity)->updateAction()
->content('《%s》%s', $activity->getAttribute('song_name'), $activity->getChangeColumn()->except(['audit_status', 'status'])->format(',修改了:'));
if ($activity->wasChanged(['lyric', 'clip_lyric', 'expand->guide_source->url', 'expand->karaoke_source->url'])) {
ActivityMakeMediaJob::dispatch($activity, $activity->getAttribute('status'), $activity->wasChanged(['expand->guide_source->url', 'expand->karaoke_source->url']));
$activity->update(['status' => 0]);
}
return $this->successWithData($activity, ApiCode::UPDATE_SUCCESS);
}
/**
* @param \App\Http\Container\AdminSection\Requests\Audition\ActivityChangeStatusRequest $request
* @param \App\Models\Activity $activity
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function changeStatus(ActivityChangeStatusRequest $request, Activity $activity, ActivityService $service): Response
{
$attribute = $request->validated();
//上架
if ($attribute['status'] === 'up' && $activity->getAttribute('status') === ActivityStatusEnum::DOWN) {
$activity->update(['status' => ActivityStatusEnum::UP, 'p_is_top' => 0, 'publish_at' => now()->toDateTimeString()]);
OperationLog::manage()->statusAction()->subject($activity)->content('上架试唱歌曲《%s》', $activity->getAttribute('song_name'));
if ($activity->getAttribute('song_type') === ActivitySongTypeEnum::SONG->value) {
ActivityChangeStatusJob::dispatch($activity, Auth::user());
}
return $this->success(ApiCode::UPDATE_SUCCESS);
}
//下架
if ($attribute['status'] === 'down' && $activity->getAttribute('status') === ActivityStatusEnum::UP) {
$activity->update(['status' => ActivityStatusEnum::DOWN]);
OperationLog::manage()->statusAction()->subject($activity)->content('下架试唱歌曲《%s》,理由:%s', $activity->getAttribute('song_name'), $attribute['msg']);
if ($activity->getAttribute('song_type') === ActivitySongTypeEnum::SONG->value) {
ActivityChangeStatusJob::dispatch($activity, Auth::user(), ['msg' => $attribute['msg']]);
}
return $this->success(ApiCode::UPDATE_SUCCESS);
}
//发行
if ($attribute['status'] === 'send' && $activity->getAttribute('status') === ActivityStatusEnum::MATCH) {
if ($activity->getAttribute('status') === ActivityStatusEnum::MATCH) {
$activity->update(['status' => ActivityStatusEnum::SEND, 'send_at' => now()->toDateTimeString(), 'send_url' => [['type' => 'default', 'url' => $attribute['link']]]]);
OperationLog::manage()->statusAction()->subject($activity)->content('发行试唱歌曲《%s》', $activity->getAttribute('song_name'));
if ($activity->getAttribute('song_type') === ActivitySongTypeEnum::SONG->value) {
ActivityChangeStatusJob::dispatch($activity, Auth::user());
}
}
if ($activity->getAttribute('status') === ActivityStatusEnum::SEND) {
$activity->update(['send_url->url' => $attribute['link']]);
OperationLog::manage()->updateAction()->subject($activity)->content('试唱歌曲《%s》,修改了:外部平台播放链接', $activity->getAttribute('song_name'));
}
return $this->success(ApiCode::UPDATE_SUCCESS, $activity);
}
//重新上架
if ($attribute['status'] === 'reUp' && $activity->getAttribute('status') === ActivityStatusEnum::MATCH) {
$service->reUp($activity);
OperationLog::manage()->statusAction()->subject($activity)->content('重新上架试唱歌曲《%s》', $activity->getAttribute('song_name'));
return $this->success(ApiCode::UPDATE_SUCCESS);
}
return $this->fail(ApiCode::VALIDATION_ERROR, '此活动状态无法变更状态');
}
/**
* @param \App\Models\Activity $activity
* @param \App\Http\Service\ActivityService $service
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function destroy(Activity $activity, ActivityService $service): Response
{
if ($activity->getAttribute('status') !== ActivityStatusEnum::DOWN) {
return $this->fail(ApiCode::VALIDATION_ERROR, '只允许删除下架歌曲');
}
$service->delete($activity);
OperationLog::admin()->subject($activity)->deleteAction()->content('歌曲《%s》', $activity->getAttribute('song_name'));
return $this->success(ApiCode::DELETE_SUCCESS);
}
/**
* @param \Illuminate\Http\Request $request
* @param \App\Models\Activity $activity
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function viewUser(Request $request, Activity $activity): Response
{
$pageSize = $request->get('pageSize', 20);
$viewPivotQuery = UserActivityViewPivot::query()
->selectRaw('user_id,activity_id,updated_at as last_listen_at')
->where('activity_id', $activity->getKey());
$data = User::withTrashed()
->filter($request->except(['page', 'pageSize']))
->select(['id', 'nick_name', 'real_name', 'avatar', 'role', 'sex', 'identity', 'last_listen_at'])
->joinSub($viewPivotQuery, 'views', 'user_id', 'users.id')
->withCount(['listenActivities as listen_count', 'likeActivities as collection_count', 'submitActivities as submit_work'])
->paginate($pageSize);
return $this->successWithData($data);
}
/**
* @param \Illuminate\Http\Request $request
* @param \App\Models\Activity $activity
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function likeUser(Request $request, Activity $activity): Response
{
$pageSize = $request->get('pageSize', 20);
$viewPivotQuery = UserActivityCollectionPivot::query()
->selectRaw('user_id,activity_id,updated_at as last_listen_at')
->where('activity_id', $activity->getKey());
$data = User::withTrashed()
->filter($request->except(['page', 'pageSize']))
->select(['id', 'nick_name', 'real_name', 'avatar', 'role', 'sex', 'identity', 'last_listen_at'])
->joinSub($viewPivotQuery, 'views', 'user_id', 'users.id')
->withCount(['listenActivities as listen_count', 'likeActivities as collection_count', 'submitActivities as submit_work'])
->paginate($pageSize);
return $this->successWithData($data);
}
/**
* @param \Illuminate\Http\Request $request
* @param \App\Models\Activity $activity
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function manageUser(Request $request, Activity $activity): Response
{
$pageSize = $request->get('pageSize', 20);
$filter = $request->except(['page', 'pageSize', 'unSet']);
if ($request->has('unSet')) {
User::resolveRelationUsing('manageActivities', static fn(User $user) => $user->hasMany(UserManageActivity::class));
return $this->successWithData(User::filter($filter)->withTrashed()
->whereDoesntHave('manageActivities', fn($query) => $query->where('activity_id', $activity->getKey()))
->paginate($pageSize));
}
return $this->successWithData(UserManageActivity::filter($filter)
->with('user:id,avatar,nick_name,real_name,sex,email,area_code,phone,official_status,status,identity')
->where('activity_id', $activity->getKey())
->paginate($pageSize));
}
}