Commit a823570f by Vladislav

#20794 Сделать чтобы в целевых РК оставалась одна карточка

1 parent 1c27787c
...@@ -23,7 +23,7 @@ class VCardsAdd extends Command ...@@ -23,7 +23,7 @@ class VCardsAdd extends Command
* *
* @var string * @var string
*/ */
protected $description = 'Добавление не созданных виртуальные визитки с целевого аккаунта'; protected $description = 'Добавление не созданные виртуальные визитки с целевого аккаунта';
/** /**
* Create a new command instance. * Create a new command instance.
......
<?php
namespace App\Console\Commands;
use App\Models\VCard;
use App\Models\Pivots\GoalVCard;
use App\Models\Tokens;
use App\Service\Requests\Direct\DeleteAds;
use App\Service\Requests\Direct\DeleteVCards;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Relations\HasMany;
class VCardsDelete extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'vcards:delete';
/**
* 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()
{
$tokens = Tokens::has('dictionaryCampaignsEnabledForExternalSynchronized.goalVCardsForNeedDeleteForNotReserveDelete')
->where('type', '!=', Tokens::MAIN)
->get();
foreach ($tokens as $token) {
$this->sendRequest(
$token,
$token->dictionaryCampaignsEnabledForExternalSynchronized->pluck('goalVCardsForNeedDeleteForNotReserveDelete')->collapse()
);
}
return 0;
}
private function sendRequest($token, $ads)
{
/* @var $ads VCard[]|GoalVCard[] */
if (!$ads->count()) {
return;
}
foreach (array_chunk($ads->pluck('external_id')->toArray(), 1000) as $items) {
if ($token->isMain()) {
VCard::whereIn('external_id', $items)
->update([
'reserve_delete_at' => Carbon::now(),
]);
} else {
GoalVCard::whereIn('external_id', $items)
->update([
'reserve_delete_at' => Carbon::now(),
]);
}
}
$request = new DeleteVCards();
$request->setToken($token)
->call([
'ids' => $ads->pluck('external_id')->toArray(),
]);
}
}
...@@ -11,6 +11,7 @@ use App\Console\Commands\AdImagesAdd; ...@@ -11,6 +11,7 @@ use App\Console\Commands\AdImagesAdd;
use App\Console\Commands\AdImagesLoad; use App\Console\Commands\AdImagesLoad;
use App\Console\Commands\AdvertisementsAdd; use App\Console\Commands\AdvertisementsAdd;
use App\Console\Commands\AdvertisementsArchive; use App\Console\Commands\AdvertisementsArchive;
use App\Console\Commands\AdvertisementsDelete;
use App\Console\Commands\AdvertisementsLoadUpdated; use App\Console\Commands\AdvertisementsLoadUpdated;
use App\Console\Commands\AdvertisementsUpdate; use App\Console\Commands\AdvertisementsUpdate;
use App\Console\Commands\AudienceTargetsAdd; use App\Console\Commands\AudienceTargetsAdd;
...@@ -42,6 +43,7 @@ use App\Console\Commands\RetargetinglistsUpdate; ...@@ -42,6 +43,7 @@ use App\Console\Commands\RetargetinglistsUpdate;
use App\Console\Commands\SitelinksAdd; use App\Console\Commands\SitelinksAdd;
use App\Console\Commands\SitelinksLoad; use App\Console\Commands\SitelinksLoad;
use App\Console\Commands\VCardsAdd; use App\Console\Commands\VCardsAdd;
use App\Console\Commands\VCardsDelete;
use App\Console\Commands\VCardsLoad; use App\Console\Commands\VCardsLoad;
use Illuminate\Console\Scheduling\Schedule; use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel; use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
...@@ -94,9 +96,11 @@ class Kernel extends ConsoleKernel ...@@ -94,9 +96,11 @@ class Kernel extends ConsoleKernel
$schedule->command(AdGroupsLoadKeywords::class)->hourlyAt(20); $schedule->command(AdGroupsLoadKeywords::class)->hourlyAt(20);
$schedule->command(AdExtensionsLoad::class)->hourlyAt(25); $schedule->command(AdExtensionsLoad::class)->hourlyAt(25);
$schedule->command(SitelinksLoad::class)->hourlyAt(25); $schedule->command(SitelinksLoad::class)->hourlyAt(25);
$schedule->command(VCardsLoad::class)->hourlyAt(25);
$schedule->command(VCardsDelete::class)->hourlyAt(20);
$schedule->command(VCardsLoad::class)->hourlyAt(25);
$schedule->command(VCardsAdd::class)->hourlyAt(35); $schedule->command(VCardsAdd::class)->hourlyAt(35);
$schedule->command(AdImagesAdd::class)->hourlyAt(35); $schedule->command(AdImagesAdd::class)->hourlyAt(35);
$schedule->command(AdExtensionsAdd::class)->hourlyAt(35); $schedule->command(AdExtensionsAdd::class)->hourlyAt(35);
$schedule->command(SitelinksAdd::class)->hourlyAt(35); $schedule->command(SitelinksAdd::class)->hourlyAt(35);
...@@ -116,6 +120,7 @@ class Kernel extends ConsoleKernel ...@@ -116,6 +120,7 @@ class Kernel extends ConsoleKernel
$schedule->command(KeywordsUpdate::class)->hourlyAt(40); $schedule->command(KeywordsUpdate::class)->hourlyAt(40);
$schedule->command(KeywordsDelete::class)->hourlyAt(40); $schedule->command(KeywordsDelete::class)->hourlyAt(40);
$schedule->command(AdvertisementsDelete::class)->hourlyAt(45);
$schedule->command(AdvertisementsAdd::class)->hourlyAt(50); $schedule->command(AdvertisementsAdd::class)->hourlyAt(50);
$schedule->command(AdvertisementsUpdate::class)->hourlyAt(50); $schedule->command(AdvertisementsUpdate::class)->hourlyAt(50);
$schedule->command(AdvertisementsArchive::class)->hourlyAt(50); $schedule->command(AdvertisementsArchive::class)->hourlyAt(50);
......
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
namespace App\Models\Pivots; namespace App\Models\Pivots;
use App\Models\BidModifier;
use App\Models\Campaigns; use App\Models\Campaigns;
use App\Models\Dictionary; use App\Models\Dictionary;
use App\Models\Variable; use App\Models\Variable;
...@@ -11,6 +10,7 @@ use Illuminate\Database\Eloquent\Builder; ...@@ -11,6 +10,7 @@ use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Pivot; use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
/** /**
* App\Models\Pivots\DictionaryCampaign * App\Models\Pivots\DictionaryCampaign
...@@ -531,6 +531,23 @@ class DictionaryCampaign extends Pivot ...@@ -531,6 +531,23 @@ class DictionaryCampaign extends Pivot
return $this->goalVCards()->forNotExternal()->forNotReserveCreate(); return $this->goalVCards()->forNotExternal()->forNotReserveCreate();
} }
public function goalVCardsForNeedDeleteForNotReserveDelete()
{
return $this->goalVCards()->whereNotExists(function (\Illuminate\Database\Query\Builder $query) {
$query->select(DB::raw(1))
->from('goal_advertisements')
->whereColumn('goal_v_cards.id', 'goal_advertisements.goal_v_card_id')
->where(function (\Illuminate\Database\Query\Builder $query) {
$query->where(function (\Illuminate\Database\Query\Builder $query) {
$query->whereNotNull('goal_advertisements.archive_at');
})->orWhere(function (\Illuminate\Database\Query\Builder $query) {
$query->whereNotNull('goal_advertisements.deleted_at');
});
});
return $query;
})->forExternal()->forNotReserveDelete();
}
public function goalAudienceTargets() public function goalAudienceTargets()
{ {
return $this->hasMany(GoalAudienceTarget::class, 'dictionary_campaign_id'); return $this->hasMany(GoalAudienceTarget::class, 'dictionary_campaign_id');
......
...@@ -25,6 +25,7 @@ class GoalVCard extends Pivot ...@@ -25,6 +25,7 @@ class GoalVCard 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 = [
...@@ -33,6 +34,7 @@ class GoalVCard extends Pivot ...@@ -33,6 +34,7 @@ class GoalVCard 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;
...@@ -50,6 +52,7 @@ class GoalVCard extends Pivot ...@@ -50,6 +52,7 @@ class GoalVCard extends Pivot
'updated_need', 'updated_need',
'reserve_create_at', 'reserve_create_at',
'reserve_update_at', 'reserve_update_at',
'reserve_delete_at',
]; ];
} }
...@@ -98,6 +101,15 @@ class GoalVCard extends Pivot ...@@ -98,6 +101,15 @@ class GoalVCard extends Pivot
return $query->whereNotNull('updated_need'); return $query->whereNotNull('updated_need');
} }
/**
* @param Builder $query
* @return Builder
*/
public function scopeForNotReserveDelete($query)
{
return $query->whereNull('reserve_delete_at');
}
public function vCard() public function vCard()
{ {
return $this->belongsTo(VCard::class, 'v_card_id'); return $this->belongsTo(VCard::class, 'v_card_id');
......
...@@ -4,6 +4,7 @@ namespace App\Models; ...@@ -4,6 +4,7 @@ namespace App\Models;
use App\Models\Pivots\GoalAdvertisement; use App\Models\Pivots\GoalAdvertisement;
use App\Models\Pivots\GoalVCard; use App\Models\Pivots\GoalVCard;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
...@@ -34,12 +35,14 @@ class VCard extends Model ...@@ -34,12 +35,14 @@ class VCard extends Model
'metro_station_id', 'metro_station_id',
'point_on_map', 'point_on_map',
'contact_person', 'contact_person',
'reserve_delete_at',
]; ];
protected $casts = [ protected $casts = [
'phone' => 'json', 'phone' => 'json',
'instant_messenger' => 'json', 'instant_messenger' => 'json',
'point_on_map' => 'json', 'point_on_map' => 'json',
'reserve_delete_at' => 'datetime',
]; ];
/** /**
...@@ -67,12 +70,21 @@ class VCard extends Model ...@@ -67,12 +70,21 @@ class VCard extends Model
]); ]);
} }
/**
* @param Builder $query
* @return Builder
*/
public function scopeForNotReserveDelete($query)
{
return $query->whereNull('reserve_delete_at');
}
public function goalVCards() public function goalVCards()
{ {
return $this->hasMany(GoalVCard::class, 'v_card_id'); return $this->hasMany(GoalVCard::class, 'v_card_id');
} }
public function goalAdvertisement() public function goalAdvertisements()
{ {
return $this->hasMany(GoalAdvertisement::class, 'v_card_id'); return $this->hasMany(GoalAdvertisement::class, 'v_card_id');
} }
......
<?php
namespace App\Service\Requests\Direct;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Pivots\GoalVCard;
use App\Models\VCard;
use App\Service\Contract\APIRequest;
use App\Service\Requests\DirectRequest;
use Illuminate\Support\Facades\Log;
class DeleteVCards extends DirectRequest
{
protected $max_count = 10000;
protected $timestamp;
public function call($params = null)
{
$this->requestPrepare($params);
$process = new ProcessCallLimitedAPI($this);
dispatch($process)->onQueue('limits');
}
public function getObjectsCount()
{
return count($this->getParams()['SelectionCriteria']['Ids']);
}
public function slice($maxObjects): ?APIRequest
{
return $this->sliceByKey($maxObjects, ['SelectionCriteria', 'Ids']);;
}
public function handle($response)
{
try {
if (!isset($response['result']['DeleteResults'])) {
return;
}
foreach ($response['result']['DeleteResults'] as $key => $delete_result) {
if (!isset($delete_result['Id'])) {
if (
isset($delete_result['Errors'][0]['Code'])
&&
$delete_result['Errors'][0]['Code'] == 8800
) {
if ($external_id = $this->getParams()['SelectionCriteria']['Ids'][$key]){
if ($this->getToken()->isMain()) {
VCard::whereExternalId($external_id)
->delete();
} else {
GoalVCard::whereExternalId($external_id)
->delete();
}
continue;
}
}
Log::debug("DeleteVCards, empty Id");
Log::debug($delete_result);
$external_id = $this->getParams()['SelectionCriteria']['Ids'][$key];
Log::debug($external_id);
if ($this->getToken()->isMain()) {
VCard::whereExternalId($external_id)
->update([
'reserve_delete_at' => null,
]);
} else {
GoalVCard::whereExternalId($external_id)
->update([
'reserve_delete_at' => null,
]);
}
continue;
}
$external_id = (string)$delete_result['Id'];
if ($this->getToken()->isMain()) {
VCard::whereExternalId($external_id)
->delete();
} else {
GoalVCard::whereExternalId($external_id)
->delete();
}
}
} catch (\Exception $e) {
Log::debug($e);
throw $e;
}
}
public function failed()
{
if ($this->getToken()->isMain()) {
VCard::whereIn('external_id', $this->getParams()['SelectionCriteria']['Ids'])
->update([
'reserve_delete_at' => null,
]);
} else {
VCard::whereIn('external_id', $this->getParams()['SelectionCriteria']['Ids'])
->update([
'reserve_delete_at' => null,
]);
}
}
private function requestPrepare($params)
{
$this->setService('VCards');
$this->setMethod('delete');
$this->setParams([
'SelectionCriteria' => [
'Ids' => $params['ids'],
],
]);
}
}
...@@ -49,6 +49,7 @@ class GetVCards extends DirectRequest ...@@ -49,6 +49,7 @@ class GetVCards extends DirectRequest
function handle($response) function handle($response)
{ {
try { try {
\Log::debug($response);
if (isset($response['result']['VCards'])) { if (isset($response['result']['VCards'])) {
foreach ($response['result']['VCards'] as $v_card) { foreach ($response['result']['VCards'] as $v_card) {
......
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddVCardsDeleteColumn extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('v_cards', function (Blueprint $table) {
$table->timestamp('reserve_delete_at')->nullable();
});
Schema::table('goal_v_cards', 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!