ActivityController.php
15.4 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
namespace App\Http\Container\AdminSection\Controllers\Audition;
use App\Enums\ActivityAuditStatusEnum;
use App\Enums\ActivitySongTypeEnum;
use App\Enums\ActivityStatusEnum;
use App\Enums\ActivityWorkStatusEnum;
use App\Enums\ActivityWorkTypeEnum;
use App\Excel\ActivityDemoExcel;
use App\Excel\ActivityExcel;
use App\Excel\ActivitySongExcel;
use App\Excel\ActivityWorkExcel;
use App\Helpers\OperationLog;
use App\Http\Container\AdminSection\Requests\Audition\ActivityChangeStatusRequest;
use App\Http\Container\AdminSection\Requests\Audition\ActivityCreateRequest;
use App\Http\Container\AdminSection\Requests\Audition\ActivityUpdateRequest;
use App\Http\Service\ActivityService;
use App\Jobs\ActivityChangeStatusJob;
use App\Jobs\ActivityMakeMediaJob;
use App\Models\Activity;
use App\Models\ActivityShareUser;
use App\Models\Pivots\UserActivityCollectionPivot;
use App\Models\Pivots\UserActivityViewPivot;
use App\Models\User;
use App\Models\Views\ActivityWork;
use App\Models\Views\UserManageActivity;
use App\Support\Controller;
use App\Support\Model;
use Arr;
use Auth;
use DB;
use Hikoon\LaravelApi\Facades\Response;
use Hikoon\LaravelApi\Support\ApiCode;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
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)->where('audit_status', 1);
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\Http\Container\AdminSection\Requests\Audition\ActivityCreateRequest $request
* @param \App\Http\Service\ActivityService $service
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function store(ActivityCreateRequest $request, ActivityService $service): Response
{
$data = array_merge($request->safe()->toArray(), [
'song_type' => 1, 'audit_status' => 1, 'audit_at' => now()->toDateTimeString(), 'status' => 0, 'user_id' => Auth::id(), 'publish_at' => now()->toDateTimeString()
]);
if (Cache::lock('user-' . Auth::id() . '-create-activity', 5)->get()) {
$result = DB::transaction(static function () use ($data, $service) {
$activity = Activity::query()->create(Arr::except($data, 'out_side_manages'));
if (isset($data['out_side_manages'])) {
$service->syncManageLink($activity, $data['out_side_manages']);
}
OperationLog::admin()->subject($activity)->createAction()->content('歌曲《%s》', $activity->getAttribute('song_name'));
ActivityMakeMediaJob::dispatch($activity, ActivityStatusEnum::UP, true);
return $activity->loadMissing('user:id,nick_name,real_name');
});
return $this->successWithData($result, ApiCode::CREATE_SUCCESS);
}
return $this->fail(ApiCode::REQUEST_LIMIT_ERROR);
}
/**
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function show(Request $request, int $id): Response
{
$activity = Activity::filter($request->all())->findOrFail($id);
return $this->successWithData($activity);
}
/**
* @param \App\Http\Container\AdminSection\Requests\Audition\ActivityUpdateRequest $request
* @param \App\Models\Activity $activity
* @param \App\Http\Service\ActivityService $service
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function update(ActivityUpdateRequest $request, Activity $activity, ActivityService $service): Response
{
if ($activity->getAttribute('status') === ActivityStatusEnum::PROCESS) {
return $this->fail(ApiCode::VALIDATION_ERROR, '音频处理中');
}
$data = $request->safe()->toArray();
$activity->update(Model::dot(Arr::except($data, 'out_side_manages')));
$change = $activity->getChangeColumn();
if (isset($data['out_side_manages'])) {
$syncResult = $service->syncManageLink($activity, $data['out_side_manages']);
if (count($syncResult) !== 0) {
$change->add('manage_link');
}
}
OperationLog::admin()->subject($activity)->updateAction()
->content('《%s》%s', $activity->getAttribute('song_name'), $change->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
* @param \App\Http\Service\ActivityService $service
* @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::admin()->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::admin()->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::admin()->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::admin()->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::admin()->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('audit_status') !== ActivityAuditStatusEnum::SUCCESS->value && $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', 'sex', 'scope', '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', 'sex', 'scope', '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|\Symfony\Component\HttpFoundation\BinaryFileResponse
*/
public function submitUser(Request $request, Activity $activity): Response|BinaryFileResponse
{
$pageSize = $request->get('pageSize');
$filter = $request->except(['page', 'pageSize', 'fetchType']);
$query = ActivityWork::filter($filter)->where('activity_id', $activity->getKey())->where('type', ActivityWorkTypeEnum::SUBMIT);
if ($request->get('fetchType') === 'excel') {
return ActivityWorkExcel::withBuilder($query)->download();
}
return $this->successWithData($query->paginate($pageSize));
}
/**
* @param \Illuminate\Http\Request $request
* @param \App\Models\Activity $activity
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function changeUserShare(Request $request, Activity $activity): Response
{
$data = $request->validate(['user_id' => 'bail|required', 'share_id' => 'bail|required'], ['user_id.required' => '请选择提交者', 'share_id.required' => '请选择分享者']);
ActivityShareUser::query()->updateOrCreate(['user_id' => $data['user_id'], 'activity_id' => $activity->getKey()], ['share_id' => $data['share_id']]);
return $this->success(ApiCode::UPDATE_SUCCESS);
}
/**
* @param \Illuminate\Http\Request $request
* @param \App\Models\Activity $activity
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function matchUser(Request $request, Activity $activity): Response
{
$pageSize = $request->get('pageSize');
$filter = $request->except(['page', 'pageSize', 'fetchType']);
$query = ActivityWork::filter($filter)
->where('activity_id', $activity->getKey())
->where('type', 'submit')
->where('status', ActivityWorkStatusEnum::SUCCESS);
return $this->successWithData($query->paginate($pageSize));
}
/**
* @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']);
$data = 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);
return $this->successWithData($data);
}
/**
* @param \Illuminate\Http\Request $request
* @param \App\Models\Activity $activity
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function sendNotify(Request $request, Activity $activity): Response
{
$request->validate([
'push_type' => 'bail|required_if:is_push,1|array',
'push_user' => 'bail|array',
'push_user.*' => 'bail|sometimes|numeric'
], [
'push_type.required_if' => '请选择推送方式',
'push_user.required_if' => '请选择推送歌手',
'push_user.array' => '请选择推送歌手',
'push_user.*.numeric' => '推送歌手参数错误'
]);
return $this->success(ApiCode::UPDATE_SUCCESS);
}
}