ActivityController.php 3.72 KB
<?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);
    }


}