GoalKeyword.php 5.22 KB
<?php

namespace App\Models\Pivots;

use App\Models\Keyword;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;

/**
 * App\Models\Pivots\GoalKeyword
 *
 * @property int $id
 * @property int|null $external_id
 * @property int|null $dictionary_campaign_external_id
 * @property int|null $goal_ad_group_external_id
 * @property int $dictionary_campaign_id
 * @property int $goal_ad_group_id
 * @property int $keyword_id
 * @property \Illuminate\Support\Carbon|null $external_upload_at
 * @property \Illuminate\Support\Carbon|null $external_updated_at
 * @property \Illuminate\Support\Carbon|null $updated_need
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read Keyword $keyword
 * @method static Builder|GoalKeyword forExternal()
 * @method static Builder|GoalKeyword forNotExternal()
 * @method static Builder|GoalKeyword needUpdated()
 * @method static Builder|GoalKeyword newModelQuery()
 * @method static Builder|GoalKeyword newQuery()
 * @method static Builder|GoalKeyword query()
 * @method static Builder|GoalKeyword whereCreatedAt($value)
 * @method static Builder|GoalKeyword whereDictionaryCampaignExternalId($value)
 * @method static Builder|GoalKeyword whereDictionaryCampaignId($value)
 * @method static Builder|GoalKeyword whereExternalId($value)
 * @method static Builder|GoalKeyword whereExternalUpdatedAt($value)
 * @method static Builder|GoalKeyword whereExternalUploadAt($value)
 * @method static Builder|GoalKeyword whereGoalAdGroupExternalId($value)
 * @method static Builder|GoalKeyword whereGoalAdGroupId($value)
 * @method static Builder|GoalKeyword whereId($value)
 * @method static Builder|GoalKeyword whereKeywordId($value)
 * @method static Builder|GoalKeyword whereUpdatedAt($value)
 * @method static Builder|GoalKeyword whereUpdatedNeed($value)
 * @mixin \Eloquent
 */
class GoalKeyword extends Pivot
{

    use SoftDeletes;

    protected $table = 'goal_keywords';

    protected $fillable = [
        'external_id',
        'goal_ad_group_external_id',
        'dictionary_campaign_external_id',
        'goal_ad_group_id',
        'dictionary_campaign_id',
        'keyword_id',
        'external_upload_at',
        'external_updated_at',
        'updated_need',
    ];

    protected $casts = [
        'external_upload_at' => 'datetime',
        'external_updated_at' => 'datetime',
        'updated_need' => 'datetime',
    ];

    public $incrementing = true;

    static public function getWithPivot()
    {
        return [
            'id',
            'external_id',
            'goal_ad_group_external_id',
            'dictionary_campaign_external_id',
            'goal_ad_group_id',
            'dictionary_campaign_id',
            'keyword_id',
            'external_upload_at',
            'external_updated_at',
            'updated_need',
        ];
    }

    /**
     * @return Collection
     */
    static public function getPropertiesCopyWithPivot()
    {
        return collect([
        ]);
    }

    /**
     * @param Keyword|array $keyword
     *
     * @return array
     */
    static public function copyPropertyFromMain($keyword)
    {
        return self::getPropertiesCopyWithPivot()
            ->transform(function ($property_name) use ($keyword) {
                $value = null;

                if ($keyword instanceof Keyword) {
                    $value = $keyword->{$property_name};
                } elseif (is_array($keyword) && isset($keyword[$property_name])) {
                    $value = $keyword[$property_name];
                }

                return [
                    $property_name => $value
                ];
            })
            ->collapse()
            ->put('keyword_id', $keyword[GoalKeyword::getModel()->getKeyName()])
            ->all();
    }

    static public function updateOrCreateByMain(Keyword $keyword, GoalAdGroup $goalAdGroup, DictionaryCampaign $dictionaryCampaign)
    {
        return GoalKeyword::updateOrCreate([
            'dictionary_campaign_id' => $dictionaryCampaign->getKey(),
            'dictionary_campaign_external_id' => $dictionaryCampaign->external_id,
            'goal_ad_group_id' => $goalAdGroup->getKey(),
            'goal_ad_group_external_id' => $goalAdGroup->external_id,
            'keyword_id' => $keyword->getKey(),
            'deleted_at' => null,
        ], GoalKeyword::copyPropertyFromMain($keyword));
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeForExternal($query)
    {
        return $query->whereNotNull('external_id');
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeForNotExternal($query)
    {
        return $query->whereNull('external_id');
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeNeedUpdated($query)
    {
        return $query->whereNotNull('updated_need');
    }

    public function keyword()
    {
        return $this->belongsTo(Keyword::class, 'keyword_id');
    }

    public function dictionaryCampaign()
    {
        return $this->belongsTo(DictionaryCampaign::class, 'dictionary_campaign_id');
    }

}