DashboardController.php 7.65 KB
<?php

namespace App\Http\Container\ManageSection\Controllers;

use App\Enums\ActivitySongTypeEnum;
use App\Enums\ActivityStatusEnum;
use App\Enums\ActivityWorkModeEnum;
use App\Enums\ActivityWorkTypeEnum;
use App\Http\Container\AdminSection\Controllers\Dashboard\UserController;
use App\Http\Container\ManageSection\Filters\ActivityFilter;
use App\Models\Activity;
use App\Models\ActivityApplyRecord;
use App\Models\Pivots\ActivityTagPivot;
use App\Models\Pivots\UserProjectPivot;
use App\Models\Project;
use App\Models\SystemTag;
use App\Models\Views\ActivityWork;
use App\Support\Controller;
use Auth;
use Hikoon\LaravelApi\Facades\Response;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Str;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Vtiful\Kernel\Excel;
use Vtiful\Kernel\Format;

class DashboardController extends Controller
{
    public function userStyle()
    {
        return App::call(UserController::class . '@style');
    }

    /**
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function activityStyle(): Response
    {
        $authProjectIds = UserProjectPivot::query()->where('user_id', Auth::id())->pluck('project_id')->toArray();
        $project        = Project::query()->whereIn('id', $authProjectIds)->get(['id', 'name', 'is_public']);
        $publicProject  = $project->where('is_public', 1);
        $privateProject = $project->where('is_public', 0);

        SystemTag::resolveRelationUsing('activities', static function (SystemTag $tag) use ($publicProject, $privateProject) {
            return $tag->belongsToMany(Activity::class, ActivityTagPivot::class, 'tag_id', 'activity_id')
                ->where('activitys.status', ActivityStatusEnum::UP)
                ->where('activitys.song_type', 1)
                ->where(function (Builder $builder) use ($publicProject, $privateProject) {
                    $builder->whereIn('activitys.project_id', $publicProject->modelKeys())
                        ->orWhere(fn($query) => $query->whereIn('project_id', $privateProject->modelKeys())
                            ->where('activitys.user_id', Auth::id()));
                });
        });

        $result = collect();

        SystemTag::query()->select(['id', 'name'])->withCount('activities')->whereHas('activities')->where('type', 1)->get()
            ->each(fn(SystemTag $tag) => $result->push(['value' => $tag->getAttribute('activities_count'), 'name' => $tag->getAttribute('name')]));

        return $this->successWithData($result);
    }

    /**
     * @param \Illuminate\Http\Request $request
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function todo(Request $request): Response
    {
        $pageSize = $request->get('pageSize', 20);
        $data     = Activity::filter(['auditStatus' => 2, 'createdForm' => 0], ActivityFilter::class)
            ->with('project:id,name')
            ->select(['id', 'song_name', 'song_type', 'project_id'])
            ->addSelect(['msg' => ActivityApplyRecord::query()->select('audit_msg')->whereColumn('activity_id', 'activitys.id')->latest('id')->limit(1)])
            ->latest('updated_at')->paginate($pageSize);
        return $this->successWithData($data);
    }

    /**
     * @param \Illuminate\Http\Request $request
     * @return \Hikoon\LaravelApi\Facades\Response|\Symfony\Component\HttpFoundation\BinaryFileResponse
     */
    public function submitWork(Request $request): Response|BinaryFileResponse
    {
        $pageSize = $request->get('pageSize', 20);
        $filter   = $request->except('page', 'pageSize');

        $build = ActivityWork::filter($filter)
            ->joinSub(Activity::filter([], ActivityFilter::class)->selectRaw('id as ac_id'), 'ac', 'ac_id', 'activity_works.activity_id')
            ->select([
                'id', 'activity_id', 'activity_name', 'activity_type', 'activity_title', 'activity_status', 'project_id',
                'user_id', 'user_nick_name', 'user_real_name', 'user_identity', 'business_id',
                'price_id', 'mode', 'sing_type', 'demo_url', 'submit_at', 'status'
            ])
            ->with([
                'business' => fn($query) => $query->select(['id', 'nick_name', 'real_name', 'identity'])->where('identity', 3),
                'project:id,name',
                'tags:id,name',
                'price:id,value,is_deduct,is_talk,is_accept_address,address_id',
            ])
            ->where('type', ActivityWorkTypeEnum::SUBMIT);


        if ($request->get('fetchType') === 'excel') {
            $fileName   = Str::random(32) . '.xlsx';
            $excel      = new Excel(['path' => storage_path('excels')]);
            $fileObject = $excel->constMemory($fileName, NULL, false);
            $format     = new Format($fileObject->getHandle());
            $headStyle  = $format->align(Format::FORMAT_ALIGN_CENTER, Format::FORMAT_ALIGN_VERTICAL_CENTER)->background(0XF2F2F2)->bold()->toResource();


            $row = $fileObject
                ->header(['名称', '类型', '推荐语', '歌曲风格', '厂牌', '用户艺名', '用户真名', '身份', '经纪人艺名', '经纪人真名', '试唱方式', '唱酬', '分成', '价格是否可谈', '录音地点', '是否接受其他录音地点', '提交时间', '试唱音频', '试唱结果'])
                ->setRow('A1', 22, $headStyle)
                ->setColumn('A:J', 16)
                ->setColumn('K:U', 14);

            $build->chunk(1000, static function (Collection $collection) use ($fileObject) {
                $fileObject->data($collection->map(static function (ActivityWork $work) {
                    $price        = $work->getAttribute('price');
                    $userIdentity = data_get($work, 'user_identity', 0);

                    return [
                        data_get($work, 'activity_name', ''),
                        ActivitySongTypeEnum::tryFrom(data_get($work, 'activity_type', 1))?->label(),
                        data_get($work, 'activity_title', ''),
                        $work->getRelation('tags')->implode('name', '、'),
                        data_get($work, 'project.name', ''),
                        data_get($work, 'user_nick_name', ''),
                        data_get($work, 'user_real_name', ''),
                        match ($userIdentity) {
                            0 => '未认证',
                            1 => '音乐人',
                            2, 3 => "经纪人",
                            default => ''
                        },
                        data_get($work, 'business.nick_name', ''),
                        data_get($work, 'business.real_name', ''),
                        $work->getAttribute('mode') === ActivityWorkModeEnum::OFFLINE ? $work->getAttribute('mode')?->label() : $work->getAttribute('sing_type')?->label(),
                        data_get($price, 'value.is_reward', 0) === 1 ? data_get($price, 'value.amounts') : '无',
                        data_get($price, 'value.is_dividend', 0) === 1 ? $price->getDividendFormat() : '无',
                        data_get($price, 'is_talk', 0) === 1 ? '是' : '否',
                        $price ? $price->getAddressFormat() : '',
                        data_get($price, 'is_accept_address', 0) === 1 ? '是' : '否',
                        data_get($work, 'submit_at', ''),
                        data_get($work, 'demo_url', ''),
                        $work->getFormatStatus()
                    ];
                })->toArray());
            });
            return response()->download($row->output(), $fileName)->deleteFileAfterSend();
        }

        return $this->successWithData($build->paginate($pageSize));
    }
}