Commit f544823d by Vladislav

#20695 Отключение объявлений

1 parent 6314cf0c
<?php
namespace App\Console\Commands;
use App\Models\Advertisement;
use App\Models\Pivots\GoalAdvertisement;
use App\Models\Tokens;
use App\Service\Requests\Direct\ArchiveAds;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;
class AdvertisementsArchive extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'ads:archive';
/**
* 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()
{
$token = Tokens::has('goalAdvertisementsForNeedArchivedForNotReserveArchiveForNotArchived')
->firstWhere('type', '!=', Tokens::MAIN);
if ($token) {
$this->sendRequest($token, Advertisement::forNotArchived()->notNeedArchived()->forNotReserveArchive()->get());
}
$tokens = Tokens::has('dictionaryCampaignsEnabledForExternalSynchronized.goalAdvertisementsForNeedArchivedForNotReserveArchiveForNotArchived')
->where('type', Tokens::MAIN)
->get();
foreach ($tokens as $token) {
$this->sendRequest($token, $token->dictionaryCampaignsEnabledForExternalSynchronized->pluck('goalAdvertisementsForNeedArchivedForNotReserveArchiveForNotArchived'));
}
return 0;
}
private function sendRequest($token, $ads)
{
/* @var $ads Advertisement[]|GoalAdvertisement[] */
if (!$ads->count()) {
return;
}
foreach (array_chunk($ads->pluck('external_id')->toArray(), 1000) as $items) {
if ($token->isMain()) {
Advertisement::whereIn('external_id', $items)
->update([
'reserve_archive_at' => Carbon::now(),
]);
} else {
GoalAdvertisement::whereIn('external_id', $items)
->update([
'reserve_archive_at' => Carbon::now(),
]);
}
}
$request = new ArchiveAds();
$request->setToken($token)
->call([
'ids' => $ads->pluck('external_id')->toArray(),
]);
}
}
<?php
namespace App\Console\Commands;
use App\Models\Advertisement;
use App\Models\Tokens;
use App\Service\Requests\Direct\GetAds;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;
class AdvertisementsCheckArchive extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'ads:loadArchive';
/**
* 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()
{
$advertisements = Advertisement::forNotArchived()->notNeedArchived()->get();
if ($advertisements->count()) {
$token = Tokens::where('type', Tokens::MAIN)->first();
if (!$token) {
throw new \Exception('Не найден токен блин');
}
$ids = $advertisements->pluck('external_id')->toArray();
$request = new GetAds();
$request->setToken($token)
->call([
'Ids' => $ids,
'States' => [
Advertisement::STATE_ARCHIVED,
],
]);
}
return 0;
}
}
...@@ -47,7 +47,7 @@ class AdvertisementsLoadUpdated extends Command ...@@ -47,7 +47,7 @@ class AdvertisementsLoadUpdated extends Command
})->orWhere(function (Builder $query) { })->orWhere(function (Builder $query) {
$query->whereNull('campaign_id'); $query->whereNull('campaign_id');
}); });
})->forUpdatedSelf()->get(); })->forUpdatedSelf()->forNotArchived()->notNeedArchived()->get();
if ($advertisements->count()) { if ($advertisements->count()) {
......
...@@ -221,9 +221,9 @@ class DictionaryCampaignsSyncByCampaign extends Command ...@@ -221,9 +221,9 @@ class DictionaryCampaignsSyncByCampaign extends Command
LEFT JOIN goal_v_cards gvc on vc.id = gvc.v_card_id and gag.dictionary_campaign_id = gvc.dictionary_campaign_id LEFT JOIN goal_v_cards gvc on vc.id = gvc.v_card_id and gag.dictionary_campaign_id = gvc.dictionary_campaign_id
LEFT JOIN sitelinks s on ad.sitelink_external_id = s.external_id LEFT JOIN sitelinks s on ad.sitelink_external_id = s.external_id
LEFT JOIN goal_sitelinks gs on s.id = gs.sitelink_id and gs.token_id = d.token_id LEFT JOIN goal_sitelinks gs on s.id = gs.sitelink_id and gs.token_id = d.token_id
WHERE gad.advertisement_id is null and ad.campaign_id is not null WHERE gad.advertisement_id is null and ad.archive_at is not and ad.archived_need is not 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.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)) and (ad.v_card_external_id is null or (ad.v_card_external_id is not null and vc.id is not null))
"); ");
$this->info('goal_advertisements successful!'); $this->info('goal_advertisements successful!');
......
...@@ -99,6 +99,9 @@ class Advertisement extends Model ...@@ -99,6 +99,9 @@ class Advertisement extends Model
'prefer_v_card_over_business', 'prefer_v_card_over_business',
'updated_self', 'updated_self',
'archived_need',
'reserve_archive_at',
'archive_at',
]; ];
protected $casts = [ protected $casts = [
...@@ -113,6 +116,9 @@ class Advertisement extends Model ...@@ -113,6 +116,9 @@ class Advertisement extends Model
'price_extension' => 'json', 'price_extension' => 'json',
'turbo_page_moderation' => 'json', 'turbo_page_moderation' => 'json',
'prefer_v_card_over_business' => 'boolean', 'prefer_v_card_over_business' => 'boolean',
'archived_need' => 'datetime',
'reserve_archive_at' => 'datetime',
'archive_at' => 'datetime',
]; ];
/** /**
...@@ -149,6 +155,51 @@ class Advertisement extends Model ...@@ -149,6 +155,51 @@ class Advertisement extends Model
return $query->whereNotNull("{$query->getModel()->getTable()}.updated_self"); return $query->whereNotNull("{$query->getModel()->getTable()}.updated_self");
} }
/**
* @param Builder $query
* @return Builder
*/
public function scopeForArchived($query)
{
return $query->whereNotNull('archive_at');
}
/**
* @param Builder $query
* @return Builder
*/
public function scopeForNotArchived($query)
{
return $query->whereNull('archive_at');
}
/**
* @param Builder $query
* @return Builder
*/
public function scopeNeedArchived($query)
{
return $query->whereNotNull('archived_need');
}
/**
* @param Builder $query
* @return Builder
*/
public function scopeNotNeedArchived($query)
{
return $query->whereNull('archived_need');
}
/**
* @param Builder $query
* @return Builder
*/
public function scopeForNotReserveArchive($query)
{
return $query->whereNull('reserve_archive_at');
}
public function groups() public function groups()
{ {
return $this->hasMany(AdGroup::class, 'ad_group_id'); return $this->hasMany(AdGroup::class, 'ad_group_id');
......
...@@ -501,6 +501,11 @@ class DictionaryCampaign extends Pivot ...@@ -501,6 +501,11 @@ class DictionaryCampaign extends Pivot
return $this->goalAdvertisements()->needUpdated()->forNotReserveUpdate(); return $this->goalAdvertisements()->needUpdated()->forNotReserveUpdate();
} }
public function goalAdvertisementsForNeedArchivedForNotReserveArchiveForNotArchived()
{
return $this->goalAdvertisements()->needArchived()->forNotReserveArchive()->forNotArchived();
}
public function goalBidModifiers() public function goalBidModifiers()
{ {
return $this->hasMany(GoalBidModifier::class, 'dictionary_campaign_id'); return $this->hasMany(GoalBidModifier::class, 'dictionary_campaign_id');
......
...@@ -22,6 +22,8 @@ use Illuminate\Database\Eloquent\SoftDeletes; ...@@ -22,6 +22,8 @@ use Illuminate\Database\Eloquent\SoftDeletes;
* @property \Illuminate\Support\Carbon|null $external_upload_at * @property \Illuminate\Support\Carbon|null $external_upload_at
* @property \Illuminate\Support\Carbon|null $external_updated_at * @property \Illuminate\Support\Carbon|null $external_updated_at
* @property \Illuminate\Support\Carbon|null $updated_need * @property \Illuminate\Support\Carbon|null $updated_need
* @property \Illuminate\Support\Carbon|null $archived_need
* @property \Illuminate\Support\Carbon|null $reserve_archive_at
* @property \Illuminate\Support\Carbon|null $created_at * @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at * @property \Illuminate\Support\Carbon|null $updated_at
* @property-read Advertisement $advertisement * @property-read Advertisement $advertisement
...@@ -29,7 +31,9 @@ use Illuminate\Database\Eloquent\SoftDeletes; ...@@ -29,7 +31,9 @@ use Illuminate\Database\Eloquent\SoftDeletes;
* @method static Builder|GoalAdvertisement forNotExternal() * @method static Builder|GoalAdvertisement forNotExternal()
* @method static Builder|GoalAdvertisement forNotReserveCreate() * @method static Builder|GoalAdvertisement forNotReserveCreate()
* @method static Builder|GoalAdvertisement forNotReserveUpdate() * @method static Builder|GoalAdvertisement forNotReserveUpdate()
* @method static Builder|GoalAdvertisement forNotReserveArchive()
* @method static Builder|GoalAdvertisement needUpdated() * @method static Builder|GoalAdvertisement needUpdated()
* @method static Builder|GoalAdvertisement needArchived()
* @method static Builder|GoalAdvertisement newModelQuery() * @method static Builder|GoalAdvertisement newModelQuery()
* @method static Builder|GoalAdvertisement newQuery() * @method static Builder|GoalAdvertisement newQuery()
* @method static Builder|GoalAdvertisement query() * @method static Builder|GoalAdvertisement query()
...@@ -69,6 +73,9 @@ class GoalAdvertisement extends Pivot ...@@ -69,6 +73,9 @@ class GoalAdvertisement extends Pivot
'updated_need', 'updated_need',
'reserve_create_at', 'reserve_create_at',
'reserve_update_at', 'reserve_update_at',
'archive_at',
'archived_need',
'reserve_archive_at',
]; ];
protected $casts = [ protected $casts = [
...@@ -77,6 +84,9 @@ class GoalAdvertisement extends Pivot ...@@ -77,6 +84,9 @@ class GoalAdvertisement extends Pivot
'updated_need' => 'datetime', 'updated_need' => 'datetime',
'reserve_create_at' => 'datetime', 'reserve_create_at' => 'datetime',
'reserve_update_at' => 'datetime', 'reserve_update_at' => 'datetime',
'archive_at' => 'datetime',
'archived_need' => 'datetime',
'reserve_archive_at' => 'datetime',
]; ];
public $incrementing = true; public $incrementing = true;
...@@ -100,6 +110,9 @@ class GoalAdvertisement extends Pivot ...@@ -100,6 +110,9 @@ class GoalAdvertisement extends Pivot
'updated_need', 'updated_need',
'reserve_create_at', 'reserve_create_at',
'reserve_update_at', 'reserve_update_at',
'archived_need',
'reserve_archive_at',
'archive_at',
]; ];
} }
...@@ -148,6 +161,42 @@ class GoalAdvertisement extends Pivot ...@@ -148,6 +161,42 @@ class GoalAdvertisement extends Pivot
return $query->whereNotNull('updated_need'); return $query->whereNotNull('updated_need');
} }
/**
* @param Builder $query
* @return Builder
*/
public function scopeForArchived($query)
{
return $query->whereNotNull('archive_at');
}
/**
* @param Builder $query
* @return Builder
*/
public function scopeForNotArchived($query)
{
return $query->whereNull('archive_at');
}
/**
* @param Builder $query
* @return Builder
*/
public function scopeNeedArchived($query)
{
return $query->whereNotNull('archived_need');
}
/**
* @param Builder $query
* @return Builder
*/
public function scopeForNotReserveArchive($query)
{
return $query->whereNull('reserve_archive_at');
}
public function advertisement() public function advertisement()
{ {
return $this->belongsTo(Advertisement::class, 'advertisement_id'); return $this->belongsTo(Advertisement::class, 'advertisement_id');
......
<?php
namespace App\Service\Requests\Direct;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Advertisement;
use App\Models\Pivots\GoalAdvertisement;
use App\Service\Contract\APIRequest;
use App\Service\Requests\DirectRequest;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
class ArchiveAds 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']['Ads']);
}
public function slice($maxObjects): ?APIRequest
{
return $this->sliceByKey($maxObjects, 'Ads');;
}
public function handle($response)
{
try {
if (!isset($response['result']['ArchiveResults'])) {
return;
}
foreach ($response['result']['ArchiveResults'] as $key => $archive_result) {
if (!isset($archive_result['Id'])) {
Log::debug("ArchiveAds, empty Id");
Log::debug($archive_result);
Log::debug($this->getParams()['Ads'][$key]);
if ($this->getToken()->isMain()) {
Advertisement::whereExternalId($this->getParams()['SelectionCriteria']['Ads']['Ids'][$key])
->update([
'reserve_archive_at' => null,
]);
} else {
GoalAdvertisement::whereExternalId($this->getParams()['SelectionCriteria']['Ads']['Ids'][$key])
->update([
'reserve_archive_at' => null,
]);
}
continue;
}
$external_id = (string)$archive_result['Id'];
if ($this->getToken()->isMain()) {
Advertisement::needArchived()
->where('external_id', $external_id)
->update([
'archive_at' => Carbon::now(),
'archived_need' => null,
'reserve_archive_at' => null,
]);
} else {
GoalAdvertisement::forExternal()->needArchived()
->where('external_id', $external_id)
->update([
'archive_at' => Carbon::now(),
'archived_need' => null,
'reserve_archive_at' => null,
]);
}
}
} catch (\Exception $e) {
Log::debug($e);
throw $e;
}
}
public function failed()
{
if ($this->getToken()->isMain()) {
Advertisement::whereIn('external_id', $this->getParams()['SelectionCriteria']['Ads']['Ids'])
->update([
'reserve_archive_at' => null,
]);
} else {
GoalAdvertisement::whereIn('external_id', $this->getParams()['SelectionCriteria']['Ads']['Ids'])
->update([
'reserve_archive_at' => null,
]);
}
}
private function requestPrepare($params)
{
$this->setService('Ads');
$this->setMethod('archive');
$this->setParams([
'SelectionCriteria' => [
'Ids' => $params['ids'],
],
]);
}
}
...@@ -111,6 +111,9 @@ class GetAds extends DirectRequest ...@@ -111,6 +111,9 @@ class GetAds extends DirectRequest
'sub_type' => $ad['Subtype'], 'sub_type' => $ad['Subtype'],
'updated_self' => null, 'updated_self' => null,
'archived_need' => $ad['State'] === Advertisement::STATE_ARCHIVED
? Carbon::now()
: null,
]; ];
if (isset($ad['TextAd'])) { if (isset($ad['TextAd'])) {
...@@ -161,6 +164,12 @@ class GetAds extends DirectRequest ...@@ -161,6 +164,12 @@ class GetAds extends DirectRequest
'external_id' => $external_id 'external_id' => $external_id
], $data); ], $data);
if ($advertisement->archived_need) {
$advertisement->goalAdvertisements()->update([
'archived_need' => Carbon::now(),
]);
}
$adExtensions_sync = $advertisement->adExtensions()->sync($ad_extensions->pluck('id')); $adExtensions_sync = $advertisement->adExtensions()->sync($ad_extensions->pluck('id'));
if ($advertisement->wasRecentlyCreated) { if ($advertisement->wasRecentlyCreated) {
...@@ -224,6 +233,11 @@ class GetAds extends DirectRequest ...@@ -224,6 +233,11 @@ class GetAds extends DirectRequest
"Price", "OldPrice", "PriceCurrency", "PriceQualifier", "Price", "OldPrice", "PriceCurrency", "PriceQualifier",
], ],
]; ];
if (isset($filter['States'])) {
$params['SelectionCriteria']['States'] = $filter['States'];
}
if (isset($filter['CampaignIds'])) { if (isset($filter['CampaignIds'])) {
$params['SelectionCriteria']['CampaignIds'] = $filter['CampaignIds']; $params['SelectionCriteria']['CampaignIds'] = $filter['CampaignIds'];
} }
......
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddAdvertisementsArchiveColumn extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('advertisements', function (Blueprint $table) {
$table->timestamp('archive_at')->nullable();
$table->timestamp('archived_need')->nullable();
$table->timestamp('reserve_archive_at')->nullable();
});
Schema::table('goal_advertisements', function (Blueprint $table) {
$table->timestamp('archive_at')->nullable();
$table->timestamp('archived_need')->nullable();
$table->timestamp('reserve_archive_at')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
# Developer # Developer
Generate local ide helper meta PHPDoc Generate local ide helper meta PHPDoc
```shell script ```sh
php artisan ide-helper:generate php artisan ide-helper:generate
php artisan ide-helper:meta php artisan ide-helper:meta
php artisan ide-helper:models php artisan ide-helper:models
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!