Commit 886c1fd3 by Vladislav

#20364 Загрузка и синхронизация расширений

1 parent e9f07f96
<?php
namespace App\Console\Commands;
use App\Models\Campaigns;
use App\Models\Tokens;
use App\Service\API\API;
use App\Service\Requests\APIRequest;
use Illuminate\Console\Command;
class AdExtensionsAdd extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'adextensions: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
* @throws \Exception
*/
public function handle()
{
$token = Tokens::where('type', Tokens::MAIN)->first();
if (!$token) {
throw new \Exception('Не найден токен блин');
}
$factory = APIRequest::getInstance(API::YANDEX);
$factory->setToken($token);
$factory->getRequest('AdExtensions', 'get')
->call([
'ModifiedSince' => $token->check_changes_ad_extension,
]);
return 0;
}
}
...@@ -10,6 +10,7 @@ use Carbon\Carbon; ...@@ -10,6 +10,7 @@ use Carbon\Carbon;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough; use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
class AdvertisementsAdd extends Command class AdvertisementsAdd extends Command
...@@ -46,11 +47,10 @@ class AdvertisementsAdd extends Command ...@@ -46,11 +47,10 @@ class AdvertisementsAdd extends Command
public function handle() public function handle()
{ {
$tokens = Tokens::has('dictionaryCampaignsEnabledForExternalSynchronized.goalAdvertisementsForNotExternalForNotReserveCreate.advertisement') $tokens = Tokens::has('dictionaryCampaignsEnabledForExternalSynchronized.goalAdvertisementsForNotExternalForNotReserveCreate.advertisement')
->whereDoesntHave('dictionaryCampaignsEnabledForExternalSynchronized.goalAdvertisementsForNotExternalForNotReserveCreate.goalAdExtensionsForNotExternal')
->where('type', '!=', Tokens::MAIN) ->where('type', '!=', Tokens::MAIN)
->get(); ->get();
dd($tokens);
foreach ($tokens as $token) { foreach ($tokens as $token) {
$factory = APIRequest::getInstance(API::YANDEX); $factory = APIRequest::getInstance(API::YANDEX);
...@@ -60,6 +60,12 @@ class AdvertisementsAdd extends Command ...@@ -60,6 +60,12 @@ class AdvertisementsAdd extends Command
->join('advertisements', 'goal_advertisements.advertisement_id', '=', 'advertisements.id') ->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_advertisement_goal_ad_extensions', 'goal_advertisements.id', '=', 'goal_advertisement_goal_ad_extensions.goal_advertisement_id')
->leftJoin('goal_ad_extensions', 'goal_advertisements.id', '=', 'goal_ad_extensions.goal_ad_extension_id') ->leftJoin('goal_ad_extensions', 'goal_advertisements.id', '=', 'goal_ad_extensions.goal_ad_extension_id')
->whereNotExists(function (Builder $query) {
$query->select(DB::raw(1))
->from('goal_advertisement_goal_ad_extensions')
->join('goal_ad_extensions', 'goal_advertisements.id', '=', 'goal_ad_extensions.goal_ad_extension_id')
->whereColumn('goal_advertisements.id', 'goal_advertisement_goal_ad_extensions.goal_advertisement_id');
})
->whereNull('advertisements.deleted_at') ->whereNull('advertisements.deleted_at')
->whereNull('goal_advertisements.external_id') ->whereNull('goal_advertisements.external_id')
->whereNull('goal_advertisements.reserve_create_at') ->whereNull('goal_advertisements.reserve_create_at')
...@@ -79,7 +85,7 @@ class AdvertisementsAdd extends Command ...@@ -79,7 +85,7 @@ class AdvertisementsAdd extends Command
'advertisements.v_card_id as v_card_id', 'advertisements.v_card_id as v_card_id',
'advertisements.ad_image_hash as ad_image_hash', 'advertisements.ad_image_hash as ad_image_hash',
'advertisements.site_link_set_id as site_link_set_id', 'advertisements.site_link_set_id as site_link_set_id',
DB::raw('JSON_OBJECTAGG(goal_ad_extensions.external_id) as ad_extension_ids'), DB::raw('JSON_ARRAYAGG(goal_ad_extensions.external_id) as ad_extension_ids'),
'advertisements.ad_extensions as ad_extensions', 'advertisements.ad_extensions as ad_extensions',
'advertisements.video_extension as video_extension', 'advertisements.video_extension as video_extension',
'advertisements.price_extension as price_extension', 'advertisements.price_extension as price_extension',
...@@ -87,6 +93,7 @@ class AdvertisementsAdd extends Command ...@@ -87,6 +93,7 @@ class AdvertisementsAdd extends Command
'advertisements.business_id as business_id', 'advertisements.business_id as business_id',
'advertisements.prefer_v_card_over_business as prefer_v_card_over_business', 'advertisements.prefer_v_card_over_business as prefer_v_card_over_business',
]) ])
->groupBy('goal_advertisements.id')
->get(); ->get();
foreach (array_chunk($goalAds->pluck('id')->toArray(), 1000) as $items) { foreach (array_chunk($goalAds->pluck('id')->toArray(), 1000) as $items) {
......
...@@ -53,6 +53,7 @@ class CampaignsFirstLoad extends Command ...@@ -53,6 +53,7 @@ class CampaignsFirstLoad extends Command
Artisan::call(DictionariesLoad::class); Artisan::call(DictionariesLoad::class);
Artisan::call(CampaignsLoadGroups::class); Artisan::call(CampaignsLoadGroups::class);
Artisan::call(AdGroupsLoadKeywords::class); Artisan::call(AdGroupsLoadKeywords::class);
Artisan::call(AdExtensionsLoad::class);
Artisan::call(CampaignsLoadAds::class); Artisan::call(CampaignsLoadAds::class);
Artisan::call(CampaignsLoadBidModifiers::class); Artisan::call(CampaignsLoadBidModifiers::class);
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace App\Models; namespace App\Models;
use App\Models\Pivots\AdvertisementAdExtension;
use App\Models\Pivots\GoalAdvertisement; use App\Models\Pivots\GoalAdvertisement;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
...@@ -168,4 +169,12 @@ class Advertisement extends Model ...@@ -168,4 +169,12 @@ class Advertisement extends Model
return $this->hasMany(GoalAdvertisement::class, 'advertisement_id'); return $this->hasMany(GoalAdvertisement::class, 'advertisement_id');
} }
public function adExtensions()
{
return $this->belongsToMany(AdExtension::class, AdvertisementAdExtension::getModel()->getTable(), 'advertisement_id', 'ad_extension_id')
->using(AdvertisementAdExtension::class)
->withPivot(AdvertisementAdExtension::getWithPivot())
->withTimestamps();
}
} }
<?php
namespace App\Models\Pivots;
use App\Models\AdExtension;
use App\Models\Advertisement;
use Illuminate\Database\Eloquent\Relations\Pivot;
class AdvertisementAdExtension extends Pivot
{
protected $table = 'advertisement_ad_extensions';
protected $fillable = [
'advertisement_id',
'ad_extension_id',
];
public $incrementing = true;
static public function getWithPivot()
{
return [
'id',
'advertisement_id',
'ad_extension_id',
];
}
public function Advertisement()
{
return $this->belongsTo(Advertisement::class, 'advertisement_id');
}
public function adExtension()
{
return $this->belongsTo(AdExtension::class, 'ad_extension_id');
}
}
...@@ -148,4 +148,17 @@ class GoalAdvertisement extends Pivot ...@@ -148,4 +148,17 @@ class GoalAdvertisement extends Pivot
return $this->belongsTo(DictionaryCampaign::class, 'dictionary_campaign_id'); return $this->belongsTo(DictionaryCampaign::class, 'dictionary_campaign_id');
} }
public function goalAdExtensions()
{
return $this->belongsToMany(GoalAdExtension::class, GoalAdvertisementGoalAdExtension::getModel()->getTable(), 'goal_advertisement_id', 'goal_ad_extension_id')
->using(GoalAdvertisementGoalAdExtension::class)
->withPivot(GoalAdvertisementGoalAdExtension::getWithPivot())
->withTimestamps();
}
public function goalAdExtensionsForNotExternal()
{
return $this->goalAdExtensions()->forNotExternal();
}
} }
<?php
namespace App\Models\Pivots;
use App\Models\AdExtension;
use App\Models\Advertisement;
use Illuminate\Database\Eloquent\Relations\Pivot;
class GoalAdvertisementGoalAdExtension extends Pivot
{
protected $table = 'goal_advertisement_goal_ad_extensions';
protected $fillable = [
'goal_advertisement_id',
'goal_ad_extension_id',
];
public $incrementing = true;
static public function getWithPivot()
{
return [
'id',
'goal_advertisement_id',
'goal_ad_extension_id',
];
}
public function GoalAdvertisement()
{
return $this->belongsTo(GoalAdvertisement::class, 'goal_advertisement_id');
}
public function adExtension()
{
return $this->belongsTo(GoalAdExtension::class, 'goal_ad_extension_id');
}
}
...@@ -46,7 +46,6 @@ class GetAdExtensions extends DirectRequest ...@@ -46,7 +46,6 @@ class GetAdExtensions extends DirectRequest
function handle($response) function handle($response)
{ {
try { try {
dd($response);
if (isset($response['result']['AdExtensions'])) { if (isset($response['result']['AdExtensions'])) {
foreach ($response['result']['AdExtensions'] as $ad_extension) { foreach ($response['result']['AdExtensions'] as $ad_extension) {
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace App\Service\Requests\Direct; namespace App\Service\Requests\Direct;
use App\Jobs\ProcessCallLimitedAPI; use App\Jobs\ProcessCallLimitedAPI;
use App\Models\AdExtension;
use App\Models\AdGroup; use App\Models\AdGroup;
use App\Models\Advertisement; use App\Models\Advertisement;
use App\Service\Contract\APIRequest; use App\Service\Contract\APIRequest;
...@@ -92,6 +93,8 @@ class GetAds extends DirectRequest ...@@ -92,6 +93,8 @@ class GetAds extends DirectRequest
if ($this->getToken()->isMain()) { if ($this->getToken()->isMain()) {
$ad_extensions = collect();
$data = [ $data = [
'external_id' => $external_id, 'external_id' => $external_id,
'campaign_external_id' => $ad_group->campaign->external_id, 'campaign_external_id' => $ad_group->campaign->external_id,
...@@ -126,7 +129,6 @@ class GetAds extends DirectRequest ...@@ -126,7 +129,6 @@ class GetAds extends DirectRequest
'v_card_moderation' => $ad['TextAd']['VCardModeration'], 'v_card_moderation' => $ad['TextAd']['VCardModeration'],
'site_links_moderation' => $ad['TextAd']['SitelinksModeration'], 'site_links_moderation' => $ad['TextAd']['SitelinksModeration'],
'ad_image_moderation' => $ad['TextAd']['AdImageModeration'], 'ad_image_moderation' => $ad['TextAd']['AdImageModeration'],
'ad_extensions' => $ad['TextAd']['AdExtensions'],
'video_extension' => $ad['TextAd']['VideoExtension'], 'video_extension' => $ad['TextAd']['VideoExtension'],
'price_extension' => $ad['TextAd']['PriceExtension'], 'price_extension' => $ad['TextAd']['PriceExtension'],
'turbo_page_id' => $ad['TextAd']['TurboPageId'], 'turbo_page_id' => $ad['TextAd']['TurboPageId'],
...@@ -135,19 +137,32 @@ class GetAds extends DirectRequest ...@@ -135,19 +137,32 @@ class GetAds extends DirectRequest
'prefer_v_card_over_business' => isset($ad['TextAd']['PreferVCardOverBusiness']) ? $ad['TextAd']['PreferVCardOverBusiness'] === 'YES' : null, 'prefer_v_card_over_business' => isset($ad['TextAd']['PreferVCardOverBusiness']) ? $ad['TextAd']['PreferVCardOverBusiness'] === 'YES' : null,
]); ]);
$ad_extensions_array = [];
foreach ($ad['TextAd']['AdExtensions'] as $ad_extensions_data) { foreach ($ad['TextAd']['AdExtensions'] as $ad_extensions_data) {
/* $ad_extension_external_id = (string)$ad_extensions_data['AdExtensionId'];
* TODO: #20364 Загрузка и синхронизация расширений
*/ $ad_extension = AdExtension::firstWhere('external_id', $ad_extension_external_id);
if (!$ad_extension) {
$ad_extensions_array[] = $ad_extensions_data;
continue;
}
$ad_extensions->push($ad_extension);
} }
$data['ad_extensions'] = $ad_extensions_array;
} }
$advertisement = Advertisement::updateOrCreate([ $advertisement = Advertisement::updateOrCreate([
'external_id' => $external_id 'external_id' => $external_id
], $data); ], $data);
$advertisement->adExtensions()->sync($ad_extensions->pluck('id'));
if ($advertisement->wasRecentlyCreated) { if ($advertisement->wasRecentlyCreated) {
$campaign_ids_synced_need[$advertisement->campaign_id] = true; $campaign_ids_synced_need[$advertisement->campaign_id] = true;
} elseif ($advertisement->wasChanged(['campaign_id'])) { } elseif ($advertisement->wasChanged(['campaign_id'])) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!