Commit 879faa7e by Vladislav

#19468 Первоначальная загрузка групп объявлений.

1 parent 3c283578
<?php
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 AdGroupLoadUpdated extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'adgroups:loadUpdated';
/**
* 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()
{
$campaigns = AdGroup::forUpdatedSelf()->get();
if (!$campaigns->count()) {
return;
}
$token = Tokens::where('type', Tokens::MAIN)->first();
if (!$token) {
throw new \Exception('Не найден токен блин');
}
$factory = APIRequest::getInstance(API::YANDEX);
$factory->setToken($token);
$factory->getRequest('Campaigns', 'get')->call([
'ids' => $campaigns->pluck('external_id')->all(),
]);
$tokens = Tokens::has('dictionaryCampaignsEnabledForExternalSynchronized.groupsForExternalForUpdatedSelf')
->with('dictionaryCampaignsEnabledForExternalSynchronized.groupsForExternalForUpdatedSelf')
->where('type', '!=', Tokens::MAIN)
->get();
foreach ($tokens as $token) {
$factory = APIRequest::getInstance(API::YANDEX);
$factory->setToken($token);
$factory->getRequest('AdGroups', 'get')->call([
'Ids' => $token->dictionaryCampaignsEnabledForExternalSynchronized->pluck('groupsForExternalForUpdatedSelf')
->collapse()->all(),
]);
}
return 0;
}
}
......@@ -54,10 +54,9 @@ class AdGroupsAdd extends Command
$factory = APIRequest::getInstance(API::YANDEX);
$factory->setToken($token);
$factory->getRequest('AdGroups', 'add')
->call([
'goalAdGroups' => $token->dictionaryCampaignsEnabledForExternal->pluck('dictionaryCampaignsEnabledForExternalSynchronized')
'goalAdGroups' => $token->dictionaryCampaignsEnabledForExternalSynchronized->pluck('groupsForNotExternal')
->collapse(),
]);
}
......
......@@ -2,11 +2,18 @@
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
{
......@@ -22,7 +29,7 @@ class AdGroupsLoadUpdated extends Command
*
* @var string
*/
protected $description = 'Загрузка измененных групп';
protected $description = 'Загрузка измененные группы';
/**
* Create a new command instance.
......@@ -38,26 +45,42 @@ class AdGroupsLoadUpdated extends Command
* Execute the console command.
*
* @return int
* @throws \Exception
*/
public function handle()
{
$token = Tokens::has('campaignsAdGroupsForUpdatedSelf')
->with('campaignsAdGroupsForUpdatedSelf')
->where('type', '!=', Tokens::MAIN)
->first();
$adGroups = AdGroup::forUpdatedSelf()->get();
if (!$adGroups->count()) {
return;
}
$token = Tokens::where('type', Tokens::MAIN)->first();
if (!$token) {
return 0;
throw new \Exception('Не найден токен блин');
}
$factory = APIRequest::getInstance(API::YANDEX);
$factory->setToken($token);
$factory->getRequest('AdGroups', 'get')
->call([
'CampaignIds' => $token->campaignsAdGroupsForUpdatedSelf->pluck('campaign_external_id')->all(),
$factory->getRequest('AdGroups', 'get')->call([
'Ids' => $adGroups->pluck('external_id')->all(),
]);
$tokens = Tokens::has('dictionaryCampaignsEnabledForExternalSynchronized.groupsForExternalForUpdatedSelf')
->with('dictionaryCampaignsEnabledForExternalSynchronized.groupsForExternalForUpdatedSelf')
->where('type', '!=', Tokens::MAIN)
->get();
foreach ($tokens as $token) {
$factory = APIRequest::getInstance(API::YANDEX);
$factory->setToken($token);
$factory->getRequest('AdGroups', 'get')->call([
'Ids' => $token->dictionaryCampaignsEnabledForExternalSynchronized->pluck('groupsForExternalForUpdatedSelf')
->collapse()
->pluck('external_id')
->all(),
]);
}
return 0;
}
......
......@@ -105,8 +105,6 @@ class AdGroup extends Model
protected $casts = [
'campaign_id' => 'int',
'external_id' => 'int',
'campaign_external_id' => 'int',
'region_ids' => 'array',
'negative_keywords' => 'json',
'negative_keyword_shared_set_ids' => 'json',
......@@ -126,7 +124,6 @@ class AdGroup extends Model
static public function getPropertiesWatch()
{
return collect([
'campaign_external_id',
'name',
'region_ids',
'negative_keywords',
......@@ -156,9 +153,13 @@ class AdGroup extends Model
if (GoalAdGroup::getPropertiesCopyWithPivot()->first(function ($property_name) use ($ad_group) {
return $ad_group->{$property_name} !== $ad_group->getOriginal($property_name);
})) {
$ad_group->goalGroups()->update(
GoalAdGroup::copyPropertyFromMain($ad_group)
);
if (!is_null($ad_group->campaign_id) && is_null($ad_group->getOriginal('campaign_id'))) {
$ad_group->campaign->copyGroupInGoalGroup();
} else {
$ad_group->goalGroups()->update(
GoalAdGroup::copyPropertyFromMain($ad_group)
);
}
}
if (self::getPropertiesWatch()->first(function ($property_name) use ($ad_group) {
......
......@@ -111,7 +111,6 @@ class Campaigns extends Model
];
protected $casts = [
'external_id' => 'int',
'time_targeting' => 'array',
'negative_keywords' => 'array',
'blocked_ips' => 'array',
......@@ -232,7 +231,8 @@ class Campaigns extends Model
$campaign->groups()->get()->each(function (AdGroup $adGroup) use ($dictionaryCampaign) {
$dictionaryCampaign->groups()->updateOrCreate([
'campaign_id' => $dictionaryCampaign->external_id,
'campaign_external_id' => $dictionaryCampaign->external_id,
'ad_group_id' => $adGroup->getKey(),
], GoalAdGroup::copyPropertyFromMain($adGroup));
});
......
......@@ -102,7 +102,6 @@ class DictionaryCampaign extends Pivot
];
protected $casts = [
'external_id' => 'int',
'campaign_id' => 'int',
'dictionary_id' => 'int',
'negative_keywords' => 'array',
......
......@@ -71,8 +71,6 @@ class GoalAdGroup extends Pivot
];
protected $casts = [
'external_id' => 'int',
'campaign_external_id' => 'int',
'ad_group_id' => 'int',
'dictionary_campaign_id' => 'int',
'negative_keywords' => 'array',
......
......@@ -27,8 +27,8 @@ use Illuminate\Database\Eloquent\Model;
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Campaigns[] $campaigns
* @property-read int|null $campaigns_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\AdGroup[] $campaignsAdGroups
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\AdGroup[] $campaignsAdGroupsForUpdatedSelf
* @property-read int|null $campaigns_ad_groups_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\AdGroup[] $campaignsForEnabledAdGroupsForUpdatedSelf
* @property-read int|null $campaigns_for_enabled_ad_groups_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Campaigns[] $campaignsNotEnabledNotDisabled
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Campaigns[] $campaignsEnabledDisabled
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Campaigns[] $campaignsEnabledNotDisabledUpdatedChildren
......@@ -145,9 +145,9 @@ class Tokens extends Model
return $this->hasManyThrough(AdGroup::class, Campaigns::class, 'token', 'campaign_id');
}
public function campaignsAdGroupsForUpdatedSelf()
public function campaignsForEnabledAdGroupsForUpdatedSelf()
{
return $this->campaignsAdGroups()->forUpdatedSelf();
return $this->campaignsAdGroups()->forUpdatedSelf()->where(Campaigns::getModel()->getTable() . '.enabled', true);
}
public function campaignsNotEnabledNotDisabled()
......
......@@ -72,6 +72,12 @@ class CheckChanges extends DirectRequest
$adGroup->update([
'updated_self' => Carbon::now(),
]);
} else {
$adGroup = AdGroup::create([
'token_id' => $this->getToken()->getKey(),
'external_id' => $ad_group_id,
'updated_self' => Carbon::now(),
]);
}
} else {
$goalAdGroup = GoalAdGroup::where('external_id', $ad_group_id)->first();
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!