Commit c938fbf8 by Vladislav

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

1 parent f544823d
...@@ -55,8 +55,8 @@ class AdvertisementsAdd extends Command ...@@ -55,8 +55,8 @@ class AdvertisementsAdd extends Command
->leftJoin('goal_ad_extensions', 'goal_advertisement_goal_ad_extensions.goal_ad_extension_id', '=', 'goal_ad_extensions.id') ->leftJoin('goal_ad_extensions', 'goal_advertisement_goal_ad_extensions.goal_ad_extension_id', '=', 'goal_ad_extensions.id')
->leftJoin('ad_images', 'advertisements.ad_image_hash', '=', 'ad_images.hash') ->leftJoin('ad_images', 'advertisements.ad_image_hash', '=', 'ad_images.hash')
->leftJoin('goal_ad_images', function($join) use ($token) { ->leftJoin('goal_ad_images', function($join) use ($token) {
$join->on('goal_ad_images.ad_image_id', '=', 'ad_images.id'); $join->on('goal_ad_images.ad_image_id', '=', 'ad_images.id')
$join->on('goal_ad_images.token_id', '=', $token->id); ->where('goal_ad_images.token_id', '=', $token->id);
}) })
->whereNotExists(function (Builder $query) { ->whereNotExists(function (Builder $query) {
$query->select(DB::raw(1)) $query->select(DB::raw(1))
......
...@@ -8,7 +8,6 @@ use App\Models\Tokens; ...@@ -8,7 +8,6 @@ use App\Models\Tokens;
use App\Service\Requests\Direct\ArchiveAds; use App\Service\Requests\Direct\ArchiveAds;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;
class AdvertisementsArchive extends Command class AdvertisementsArchive extends Command
{ {
...@@ -43,19 +42,18 @@ class AdvertisementsArchive extends Command ...@@ -43,19 +42,18 @@ class AdvertisementsArchive extends Command
*/ */
public function handle() public function handle()
{ {
$token = Tokens::has('goalAdvertisementsForNeedArchivedForNotReserveArchiveForNotArchived') $token = Tokens::firstWhere('type', '!=', Tokens::MAIN);
->firstWhere('type', '!=', Tokens::MAIN);
if ($token) { if ($token) {
$this->sendRequest($token, Advertisement::forNotArchived()->notNeedArchived()->forNotReserveArchive()->get()); $this->sendRequest($token, Advertisement::forNotArchived()->needArchived()->forNotReserveArchive()->get());
} }
$tokens = Tokens::has('dictionaryCampaignsEnabledForExternalSynchronized.goalAdvertisementsForNeedArchivedForNotReserveArchiveForNotArchived') $tokens = Tokens::has('dictionaryCampaignsEnabledForExternalSynchronized.goalAdvertisementsForNeedArchivedForNotReserveArchiveForNotArchived')
->where('type', Tokens::MAIN) ->where('type', '!=', Tokens::MAIN)
->get(); ->get();
foreach ($tokens as $token) { foreach ($tokens as $token) {
$this->sendRequest($token, $token->dictionaryCampaignsEnabledForExternalSynchronized->pluck('goalAdvertisementsForNeedArchivedForNotReserveArchiveForNotArchived')); $this->sendRequest($token, $token->dictionaryCampaignsEnabledForExternalSynchronized->pluck('goalAdvertisementsForNeedArchivedForNotReserveArchiveForNotArchived')->collapse());
} }
return 0; return 0;
...@@ -82,7 +80,6 @@ class AdvertisementsArchive extends Command ...@@ -82,7 +80,6 @@ class AdvertisementsArchive extends Command
} }
} }
$request = new ArchiveAds(); $request = new ArchiveAds();
$request->setToken($token) $request->setToken($token)
->call([ ->call([
......
...@@ -5,6 +5,7 @@ namespace App\Console\Commands; ...@@ -5,6 +5,7 @@ namespace App\Console\Commands;
use App\Models\Advertisement; use App\Models\Advertisement;
use App\Models\Tokens; use App\Models\Tokens;
use App\Service\Requests\Direct\GetAds; use App\Service\Requests\Direct\GetAds;
use Carbon\Carbon;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
...@@ -41,25 +42,18 @@ class AdvertisementsCheckArchive extends Command ...@@ -41,25 +42,18 @@ class AdvertisementsCheckArchive extends Command
*/ */
public function handle() public function handle()
{ {
$advertisements = Advertisement::forNotArchived()->notNeedArchived()->get(); /* @var Advertisement[] $advertisements */
$advertisements = Advertisement::forArchived()->get();
if ($advertisements->count()) { if ($advertisements->count()) {
$token = Tokens::where('type', Tokens::MAIN)->first(); foreach ($advertisements as $advertisement) {
if (!$token) { $advertisement->goalAdvertisements()
throw new \Exception('Не найден токен блин'); ->notNeedArchived()
} ->update([
'archived_need' => Carbon::now(),
$ids = $advertisements->pluck('external_id')->toArray();
$request = new GetAds();
$request->setToken($token)
->call([
'Ids' => $ids,
'States' => [
Advertisement::STATE_ARCHIVED,
],
]); ]);
}
} }
......
...@@ -221,7 +221,7 @@ class DictionaryCampaignsSyncByCampaign extends Command ...@@ -221,7 +221,7 @@ 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.archive_at is not and ad.archived_need is not and ad.campaign_id is not null WHERE 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.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))
"); ");
......
...@@ -10,6 +10,7 @@ use App\Console\Commands\AdGroupsUpdate; ...@@ -10,6 +10,7 @@ use App\Console\Commands\AdGroupsUpdate;
use App\Console\Commands\AdImagesAdd; 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\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;
...@@ -117,6 +118,7 @@ class Kernel extends ConsoleKernel ...@@ -117,6 +118,7 @@ class Kernel extends ConsoleKernel
$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->call(function () { $schedule->call(function () {
$items = new \FilesystemIterator(config('clockwork.storage_files_path')); $items = new \FilesystemIterator(config('clockwork.storage_files_path'));
......
...@@ -101,7 +101,6 @@ class Advertisement extends Model ...@@ -101,7 +101,6 @@ class Advertisement extends Model
'updated_self', 'updated_self',
'archived_need', 'archived_need',
'reserve_archive_at', 'reserve_archive_at',
'archive_at',
]; ];
protected $casts = [ protected $casts = [
...@@ -118,7 +117,6 @@ class Advertisement extends Model ...@@ -118,7 +117,6 @@ class Advertisement extends Model
'prefer_v_card_over_business' => 'boolean', 'prefer_v_card_over_business' => 'boolean',
'archived_need' => 'datetime', 'archived_need' => 'datetime',
'reserve_archive_at' => 'datetime', 'reserve_archive_at' => 'datetime',
'archive_at' => 'datetime',
]; ];
/** /**
...@@ -161,7 +159,7 @@ class Advertisement extends Model ...@@ -161,7 +159,7 @@ class Advertisement extends Model
*/ */
public function scopeForArchived($query) public function scopeForArchived($query)
{ {
return $query->whereNotNull('archive_at'); return $query->where('state', Advertisement::STATE_ARCHIVED);
} }
/** /**
...@@ -170,7 +168,7 @@ class Advertisement extends Model ...@@ -170,7 +168,7 @@ class Advertisement extends Model
*/ */
public function scopeForNotArchived($query) public function scopeForNotArchived($query)
{ {
return $query->whereNull('archive_at'); return $query->where('state', '!==', Advertisement::STATE_ARCHIVED);
} }
/** /**
......
...@@ -247,7 +247,7 @@ class GoalAdGroup extends Pivot ...@@ -247,7 +247,7 @@ class GoalAdGroup extends Pivot
public function goalAudienceTargets() public function goalAudienceTargets()
{ {
return $this->hasMany(GoalAudienceTarget::class, 'ad_group_id'); return $this->hasMany(GoalAudienceTarget::class, 'goal_ad_group_id');
} }
} }
...@@ -34,6 +34,7 @@ use Illuminate\Database\Eloquent\SoftDeletes; ...@@ -34,6 +34,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
* @method static Builder|GoalAdvertisement forNotReserveArchive() * @method static Builder|GoalAdvertisement forNotReserveArchive()
* @method static Builder|GoalAdvertisement needUpdated() * @method static Builder|GoalAdvertisement needUpdated()
* @method static Builder|GoalAdvertisement needArchived() * @method static Builder|GoalAdvertisement needArchived()
* @method static Builder|GoalAdvertisement notNeedArchived()
* @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()
...@@ -192,6 +193,15 @@ class GoalAdvertisement extends Pivot ...@@ -192,6 +193,15 @@ class GoalAdvertisement extends Pivot
* @param Builder $query * @param Builder $query
* @return Builder * @return Builder
*/ */
public function scopeNotNeedArchived($query)
{
return $query->whereNull('archived_need');
}
/**
* @param Builder $query
* @return Builder
*/
public function scopeForNotReserveArchive($query) public function scopeForNotReserveArchive($query)
{ {
return $query->whereNull('reserve_archive_at'); return $query->whereNull('reserve_archive_at');
......
...@@ -25,12 +25,12 @@ class ArchiveAds extends DirectRequest ...@@ -25,12 +25,12 @@ class ArchiveAds extends DirectRequest
public function getObjectsCount() public function getObjectsCount()
{ {
return count($this->getParams()['SelectionCriteria']['Ads']); return count($this->getParams()['SelectionCriteria']['Ids']);
} }
public function slice($maxObjects): ?APIRequest public function slice($maxObjects): ?APIRequest
{ {
return $this->sliceByKey($maxObjects, 'Ads');; return $this->sliceByKey($maxObjects, ['SelectionCriteria', 'Ids']);;
} }
public function handle($response) public function handle($response)
...@@ -44,15 +44,15 @@ class ArchiveAds extends DirectRequest ...@@ -44,15 +44,15 @@ class ArchiveAds extends DirectRequest
if (!isset($archive_result['Id'])) { if (!isset($archive_result['Id'])) {
Log::debug("ArchiveAds, empty Id"); Log::debug("ArchiveAds, empty Id");
Log::debug($archive_result); Log::debug($archive_result);
Log::debug($this->getParams()['Ads'][$key]); Log::debug($this->getParams()['SelectionCriteria']['Ids'][$key]);
if ($this->getToken()->isMain()) { if ($this->getToken()->isMain()) {
Advertisement::whereExternalId($this->getParams()['SelectionCriteria']['Ads']['Ids'][$key]) Advertisement::whereExternalId($this->getParams()['SelectionCriteria']['Ids'][$key])
->update([ ->update([
'reserve_archive_at' => null, 'reserve_archive_at' => null,
]); ]);
} else { } else {
GoalAdvertisement::whereExternalId($this->getParams()['SelectionCriteria']['Ads']['Ids'][$key]) GoalAdvertisement::whereExternalId($this->getParams()['SelectionCriteria']['Ids'][$key])
->update([ ->update([
'reserve_archive_at' => null, 'reserve_archive_at' => null,
]); ]);
...@@ -67,7 +67,8 @@ class ArchiveAds extends DirectRequest ...@@ -67,7 +67,8 @@ class ArchiveAds extends DirectRequest
Advertisement::needArchived() Advertisement::needArchived()
->where('external_id', $external_id) ->where('external_id', $external_id)
->update([ ->update([
'archive_at' => Carbon::now(), 'state' => Advertisement::STATE_ARCHIVED,
'status_clarification' => 'Archived.',
'archived_need' => null, 'archived_need' => null,
'reserve_archive_at' => null, 'reserve_archive_at' => null,
]); ]);
...@@ -92,12 +93,12 @@ class ArchiveAds extends DirectRequest ...@@ -92,12 +93,12 @@ class ArchiveAds extends DirectRequest
public function failed() public function failed()
{ {
if ($this->getToken()->isMain()) { if ($this->getToken()->isMain()) {
Advertisement::whereIn('external_id', $this->getParams()['SelectionCriteria']['Ads']['Ids']) Advertisement::whereIn('external_id', $this->getParams()['SelectionCriteria']['Ids'])
->update([ ->update([
'reserve_archive_at' => null, 'reserve_archive_at' => null,
]); ]);
} else { } else {
GoalAdvertisement::whereIn('external_id', $this->getParams()['SelectionCriteria']['Ads']['Ids']) GoalAdvertisement::whereIn('external_id', $this->getParams()['SelectionCriteria']['Ids'])
->update([ ->update([
'reserve_archive_at' => null, 'reserve_archive_at' => null,
]); ]);
......
...@@ -111,9 +111,6 @@ class GetAds extends DirectRequest ...@@ -111,9 +111,6 @@ 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'])) {
...@@ -164,8 +161,10 @@ class GetAds extends DirectRequest ...@@ -164,8 +161,10 @@ class GetAds extends DirectRequest
'external_id' => $external_id 'external_id' => $external_id
], $data); ], $data);
if ($advertisement->archived_need) { if ($advertisement->state === Advertisement::STATE_ARCHIVED) {
$advertisement->goalAdvertisements()->update([ $advertisement->goalAdvertisements()
->notNeedArchived()
->update([
'archived_need' => Carbon::now(), 'archived_need' => Carbon::now(),
]); ]);
} }
......
...@@ -14,7 +14,6 @@ class AddAdvertisementsArchiveColumn extends Migration ...@@ -14,7 +14,6 @@ class AddAdvertisementsArchiveColumn extends Migration
public function up() public function up()
{ {
Schema::table('advertisements', function (Blueprint $table) { Schema::table('advertisements', function (Blueprint $table) {
$table->timestamp('archive_at')->nullable();
$table->timestamp('archived_need')->nullable(); $table->timestamp('archived_need')->nullable();
$table->timestamp('reserve_archive_at')->nullable(); $table->timestamp('reserve_archive_at')->nullable();
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!