UserController.php 3.92 KB
<?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);
    }
}