GoalKeyword.php
5.52 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
namespace App\Models\Pivots;
use App\Models\Keyword;
use App\Models\YandexError;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
/**
* 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
{
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',
'reserve_create_at',
'reserve_update_at',
'reserve_delete_at',
];
protected $casts = [
'external_upload_at' => 'datetime',
'external_updated_at' => 'datetime',
'updated_need' => 'datetime',
'reserve_create_at' => 'datetime',
'reserve_update_at' => 'datetime',
'reserve_delete_at' => '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',
'reserve_create_at',
'reserve_update_at',
'reserve_delete_at',
];
}
public static function boot()
{
parent::boot();
static::deleted(function (GoalKeyword $goalKeyword) {
GoalKeywordDelete::updateOrCreateByMain($goalKeyword);
});
}
/**
* @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 goalAdGroup()
{
return $this->belongsTo(GoalAdGroup::class, 'goal_ad_group_id');
}
public function dictionaryCampaign()
{
return $this->belongsTo(DictionaryCampaign::class, 'dictionary_campaign_id');
}
public static function getForUpdate($token){
return DB::table('goal_keywords')
->join('keywords', 'goal_keywords.keyword_id', '=', 'keywords.id')
->whereNull('keywords.deleted_at')
->whereNull('goal_keywords.reserve_update_at')
->whereNotNull('goal_keywords.updated_need')
->whereNotNull('goal_keywords.external_id')
->whereNotNull('goal_keywords.goal_ad_group_external_id')
->whereNotNull('goal_keywords.dictionary_campaign_external_id')
->whereIn('goal_keywords.dictionary_campaign_id', $token->dictionaryCampaignsEnabledForExternalSynchronized->pluck('id'))
->select([
'goal_keywords.id as id',
'goal_keywords.external_id',
'goal_keywords.dictionary_campaign_id as dictionary_campaign_id',
'keywords.keyword as keyword',
'keywords.user_param_1 as user_param_1',
'keywords.user_param_2 as user_param_2',
])
->get();
}
public function errors()
{
return $this->morphMany(YandexError::class, 'cause');
}
}