ProjectController.php
7.96 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?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);
}
}