Commit e83798dc by Vladislav

#21624 Наборы ссылок, связать с рк, обновление

1 parent c50e7b8f
......@@ -43,9 +43,7 @@ class AdvertisementsAdd extends Command
*/
public function handle()
{
$tokens = Tokens::has('dictionaryCampaignsEnabledForExternalSynchronized.goalAdvertisementsForNotExternalForNotReserveCreate.advertisement')
->whereDoesntHave('dictionaryCampaignsEnabledForExternalSynchronized.goalAdvertisementsForNotExternalForNotReserveCreate.goalAdExtensionsForNotExternal')
->where('type', '!=', Tokens::MAIN)
$tokens = Tokens::where('type', '!=', Tokens::MAIN)
->get();
foreach ($tokens as $token) {
......
......@@ -142,7 +142,7 @@ class DictionaryCampaignsSyncByCampaign extends Command
INNER JOIN tokens t on t.type != '" . Tokens::MAIN . "'
INNER JOIN dictionaries d on t.id = d.token_id
INNER JOIN dictionary_campaigns dc on dc.dictionary_id = d.id AND dc.deleted_at is null
LEFT JOIN goal_sitelinks gs on s.id = gs.sitelink_id and t.id = gs.token_id AND gs.dictionary_campaign_id = dc.id AND gs.deleted_at is null
LEFT JOIN goal_sitelinks gs on s.id = gs.sitelink_id and t.id = gs.token_id AND gs.dictionary_campaign_id = dc.id AND gs.updated_need is null AND gs.deleted_at is null
WHERE gs.sitelink_id is null AND s.deleted_at is null
");
$this->info('goal_sitelinks successful!');
......@@ -171,7 +171,7 @@ class DictionaryCampaignsSyncByCampaign extends Command
LEFT JOIN v_cards vc on ad.v_card_external_id = vc.external_id AND vc.deleted_at is null
LEFT JOIN goal_v_cards gvc on vc.id = gvc.v_card_id and gag.dictionary_campaign_id = gvc.dictionary_campaign_id and gvc.deleted_at is null
LEFT JOIN sitelinks s on ad.sitelink_external_id = s.external_id AND s.deleted_at is null
LEFT JOIN goal_sitelinks gs on s.id = gs.sitelink_id and gs.token_id = d.token_id AND gs.dictionary_campaign_id = gag.dictionary_campaign_id AND gs.deleted_at is null
LEFT JOIN goal_sitelinks gs on s.id = gs.sitelink_id and gs.token_id = d.token_id AND gs.dictionary_campaign_id = gag.dictionary_campaign_id AND gs.updated_need is null AND gs.deleted_at is null
WHERE ad.deleted_at is null AND gad.advertisement_id is null and ad.state != '" . Advertisement::STATE_ARCHIVED . "' and ad.archived_need is null and ad.campaign_id is not null
and (ad.sitelink_external_id is null or (ad.sitelink_external_id is not null and s.id is not null))
and (ad.v_card_external_id is null or (ad.v_card_external_id is not null and vc.id is not null))
......@@ -191,7 +191,7 @@ class DictionaryCampaignsSyncByCampaign extends Command
// LEFT JOIN v_cards vc on ad.v_card_external_id = vc.external_id AND vc.deleted_at is null
// LEFT JOIN goal_v_cards gvc on vc.id = gvc.v_card_id and gad.dictionary_campaign_id = gvc.dictionary_campaign_id
// LEFT JOIN sitelinks s on ad.sitelink_external_id = s.external_id AND s.deleted_at is null
// LEFT JOIN goal_sitelinks gs on s.id = gs.sitelink_id and gs.token_id = d.token_id AND gs.dictionary_campaign_id = gad.dictionary_campaign_id AND gs.deleted_at is null
// LEFT JOIN goal_sitelinks gs on s.id = gs.sitelink_id and gs.token_id = d.token_id AND gs.dictionary_campaign_id = gad.dictionary_campaign_id AND gs.updated_need is null AND gs.deleted_at is null
//
//
// SET gad.goal_v_card_id = gvc.id,
......@@ -229,7 +229,7 @@ class DictionaryCampaignsSyncByCampaign extends Command
LEFT JOIN v_cards vc on ad.v_card_external_id = vc.external_id AND vc.deleted_at is null
LEFT JOIN goal_v_cards gvc on vc.id = gvc.v_card_id and gad.dictionary_campaign_id = gvc.dictionary_campaign_id and gvc.deleted_at is null
LEFT JOIN sitelinks s on ad.sitelink_external_id = s.external_id AND s.deleted_at is null
LEFT JOIN goal_sitelinks gs on s.id = gs.sitelink_id and gs.token_id = d.token_id AND gs.dictionary_campaign_id = gad.dictionary_campaign_id AND gs.deleted_at is null
LEFT JOIN goal_sitelinks gs on s.id = gs.sitelink_id and gs.token_id = d.token_id AND gs.dictionary_campaign_id = gad.dictionary_campaign_id AND gs.updated_need is null AND gs.deleted_at is null
SET gad.goal_v_card_id = gvc.id,
......
......@@ -2,6 +2,7 @@
namespace App\Console\Commands;
use App\Models\Pivots\GoalAdvertisement;
use App\Models\Pivots\GoalSitelink;
use App\Models\Tokens;
use App\Service\Requests\Direct\AddSitelinks;
......@@ -43,19 +44,30 @@ class SitelinksAdd extends Command
*/
public function handle()
{
$tokens = Tokens::whereHas('goalSitelinksForNotExternalForNotReserveCreate.sitelink')
->whereHas('goalSitelinksForNotExternalForNotReserveCreate.goalAdvertisements')
->where('type', '!=', Tokens::MAIN)
$tokens = Tokens::where('type', '!=', Tokens::MAIN)
->get();
foreach ($tokens as $token) {
$token->load([
'goalSitelinksForNotExternalForNotReserveCreate' => function (HasMany $query) {
return $query->has('sitelink')->has('goalAdvertisements');
$query->has('sitelink');
$query->join('goal_advertisements', 'goal_sitelinks.id', 'goal_advertisements.goal_sitelink_id')
->groupBy('goal_sitelinks.id')
->select('goal_sitelinks.*');
return $query;
},
'goalSitelinksForNotExternalForNotReserveCreate.sitelink',
]);
if (!$token->goalSitelinksForNotExternalForNotReserveCreate->count()) {
continue;
}
$goalSitelinks = $token->goalSitelinksForNotExternalForNotReserveCreate;
foreach (array_chunk($goalSitelinks->pluck('id')->toArray(), 100) as $items) {
......
<?php
namespace App\Console\Commands;
use App\Models\Pivots\GoalSitelink;
use App\Models\Tokens;
use App\Service\Requests\Direct\DeleteSitelinks;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Relations\HasMany;
class SitelinksDelete extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sitelinks: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
* @throws \Exception
*/
public function handle()
{
$tokens = Tokens::whereHas('goalSitelinksForExternalNeedDeletedForNotReserveCreate')
->where('type', '!=', Tokens::MAIN)
->get();
foreach ($tokens as $token) {
$token->load([
'goalSitelinksForExternalNeedDeletedForNotReserveCreate'
]);
$goalSitelinks = $token->goalSitelinksForExternalNeedDeletedForNotReserveCreate;
foreach (array_chunk($goalSitelinks->pluck('id')->toArray(), 100) as $items) {
GoalSitelink::whereIn('id', $items)
->update([
'reserve_delete_at' => Carbon::now(),
]);
}
$request = new DeleteSitelinks();
$request->setToken($token)
->call([
'Ids' => $goalSitelinks->pluck('external_id')->unique()->values()->toArray(),
]);
}
return 0;
}
}
......@@ -45,6 +45,7 @@ use App\Console\Commands\RetargetinglistsDelete;
use App\Console\Commands\RetargetinglistsLoad;
use App\Console\Commands\RetargetinglistsUpdate;
use App\Console\Commands\SitelinksAdd;
use App\Console\Commands\SitelinksDelete;
use App\Console\Commands\SitelinksLoad;
use App\Console\Commands\VCardsAdd;
use App\Console\Commands\VCardsDelete;
......@@ -69,7 +70,7 @@ class Kernel extends ConsoleKernel
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
protected function schedule(Schedule $schedule)
{
$schedule->command(DictionaryCampaignsSyncByCampaign::class)->everyThirtyMinutes();
......@@ -116,6 +117,7 @@ protected function schedule(Schedule $schedule)
$schedule->command(AdImagesAdd::class)->hourlyAt(35);
$schedule->command(AdExtensionsAdd::class)->hourlyAt(35);
$schedule->command(SitelinksDelete::class)->hourlyAt(20);
$schedule->command(SitelinksAdd::class)->hourlyAt(35);
$schedule->command(NegativeKeywordSharedSetsAdd::class)->hourlyAt(35);
$schedule->command(NegativeKeywordSharedSetsUpdate::class)->hourlyAt(35);
......
......@@ -8,6 +8,7 @@ use App\Models\YandexError;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;
class GoalSitelink extends Pivot
{
......@@ -67,7 +68,7 @@ class GoalSitelink extends Pivot
*/
public function scopeForNotExternal($query)
{
return $query->whereNull('external_id');
return $query->whereNull($query->getModel()->getTable() . '.external_id');
}
/**
......@@ -76,7 +77,16 @@ class GoalSitelink extends Pivot
*/
public function scopeForNotReserveCreate($query)
{
return $query->whereNull('reserve_create_at');
return $query->whereNull($query->getModel()->getTable() . '.reserve_create_at');
}
/**
* @param Builder $query
* @return Builder
*/
public function scopeForNotReserveDelete($query)
{
return $query->whereNull('reserve_delete_at');
}
/**
......@@ -97,6 +107,21 @@ class GoalSitelink extends Pivot
return $query->whereNotNull('updated_need');
}
/**
* @param Builder $query
* @return Builder
*/
public function scopeNeedDeleted($query)
{
return $query->needUpdated()->whereNotExists(function (\Illuminate\Database\Query\Builder $query) {
$query->select(DB::raw(1))
->from('goal_advertisements')
->whereNotNull('goal_advertisements.external_id')
->whereNull('goal_advertisements.deleted_at')
->whereColumn('goal_advertisements.goal_sitelink_external_id', 'goal_sitelinks.external_id');
});
}
public function token()
{
return $this->belongsTo(Tokens::class, 'token_id');
......
......@@ -309,6 +309,11 @@ class Tokens extends Model
return $this->goalSitelinks()->forNotExternal()->forNotReserveCreate();
}
public function goalSitelinksForExternalNeedDeletedForNotReserveCreate()
{
return $this->goalSitelinks()->forExternal()->needDeleted()->forNotReserveDelete();
}
public function goalRetargetinglists()
{
return $this->hasMany(GoalRetargetinglist::class, 'token_id');
......
......@@ -9,7 +9,7 @@ class DirectResponseHelper
{
return array_chunk(array_column(array_filter($data, function ($result) {
return isset($result['Id']);
}, ARRAY_FILTER_USE_BOTH), 'Id'), 1000);
}, ARRAY_FILTER_USE_BOTH), 'Id'), 100);
}
}
<?php
namespace App\Service\Requests\Direct;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Pivots\GoalSitelink;
use App\Models\Sitelink;
use App\Service\Contract\APIRequest;
use App\Service\DirectResponseHelper;
use App\Service\Requests\DirectRequest;
use Illuminate\Support\Facades\Log;
class DeleteSitelinks 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)
{
if (!isset($response['result']['DeleteResults'])) {
return;
}
foreach (DirectResponseHelper::getExternalIdsChunkByResult($response['result']['DeleteResults']) as $external_ids) {
if ($this->getToken()->isMain()) {
Sitelink::whereIn('external_id', $external_ids)
->delete();
} else {
GoalSitelink::whereIn('external_id', $external_ids)
->delete();
}
}
foreach ($response['result']['DeleteResults'] as $key => $delete_result) {
if (isset($delete_result['Id'])) {
continue;
}
$external_id = $this->getParams()['SelectionCriteria']['Ids'][$key];
if (isset($delete_result['Errors']) && count($delete_result['Errors'])) {
if ($this->getToken()->isMain()) {
$model = Sitelink::whereExternalId($external_id)->first();
} else {
$model = GoalSitelink::whereExternalId($external_id)->first();
}
if ($model) {
$model->errors()->create([
'token_id' => $this->getToken()->getKey(),
'service' => $this->getService(),
'method' => $this->getMethod(),
'params' => $external_id,
'errors' => $delete_result['Errors'],
]);
}
} else {
Log::debug("DeleteSitelinks, empty Id, token_id {$this->getToken()->getKey()}");
Log::debug($delete_result);
Log::debug($external_id);
}
if ($this->getToken()->isMain()) {
Sitelink::whereExternalId($external_id)
->update([
'reserve_delete_at' => null,
]);
} else {
GoalSitelink::whereExternalId($external_id)
->update([
'reserve_delete_at' => null,
]);
}
}
}
public function failed()
{
if ($this->getToken()->isMain()) {
foreach (array_chunk($this->getParams()['SelectionCriteria']['Ids'], 100) as $items) {
Sitelink::whereIn('external_id', $items)
->update([
'reserve_delete_at' => null,
]);
}
} else {
foreach (array_chunk($this->getParams()['SelectionCriteria']['Ids'], 100) as $items) {
GoalSitelink::whereIn('external_id', $items)
->update([
'reserve_delete_at' => null,
]);
}
}
}
private function requestPrepare($params)
{
$this->setService('Sitelinks');
$this->setMethod('delete');
$this->setParams([
'SelectionCriteria' => [
'Ids' => $params['Ids'],
],
]);
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateColumnGoalSitelinksDeletedNeed extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('goal_sitelinks', function (Blueprint $table) {
$table->timestamp('reserve_delete_at')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateIndex extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('goal_sitelinks', function (Blueprint $table) {
$table->index('external_id');
$table->index('updated_need');
$table->index('deleted_at');
$table->index('reserve_create_at');
$table->index('reserve_delete_at');
});
Schema::table('sitelinks', function (Blueprint $table) {
$table->index('deleted_at');
});
Schema::table('goal_advertisements', function (Blueprint $table) {
$table->index('deleted_at');
});
}
/**
* 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!