Commit b6a04153 by Vladislav

#19473 Получение кеша фраз

1 parent 3a0613cf
Showing with 597 additions and 137 deletions
......@@ -3,11 +3,9 @@
namespace App\Console\Commands;
use App\Models\Tokens;
use App\Models\Variable;
use App\Service\API\API;
use App\Service\Requests\APIRequest;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
class AdGroupsAdd extends Command
{
......
......@@ -2,18 +2,11 @@
namespace App\Console\Commands;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\AdGroup;
use App\Models\Campaigns;
use App\Models\Pivots\DictionaryCampaign;
use App\Models\Tokens;
use App\Service\API\API;
use App\Service\Direct\CheckDictionaries;
use App\Service\Direct\GetCampaigns;
use App\Service\Requests\APIRequest;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Support\Facades\Bus;
class AdGroupsLoadUpdated extends Command
{
......@@ -58,11 +51,20 @@ class AdGroupsLoadUpdated extends Command
throw new \Exception('Не найден токен блин');
}
$ids = $adGroups->pluck('external_id')->all();
$factory = APIRequest::getInstance(API::YANDEX);
$factory->setToken($token);
$factory->getRequest('AdGroups', 'get')->call([
'Ids' => $adGroups->pluck('external_id')->all(),
'Ids' => $ids,
]);
$factory = APIRequest::getInstance(API::YANDEX);
$factory->setToken($token);
$factory->getRequest('Keywords', 'get')->call([
'AdGroupIds' => $ids,
]);
$tokens = Tokens::has('dictionaryCampaignsEnabledForExternalSynchronized.groupsForExternalForUpdatedSelf')
......@@ -74,11 +76,20 @@ class AdGroupsLoadUpdated extends Command
$factory = APIRequest::getInstance(API::YANDEX);
$factory->setToken($token);
$ids = $token->dictionaryCampaignsEnabledForExternalSynchronized->pluck('groupsForExternalForUpdatedSelf')
->collapse()
->pluck('external_id')
->all();
$factory->getRequest('AdGroups', 'get')->call([
'Ids' => $token->dictionaryCampaignsEnabledForExternalSynchronized->pluck('groupsForExternalForUpdatedSelf')
->collapse()
->pluck('external_id')
->all(),
'Ids' => $ids,
]);
$factory = APIRequest::getInstance(API::YANDEX);
$factory->setToken($token);
$factory->getRequest('Keywords', 'get')->call([
'AdGroupIds' => $ids,
]);
}
......
......@@ -3,11 +3,9 @@
namespace App\Console\Commands;
use App\Models\Tokens;
use App\Models\Variable;
use App\Service\API\API;
use App\Service\Requests\APIRequest;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
class AdGroupsUpdate extends Command
{
......
......@@ -3,11 +3,9 @@
namespace App\Console\Commands;
use App\Models\Tokens;
use App\Models\Variable;
use App\Service\API\API;
use App\Service\Requests\APIRequest;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
class CampaignsAdd extends Command
{
......
......@@ -2,17 +2,11 @@
namespace App\Console\Commands;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Campaigns;
use App\Models\Pivots\DictionaryCampaign;
use App\Models\Tokens;
use App\Service\API\API;
use App\Service\Direct\CheckDictionaries;
use App\Service\Direct\GetCampaigns;
use App\Service\Requests\APIRequest;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Support\Facades\Bus;
class CampaignsCheckUpdatedChildrenAdGroups extends Command
{
......
......@@ -43,6 +43,7 @@ class CampaignsLoadGroups extends Command
public function handle()
{
$campaigns = Campaigns::forManaged()->forEnabled()->forGroupsLoadable()->get();
if (!$campaigns->count()) {
return;
}
......@@ -55,10 +56,20 @@ class CampaignsLoadGroups extends Command
$factory = APIRequest::getInstance(API::YANDEX);
$factory->setToken($token);
$campaign_ids = $campaigns->pluck('external_id')->all();
$factory->getRequest('AdGroups', 'get')
->call([
'CampaignIds' => $campaigns->pluck('external_id')->all(),
'CampaignIds' => $campaign_ids,
]);
$factory = APIRequest::getInstance(API::YANDEX);
$factory->setToken($token);
$factory->getRequest('Keywords', 'get')
->call([
'CampaignIds' => $campaign_ids,
]);
return 0;
......
<?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 CampaignsLoadKeywords extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'campaigns:loadKeywords';
/**
* 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()
{
$campaigns = Campaigns::forManaged()->forEnabled()->forNotGroupsLoadable()->forKeywordsLoadable()->get();
if (!$campaigns->count()) {
return;
}
$token = Tokens::where('type', Tokens::MAIN)->first();
if (!$token) {
throw new \Exception('Не найден токен блин');
}
$campaign_ids = $campaigns->pluck('external_id')->all();
$factory = APIRequest::getInstance(API::YANDEX);
$factory->setToken($token);
$factory->getRequest('Keywords', 'get')
->call([
'CampaignIds' => $campaign_ids,
]);
return 0;
}
}
......@@ -2,17 +2,13 @@
namespace App\Console\Commands;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Campaigns;
use App\Models\Pivots\DictionaryCampaign;
use App\Models\Tokens;
use App\Service\API\API;
use App\Service\Direct\CheckDictionaries;
use App\Service\Direct\GetCampaigns;
use App\Service\Requests\APIRequest;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Support\Facades\Bus;
class CampaignsLoadUpdated extends Command
{
......
......@@ -3,12 +3,9 @@
namespace App\Console\Commands;
use App\Models\Tokens;
use App\Models\Variable;
use App\Service\API\API;
use App\Service\Requests\APIRequest;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
class CampaignsResume extends Command
{
......
......@@ -6,8 +6,6 @@ use App\Models\Tokens;
use App\Service\API\API;
use App\Service\Requests\APIRequest;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
class CampaignsSuspend extends Command
{
......
......@@ -3,11 +3,9 @@
namespace App\Console\Commands;
use App\Models\Tokens;
use App\Models\Variable;
use App\Service\API\API;
use App\Service\Requests\APIRequest;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
class CampaignsUpdate extends Command
{
......
......@@ -4,8 +4,6 @@ namespace App\Console\Commands;
use App\Models\Tokens;
use App\Service\API\API;
use App\Service\Direct\CheckDictionaries;
use App\Service\Direct\GetCampaigns;
use App\Service\Requests\APIRequest;
use Illuminate\Console\Command;
......
......@@ -9,6 +9,7 @@ use App\Console\Commands\CampaignsAdd;
use App\Console\Commands\CampaignsCheckChange;
use App\Console\Commands\CampaignsCheckUpdatedChildrenAdGroups;
use App\Console\Commands\CampaignsLoadGroups;
use App\Console\Commands\CampaignsLoadKeywords;
use App\Console\Commands\CampaignsResume;
use App\Console\Commands\CampaignsSuspend;
use App\Console\Commands\CampaignsUpdate;
......@@ -49,6 +50,7 @@ class Kernel extends ConsoleKernel
$schedule->command(CampaignsSuspend::class)->hourlyAt(20);
$schedule->command(CampaignsLoadGroups::class)->hourlyAt(40);
$schedule->command(CampaignsLoadKeywords::class)->hourlyAt(45);
$schedule->command(AdGroupsLoadUpdated::class)->hourlyAt(45);
$schedule->command(AdGroupsAdd::class)->hourlyAt(50);
$schedule->command(AdGroupsUpdate::class)->hourlyAt(55);
......
......@@ -106,7 +106,6 @@ class AdGroup extends Model
];
protected $casts = [
'campaign_id' => 'int',
'region_ids' => 'array',
'negative_keywords' => 'json',
'negative_keyword_shared_set_ids' => 'json',
......@@ -190,6 +189,11 @@ class AdGroup extends Model
return $this->hasMany(GoalAdGroup::class, 'ad_group_id');
}
public function keywords()
{
return $this->hasMany(Keyword::class, 'ad_group_id');
}
public function campaign()
{
return $this->belongsTo(Campaigns::class, 'campaign_id');
......
......@@ -5,6 +5,7 @@ namespace App\Models;
use App\Models\Pivots\DictionaryCampaignVariable;
use App\Models\Pivots\DictionaryCampaign;
use App\Models\Pivots\GoalAdGroup;
use App\Models\Pivots\GoalKeyword;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
......@@ -34,6 +35,7 @@ use Illuminate\Support\Collection;
* @property bool $manage
* @property bool $enabled
* @property \Illuminate\Support\Carbon|null $groups_loaded_at
* @property \Illuminate\Support\Carbon|null $keywords_loaded_at
* @property \Illuminate\Support\Carbon|null $disabled_at
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
......@@ -46,6 +48,8 @@ use Illuminate\Support\Collection;
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Variable[] $dictionaryCampaigns
* @property-read int|null $dictionary_campaigns_count
* @method static Builder|Campaigns forGroupsLoadable()
* @method static Builder|Campaigns forNotGroupsLoadable()
* @method static Builder|Campaigns forKeywordsLoadable()
* @method static Builder|Campaigns forManaged($value = true)
* @method static Builder|Campaigns forEnabled($value = true)
* @method static Builder|Campaigns notDisabled()
......@@ -103,6 +107,8 @@ class Campaigns extends Model
'relevant_keywords',
'attribution_model',
'priority_goals',
'groups_loaded_at',
'keywords_loaded_at',
'updated_self',
'updated_children',
'manage',
......@@ -122,6 +128,7 @@ class Campaigns extends Model
'daily_budget' => 'array',
'priority_goals' => 'array',
'groups_loaded_at' => 'datetime',
'keywords_loaded_at' => 'datetime',
'updated_self' => 'datetime',
'updated_children' => 'datetime',
'manage' => 'boolean',
......@@ -176,6 +183,7 @@ class Campaigns extends Model
if ($campaign->manage) {
$campaign->copyInGoalCampaign();
$campaign->copyGroupInGoalGroup();
$campaign->copyGroupInGoalKeyword();
}
});
......@@ -185,6 +193,7 @@ class Campaigns extends Model
if ($campaign->manage) {
$campaign->copyInGoalCampaign();
$campaign->copyGroupInGoalGroup();
$campaign->copyGroupInGoalKeyword();
} else {
$campaign->dictionaries()->detach();
}
......@@ -231,7 +240,8 @@ class Campaigns extends Model
$campaign->groups()->get()->each(function (AdGroup $adGroup) use ($dictionaryCampaign) {
$dictionaryCampaign->groups()->updateOrCreate([
'campaign_external_id' => $dictionaryCampaign->external_id,
'dictionary_campaign_id' => $dictionaryCampaign->getKey(),
'dictionary_campaign_external_id' => $dictionaryCampaign->external_id,
'ad_group_id' => $adGroup->getKey(),
], GoalAdGroup::copyPropertyFromMain($adGroup));
......@@ -240,6 +250,36 @@ class Campaigns extends Model
});
}
public function copyGroupInGoalKeyword()
{
$campaign = $this;
\Log::debug('==========================');
$campaign->dictionaryCampaigns()->get()->each(function (DictionaryCampaign $dictionaryCampaign) use ($campaign) {
$dictionaryCampaign->groups()->get()->each(function (GoalAdGroup $goalAdGroup) use ($campaign, $dictionaryCampaign) {
$goalAdGroup->group->keywords()->where('campaign_id', $campaign->getKey())->get()->each(function (Keyword $keyword) use ($dictionaryCampaign, $goalAdGroup) {
$goalAdGroup->goalKeywords()->updateOrCreate([
'dictionary_campaign_id' => $dictionaryCampaign->getKey(),
'goal_ad_group_id' => $goalAdGroup->getKey(),
'keyword_id' => $keyword->getKey(),
], array_merge(
GoalKeyword::copyPropertyFromMain($keyword),
[
'dictionary_campaign_external_id' => $dictionaryCampaign->external_id,
'goal_ad_group_external_id' => $goalAdGroup->external_id,
]
));
});
});
});
}
public function getBiddingStrategyAttribute($bidding_strategy)
{
$bidding_strategy = json_decode($bidding_strategy, true);
......@@ -368,6 +408,24 @@ class Campaigns extends Model
/**
* @param Builder $query
* @return Builder
*/
public function scopeForNotGroupsLoadable($query)
{
return $query->whereNotNull('groups_loaded_at');
}
/**
* @param Builder $query
* @return Builder
*/
public function scopeForKeywordsLoadable($query)
{
return $query->whereNull('keywords_loaded_at');
}
/**
* @param Builder $query
* @param bool $manage
* @return Builder
*/
......
......@@ -3,7 +3,6 @@
namespace App\Models;
use App\Models\Pivots\DictionaryCampaign;
use App\Models\Pivots\DictionaryCampaignVariable;
use Illuminate\Database\Eloquent\Builder;
/**
......
......@@ -2,9 +2,8 @@
namespace App\Models;
use App\Models\Pivots\GoalAdGroup;
use App\Models\Pivots\GoalKeyword;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
......@@ -25,7 +24,9 @@ class Keyword extends Model
protected $fillable = [
'external_id',
'ad_group_external_id',
'campaign_external_id',
'ad_group_id',
'campaign_id',
'keyword',
'user_param_1',
'user_param_2',
......@@ -34,18 +35,9 @@ class Keyword extends Model
'state',
'status',
'serving_status',
'statistics_search',
'statistics_network',
];
protected $casts = [
'external_id' => 'int',
'ad_group_external_id' => 'int',
'ad_group_id' => 'int',
'bid' => 'int',
'context_bid' => 'int',
'statistics_search' => 'json',
'statistics_network' => 'json',
];
/**
......@@ -54,17 +46,9 @@ class Keyword extends Model
static public function getPropertiesWatch()
{
return collect([
'ad_group_external_id',
'keyword',
'user_param_1',
'user_param_2',
'bid',
'context_bid',
'state',
'status',
'serving_status',
'statistics_search',
'statistics_network',
]);
}
......@@ -72,8 +56,11 @@ class Keyword extends Model
{
parent::boot();
static::created(function (Keyword $keyword) {
//
if ($keyword->campaign) {
$keyword->campaign->copyGroupInGoalKeyword();
}
});
static::updated(function (Keyword $keyword) {
......@@ -81,9 +68,15 @@ class Keyword extends Model
if (GoalKeyword::getPropertiesCopyWithPivot()->first(function ($property_name) use ($keyword) {
return $keyword->{$property_name} !== $keyword->getOriginal($property_name);
})) {
$keyword->goalKeywords()->update(
GoalKeyword::copyPropertyFromMain($keyword)
);
if (!is_null($keyword->campaign_id) && is_null($keyword->getOriginal('campaign_id'))) {
$keyword->campaign->copyGroupInGoalKeyword();
} else {
$keyword->goalKeywords()->update(
GoalKeyword::copyPropertyFromMain($keyword)
);
}
}
if (self::getPropertiesWatch()->first(function ($property_name) use ($keyword) {
......@@ -107,4 +100,9 @@ class Keyword extends Model
return $this->belongsTo(AdGroup::class, 'ad_group_id');
}
public function campaign()
{
return $this->belongsTo(Campaigns::class, 'campaign_id');
}
}
......@@ -2,7 +2,6 @@
namespace App\Models\Pivots;
use \App\Models\Pivots\GoalAdGroup;
use App\Models\Campaigns;
use App\Models\Dictionary;
use App\Models\Variable;
......@@ -102,8 +101,6 @@ class DictionaryCampaign extends Pivot
];
protected $casts = [
'campaign_id' => 'int',
'dictionary_id' => 'int',
'negative_keywords' => 'array',
'excluded_sites' => 'array',
'external_upload_at' => 'datetime',
......@@ -330,4 +327,9 @@ class DictionaryCampaign extends Pivot
return $this->groups()->forExternal()->needUpdated();
}
public function goalKeywords()
{
return $this->hasMany(GoalKeyword::class, 'dictionary_campaign_id');
}
}
......@@ -40,8 +40,6 @@ class DictionaryCampaignVariable extends Pivot
];
protected $casts = [
'dictionary_campaign_id' => 'int',
'variable_id' => 'int',
];
static public function getWithPivot()
......
......@@ -13,7 +13,7 @@ use Illuminate\Support\Collection;
*
* @property int $id
* @property int|null $external_id
* @property int|null $campaign_external_id
* @property int|null $dictionary_campaign_external_id
* @property int $dictionary_campaign_id
* @property string|null $name
* @property array|null $negative_keywords
......@@ -59,7 +59,7 @@ class GoalAdGroup extends Pivot
protected $fillable = [
'external_id',
'campaign_external_id',
'dictionary_campaign_external_id',
'ad_group_id',
'dictionary_campaign_id',
'name',
......@@ -72,8 +72,6 @@ class GoalAdGroup extends Pivot
];
protected $casts = [
'ad_group_id' => 'int',
'dictionary_campaign_id' => 'int',
'negative_keywords' => 'array',
'external_upload_at' => 'datetime',
'external_updated_at' => 'datetime',
......@@ -87,7 +85,7 @@ class GoalAdGroup extends Pivot
return [
'id',
'external_id',
'campaign_external_id',
'dictionary_campaign_external_id',
'ad_group_id',
'dictionary_campaign_id',
'name',
......@@ -183,17 +181,9 @@ class GoalAdGroup extends Pivot
return $this->belongsTo(DictionaryCampaign::class, 'dictionary_campaign_id');
}
public function dictionaryCampaignVariables()
public function goalKeywords()
{
return $this->hasMany(DictionaryCampaignVariable::class, 'dictionary_campaign_id');
}
public function variables()
{
return $this->belongsToMany(Variable::class, 'dictionary_campaign_variables', 'dictionary_campaign_id', 'variable_id')
->using(DictionaryCampaignVariable::class)
->withPivot(DictionaryCampaignVariable::getWithPivot())
->withTimestamps();
return $this->belongsTo(GoalKeyword::class, 'goal_ad_group_id');
}
}
<?php
namespace App\Models\Pivots;
use App\Models\Keyword;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Support\Collection;
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',
];
protected $casts = [
'external_upload_at' => 'datetime',
'external_updated_at' => 'datetime',
'updated_need' => 'datetime',
];
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();
}
/**
* @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');
}
}
......@@ -58,7 +58,7 @@ class AddAdGroups extends DirectRequest
}
foreach ($response['result']['AddResults'] as $key => $add_result) {
$external_id = $add_result['Id'] ?? '';
$external_id = (string)$add_result['Id'] ?? '';
if (!$external_id) {
Log::debug("AddAdGroup, empty Id");
......@@ -74,6 +74,10 @@ class AddAdGroups extends DirectRequest
'external_upload_at' => Carbon::now(),
]);
$goalAdGroup->goalKeywords()->update([
'goal_ad_group_external_id' => $external_id,
]);
}
} catch (\Exception $e) {
Log::debug($e);
......@@ -102,7 +106,7 @@ class AddAdGroups extends DirectRequest
$data = [
'Name' => StrReplaceByVariables::getInstance($goalAdGroup->name, $list)->get(),
'CampaignId' => $goalAdGroup->campaign_external_id,
'CampaignId' => $goalAdGroup->dictionary_campaign_external_id,
'RegionIds' => $goalAdGroup->group->region_ids,
];
......
......@@ -58,7 +58,7 @@ class AddCampaigns extends DirectRequest
}
foreach ($response['result']['AddResults'] as $key => $add_result) {
$external_id = $add_result['Id'] ?? '';
$external_id = (string)$add_result['Id'] ?? '';
if (!$external_id) {
Log::debug("AddCampaigns, empty Id");
......@@ -75,7 +75,11 @@ class AddCampaigns extends DirectRequest
]);
$dictionaryCampaign->groups()->update([
'campaign_external_id' => $external_id,
'dictionary_campaign_external_id' => $external_id,
]);
$dictionaryCampaign->goalKeywords()->update([
'dictionary_campaign_external_id' => $external_id,
]);
}
......
......@@ -39,7 +39,7 @@ class CheckCampaignsChanges extends DirectRequest
$data['updated_children'] = Carbon::now();
}
$external_id = $campaign_data['CampaignId'];
$external_id = (string)$campaign_data['CampaignId'];
if ($this->getToken()->isMain()) {
......
......@@ -74,6 +74,8 @@ class CheckChanges extends DirectRequest
if (isset($response['result']['Modified']['AdGroupIds'])) {
foreach ($response['result']['Modified']['AdGroupIds'] as $ad_group_id) {
$ad_group_id = (string)$ad_group_id;
if ($this->getToken()->isMain()) {
$adGroup = AdGroup::where('external_id', $ad_group_id)->first();
......
......@@ -62,10 +62,12 @@ class GetAdGroups extends DirectRequest
}
foreach ($response['result']['AdGroups'] as $ad_group) {
if (isset($external_ids[$ad_group['CampaignId']])) {
$external_id = (string)$ad_group['CampaignId'];
if (isset($external_ids[$external_id])) {
continue;
}
$external_ids[$ad_group['CampaignId']] = true;
$external_ids[$external_id] = true;
}
if (!count($external_ids)) {
......@@ -84,56 +86,67 @@ class GetAdGroups extends DirectRequest
foreach ($response['result']['AdGroups'] as $ad_group) {
$campaign = $campaigns->firstWhere('external_id', $ad_group['CampaignId']);
$external_id = (string)$ad_group['CampaignId'];
$campaign = $campaigns->firstWhere('external_id', $external_id);
if (!$campaign)
continue;
$external_id = $ad_group['Id'];
$data = [
'campaign_id' => $campaign->getKey(),
'external_id' => $external_id,
'campaign_external_id' => $ad_group['CampaignId'],
'name' => $ad_group['Name'],
'region_ids' => $ad_group['RegionIds'],
'negative_keywords' => $ad_group['NegativeKeywords'],
'negative_keyword_shared_set_ids' => $ad_group['NegativeKeywordSharedSetIds'],
'tracking_params' => $ad_group['TrackingParams'],
'mobile_app_ad_group' => $ad_group['MobileAppAdGroup'] ?? null,
'dynamic_text_ad_group' => $ad_group['DynamicTextAdGroup'] ?? null,
'dynamic_text_feed_ad_group' => $ad_group['DynamicTextFeedAdGroup'] ?? null,
'cpm_banner_user_profile_ad_group' => $ad_group['CpmBannerUserProfileAdGroup'] ?? null,
'cpm_video_ad_group' => $ad_group['CpmVideoAdGroup'] ?? null,
'smart_ad_group' => $ad_group['SmartAdGroup'] ?? null,
'status' => $ad_group['Status'],
'type' => $ad_group['Type'],
'sub_type' => $ad_group['Subtype'],
'serving_status' => $ad_group['ServingStatus'],
'restricted_region_ids' => $ad_group['RestrictedRegionIds'],
'updated_self' => null,
];
$external_id = (string)$ad_group['Id'];
if ($this->getToken()->isMain()) {
$data = [
'campaign_id' => $campaign->getKey(),
'external_id' => $external_id,
'campaign_external_id' => $campaign->external_id,
'name' => $ad_group['Name'],
'region_ids' => $ad_group['RegionIds'],
'negative_keywords' => $ad_group['NegativeKeywords'],
'negative_keyword_shared_set_ids' => $ad_group['NegativeKeywordSharedSetIds'],
'tracking_params' => $ad_group['TrackingParams'],
'mobile_app_ad_group' => $ad_group['MobileAppAdGroup'] ?? null,
'dynamic_text_ad_group' => $ad_group['DynamicTextAdGroup'] ?? null,
'dynamic_text_feed_ad_group' => $ad_group['DynamicTextFeedAdGroup'] ?? null,
'cpm_banner_user_profile_ad_group' => $ad_group['CpmBannerUserProfileAdGroup'] ?? null,
'cpm_video_ad_group' => $ad_group['CpmVideoAdGroup'] ?? null,
'smart_ad_group' => $ad_group['SmartAdGroup'] ?? null,
'status' => $ad_group['Status'],
'type' => $ad_group['Type'],
'sub_type' => $ad_group['Subtype'],
'serving_status' => $ad_group['ServingStatus'],
'restricted_region_ids' => $ad_group['RestrictedRegionIds'],
'updated_self' => null,
];
AdGroup::updateOrCreate([
'external_id' => $external_id
'external_id' => $external_id,
'campaign_external_id' => $campaign->external_id,
], $data);
$campaign->update([
'groups_loaded_at' => Carbon::now(),
]);
} else {
$goalAdGroup = GoalAdGroup::find($external_id);
$goalAdGroup = GoalAdGroup::firstWhere('external_id', $external_id);
if (!$goalAdGroup)
continue;
$goalAdGroup->update(
$goalAdGroup::copyPropertyFromMain($data)
);
}
$data = [
'external_updated_at' => Carbon::now(),
];
$campaign->update([
'groups_loaded_at' => Carbon::now(),
]);
if ($campaign->updated) {
$data['updated_need'] = Carbon::now();
}
$goalAdGroup->update($data);
}
}
......
......@@ -50,7 +50,7 @@ class GetCampaigns extends DirectRequest
foreach ($response['result']['Campaigns'] as $campaign_data) {
$external_id = $campaign_data['Id'];
$external_id = (string)$campaign_data['Id'];
$data = [
'external_id' => $external_id,
......
<?php
namespace App\Service\Requests\Direct;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\AdGroup;
use App\Models\Campaigns;
use App\Models\Keyword;
use App\Models\Pivots\DictionaryCampaign;
use App\Models\Pivots\GoalAdGroup;
use App\Models\Pivots\GoalKeyword;
use App\Service\Contract\APIRequest;
use App\Service\Requests\DirectRequest;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class GetKeywords extends DirectRequest
{
protected $max_count = -1;
protected $max_count_CampaignIds = 10;
protected $max_count_AdGroupIds = 1000;
protected $max_count_Ids = 10000;
function call($params = null)
{
$this->requestPrepare($params);
$process = new ProcessCallLimitedAPI($this);
dispatch($process)->onQueue('limits');
}
public function getObjectsCount()
{
$params = $this->getParams();
if (isset($params['SelectionCriteria']['CampaignIds'])) {
return count($params['SelectionCriteria']['CampaignIds']);
}
if (isset($params['SelectionCriteria']['AdGroupIds'])) {
return count($params['SelectionCriteria']['AdGroupIds']);
}
if (isset($params['SelectionCriteria']['Ids'])) {
return count($params['SelectionCriteria']['Ids']);
}
return -1;
}
public function slice($maxObjects): ?APIRequest
{
$params = $this->getParams();
if (isset($params['SelectionCriteria']['CampaignIds'])) {
return $this->sliceByKey($maxObjects, ['SelectionCriteria', 'CampaignIds']);
}
if (isset($params['SelectionCriteria']['AdGroupIds'])) {
return $this->sliceByKey($maxObjects, ['SelectionCriteria', 'AdGroupIds']);
}
if (isset($params['SelectionCriteria']['Ids'])) {
return $this->sliceByKey($maxObjects, ['SelectionCriteria', 'Ids']);
}
return null;
}
function handle($response)
{
DB::beginTransaction();
try {
$external_ids = [];
if (!isset($response['result']['Keywords'])) {
return;
}
foreach ($response['result']['Keywords'] as $keyword) {
$adGroupId = (string)$keyword['AdGroupId'];
if (isset($external_ids[$adGroupId])) {
continue;
}
$external_ids[$adGroupId] = true;
}
if (!count($external_ids)) {
return;
}
if ($this->getToken()->isMain()) {
$ad_groups = AdGroup::whereIn('external_id', array_keys($external_ids))
->get()
->keyBy('external_id');
} else {
$ad_groups = GoalAdGroup::whereIn('external_id', array_keys($external_ids))
->get()
->keyBy('external_id');
}
foreach ($response['result']['Keywords'] as $keyword) {
$ad_group = $ad_groups->firstWhere('external_id', $keyword['AdGroupId']);
if (!$ad_group)
continue;
$external_id = (string)$keyword['Id'];
if ($this->getToken()->isMain()) {
$data = [
'external_id' => $external_id,
'campaign_external_id' => $ad_group->campaign->external_id,
'ad_group_external_id' => $ad_group->external_id,
'ad_group_id' => $ad_group->getKey(),
'campaign_id' => $ad_group->campaign->getKey(),
'keyword' => $keyword['Keyword'],
'user_param_1' => $keyword['UserParam1'],
'user_param_2' => $keyword['UserParam2'],
'bid' => $keyword['Bid'],
'context_bid' => $keyword['ContextBid'],
'state' => $keyword['State'],
'status' => $keyword['Status'],
'serving_status' => $keyword['ServingStatus'],
];
Keyword::updateOrCreate([
'external_id' => $external_id
], $data);
$ad_group->campaign->update([
'keywords_loaded_at' => Carbon::now(),
]);
} else {
$goalKeyword = GoalKeyword::firstWhere('external_id', $external_id);
if (!$goalKeyword)
continue;
$data = [
'external_updated_at' => Carbon::now(),
];
if ($goalKeyword->dictionaryCampaign->updated) {
$data['updated_need'] = Carbon::now();
}
$goalKeyword->update($data);
}
}
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
Log::debug($e);
throw $e;
}
}
private function requestPrepare($filter)
{
$this->setService('Keywords');
$this->setMethod('get');
$params = [
"FieldNames" => [
"Id", "Keyword", "AdGroupId", "CampaignId",
"UserParam1", "UserParam2", "Bid",
"ContextBid", "State", "Status", "ServingStatus",
],
];
if (isset($filter['CampaignIds'])) {
$this->max_count = $this->max_count_CampaignIds;
$params['SelectionCriteria'] = [
'CampaignIds' => $filter['CampaignIds'],
];
}
if (isset($filter['AdGroupIds'])) {
$this->max_count = $this->max_count_AdGroupIds;
$params['SelectionCriteria'] = [
'AdGroupIds' => $filter['AdGroupIds'],
];
}
if (isset($filter['Ids'])) {
$this->max_count = $this->max_count_Ids;
$params['SelectionCriteria'] = [
'Ids' => $filter['Ids'],
];
}
$this->setParams($params);
}
}
......@@ -34,7 +34,7 @@ class ResumeCampaigns extends DirectRequest
{
try {
foreach ($response['result']['ResumeResults'] as $resume_result) {
$external_id = $resume_result['Id'] ?? '';
$external_id = (string)$resume_result['Id'] ?? '';
if (!$external_id) {
Log::debug("resumeCampaigns, empty Id");
......
......@@ -35,7 +35,7 @@ class SuspendCampaigns extends DirectRequest
{
try {
foreach ($response['result']['SuspendResults'] as $suspend_result) {
$external_id = $suspend_result['Id'] ?? '';
$external_id = (string)$suspend_result['Id'] ?? '';
if (!$external_id) {
Log::debug("SuspendCampaigns, empty Id");
......
......@@ -48,7 +48,7 @@ class UpdateAdGroups extends DirectRequest
}
foreach ($response['result']['UpdateResults'] as $key => $update_result) {
$external_id = $update_result['Id'] ?? '';
$external_id =(string) $update_result['Id'] ?? '';
if (!$external_id) {
Log::debug("AddAdGroup, empty Id");
......
......@@ -39,7 +39,7 @@ class UpdateCampaigns extends DirectRequest
{
try {
foreach ($response['result']['UpdateResults'] as $key => $add_result) {
$external_id = $add_result['Id'] ?? '';
$external_id = (string)$add_result['Id'] ?? '';
if (!$external_id) {
Log::debug("AddCampaigns, empty Id");
......
......@@ -34,6 +34,7 @@ class CreateCampaignsTable extends Migration
$table->boolean('manage')->default(0);
$table->boolean('enabled')->default(1);
$table->timestamp('groups_loaded_at')->nullable();
$table->timestamp('keywords_loaded_at')->nullable();
$table->timestamp('disabled_at')->nullable();
$table->timestamps();
......
......@@ -16,7 +16,7 @@ class CreateGoalAdGroupsTable extends Migration
Schema::create('goal_ad_groups', function (Blueprint $table) {
$table->id();
$table->bigInteger('external_id')->nullable();
$table->bigInteger('campaign_external_id')->nullable();
$table->bigInteger('dictionary_campaign_external_id')->nullable();
$table->bigInteger('ad_group_id')->unsigned();
$table->bigInteger('dictionary_campaign_id')->unsigned();
$table->string('name', 255)->nullable();
......
......@@ -16,9 +16,11 @@ class CreateKeywordsTable extends Migration
{
Schema::create('keywords', function (Blueprint $table) {
$table->id();
$table->bigInteger('external_id')->nullable();
$table->bigInteger('ad_group_external_id')->nullable();
$table->bigInteger('external_id');
$table->bigInteger('ad_group_external_id');
$table->bigInteger('campaign_external_id');
$table->bigInteger('ad_group_id')->unsigned();
$table->bigInteger('campaign_id')->unsigned();
$table->text('keyword')->nullable();
$table->string('user_param_1', 255)->nullable();
$table->string('user_param_2', 255)->nullable();
......@@ -39,12 +41,12 @@ class CreateKeywordsTable extends Migration
Keyword::SERVING_STATUS_ELIGIBLE,
Keyword::SERVING_STATUS_RARELY_SERVED,
])->nullable();
$table->json('statistics_search')->nullable();
$table->json('statistics_network')->nullable();
$table->timestamps();
$table->foreign('ad_group_id')->references('id')->on('ad_groups')
->cascadeOnDelete();
$table->foreign('campaign_id')->references('id')->on('campaigns')
->cascadeOnDelete();
});
}
......
......@@ -16,7 +16,9 @@ class CreateGoalKeywordsTable extends Migration
Schema::create('goal_keywords', function (Blueprint $table) {
$table->id();
$table->bigInteger('external_id')->nullable();
$table->bigInteger('dictionary_campaign_external_id')->nullable();
$table->bigInteger('goal_ad_group_external_id')->nullable();
$table->bigInteger('dictionary_campaign_id')->unsigned();
$table->bigInteger('goal_ad_group_id')->unsigned();
$table->bigInteger('keyword_id')->unsigned();
$table->timestamp('external_upload_at')->nullable();
......@@ -25,7 +27,9 @@ class CreateGoalKeywordsTable extends Migration
$table->timestamps();
$table->foreign('ad_group_id')->references('id')->on('ad_groups')
$table->foreign('dictionary_campaign_id')->references('id')->on('dictionary_campaigns')
->cascadeOnDelete();
$table->foreign('goal_ad_group_id')->references('id')->on('goal_ad_groups')
->cascadeOnDelete();
$table->foreign('keyword_id')->references('id')->on('keywords')
->cascadeOnDelete();
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!