MusicianSongService.php
5.89 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
<?php
namespace App\Services;
use App\Helper\Response;
use App\Models\Legal\Contract;
use App\Models\Legal\Song;
use App\Models\Legal\SongsIp;
use App\Models\Legal\SongsIpExts;
use App\Models\Legal\SongStakeholder;
use App\Models\Legal\StakeholderSongCollate;
use Carbon\Carbon;
/**
* Class MusicianSongService
* @package App\Services
*/
class MusicianSongService extends Service
{
/**
* 授权发行歌曲
* @return \Illuminate\Http\JsonResponse
*/
public function releaseSong()
{
$song_ids = array_filter(array_unique(SongStakeholder::query()->identify()->pluck('song_id')->toArray()));
if (empty($song_ids)) return Response::success();
$song_table = Song::table();
$songip_table = SongsIp::table();
$res = Song::query()->join($songip_table, "{$song_table}.id", '=', "{$songip_table}.song_id")
->with(['contractDetail', 'coverResource'])->whereIn("{$songip_table}.song_id", $song_ids)->select(["{$song_table}.id", "{$songip_table}.id as sp_id", 'online_time', 'publish_song'])
->groupBy(["song_id"])->paginate($this->pageSize);
$sp_exts = [];
if ($sp_ids = array_column($res->items(), 'sp_id')) {
$sp_exts = SongsIpExts::query()->whereIn('song_ip_id', $sp_ids)->get()->keyBy('song_ip_id');
}
foreach ($res as &$item) {
list($name, $singer) = explode('|', $item->publish_song);
$item->setAttribute('name', $name);
$item->setAttribute('singer', $singer);
$item->setAttribute('cover', empty($item->coverResource) ? '' : $item->coverResource->url);
$item->setAttribute('ext', $sp_exts[$item->sp_id] ?? null);
$item->setAttribute('online_time', $item->online_time);
$tmp = $item->toArray();
if (empty($tmp['contract_detail'])) {
$item->setAttribute('role', []);
unset($item->contractDetail, $item->publish_song, $item->coverResource);
continue;
}
$role = [];
if ($clause_res = array_column($tmp['contract_detail'], 'clause')) {
foreach ($clause_res as $clause) {
if ($clause = json_decode($clause)){
foreach ($clause as $clause_item) {
if (in_array($clause_item->stakeholder_id, $this->stakeholder_ids)) {
$role = array_merge($role, Contract::transformRole($clause_item->right_type));
}
}
}
}
}
$item->setAttribute('role', array_unique($role));
unset($item->contractDetail, $item->publish_song, $item->coverResource);
}
return Response::success($res);
}
/**
* 授权发行歌曲详情
* @param $song_id
* @return \Illuminate\Http\JsonResponse
*/
public function songDetail($song_id)
{
//1. 发行记录
$res = SongsIp::query()->where('song_id', $song_id)->groupBy(['auth_channel'])
->select(['song', 'auth_channel', 'online_time'])
->orderBy('online_time')->get();
$release = [];
foreach ($res as $item) {
if (!isset($release[$item->online_time])) {
$release[$item->online_time] = ['online_time'=>$item->online_time];
}
$release[$item->online_time]['auth_channel'][] = $item->auth_channel;
}
//2. 计算发行天数
$release_day = '';
if ($item = $res->first()) {
$release_day = Carbon::now()->diffInDays($item->online_time);
}
//3. 权益人and歌曲累计收益
$income = StakeholderSongCollate::query()->identify()->where('song_id', $song_id)
->sum('real_share_amount');
return Response::success([
'release' => array_values($release),
'release_day' => $release_day,
'income' => $income,
]);
}
/**
* 权益人歌曲权益
* @param $song_id
* @return \Illuminate\Http\JsonResponse
*/
public function right($song_id)
{
$res = SongStakeholder::query()->where('song_id', $song_id)->identify()
->with(['contract:id,cooperation_type', 'contract.stakeholderContract', 'contract.files', 'contract.files.fileInfo:id,location'])
->get()->toArray();
$contracts = [];
foreach ($res as $k=>$item) {
$files = [];
if (!empty($item['contract']['files'])) {
if ($file_info = array_column($item['contract']['files'], 'file_info')){
$files = array_column($file_info, 'location');
array_walk($files, function (&$location) { $location = env('resource_url'). $location; });
}
}
$right = $role = [];
if (!empty($item['contract']['stakeholder_contract'])) {
foreach ($item['contract']['stakeholder_contract'] as $stakeholder_contract) {
if (in_array($stakeholder_contract['stakeholder_id'], $this->stakeholder_ids)) {
$right[] = $stakeholder_contract;
$role = array_merge($role, Contract::transformRole($stakeholder_contract['right_type']));
}
}
}
$contracts[$k] = [
'contract_id'=>$item['contract_id'],
'files' => $files,
'cooperation_type'=>$item['contract']['cooperation_type'],
'right'=> Contract::getModel($item['contract']['cooperation_type'], $right),
'role'=> array_unique($role),
];
}
return Response::success(array_values($contracts));
}
}