Activity.php 9.26 KB
<?php

namespace App\Models;

use App\Enums\ActivityStatusEnum;
use App\Enums\ActivityWorkTypeEnum;
use App\Models\Pivots\ActivityLinkPivot;
use App\Models\Pivots\ActivityTagPivot;
use App\Models\Pivots\UserActivityCollectionPivot;
use App\Models\Pivots\UserActivityViewPivot;
use App\Models\Pivots\UserProjectPivot;
use App\Models\Views\UserManageActivity;
use App\Support\Model;
use App\Traits\JsonColumnDirtyTrait;
use Awobaz\Compoships\Database\Eloquent\Relations\BelongsTo;
use Awobaz\Compoships\Database\Eloquent\Relations\HasMany;
use Awobaz\Compoships\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * @method static Builder statusOfUp()
 */
class Activity extends Model
{
    use SoftDeletes, JsonColumnDirtyTrait;

    protected $table = 'activitys';

    protected $guarded = [];

    /**
     * @var string[]
     */
    protected $hidden = ['deleted_at'];

    /**
     * @var array<string,mixed>
     */
    protected $casts = [
        'status'       => ActivityStatusEnum::class,
        'created_form' => 'int',
        'audit_status' => 'int',
        'is_push'      => 'int',
        'song_type'    => 'int',
        'weight'       => 'int',
        'expand'       => 'json',
        'lang'         => 'array',
        'send_url'     => 'array',
        'push_type'    => 'array',
        'user_id'      => 'int'
    ];

    /**
     * @var array<string,string>
     */
    public array $columnMap = [
        'cover'                        => '封面图片',
        'song_name'                    => '歌曲名称',
        'lang'                         => '语种',
        'speed'                        => '语速',
        'mark'                         => '角标',
        'is_official'                  => '奖励标识',
        'push_type'                    => '推送方式',
        'sub_title'                    => '歌曲简介',
        'project_id'                   => '关联厂牌',
        'sex'                          => '性别要求',
        'lyric'                        => '歌词文本',
        'clip_lyric'                   => '片段歌词',
        'manage_link'                  => '外部管理员',
        'estimate_release_at'          => '预计发行日期',
        'expand->tag_ids'              => '曲风标签',
        'expand->lyricist->ids'        => '词作者(用户)',
        'expand->lyricist->supplement' => '词作者(未注册)',
        'expand->composer->ids'        => '曲作者(用户)',
        'expand->composer->supplement' => '曲作者(未注册)',
        'expand->arranger->ids'        => '编曲(用户)',
        'expand->arranger->supplement' => '编曲(未注册)',
        'expand->guide_source->url'    => '导唱文件',
        'expand->karaoke_source->url'  => '伴奏文件',
        'expand->track_source->url'    => '分轨文件',
    ];

    public function scopeStatusOfUp(Builder $builder): Builder
    {
        return $builder->where($this->qualifyColumn('audit_status'), 1)->where($this->qualifyColumn('status'), 1);
    }

    /**
     * @param $clip_lyric
     * @return string
     */
    public function getClipLyricAttribute($clip_lyric): string
    {
        return $clip_lyric ?? '';
    }

    /**
     * @param $lang
     * @return array
     * @throws \JsonException
     */
    public function getLangAttribute($lang): array
    {
        return is_null($lang) ? [] : json_decode($lang, false, 512, JSON_THROW_ON_ERROR);
    }

    /**
     * @return \Awobaz\Compoships\Database\Eloquent\Relations\BelongsTo
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class)->withTrashed();
    }

    /**
     * @return \Awobaz\Compoships\Database\Eloquent\Relations\BelongsTo
     */
    public function project(): BelongsTo
    {
        return $this->belongsTo(Project::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function is_unlike(): BelongsToMany
    {
        return $this->belongsToMany(User::class, UserActivityUnlikes::class, 'activity_id', 'user_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function tags(): BelongsToMany
    {
        return $this
            ->belongsToMany(SystemTag::class, ActivityTagPivot::class, 'activity_id', 'tag_id')
            ->wherePivotNull('deleted_at')->orderBy('id', 'asc')->withTimestamps();
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function viewUsers(): BelongsToMany
    {
        return $this->belongsToMany(User::class, UserActivityViewPivot::class, 'activity_id', 'user_id')
            ->wherePivotNull('deleted_at');
    }

    /**
     * @return \Awobaz\Compoships\Database\Eloquent\Relations\HasMany
     */
    public function viewUsersCount(): HasMany
    {
        return $this->hasMany(UserActivityViewPivot::class)->whereNull('deleted_at');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function collectionUsers(): BelongsToMany
    {
        return $this->belongsToMany(User::class, UserActivityCollectionPivot::class, 'activity_id', 'user_id')
            ->wherePivotNull('deleted_at');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function workUsers(): BelongsToMany
    {
        return $this->belongsToMany(User::class, ActivityUser::class, 'activity_id', 'user_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function submitUsers(): BelongsToMany
    {
        return $this->workUsers()->wherePivot('type', ActivityWorkTypeEnum::SUBMIT)->orderBy('id', 'asc');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function saveUsers(): BelongsToMany
    {
        return $this->workUsers()->wherePivot('type', ActivityWorkTypeEnum::SAVE);
    }

    /**
     * @return \Awobaz\Compoships\Database\Eloquent\Relations\HasMany
     */
    public function submit(): HasMany
    {
        return $this->hasMany(ActivityUser::class)->where('type', '=', "Submit")->with(['SelfPrice.AddressInfo', 'ShareUser.user:id,nick_name,real_name', 'unread', 'user.business:id,nick_name,real_name,avatar', 'user:id,nick_name,real_name,avatar,province,city,company,rate,chat_mode,business_id'])->orderBy('created_at', 'desc');
    }

    /**
     * @return HasMany
     */
    public function applyRecords(): HasMany
    {
        return $this->hasMany(ActivityApplyRecord::class)->latest();
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
     */
    public function links(): MorphToMany
    {
        return $this->morphedByMany(User::class, 'link', ActivityLinkPivot::class)->withTrashed();
    }

    /**
     * @return \Awobaz\Compoships\Database\Eloquent\Relations\HasMany
     */
    public function outSideManages(): HasMany
    {
        return $this->hasMany(UserManageActivity::class);
    }

    public function totalSong(): HasMany
    {
        return $this->hasMany(__CLASS__, 'project_id', 'project_id')->whereNotIn('activitys.status', [0, 2, 4])->where('activitys.audit_status', 1);
    }

    public function onlineSong(): HasMany
    {
        return $this->hasMany(__CLASS__, 'project_id', 'project_id')->where(['status' => 1, 'audit_status' => 1]);
    }

    public function comfirmTime(): HasOne
    {
        return $this->hasOne(ProjectConfirmationTime::class, 'project_id', 'project_id');
    }

    public function sexInfo(): HasOne
    {
        return $this->hasOne(SystemConfig::class, 'identifier', 'sex');
    }

    public function publicAudio(): HasOne
    {
        return $this->hasOne(ActivityUser::class, 'activity_id', 'id')->where(['type' => 'Submit', 'syn_status' => 1, 'is_hide' => 0]);
    }

    public function linkArranger(): MorphToMany
    {
        return $this->morphedByMany(User::class, 'link', ActivityLinkPivot::class)->wherePivot('type', 'arranger')->withTrashed();
    }

    /**
     * @return \Awobaz\Compoships\Database\Eloquent\Relations\HasMany
     */
    public function attend_user(): HasMany
    {
        return $this->hasMany(ActivityUser::class)->select('user_id', 'activity_id')->where('type', '=', "Submit")->with(['user:id,nick_name,real_name,intro,avatar,province,city,company,rate'])->orderBy('created_at', 'desc')->limit(6);
    }

    /**
     * @return \Awobaz\Compoships\Database\Eloquent\Relations\HasMany
     */
    public function attend_user_count(): HasMany
    {
        return $this->hasMany(ActivityUser::class)->select('user_id', 'activity_id')->where('type', '=', "Submit");
    }

    /**
     * @return \Awobaz\Compoships\Database\Eloquent\Relations\HasMany
     */
    public function attend_union_user_count(): HasMany
    {
        return $this->hasMany(ActivityUser::class)->select('user_id', 'activity_id')->where('type', '=', "Submit")->distinct();
    }

    public function is_master(): HasOne
    {
        return $this->hasOne(UserProjectPivot::class, 'project_id', 'user_id');
    }

    public function is_confirm_share(): HasOne
    {
        return $this->hasOne(ActivityShareUser::class, 'activity_id', 'user_id');
    }

}