Commit 6a102696 by Евгений

Улучшение #19474

Синхронизация фраз и групп
1 parent d3ae55a0
...@@ -79,10 +79,13 @@ class KeywordsAdd extends Command ...@@ -79,10 +79,13 @@ class KeywordsAdd extends Command
]) ])
->get(); ->get();
GoalKeyword::whereIn('id', $goalKeywords->pluck('id')->toArray()) foreach (array_chunk($goalKeywords->pluck('id')->toArray(), 1000) as $items){
GoalKeyword::whereIn('id', $items)
->update([ ->update([
'reserve_create_at' => Carbon::now(), 'reserve_create_at' => Carbon::now(),
]); ]);
}
$factory->getRequest('Keywords', 'add') $factory->getRequest('Keywords', 'add')
->call([ ->call([
......
...@@ -15,7 +15,7 @@ use Illuminate\Support\Facades\Log; ...@@ -15,7 +15,7 @@ use Illuminate\Support\Facades\Log;
class GetAdGroups extends DirectRequest class GetAdGroups extends DirectRequest
{ {
protected $max_count = -1; protected $max_count = 10000;
protected $max_count_CampaignIds = 10; protected $max_count_CampaignIds = 10;
protected $max_count_Ids = 10000; protected $max_count_Ids = 10000;
...@@ -29,13 +29,10 @@ class GetAdGroups extends DirectRequest ...@@ -29,13 +29,10 @@ class GetAdGroups extends DirectRequest
public function getObjectsCount() public function getObjectsCount()
{ {
$params = $this->getParams(); $params = $this->getParams();
if (isset($params['SelectionCriteria']['CampaignIds'])) {
return -1;
}
if (isset($params['SelectionCriteria']['Ids'])) { if (isset($params['SelectionCriteria']['Ids'])) {
return count($params['SelectionCriteria']['Ids']); return count($params['SelectionCriteria']['Ids']);
} }
return -1; return $this->getMaxCount();
} }
public function slice($maxObjects): ?APIRequest public function slice($maxObjects): ?APIRequest
...@@ -87,9 +84,9 @@ class GetAdGroups extends DirectRequest ...@@ -87,9 +84,9 @@ class GetAdGroups extends DirectRequest
$external_id = (string)$ad_group['CampaignId']; $external_id = (string)$ad_group['CampaignId'];
$campaign = $campaigns->firstWhere('external_id', $external_id); //$campaigns->firstWhere('external_id', $external_id);
if (!$campaign) { if (!$campaign = $campaigns[$external_id] ?? false) {
continue; continue;
} }
...@@ -136,7 +133,7 @@ class GetAdGroups extends DirectRequest ...@@ -136,7 +133,7 @@ class GetAdGroups extends DirectRequest
]); ]);
} }
} else { } /*else {
$goalAdGroup = GoalAdGroup::firstWhere('external_id', $external_id); $goalAdGroup = GoalAdGroup::firstWhere('external_id', $external_id);
if (!$goalAdGroup) if (!$goalAdGroup)
...@@ -151,7 +148,7 @@ class GetAdGroups extends DirectRequest ...@@ -151,7 +148,7 @@ class GetAdGroups extends DirectRequest
} }
$goalAdGroup->update($data); $goalAdGroup->update($data);
} }*/
} }
......
...@@ -16,7 +16,7 @@ use Illuminate\Support\Facades\Log; ...@@ -16,7 +16,7 @@ use Illuminate\Support\Facades\Log;
class GetKeywords extends DirectRequest class GetKeywords extends DirectRequest
{ {
protected $max_count = -1; protected $max_count = 10000;
protected $max_count_CampaignIds = 10; protected $max_count_CampaignIds = 10;
protected $max_count_AdGroupIds = 1000; protected $max_count_AdGroupIds = 1000;
protected $max_count_Ids = 10000; protected $max_count_Ids = 10000;
...@@ -59,6 +59,10 @@ class GetKeywords extends DirectRequest ...@@ -59,6 +59,10 @@ class GetKeywords extends DirectRequest
try { try {
$external_ids = []; $external_ids = [];
if (!$this->getToken()->isMain()) {
return;//добавлять фразы будем только по основному. По цеелвым будем наоборот выгружать.
}
if (!isset($response['result']['Keywords'])) { if (!isset($response['result']['Keywords'])) {
return; return;
} }
...@@ -80,14 +84,11 @@ class GetKeywords extends DirectRequest ...@@ -80,14 +84,11 @@ class GetKeywords extends DirectRequest
$ad_groups = AdGroup::whereIn('external_id', array_keys($external_ids)) $ad_groups = AdGroup::whereIn('external_id', array_keys($external_ids))
->get() ->get()
->keyBy('external_id'); ->keyBy('external_id');
} else {
$ad_groups = GoalAdGroup::whereIn('external_id', array_keys($external_ids))
->get()
->keyBy('external_id');
} }
$ids = []; // $ids = [];
$campaign_ids_synced_need = []; // $campaign_ids_synced_need = [];
$insertData = [];
foreach ($response['result']['Keywords'] as $keyword) { foreach ($response['result']['Keywords'] as $keyword) {
...@@ -97,17 +98,19 @@ class GetKeywords extends DirectRequest ...@@ -97,17 +98,19 @@ class GetKeywords extends DirectRequest
continue; continue;
} }
$external_id = (string)$keyword['Id']; $external_id = (string)$keyword['Id'];
if ($this->getToken()->isMain()) {
//идентификатор фразы у нас уникален. Если такая фраза уже есть, нет смысла ее менять, т.к. данные в общем то неизменны.
//если будет изменена фраза, то на самом деле будет фраза с новым идентификатором
//поэтому будем всегда новые добавлять и игонрить те что уже есть
$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,
'ad_group_external_id' => $ad_group->external_id, 'ad_group_external_id' => $ad_group->external_id,
'ad_group_id' => $ad_group->getKey(), 'ad_group_id' => $ad_group->getKey(),
'campaign_id' => $ad_group->campaign->getKey(), 'campaign_id' => $ad_group->campaign_id,
'keyword' => $keyword['Keyword'], 'keyword' => $keyword['Keyword'],
'user_param_1' => $keyword['UserParam1'] ?? null, 'user_param_1' => $keyword['UserParam1'] ?? null,
'user_param_2' => $keyword['UserParam2'] ?? null, 'user_param_2' => $keyword['UserParam2'] ?? null,
...@@ -120,43 +123,27 @@ class GetKeywords extends DirectRequest ...@@ -120,43 +123,27 @@ class GetKeywords extends DirectRequest
'deleted_at' => null//не забыть убрать признак удаления, если вдруг опять пришла удаленная ранее фраза 'deleted_at' => null//не забыть убрать признак удаления, если вдруг опять пришла удаленная ранее фраза
]; ];
$keyword = Keyword::updateOrCreate([ $insertData[] = $data;
'external_id' => $external_id // $keyword = Keyword::updateOrCreate([
], $data); // 'external_id' => $external_id
// ], $data);
if ($keyword->wasRecentlyCreated) {
$campaign_ids_synced_need[$keyword->campaign_id] = true; // if ($keyword->wasRecentlyCreated) {
} elseif ($keyword->wasChanged(['campaign_id'])) { // $campaign_ids_synced_need[$keyword->campaign_id] = true;
$campaign_ids_synced_need[$keyword->campaign_id] = true; // } elseif ($keyword->wasChanged(['campaign_id'])) {
} elseif ($keyword->wasChanged($keyword::getPropertiesWatch()->toArray())) { // $campaign_ids_synced_need[$keyword->campaign_id] = true;
$keyword->goalKeywords()->has('dictionaryCampaign')->forExternal()->update([ // } elseif ($keyword->wasChanged($keyword::getPropertiesWatch()->toArray())) {
'updated_need' => Carbon::now(), // $keyword->goalKeywords()->has('dictionaryCampaign')->forExternal()->update([
]); // 'updated_need' => Carbon::now(),
} // ]);
// }
$ids[] = $keyword->getKey();
} else {
$goalKeyword = GoalKeyword::firstWhere('external_id', $external_id);
if (!$goalKeyword)
continue;
$ids[] = $goalKeyword->getKey();
$data = [
'external_updated_at' => Carbon::now(),
];
if ($goalKeyword->dictionaryCampaign->updated) {
$data['updated_need'] = Carbon::now();
} }
$goalKeyword->update($data); foreach (array_chunk($insertData, 1000) as $data) {
Keyword::insertOrIgnore($data);
} }
}
// if ($this->getToken()->isMain()) { // if ($this->getToken()->isMain()) {
// //
......
...@@ -32,7 +32,7 @@ class CreateKeywordsTable extends Migration ...@@ -32,7 +32,7 @@ class CreateKeywordsTable extends Migration
Schema::create('keywords', function (Blueprint $table) { Schema::create('keywords', function (Blueprint $table) {
$table->id(); $table->id();
$table->bigInteger('external_id'); $table->bigInteger('external_id')->unique();
$table->bigInteger('ad_group_external_id'); $table->bigInteger('ad_group_external_id');
$table->bigInteger('campaign_external_id'); $table->bigInteger('campaign_external_id');
$table->bigInteger('ad_group_id')->unsigned(); $table->bigInteger('ad_group_id')->unsigned();
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!