UserController.php
3.92 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\AdminSection\Controllers\Dashboard;
use App\Enums\UserStatusEnum;
use App\Models\SystemConfig;
use App\Models\SystemTag;
use App\Models\User;
use App\Models\UserCertify;
use App\Models\UserTagRelation;
use App\Support\Controller;
use Arr;
use Carbon\Carbon;
use Carbon\CarbonPeriod;
use Hikoon\LaravelApi\Facades\Response;
use Illuminate\Database\Query\Builder;
class UserController extends Controller
{
/**
* 认证数统计
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function certify(): Response
{
$calendar = collect();
CarbonPeriod::create(now()->startOfDay()->subDays(7), '1 day', 'now')->forEach(fn(Carbon $item) => $calendar->put($item->toDateString(), 0));
$keys = $calendar->keys()->toArray();
$trend = UserCertify::query()
->selectRaw("DATE_FORMAT(created_at,'%Y-%m-%d') as days,count(distinct user_id) as num")
->whereBetween('created_at', [Carbon::parse(Arr::first($keys))->startOfDay()->toDateTimeString(), Carbon::parse(Arr::last($keys))->endOfDay()->toDateTimeString()])
->groupByRaw('days')->pluck('num', 'days');
return $this->successWithData([
'time' => $keys,
'data' => $calendar->merge($trend)->values()
]);
}
/**
* 用户数统计
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function total(): Response
{
return $this->successWithData([
'type' => ['未认证', '音乐人', '经纪人', '厂牌管理员', '系统管理员'],
'data' => [
User::query()->where('identity', 0)->where('status', UserStatusEnum::ACTIVATE)->count(),
User::query()->whereIn('identity', [1, 3])->where('status', UserStatusEnum::ACTIVATE)->count(),
User::query()->whereIn('identity', [2, 3])->where('status', UserStatusEnum::ACTIVATE)->count(),
User::query()->where('scope', 2)->where('status', UserStatusEnum::ACTIVATE)->count(),
User::query()->where('scope', 1)->where('status', UserStatusEnum::ACTIVATE)->count(),
]
]);
}
/**
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function style(): Response
{
$singerTagIds = explode(',', SystemConfig::query()->where('identifier', 'auth_singer_tag')->value('content') ?? '');
SystemTag::resolveRelationUsing('users', static function (SystemTag $tag) use ($singerTagIds) {
return $tag->belongsToMany(User::class, UserTagRelation::class, 'tag_id', 'user_id')
->whereExists(fn(Builder $query) => $query->from('user_tag_relations', 'r')->selectRaw('1')->whereIn('r.tag_id', $singerTagIds)->where('r.type', 4)->whereColumn('r.user_id', 'users.id'))
->wherePivot('type', 1)->where('users.status', UserStatusEnum::ACTIVATE);
});
$result = collect();
SystemTag::query()->select(['id', 'name'])->withCount('users')->where('type', 1)->get()
->each(fn(SystemTag $tag) => $result->push(['value' => $tag->getAttribute('users_count'), 'name' => $tag->getAttribute('name')]));
return $this->successWithData($result);
}
/**
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function skill(): Response
{
SystemTag::resolveRelationUsing('users', static function (SystemTag $tag) {
return $tag->belongsToMany(User::class, UserTagRelation::class, 'tag_id', 'user_id')
->wherePivot('type', 4)->whereIn('users.identity', [1, 3])->where('users.status', UserStatusEnum::ACTIVATE);
});
$result = collect();
SystemTag::query()->select(['id', 'name'])->withCount('users')->where('type', 4)->get()
->each(fn(SystemTag $tag) => $result->push(['value' => $tag->getAttribute('users_count'), 'name' => $tag->getAttribute('name')]));
return $this->successWithData($result);
}
}