ProjectController.php 7.96 KB
<?php

namespace App\Http\Container\ManageSection\Controllers\Audition;

use App\Enums\ActivitySongTypeEnum;
use App\Enums\ActivityStatusEnum;
use App\Excel\ProjectExcel;
use App\Helpers\OperationLog;
use App\Http\Container\AdminSection\Controllers\Audition\ProjectController as AdminProjectController;
use App\Http\Container\ManageSection\Filters\ActivityFilter;
use App\Models\Activity;
use App\Models\Pivots\ActivityLinkPivot;
use App\Models\Pivots\ProjectMemberPivot;
use App\Models\Pivots\UserProjectPivot;
use App\Models\Project;
use App\Models\ProjectDynamic;
use App\Models\User;
use App\Support\Controller;
use Auth;
use Hikoon\LaravelApi\Facades\Response;
use Hikoon\LaravelApi\Support\ApiCode;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

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

        Project::resolveRelationUsing('activity_up', static fn(Project $project) => $project->activities()
            ->where(fn($query) => $query->where('projects.is_public', 1)->orWhere(fn($query) => $query->where('projects.is_public', 0)->where('activitys.user_id', Auth::id())))
            ->where('activitys.song_type', ActivitySongTypeEnum::SONG)
            ->where('activitys.status', ActivityStatusEnum::UP));
        Project::resolveRelationUsing('activity_match', static fn(Project $project) => $project->activities()
            ->where(fn($query) => $query->where('projects.is_public', 1)->orWhere(fn($query) => $query->where('projects.is_public', 0)->where('activitys.user_id', Auth::id())))
            ->where('activitys.song_type', ActivitySongTypeEnum::SONG)
            ->where('status', ActivityStatusEnum::MATCH));
        Project::resolveRelationUsing('activity_send', static fn(Project $project) => $project->activities()
            ->where(fn($query) => $query->where('projects.is_public', 1)->orWhere(fn($query) => $query->where('projects.is_public', 0)->where('activitys.user_id', Auth::id())))
            ->where('activitys.song_type', ActivitySongTypeEnum::SONG)
            ->where('status', ActivityStatusEnum::SEND));
        Project::resolveRelationUsing('manage', static fn(Project $project) => $project->belongsToMany(User::class, UserProjectPivot::class));
        Project::resolveRelationUsing('member', static fn(Project $project) => $project->belongsToMany(User::class, ProjectMemberPivot::class)
            ->where('project_has_members.status', 1)
            ->where('users.status', 1));


        $build = Project::filter($filter + ['manager' => Auth::id()]);

        return match ($fetchType) {
            'excel' => ProjectExcel::withBuilder($build)->download(),
            'all' => $this->successWithData($build->get()),
            default => $this->successWithData($build->paginate($pageSize))
        };
    }

    /**
     * @param \App\Models\Project $project
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function show(Project $project): Response
    {
        $activity = Activity::filter(['project' => $project->getKey(), 'auditStatus' => 1], ActivityFilter::class)->get(['id', 'song_type', 'status']);
        $dynamics = ProjectDynamic::filter(['project' => $project->getKey()])->get(['id', 'type']);

        $project->load(['user:id,nick_name,real_name,identity', 'master:id,nick_name,real_name,identity']);
        $project->setAttribute('activity_count', $activity->where('song_type', 1)->count());
        $project->setAttribute('activity_up_count', $activity->where('song_type', 1)->where('status', ActivityStatusEnum::UP)->count());
        $project->setAttribute('activity_down_count', $activity->where('song_type', 1)->where('status', ActivityStatusEnum::DOWN)->count());
        $project->setAttribute('activity_match_count', $activity->where('song_type', 1)->where('status', ActivityStatusEnum::MATCH)->count());
        $project->setAttribute('activity_send_count', $activity->where('song_type', 1)->where('status', ActivityStatusEnum::SEND)->count());
        $project->setAttribute('demo_count', $activity->where('song_type', 2)->count());
        $project->setAttribute('demo_up_count', $activity->where('song_type', 2)->where('status', ActivityStatusEnum::UP)->count());

        $project->setAttribute('image_dynamic_count', $dynamics->where('type', 'image')->count());
        $project->setAttribute('audio_dynamic_count', $dynamics->where('type', 'audio')->count());
        $project->setAttribute('video_dynamic_count', $dynamics->where('type', 'video')->count());
        return $this->successWithData($project);
    }

    /**
     * @param \Illuminate\Http\Request $request
     * @param \App\Models\Project      $project
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function update(Request $request, Project $project): Response
    {
        $attribute = $request->validate([
            'cover' => 'bail|required|string|url',
            'name'  => 'bail|required|string',
            'intro' => 'bail|nullable|sometimes',
        ], [
            'cover.required' => "请上传厂牌图片",
            'name.required'  => "请输入厂牌名称",
            'cover.url'      => "厂牌图片格式错误",
        ]);
        $project->update($attribute);
        OperationLog::manage()->subject($project)->updateAction()->content('厂牌《%s》%s', $project->getAttribute('name'), $project->getChangeColumn()->format(',修改了:'));
        return $this->successWithData($project, ApiCode::UPDATE_SUCCESS);
    }

    /**
     * @param \Illuminate\Http\Request $request
     * @param \App\Models\Project      $project
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function manageUser(Request $request, Project $project): Response
    {
        return App(AdminProjectController::class)->manageUser($request, $project);
    }

    /**
     * @param \Illuminate\Http\Request $request
     * @param \App\Models\Project      $project
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function memberUser(Request $request, Project $project): Response
    {
        return App(AdminProjectController::class)->memberUser($request, $project);
    }

    /**
     * @param \App\Models\Project      $project
     * @param \Illuminate\Http\Request $request
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function outManageUser(Project $project, Request $request): Response
    {
        return App(AdminProjectController::class)->outManageUser($request, $project);
    }

    /**
     * @param \App\Models\Project      $project
     * @param \Illuminate\Http\Request $request
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function deleteOutManageUser(Project $project, Request $request): Response
    {

        $user = User::query()->find($request->input('user_id', 0));

        if ($user) {
            ActivityLinkPivot::query()
                ->whereRelation('activity', 'project_id', $project->getKey())
                ->where('type', 'manager')
                ->where('link_type', User::class)
                ->where('link_id', $user->getKey())
                ->delete();

            OperationLog::manage()->subject($project)->deleteAction()->content('厂牌《%s》删除外部管理员:%s', $project->getAttribute('name'), $user->getAttribute('nick_name'));
        }
        return $this->success(ApiCode::DELETE_SUCCESS);
    }

    /**
     * @param \Illuminate\Http\Request $request
     * @param int                      $id
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function dynamic(Request $request, int $id): Response
    {
        return App(AdminProjectController::class)->dynamic($request, $id);
    }
}