AuditionController.php
18 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<?php
namespace App\Http\Container\AppSection\Controllers;
use App\Helpers\JsonResource;
use App\Models\Activity;
use App\Models\ActivityShareUser;
use App\Models\Pivots\UserActivityCollectionPivot;
use App\Models\Pivots\UserProjectPivot;
use App\Models\Project;
use App\Models\SystemConfig;
use Hikoon\LaravelApi\Support\ApiController;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
class AuditionController extends ApiController
{
public function collection(Request $request)
{
$status = $request->get('status', 'all');
$text = $request->get('text', '');
$pageSize = $request->integer('size', 10);
$song_type = $request->get('song_type', 1);
$user_id = Auth::id();
$activity_ids = DB::table('user_activity_collections')
->where('user_id', $user_id)
->whereNull('deleted_at')
->orderByDesc('created_at')
->pluck('activity_id')->toArray();
$query = Activity::query();
if (!empty($text)){
$query->where('song_name', 'like', '%'.$text.'%');
}
if ($status == 'active'){
$query->where('status', 1);
}
$data =$query
->with('user:id,nick_name,avatar')
->whereIn('id', $activity_ids)
->where('song_type', $song_type)
->with(['tags:id,name', 'submitUsers:id,nick_name,intro,real_name,avatar,province,city,company,rate'])
->select(['id','cover','song_name','status','is_official','created_at','sub_title','guide','guide_clip','project_id', 'user_id','lyric','clip_lyric','sex','mark','speed','lang'])
->withCount(['attend_user as attend_count', 'viewUsersCount as view_count'])
->orderByRaw(DB::raw("FIELD(id, " . implode(',', $activity_ids) . ")"))
->paginate($pageSize);
$config_ids = DB::table('system_config')
->whereIn('identifier', ['activity_lang', 'activity_speed', 'activity_sex', 'activity_mark'])
->whereNull('deleted_at')
->pluck('id')
->toArray();
$configs = DB::table('system_config')
->whereIn('parent_id', $config_ids)
->whereNull('deleted_at')->get(['identifier', 'name', 'content']);
$view_activity_ids = array_column($data->items(), 'id');
$collection_at_list = DB::table('user_activity_collections')
->whereIn('activity_id', $view_activity_ids)
->where('user_id', $user_id)
->whereNull('deleted_at')
->pluck('created_at', 'activity_id')
->toArray();
$projectIds = array_column($data->items(), 'project_id');
$projectList = Project::query()->leftJoin('users','users.id','=','projects.master_id')->whereIn('projects.id',$projectIds)
->where('projects.status','=',1)
->where('projects.deleted_at','=',null)->select(['projects.*','users.nick_name','users.real_name','users.avatar'])->get()->toArray();
foreach ($projectList as $k=>$v){
$projectList[$k]['master'] = [
'id'=>$v['master_id'],
'nick_name'=>$v['nick_name'],
'real_name'=>$v['real_name'],
'avatar'=>$v['avatar']
];
unset($projectList[$k]['nick_name']);
unset($projectList[$k]['real_name']);
unset($projectList[$k]['avatar']);
}
$data->each(static function (Activity $activity) use ($configs){
$langs = [];
$lang = $activity->lang ?? [];
foreach ($configs as $c) {
if ($c->identifier == $activity->sex) {
$activity->setAttribute('sex', $c->name ?? NULL);
}
if ($c->identifier == $activity->mark) {
$activity->setAttribute('mark', $c->content ?? NULL);
}
if ($c->identifier == $activity->speed) {
$activity->setAttribute('speed', $c->name ?? NULL);
}
if (in_array($c->identifier, $lang)) {
$langs[] = $c->name ?? NULL;
}
}
$activity->setAttribute('lang', $langs);
$activity->setRelation('attend_user', $activity->getRelation('submitUsers')?->unique()->take(6) ?? []);
$activity->unsetRelation('submitUsers');
});
$result = json_decode(json_encode($data->items()), true);
foreach ($result as &$v) {
$project = array_filter(array_map(function ($item) use($v){
if($item['id']==$v['project_id']){
return $item;
}
},$projectList));
$v['project'] = array_values($project);
$v['collection_at'] = !empty($collection_at_list[$v['id']]) ? $collection_at_list[$v['id']]: '';
unset($v['user_id'], $v['project_id']);
}
return JsonResource::success(JsonResource::SUCCESS, ['activitys' => $result, 'count' => $data->total()]);
}
public function sub(Request $request)
{
$activity_id = $request->get('activity_id', '');
if (empty($activity_id)) {
return JsonResource::success('收藏失败-参数错误。请联系管理员', code: 201);
}
$user_id = Auth::id();
$is_activity = DB::table('user_activity_collections')
->where('user_id', $user_id)
->where('activity_id', $activity_id)
->whereNull('deleted_at')
->orderByDesc('created_at')
->pluck('id')->first();
if (!empty($is_activity)) {
return JsonResource::success('请勿重复收藏', code: 400 );
}
DB::table('user_activity_collections')->insert([
'user_id'=> $user_id,
'activity_id'=> $activity_id,
'created_at'=> date('Y-m-d H:i:s', time()),
'updated_at'=> date('Y-m-d H:i:s', time())
]);
$user = DB::table('users')->where('id', $user_id)->first();
$business_id = $user->business_id;
$role = $user->role;
# 有商务且为歌手
if ($business_id && $role=='Singer') {
$msg_data = [
'title' => '',
'content' => '',
'sender_id' => $user_id,
'receiver_id' => $user_id,
'activity_id' => $activity_id,
'type' => 2,
'is_read' => 0,
'is_bind' => 1,
'created_at'=> date('Y-m-d H:i:s', time()),
'updated_at'=> date('Y-m-d H:i:s', time())
];
DB::table('user_messages')->insert($msg_data);
$activity = DB::table('activitys')->where('id', $activity_id)->first();
$song_name = !empty($activity->id) ? $activity->song_name : '';
# 推送消息给推荐人
$business = DB::table('users')->where('id', $business_id)->first();
$business_unionid = !empty($business->id) ? $business->unionid : '';
if (!empty($business_unionid)) {
$business_wechat_official = DB::table('wechat_official_users')->where('union_id', $business_unionid)->where('is_subscribe', 1)->first();
if (!empty($business_wechat_official->id)) {
$business_open_id = $business_wechat_official->open_id;
$name = $user->nick_name . '(' . $user->real_name . ')';
$channel = 'singer_related';
if (env('TEST_URL', false)) {
$url = 'https://hi-sing-admin-dev.hikoon.com/api/provider/wechat/pushSubscribeMessage';
} else {
$url = 'https://hi-sing-admin.hikoon.com/api/provider/wechat/pushSubscribeMessage';
}
$md5_str = $channel . $business_open_id . 'tb0iwb7TE9TIbkG8iFsxldrHJRFdeP1g';
$req_data = [
'secret' => md5($md5_str),
'channel' => 'singer_related',
'open_ids' => [$business_open_id],
'data' => ["title" => "您的歌手收藏了试唱活动", "name" => $name, "intro" => "收藏了试唱活动《" . $song_name . "》",
"remark" => "点击前往 >",
"page" => "packageMy/pages/like?tab=1"]
];
$resp = Http::asJson()->post($url, $req_data)->json();
}
}
}
return JsonResource::success('收藏成功');
}
public function unsub(Request $request)
{
$activity_id = $request->get('activity_id');
$user_id = Auth::id();
$is_activity = DB::table('user_activity_collections')
->where('user_id', $user_id)
->where('activity_id', $activity_id)
->whereNull('deleted_at')
->orderByDesc('created_at')
->first();
if (empty($is_activity->id)) {
return JsonResource::success('未收藏不能取消', code: 400);
}
DB::table('user_activity_collections')->where('id', $is_activity->id)->update([
'updated_at'=> date('Y-m-d H:i:s', time()),
'deleted_at'=> date('Y-m-d H:i:s', time())
]);
$m_data = ['sender_id'=>$user_id,'activity_id'=>$activity_id,'type'=>2, 'is_read' => 0];
DB::table('user_messages')->where($m_data)->update([
'deleted_at'=> date('Y-m-d H:i:s', time())
]);
return JsonResource::success('已取消收藏');
}
public function bandLinkActivity(Request $request)
{
$id = $request->get('id');
$pageSize = $request->integer('size', 10);
$page = $request->integer('page', 1);
$song_type = $request->get('song_type', 1);
$song_name = $request->get('song_name', '');
$user_id = Auth::id();
$query = DB::table('activitys')
->where('project_id', $id)
->where('audit_status', 1)
->where('status', 1)
->where('song_type', $song_type);
if (!empty($song_name)) {
$query->where('song_name', 'like', '%'.$song_name.'%');
}
$activity_ids = $query
->whereNull('deleted_at')
->pluck('id')->toArray();
if (empty($activity_ids)) {
return JsonResource::success(JsonResource::SUCCESS, ['data' => [], 'count' => 0]);
}
$unlike_activity_ids = DB::table('user_activity_unlikes')
->where('user_id', $user_id)
->whereNull('deleted_at')
->pluck('activity_id')->toArray();
$activity_id_list = [];
foreach ($activity_ids as $v) {
if (!in_array($v, $unlike_activity_ids)){
$activity_id_list[] = $v;
}
}
if (empty($activity_id_list)) {
return JsonResource::success(JsonResource::SUCCESS, ['data' => [], 'count' => 0]);
}
$def_sql = "select activitys.id,activitys.song_name,activitys.project_id,activitys.sub_title,activitys.created_at,
IF(uva.id IS NULL,0,1) AS `is_listen`,
IF(ahu.id IS NULL,0,1) AS `is_submit`,
IF(uac.id IS NULL,0,1) AS `is_collection`,
IF(ahu.`status`=1,1,0) AS `is_take`,
(select count(*) from user_view_activitys where user_view_activitys.activity_id = activitys.id ) as view_count,
(select count(*) from activity_has_users where activity_has_users.activity_id = activitys.id and activity_has_users.type='Submit' AND activity_has_users.deleted_at is NULL ) as attend_count
from `activitys`
left join `user_activity_collections` as uac on `uac`.`activity_id` = `activitys`.`id` and `uac`.user_id=".$user_id." and `uac`.deleted_at is NULL
left join `user_view_activitys` as uva on `uva`.`activity_id` = `activitys`.`id` and `uva`.user_id=".$user_id."
left join `activity_has_users` as ahu on `ahu`.`activity_id` = `activitys`.`id` and `ahu`.user_id=".$user_id." and `ahu`.`type`='Submit' and `ahu`.deleted_at is NULL
where activitys.id IN (".implode(',', $activity_ids).") AND activitys.`status`=1 AND audit_status=1 AND activitys.deleted_at is NULL GROUP BY activitys.id,uac.id,uva.id,ahu.id ";
if ($song_type == 2) {
$sort_sql = "order by p_is_top desc,publish_at desc";
} else {
$sort_sql = "order by p_is_top desc,is_listen asc,activitys.weight desc,activitys.created_at desc";
}
$def_sql = $def_sql.$sort_sql;
$sql = $def_sql." limit ".$pageSize." offset ".($page - 1) * $pageSize;
$total_sql = "SELECT COUNT(*) AS total_count FROM (".$def_sql.") AS subquery";
$total = DB::select($total_sql)[0]->total_count;
$result = DB::select($sql);
$list = json_decode(json_encode($result), true);
$search_activity_id = array_column($list, 'id');
$data =Activity::query()
->whereIn('id', $search_activity_id)
->with(['linkArranger:id,nick_name', 'tags:id,name', 'project:id,name,cover,is_promote', 'comfirmTime:project_id,average_day,updated_at'])
->select(['id','cover','song_name','status','sub_title','guide','guide_clip','project_id', 'user_id','lyric','clip_lyric','sex','mark','speed','lang','p_is_top', 'publish_at','estimate_release_at','guide_duration','guide_clip_duration','karaoke','karaoke_clip', 'expand'])
->withCount(['publicAudio as public_audio_count', 'totalSong as total_song_count', 'onlineSong as online_song_count'])
->paginate($pageSize, pageName:'limit');
// 获取用户收藏
$userCollection = UserActivityCollectionPivot::query()
->selectRaw('activity_id,1 as num')
->where('user_id', $user_id)
->pluck('num', 'activity_id')->toArray();
$config = SystemConfig::query()->where('status', 1)
->whereHas('parent', fn(Builder $builder) => $builder->where('status', 1)->whereIn('identifier', ['activity_mark', 'activity_lang', 'activity_speed', 'activity_sex']))
->get(['name', 'identifier', 'content']);
$now_activity_ids = array_column($data->items(), 'id');
$now_project_ids = array_column($data->items(), 'project_id');
$view_id_list = DB::table('user_view_activitys')
->whereIn('activity_id', $now_activity_ids)
->where('user_id', $user_id)
->whereNull('deleted_at')
->pluck('activity_id')
->toArray();
$last_ids_list = DB::table('activitys')
->whereIn('id', $now_activity_ids)
->where('created_at', '>=', now()->subDays(7))
->whereNull('deleted_at')
->pluck('id')
->toArray();
$master_list = UserProjectPivot::query()
->whereIn('project_id', $now_project_ids)
->where('user_id', $user_id)
->pluck('project_id')
->toArray();
$share_user = ActivityShareUser::query()
->whereIn('activity_id', $now_activity_ids)
->where('user_id', $user_id)
->pluck('activity_id')
->toArray();
$data->each(static function (Activity $activity) use ($userCollection, $config, $view_id_list, $last_ids_list, $master_list, $share_user) {
$isCollection = $userCollection[$activity->getKey()] ?? 0;
$activity->setAttribute('arranger', ['in_side' => $activity->linkArranger, 'out_side' => $activity->expand['arranger']['supplement'] ?? []]);
unset($activity->linkArranger, $activity->expand);
$langs = [];
$lang = $activity->lang ?? [];
foreach ($config as $c) {
if ($c->identifier == $activity->sex) {
$activity->setAttribute('sex', $c->name ?? NULL);
}
if ($c->identifier == $activity->mark) {
$activity->setAttribute('mark', $c->content ?? NULL);
}
if ($c->identifier == $activity->speed) {
$activity->setAttribute('speed', $c->name ?? NULL);
}
if (in_array($c->identifier, $lang)) {
$langs[] = $c->name ?? NULL;
}
}
$now_id = $activity->getAttribute('id');
$is_new = 'new';
if (in_array($now_id, $view_id_list)) {
$is_new = 'listened';
}
if (!in_array($now_id, $view_id_list) && in_array($now_id, $last_ids_list)) {
$is_new = 'listened';
}
$activity->setAttribute('lang', $langs);
$activity->setAttribute('project', []);
$activity->setAttribute('status_tag', $is_new);
$is_confirm_share = in_array($now_id, $share_user);
$activity->setAttribute('is_confirm_share', $is_confirm_share);
$is_master = in_array($activity->getAttribute('project_id'), $master_list);
$activity->setAttribute('is_master', $is_master);
$activity->setAttribute('is_collection', $isCollection);
$is_promote = 0;
if ($activity->getRelation('project')) {
$is_promote = $activity->getRelation('project')->is_promote;
$project = $activity->getRelation('project')->toArray();
unset($project['is_promote']);
$activity->setAttribute('project', [$project]);
}
$activity->setAttribute('is_promote', $is_promote);
$activity->unsetRelation('project');
});
$result = json_decode(json_encode($data->items()), true);
$result_map = [];
foreach ($result as &$v) {
unset($v['user_id'], $v['project_id']);
$result_map[$v['id']] = $v;
}
$activity_list = [];
foreach ($search_activity_id as $k) {
$activity_list[] = $result_map[$k];
}
unset($result, $result_map, $data);
return JsonResource::success(JsonResource::SUCCESS, ['data' => $activity_list, 'count' => $total]);
}
}