ActivityController.php
3.72 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
<?php
namespace App\Http\Container\AdminSection\Controllers\Dashboard;
use App\Enums\ActivityStatusEnum;
use App\Models\Activity;
use App\Models\Pivots\ActivityTagPivot;
use App\Models\SystemTag;
use App\Models\UserAction;
use App\Support\Controller;
use Carbon\Carbon;
use Carbon\CarbonPeriod;
use DB;
use Hikoon\LaravelApi\Facades\Response;
class ActivityController extends Controller
{
/**
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function total(): Response
{
$calendar = CarbonPeriod::create(now()->startOfDay()->subDays(7), '1 day', 'now');
$trend = DB::table('activity_status_records')
->whereBetween('time', [$calendar->first()?->startOfDay()->toDateTimeString(), $calendar->last()?->endOfDay()->toDateTimeString()])
->pluck('up', 'time');
$today = [today()->toDateString() => Activity::query()->where('activitys.audit_status', 1)->where('activitys.status', 1)->count()];
$time = collect();
$calendar->forEach(fn(Carbon $item) => $time->put($item->toDateString(), 0));
return $this->successWithData([
'time' => $time->keys()->toArray(),
'data' => $time->merge($trend)->merge($today)->values()->toArray()
]);
}
/**
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function listen(): Response
{
$calendar = CarbonPeriod::create(now()->startOfDay()->subDays(7), '1 day', 'now');
$trend = UserAction::query()
->selectRaw("DATE_FORMAT(created_at,'%Y-%m-%d') as days,count(distinct user_id) as num")
->whereIn('event_name', ['listen_clip', 'listen_entire'])
->whereBetween('created_at', [$calendar->first()?->startOfDay()->toDateTimeString(), $calendar->last()?->endOfDay()->toDateTimeString()])
->groupByRaw('days')->pluck('num', 'days');
$time = collect();
$calendar->forEach(fn(Carbon $item) => $time->put($item->toDateString(), 0));
return $this->successWithData([
'time' => $time->keys()->toArray(),
'data' => $time->merge($trend)->values()->toArray()
]);
}
/**
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function like(): Response
{
$calendar = CarbonPeriod::create(now()->startOfDay()->subDays(7), '1 day', 'now');
$trend = UserAction::query()
->selectRaw("DATE_FORMAT(created_at,'%Y-%m-%d') as days,count(distinct user_id) as num")
->whereIn('event_name', ['bottom_like', 'song_like', 'sing_like'])
->whereBetween('created_at', [$calendar->first()?->startOfDay()->toDateTimeString(), $calendar->last()?->endOfDay()->toDateTimeString()])
->groupByRaw('days')->pluck('num', 'days');
$time = collect();
$calendar->forEach(fn(Carbon $item) => $time->put($item->toDateString(), 0));
return $this->successWithData([
'time' => $time->keys()->toArray(),
'data' => $time->merge($trend)->values()->toArray()
]);
}
/**
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function style(): Response
{
SystemTag::resolveRelationUsing('activities', static function (SystemTag $tag) {
return $tag->belongsToMany(Activity::class, ActivityTagPivot::class, 'tag_id', 'activity_id')
->where('activitys.status', ActivityStatusEnum::UP);
});
$result = collect();
SystemTag::query()->select(['id', 'name'])->withCount('activities')->where('type', 1)->get()
->each(fn(SystemTag $tag) => $result->push(['value' => $tag->getAttribute('activities_count'), 'name' => $tag->getAttribute('name')]));
return $this->successWithData($result);
}
}