DashboardController.php
7.65 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
<?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));
}
}