MusicianSongService.php 5.48 KB
<?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\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", 'publish_song'])
                    ->groupBy(["song_id"])->paginate($this->pageSize);

        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);

            $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));
    }
}