AdvertisementsUpdate.php
5.31 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
<?php
namespace App\Console\Commands;
use App\Models\Pivots\GoalAdvertisement;
use App\Models\Tokens;
use App\Service\Requests\Direct\UpdateAds;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\DB;
class AdvertisementsUpdate extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'ads:update';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Изменение созданных объявлений с целевого аккаунта';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$tokens = Tokens::whereHas('dictionaryCampaignsEnabledForExternalSynchronized.goalAdvertisementsForNeedUpdatedForNotReserveUpdate.advertisement')
->where('type', '!=', Tokens::MAIN)
->get();
foreach ($tokens as $token) {
$goalAds = DB::table('goal_advertisements')
->join('advertisements', 'goal_advertisements.advertisement_id', '=', 'advertisements.id')
->leftJoin('goal_advertisement_goal_ad_extensions', 'goal_advertisements.id', '=', 'goal_advertisement_goal_ad_extensions.goal_advertisement_id')
->leftJoin('goal_ad_extensions', 'goal_advertisement_goal_ad_extensions.goal_ad_extension_id', '=', 'goal_ad_extensions.id')
->whereNotExists(function (Builder $query) {
$query->select(DB::raw(1))
->from('goal_advertisement_goal_ad_extensions')
->join('goal_ad_extensions', 'goal_advertisement_goal_ad_extensions.goal_ad_extension_id', '=', 'goal_ad_extensions.id')
->whereNull('goal_ad_extensions.external_id')
->whereColumn('goal_advertisements.id', 'goal_advertisement_goal_ad_extensions.goal_advertisement_id');
})
->where(function (Builder $query) {
$query->whereNull('goal_advertisements.goal_v_card_id')
->orWhere(function (Builder $query) {
$query->whereNotNull('goal_advertisements.goal_v_card_id')
->whereNotNull('goal_advertisements.goal_v_card_external_id');
});
})
->where(function (Builder $query) {
$query->whereNull('goal_advertisements.goal_sitelink_id')
->orWhere(function (Builder $query) {
$query->whereNotNull('goal_advertisements.goal_sitelink_id')
->whereNotNull('goal_advertisements.goal_sitelink_external_id');
});
})
->whereNull('advertisements.deleted_at')
->whereNull('goal_advertisements.reserve_update_at')
->whereNotNull('goal_advertisements.updated_need')
->whereNotNull('goal_advertisements.goal_ad_group_external_id')
->whereNotNull('goal_advertisements.dictionary_campaign_external_id')
->whereIn('goal_advertisements.dictionary_campaign_id', $token->dictionaryCampaignsEnabledForExternalSynchronized->pluck('id'))
->select([
'goal_advertisements.external_id as external_id',
'goal_advertisements.dictionary_campaign_id as dictionary_campaign_id',
'advertisements.title as title',
'advertisements.text as text',
'advertisements.age_label as age_label',
'advertisements.title2 as title2',
'advertisements.href as href',
'advertisements.display_url_path as display_url_path',
'goal_advertisements.goal_v_card_external_id as goal_v_card_external_id',
'advertisements.ad_image_hash as ad_image_hash',
'goal_advertisements.goal_sitelink_external_id as goal_sitelink_external_id',
DB::raw("JSON_OBJECTAGG('AdExtensionId', goal_ad_extensions.external_id, 'Operation', 'SET') as ad_extensions"),
'advertisements.video_extension as video_extension',
'advertisements.price_extension as price_extension',
'advertisements.turbo_page_id as turbo_page_id',
'advertisements.business_id as business_id',
'advertisements.prefer_v_card_over_business as prefer_v_card_over_business',
])
->groupBy('goal_advertisements.id')
->get();
foreach (array_chunk($goalAds->pluck('id')->toArray(), 1000) as $items) {
GoalAdvertisement::whereIn('id', $items)
->update([
'reserve_update_at' => Carbon::now(),
]);
}
$request = new UpdateAds();
$request->setToken($token)
->call([
'goalAds' => $goalAds,
]);
}
return 0;
}
}