AdvertisementsAdd.php
3.7 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
<?php
namespace App\Console\Commands;
use App\Models\Pivots\GoalAdvertisement;
use App\Models\Tokens;
use App\Service\API\API;
use App\Service\Requests\APIRequest;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Support\Facades\DB;
class AdvertisementsAdd extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'ads:add';
/**
* 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.goalAdvertisementsForNotExternal.advertisement')
->where('type', '!=', Tokens::MAIN)
->get();
foreach ($tokens as $token) {
$factory = APIRequest::getInstance(API::YANDEX);
$factory->setToken($token);
$goalAds = DB::table('goal_advertisements')
->join('advertisements', 'goal_advertisements.advertisement_id', '=', 'advertisements.id')
->whereNull('advertisements.deleted_at')
->whereNull('goal_advertisements.external_id')
->whereNull('goal_advertisements.reserve_create_at')
->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.id as id',
'goal_advertisements.dictionary_campaign_id as dictionary_campaign_id',
'goal_advertisements.goal_ad_group_external_id as goal_ad_group_external_id',
'advertisements.title as title',
'advertisements.text as text',
'advertisements.mobile as mobile',
'advertisements.title2 as title2',
'advertisements.href as href',
'advertisements.display_url_path as display_url_path',
'advertisements.v_card_id as v_card_id',
'advertisements.ad_image_hash as ad_image_hash',
'advertisements.site_link_set_id as site_link_set_id',
'advertisements.ad_extensions 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',
])
->get();
foreach (array_chunk($goalAds->pluck('id')->toArray(), 1000) as $items) {
GoalAdvertisement::whereIn('id', $items)
->update([
'reserve_create_at' => Carbon::now(),
]);
}
$factory->getRequest('Ads', 'add')
->call([
'goalAds' => $goalAds,
]);
}
return 0;
}
}