Commit 1acc74a1 by Vladislav

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

1 parent bd616e9b
......@@ -2,8 +2,10 @@
namespace App\Console\Commands;
use App\Models\Pivots\GoalKeyword;
use App\Models\Tokens;
use App\Service\Requests\Direct\DeleteKeywords;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
......@@ -47,7 +49,8 @@ class KeywordsDelete extends Command
INNER JOIN dictionary_campaigns c ON c.id=gk.dictionary_campaign_id
INNER JOIN dictionaries d ON d.id=c.dictionary_id
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 (
(k.deleted_at is not null)
OR (
......@@ -74,6 +77,13 @@ class KeywordsDelete extends Command
continue;
}
foreach (array_chunk($ids, 1000) as $items) {
GoalKeyword::whereIn('external_id', $items)
->update([
'reserve_delete_at' => Carbon::now(),
]);
}
$request = new DeleteKeywords();
$request->setToken($token)
->call([
......
......@@ -69,6 +69,8 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
return;
$schedule->command(DictionaryCampaignsSyncByCampaign::class)->everyThirtyMinutes();
$schedule->command(RefreshLimits::class)->hourly();
......
......@@ -63,6 +63,7 @@ class GoalKeyword extends Pivot
'updated_need',
'reserve_create_at',
'reserve_update_at',
'reserve_delete_at',
];
protected $casts = [
......@@ -71,6 +72,7 @@ class GoalKeyword extends Pivot
'updated_need' => 'datetime',
'reserve_create_at' => 'datetime',
'reserve_update_at' => 'datetime',
'reserve_delete_at' => 'datetime',
];
public $incrementing = true;
......@@ -90,52 +92,7 @@ class GoalKeyword extends Pivot
'updated_need',
'reserve_create_at',
'reserve_update_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(),
'reserve_delete_at',
];
}
......
......@@ -51,12 +51,10 @@ class DeleteAds extends DirectRequest
) {
if ($this->getToken()->isMain()) {
Advertisement::needDeleted()
->where('external_id', $external_id)
Advertisement::where('external_id', $external_id)
->delete();
} else {
GoalAdvertisement::forExternal()->needDeleted()
->where('external_id', $external_id)
GoalAdvertisement::where('external_id', $external_id)
->delete();
}
......@@ -104,12 +102,10 @@ class DeleteAds extends DirectRequest
$external_id = (string)$delete_result['Id'];
if ($this->getToken()->isMain()) {
Advertisement::needDeleted()
->where('external_id', $external_id)
Advertisement::where('external_id', $external_id)
->delete();
} else {
GoalAdvertisement::forExternal()->needDeleted()
->where('external_id', $external_id)
GoalAdvertisement::where('external_id', $external_id)
->delete();
}
......
......@@ -6,6 +6,7 @@ use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Pivots\GoalKeyword;
use App\Service\Contract\APIRequest;
use App\Service\Requests\DirectRequest;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
class DeleteKeywords extends DirectRequest
......@@ -37,16 +38,23 @@ class DeleteKeywords extends DirectRequest
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') {
//объекта по какой то причине нет
GoalKeyword::where('external_id', $external_id)->delete();
} elseif (isset($delete_result['Errors']) && count($delete_result['Errors'])) {
if (isset($delete_result['Errors']) && count($delete_result['Errors'])) {
$goalKeyword = GoalKeyword::whereExternalId($external_id)->first();
if ($goalKeyword) {
......@@ -64,16 +72,22 @@ class DeleteKeywords extends DirectRequest
Log::debug($external_id);
}
continue;
}
$external_id = (string)$delete_result['Id'];
GoalKeyword::where('external_id', $external_id)->delete();
GoalKeyword::whereExternalId($external_id)
->update([
'reserve_delete_at' => null,
]);
}
}
public function failed()
{
GoalKeyword::whereIn('external_id', $this->getParams()['SelectionCriteria']['Ids'])
->update([
'reserve_delete_at' => null,
]);
}
private function requestPrepare($params)
{
$this->setService('Keywords');
......
......@@ -75,8 +75,7 @@ class SetBidModifiers extends DirectRequest
$external_id = (string)$set_result['Id'];
GoalBidModifier::forExternal()->needUpdated()
->where('external_id', $external_id)
GoalBidModifier::where('external_id', $external_id)
->update([
'updated_need' => null,
'reserve_update_at' => null,
......
......@@ -75,8 +75,7 @@ class UpdateAdGroups extends DirectRequest
$external_id = (string)$update_result['Id'];
GoalAdGroup::forExternal()->needUpdated()
->where('external_id', $external_id)
GoalAdGroup::where('external_id', $external_id)
->update([
'updated_need' => null,
'reserve_update_at' => null,
......
......@@ -89,8 +89,7 @@ class UpdateAds extends DirectRequest
$external_id = (string)$update_result['Id'];
GoalAdvertisement::forExternal()->needUpdated()
->where('external_id', $external_id)
GoalAdvertisement::where('external_id', $external_id)
->update([
'updated_need' => null,
'reserve_update_at' => null,
......
......@@ -79,8 +79,7 @@ class UpdateCampaigns extends DirectRequest
$external_id = (string)$update_result['Id'];
DictionaryCampaign::forExternal()->needUpdated()
->where('external_id', $external_id)
DictionaryCampaign::where('external_id', $external_id)
->update([
'updated_need' => null,
'reserve_update_at' => null,
......
......@@ -80,8 +80,7 @@ class UpdateKeywords extends DirectRequest
$external_id =(string) $update_result['Id'];
GoalKeyword::forExternal()->needUpdated()
->where('external_id', $external_id)
GoalKeyword::where('external_id', $external_id)
->update([
'updated_need' => 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!