ActivityWork.php 4.18 KB
<?php

namespace App\Models\Views;

use App\Enums\ActivityStatusEnum;
use App\Enums\ActivityWorkModeEnum;
use App\Enums\ActivityWorkSingTypeEnum;
use App\Enums\ActivityWorkStatusEnum;
use App\Enums\ActivityWorkTypeEnum;
use App\Models\Activity;
use App\Models\ActivityUserPrice;
use App\Models\Pivots\ActivityTagPivot;
use App\Models\Project;
use App\Models\SystemTag;
use App\Models\User;
use App\Support\Model;
use Awobaz\Compoships\Database\Eloquent\Relations\BelongsTo;
use Awobaz\Compoships\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class ActivityWork extends Model
{
    use SoftDeletes;

    protected $table = 'activity_works';

    /**
     * @var array<string,mixed>
     */
    protected $casts = [
        'share_id'        => 'int',
        'user_id'         => 'int',
        'business_id'     => 'int',
        'syn_data'        => 'json',
        'type'            => ActivityWorkTypeEnum::class,
        'mode'            => ActivityWorkModeEnum::class,
        'sing_type'       => ActivityWorkSingTypeEnum::class,
        'status'          => ActivityWorkStatusEnum::class,
        'activity_status' => ActivityStatusEnum::class
    ];

    /**
     * @return string
     */
    public function getFormatStatus(): string
    {
        if ($this->getAttribute('status') === ActivityWorkStatusEnum::SUCCESS) {
            return '确认合作';
        }
        if ($this->getAttribute('status') === ActivityWorkStatusEnum::REJECT) {
            return '不合适';
        }
        if ($this->getAttribute('status') === ActivityWorkStatusEnum::WAIT && $this->getAttribute('activity_status') === 1) {
            return '待采纳';
        }

        if ($this->getAttribute('status') === ActivityWorkStatusEnum::WAIT && $this->getAttribute('activity_status') >= 3) {
            return '未采纳';
        }

        return '其他';
    }

    /**
     * @param string $format
     * @return string
     */
    public function getUserFullName(string $format = '%s(%s)'): string
    {
        return sprintf($format, $this->getAttribute('user_nick_name'), $this->getAttribute('user_real_name'));
    }

    /**
     * @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 activity(): BelongsTo
    {
        return $this->belongsTo(Activity::class)->withTrashed();
    }

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

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

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

    /**
     * @return \Awobaz\Compoships\Database\Eloquent\Relations\HasMany
     */
    public function children(): HasMany
    {
        return $this->hasMany(__CLASS__, ['activity_id', 'user_id'], ['activity_id', 'user_id'])
            ->where('type', ActivityWorkTypeEnum::SAVE);
    }

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

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

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