Commit 1acc74a1 by Vladislav

#20794 Сделать чтобы в целевых РК оставалась одна карточка (резервирование удаление фраз)

1 parent bd616e9b
...@@ -2,8 +2,10 @@ ...@@ -2,8 +2,10 @@
namespace App\Console\Commands; namespace App\Console\Commands;
use App\Models\Pivots\GoalKeyword;
use App\Models\Tokens; use App\Models\Tokens;
use App\Service\Requests\Direct\DeleteKeywords; use App\Service\Requests\Direct\DeleteKeywords;
use Carbon\Carbon;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
...@@ -47,7 +49,8 @@ class KeywordsDelete extends Command ...@@ -47,7 +49,8 @@ class KeywordsDelete extends Command
INNER JOIN dictionary_campaigns c ON c.id=gk.dictionary_campaign_id INNER JOIN dictionary_campaigns c ON c.id=gk.dictionary_campaign_id
INNER JOIN dictionaries d ON d.id=c.dictionary_id INNER JOIN dictionaries d ON d.id=c.dictionary_id
WHERE gk.external_id is not null WHERE gk.external_id is not null
AND d.token_id=" . $token->id ." AND gk.reserve_delete_at is null
AND d.token_id=" . $token->id . "
AND ( AND (
(k.deleted_at is not null) (k.deleted_at is not null)
OR ( OR (
...@@ -74,6 +77,13 @@ class KeywordsDelete extends Command ...@@ -74,6 +77,13 @@ class KeywordsDelete extends Command
continue; continue;
} }
foreach (array_chunk($ids, 1000) as $items) {
GoalKeyword::whereIn('external_id', $items)
->update([
'reserve_delete_at' => Carbon::now(),
]);
}
$request = new DeleteKeywords(); $request = new DeleteKeywords();
$request->setToken($token) $request->setToken($token)
->call([ ->call([
......
...@@ -69,6 +69,8 @@ class Kernel extends ConsoleKernel ...@@ -69,6 +69,8 @@ class Kernel extends ConsoleKernel
*/ */
protected function schedule(Schedule $schedule) protected function schedule(Schedule $schedule)
{ {
return;
$schedule->command(DictionaryCampaignsSyncByCampaign::class)->everyThirtyMinutes(); $schedule->command(DictionaryCampaignsSyncByCampaign::class)->everyThirtyMinutes();
$schedule->command(RefreshLimits::class)->hourly(); $schedule->command(RefreshLimits::class)->hourly();
......
...@@ -63,6 +63,7 @@ class GoalKeyword extends Pivot ...@@ -63,6 +63,7 @@ class GoalKeyword extends Pivot
'updated_need', 'updated_need',
'reserve_create_at', 'reserve_create_at',
'reserve_update_at', 'reserve_update_at',
'reserve_delete_at',
]; ];
protected $casts = [ protected $casts = [
...@@ -71,6 +72,7 @@ class GoalKeyword extends Pivot ...@@ -71,6 +72,7 @@ class GoalKeyword extends Pivot
'updated_need' => 'datetime', 'updated_need' => 'datetime',
'reserve_create_at' => 'datetime', 'reserve_create_at' => 'datetime',
'reserve_update_at' => 'datetime', 'reserve_update_at' => 'datetime',
'reserve_delete_at' => 'datetime',
]; ];
public $incrementing = true; public $incrementing = true;
...@@ -90,52 +92,7 @@ class GoalKeyword extends Pivot ...@@ -90,52 +92,7 @@ class GoalKeyword extends Pivot
'updated_need', 'updated_need',
'reserve_create_at', 'reserve_create_at',
'reserve_update_at', 'reserve_update_at',
]; 'reserve_delete_at',
}
/**
* @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();
}
static public function getDataByMain(Keyword $keyword, GoalAdGroup $goalAdGroup, DictionaryCampaign $dictionaryCampaign)
{
return [
'dictionary_campaign_id' => $dictionaryCampaign->getKey(),
'dictionary_campaign_external_id' => $dictionaryCampaign->external_id,
'goal_ad_group_id' => $goalAdGroup->getKey(),
'goal_ad_group_external_id' => $goalAdGroup->external_id,
'keyword_id' => $keyword->getKey(),
]; ];
} }
......
...@@ -51,12 +51,10 @@ class DeleteAds extends DirectRequest ...@@ -51,12 +51,10 @@ class DeleteAds extends DirectRequest
) { ) {
if ($this->getToken()->isMain()) { if ($this->getToken()->isMain()) {
Advertisement::needDeleted() Advertisement::where('external_id', $external_id)
->where('external_id', $external_id)
->delete(); ->delete();
} else { } else {
GoalAdvertisement::forExternal()->needDeleted() GoalAdvertisement::where('external_id', $external_id)
->where('external_id', $external_id)
->delete(); ->delete();
} }
...@@ -104,12 +102,10 @@ class DeleteAds extends DirectRequest ...@@ -104,12 +102,10 @@ class DeleteAds extends DirectRequest
$external_id = (string)$delete_result['Id']; $external_id = (string)$delete_result['Id'];
if ($this->getToken()->isMain()) { if ($this->getToken()->isMain()) {
Advertisement::needDeleted() Advertisement::where('external_id', $external_id)
->where('external_id', $external_id)
->delete(); ->delete();
} else { } else {
GoalAdvertisement::forExternal()->needDeleted() GoalAdvertisement::where('external_id', $external_id)
->where('external_id', $external_id)
->delete(); ->delete();
} }
......
...@@ -6,6 +6,7 @@ use App\Jobs\ProcessCallLimitedAPI; ...@@ -6,6 +6,7 @@ use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Pivots\GoalKeyword; use App\Models\Pivots\GoalKeyword;
use App\Service\Contract\APIRequest; use App\Service\Contract\APIRequest;
use App\Service\Requests\DirectRequest; use App\Service\Requests\DirectRequest;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
class DeleteKeywords extends DirectRequest class DeleteKeywords extends DirectRequest
...@@ -37,16 +38,23 @@ class DeleteKeywords extends DirectRequest ...@@ -37,16 +38,23 @@ class DeleteKeywords extends DirectRequest
return; return;
} }
foreach ($response['result']['DeleteResults'] as $key => $delete_result) { $delete_results = $response['result']['DeleteResults'];
$external_id = $this->getParams()['SelectionCriteria']['Ids'][$key]; foreach (array_chunk(array_column(array_filter($delete_results, function ($delete_result) {
return isset($delete_result['Id']) || (isset($delete_result['Errors'][0]['Details']) && $delete_result['Errors'][0]['Details'] === 'Keyword not found');
}), 'Id'), 1000) as $external_ids) {
GoalKeyword::whereExternalId($external_ids)->delete();
}
foreach ($delete_results as $key => $delete_result) {
if (isset($delete_result['Id'])) {
continue;
}
if (!isset($delete_result['Id'])) { $external_id = $this->getParams()['SelectionCriteria']['Ids'][$key];
if (isset($delete_result['Errors'][0]['Details']) && $delete_result['Errors'][0]['Details'] === 'Keyword not found') { if (isset($delete_result['Errors']) && count($delete_result['Errors'])) {
//объекта по какой то причине нет
GoalKeyword::where('external_id', $external_id)->delete();
} elseif (isset($delete_result['Errors']) && count($delete_result['Errors'])) {
$goalKeyword = GoalKeyword::whereExternalId($external_id)->first(); $goalKeyword = GoalKeyword::whereExternalId($external_id)->first();
if ($goalKeyword) { if ($goalKeyword) {
...@@ -64,16 +72,22 @@ class DeleteKeywords extends DirectRequest ...@@ -64,16 +72,22 @@ class DeleteKeywords extends DirectRequest
Log::debug($external_id); Log::debug($external_id);
} }
continue; GoalKeyword::whereExternalId($external_id)
} ->update([
'reserve_delete_at' => null,
$external_id = (string)$delete_result['Id']; ]);
GoalKeyword::where('external_id', $external_id)->delete();
} }
} }
public function failed()
{
GoalKeyword::whereIn('external_id', $this->getParams()['SelectionCriteria']['Ids'])
->update([
'reserve_delete_at' => null,
]);
}
private function requestPrepare($params) private function requestPrepare($params)
{ {
$this->setService('Keywords'); $this->setService('Keywords');
......
...@@ -75,8 +75,7 @@ class SetBidModifiers extends DirectRequest ...@@ -75,8 +75,7 @@ class SetBidModifiers extends DirectRequest
$external_id = (string)$set_result['Id']; $external_id = (string)$set_result['Id'];
GoalBidModifier::forExternal()->needUpdated() GoalBidModifier::where('external_id', $external_id)
->where('external_id', $external_id)
->update([ ->update([
'updated_need' => null, 'updated_need' => null,
'reserve_update_at' => null, 'reserve_update_at' => null,
......
...@@ -75,8 +75,7 @@ class UpdateAdGroups extends DirectRequest ...@@ -75,8 +75,7 @@ class UpdateAdGroups extends DirectRequest
$external_id = (string)$update_result['Id']; $external_id = (string)$update_result['Id'];
GoalAdGroup::forExternal()->needUpdated() GoalAdGroup::where('external_id', $external_id)
->where('external_id', $external_id)
->update([ ->update([
'updated_need' => null, 'updated_need' => null,
'reserve_update_at' => null, 'reserve_update_at' => null,
......
...@@ -89,8 +89,7 @@ class UpdateAds extends DirectRequest ...@@ -89,8 +89,7 @@ class UpdateAds extends DirectRequest
$external_id = (string)$update_result['Id']; $external_id = (string)$update_result['Id'];
GoalAdvertisement::forExternal()->needUpdated() GoalAdvertisement::where('external_id', $external_id)
->where('external_id', $external_id)
->update([ ->update([
'updated_need' => null, 'updated_need' => null,
'reserve_update_at' => null, 'reserve_update_at' => null,
......
...@@ -79,8 +79,7 @@ class UpdateCampaigns extends DirectRequest ...@@ -79,8 +79,7 @@ class UpdateCampaigns extends DirectRequest
$external_id = (string)$update_result['Id']; $external_id = (string)$update_result['Id'];
DictionaryCampaign::forExternal()->needUpdated() DictionaryCampaign::where('external_id', $external_id)
->where('external_id', $external_id)
->update([ ->update([
'updated_need' => null, 'updated_need' => null,
'reserve_update_at' => null, 'reserve_update_at' => null,
......
...@@ -80,8 +80,7 @@ class UpdateKeywords extends DirectRequest ...@@ -80,8 +80,7 @@ class UpdateKeywords extends DirectRequest
$external_id =(string) $update_result['Id']; $external_id =(string) $update_result['Id'];
GoalKeyword::forExternal()->needUpdated() GoalKeyword::where('external_id', $external_id)
->where('external_id', $external_id)
->update([ ->update([
'updated_need' => null, 'updated_need' => null,
'reserve_update_at' => null, 'reserve_update_at' => null,
......
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateKeywordsReserveTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('goal_keywords', function (Blueprint $table) {
$table->timestamp('reserve_delete_at')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!