Commit c938fbf8 by Vladislav

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

1 parent f544823d
......@@ -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('ad_images', 'advertisements.ad_image_hash', '=', 'ad_images.hash')
->leftJoin('goal_ad_images', function($join) use ($token) {
$join->on('goal_ad_images.ad_image_id', '=', 'ad_images.id');
$join->on('goal_ad_images.token_id', '=', $token->id);
$join->on('goal_ad_images.ad_image_id', '=', 'ad_images.id')
->where('goal_ad_images.token_id', '=', $token->id);
})
->whereNotExists(function (Builder $query) {
$query->select(DB::raw(1))
......
......@@ -8,7 +8,6 @@ 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
{
......@@ -43,19 +42,18 @@ class AdvertisementsArchive extends Command
*/
public function handle()
{
$token = Tokens::has('goalAdvertisementsForNeedArchivedForNotReserveArchiveForNotArchived')
->firstWhere('type', '!=', Tokens::MAIN);
$token = Tokens::firstWhere('type', '!=', Tokens::MAIN);
if ($token) {
$this->sendRequest($token, Advertisement::forNotArchived()->notNeedArchived()->forNotReserveArchive()->get());
$this->sendRequest($token, Advertisement::forNotArchived()->needArchived()->forNotReserveArchive()->get());
}
$tokens = Tokens::has('dictionaryCampaignsEnabledForExternalSynchronized.goalAdvertisementsForNeedArchivedForNotReserveArchiveForNotArchived')
->where('type', Tokens::MAIN)
->where('type', '!=', Tokens::MAIN)
->get();
foreach ($tokens as $token) {
$this->sendRequest($token, $token->dictionaryCampaignsEnabledForExternalSynchronized->pluck('goalAdvertisementsForNeedArchivedForNotReserveArchiveForNotArchived'));
$this->sendRequest($token, $token->dictionaryCampaignsEnabledForExternalSynchronized->pluck('goalAdvertisementsForNeedArchivedForNotReserveArchiveForNotArchived')->collapse());
}
return 0;
......@@ -82,7 +80,6 @@ class AdvertisementsArchive extends Command
}
}
$request = new ArchiveAds();
$request->setToken($token)
->call([
......
......@@ -5,6 +5,7 @@ namespace App\Console\Commands;
use App\Models\Advertisement;
use App\Models\Tokens;
use App\Service\Requests\Direct\GetAds;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;
......@@ -41,25 +42,18 @@ class AdvertisementsCheckArchive extends Command
*/
public function handle()
{
$advertisements = Advertisement::forNotArchived()->notNeedArchived()->get();
/* @var Advertisement[] $advertisements */
$advertisements = Advertisement::forArchived()->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,
],
foreach ($advertisements as $advertisement) {
$advertisement->goalAdvertisements()
->notNeedArchived()
->update([
'archived_need' => Carbon::now(),
]);
}
}
......
......@@ -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 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
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.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;
use App\Console\Commands\AdImagesAdd;
use App\Console\Commands\AdImagesLoad;
use App\Console\Commands\AdvertisementsAdd;
use App\Console\Commands\AdvertisementsArchive;
use App\Console\Commands\AdvertisementsLoadUpdated;
use App\Console\Commands\AdvertisementsUpdate;
use App\Console\Commands\AudienceTargetsAdd;
......@@ -117,6 +118,7 @@ class Kernel extends ConsoleKernel
$schedule->command(AdvertisementsAdd::class)->hourlyAt(50);
$schedule->command(AdvertisementsUpdate::class)->hourlyAt(50);
$schedule->command(AdvertisementsArchive::class)->hourlyAt(50);
$schedule->call(function () {
$items = new \FilesystemIterator(config('clockwork.storage_files_path'));
......
......@@ -101,7 +101,6 @@ class Advertisement extends Model
'updated_self',
'archived_need',
'reserve_archive_at',
'archive_at',
];
protected $casts = [
......@@ -118,7 +117,6 @@ class Advertisement extends Model
'prefer_v_card_over_business' => 'boolean',
'archived_need' => 'datetime',
'reserve_archive_at' => 'datetime',
'archive_at' => 'datetime',
];
/**
......@@ -161,7 +159,7 @@ class Advertisement extends Model
*/
public function scopeForArchived($query)
{
return $query->whereNotNull('archive_at');
return $query->where('state', Advertisement::STATE_ARCHIVED);
}
/**
......@@ -170,7 +168,7 @@ class Advertisement extends Model
*/
public function scopeForNotArchived($query)
{
return $query->whereNull('archive_at');
return $query->where('state', '!==', Advertisement::STATE_ARCHIVED);
}
/**
......
......@@ -247,7 +247,7 @@ class GoalAdGroup extends Pivot
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;
* @method static Builder|GoalAdvertisement forNotReserveArchive()
* @method static Builder|GoalAdvertisement needUpdated()
* @method static Builder|GoalAdvertisement needArchived()
* @method static Builder|GoalAdvertisement notNeedArchived()
* @method static Builder|GoalAdvertisement newModelQuery()
* @method static Builder|GoalAdvertisement newQuery()
* @method static Builder|GoalAdvertisement query()
......@@ -192,6 +193,15 @@ class GoalAdvertisement extends Pivot
* @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');
......
......@@ -25,12 +25,12 @@ class ArchiveAds extends DirectRequest
public function getObjectsCount()
{
return count($this->getParams()['SelectionCriteria']['Ads']);
return count($this->getParams()['SelectionCriteria']['Ids']);
}
public function slice($maxObjects): ?APIRequest
{
return $this->sliceByKey($maxObjects, 'Ads');;
return $this->sliceByKey($maxObjects, ['SelectionCriteria', 'Ids']);;
}
public function handle($response)
......@@ -44,15 +44,15 @@ class ArchiveAds extends DirectRequest
if (!isset($archive_result['Id'])) {
Log::debug("ArchiveAds, empty Id");
Log::debug($archive_result);
Log::debug($this->getParams()['Ads'][$key]);
Log::debug($this->getParams()['SelectionCriteria']['Ids'][$key]);
if ($this->getToken()->isMain()) {
Advertisement::whereExternalId($this->getParams()['SelectionCriteria']['Ads']['Ids'][$key])
Advertisement::whereExternalId($this->getParams()['SelectionCriteria']['Ids'][$key])
->update([
'reserve_archive_at' => null,
]);
} else {
GoalAdvertisement::whereExternalId($this->getParams()['SelectionCriteria']['Ads']['Ids'][$key])
GoalAdvertisement::whereExternalId($this->getParams()['SelectionCriteria']['Ids'][$key])
->update([
'reserve_archive_at' => null,
]);
......@@ -67,7 +67,8 @@ class ArchiveAds extends DirectRequest
Advertisement::needArchived()
->where('external_id', $external_id)
->update([
'archive_at' => Carbon::now(),
'state' => Advertisement::STATE_ARCHIVED,
'status_clarification' => 'Archived.',
'archived_need' => null,
'reserve_archive_at' => null,
]);
......@@ -92,12 +93,12 @@ class ArchiveAds extends DirectRequest
public function failed()
{
if ($this->getToken()->isMain()) {
Advertisement::whereIn('external_id', $this->getParams()['SelectionCriteria']['Ads']['Ids'])
Advertisement::whereIn('external_id', $this->getParams()['SelectionCriteria']['Ids'])
->update([
'reserve_archive_at' => null,
]);
} else {
GoalAdvertisement::whereIn('external_id', $this->getParams()['SelectionCriteria']['Ads']['Ids'])
GoalAdvertisement::whereIn('external_id', $this->getParams()['SelectionCriteria']['Ids'])
->update([
'reserve_archive_at' => null,
]);
......
......@@ -111,9 +111,6 @@ class GetAds extends DirectRequest
'sub_type' => $ad['Subtype'],
'updated_self' => null,
'archived_need' => $ad['State'] === Advertisement::STATE_ARCHIVED
? Carbon::now()
: null,
];
if (isset($ad['TextAd'])) {
......@@ -164,8 +161,10 @@ class GetAds extends DirectRequest
'external_id' => $external_id
], $data);
if ($advertisement->archived_need) {
$advertisement->goalAdvertisements()->update([
if ($advertisement->state === Advertisement::STATE_ARCHIVED) {
$advertisement->goalAdvertisements()
->notNeedArchived()
->update([
'archived_need' => Carbon::now(),
]);
}
......
......@@ -14,7 +14,6 @@ class AddAdvertisementsArchiveColumn extends Migration
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();
});
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!