Commit 9753f212 by Vladislav

#20364 Загрузка и синхронизация расширений

1 parent fdf9d4c6
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
namespace App\Console\Commands; namespace App\Console\Commands;
use App\Models\AdExtension; use App\Models\Pivots\GoalAdExtension;
use App\Models\Tokens; use App\Models\Tokens;
use App\Service\API\API; use App\Service\API\API;
use App\Service\Requests\APIRequest; use App\Service\Requests\APIRequest;
...@@ -44,26 +44,25 @@ class AdExtensionsAdd extends Command ...@@ -44,26 +44,25 @@ class AdExtensionsAdd extends Command
*/ */
public function handle() public function handle()
{ {
$tokens = Tokens::whereHas('dictionaryCampaignsEnabledForExternalSynchronized.adExtensionsForNotExternalForNotReserveCreate.adExtension') $tokens = Tokens::whereHas('goalAdExtensionsForNotExternalForNotReserveCreate.adExtension')
->where('type', '!=', Tokens::MAIN) ->where('type', '!=', Tokens::MAIN)
->get(); ->get();
foreach ($tokens as $token) { foreach ($tokens as $token) {
$token->load([ $token->load([
'dictionaryCampaignsEnabledForExternalSynchronized.adExtensionsForNotExternalForNotReserveCreate' => function (HasMany $query) { 'goalAdExtensionsForNotExternalForNotReserveCreate' => function (HasMany $query) {
return $query->has('adExtension'); return $query->has('adExtension');
}, },
'dictionaryCampaignsEnabledForExternalSynchronized.adExtensionsForNotExternalForNotReserveCreate.adExtension', 'goalAdExtensionsForNotExternalForNotReserveCreate.adExtension',
]); ]);
$factory = APIRequest::getInstance(API::YANDEX); $factory = APIRequest::getInstance(API::YANDEX);
$factory->setToken($token); $factory->setToken($token);
$goalAdExtensions = $token->dictionaryCampaignsEnabledForExternalSynchronized->pluck('adExtensionsForNotExternalForNotReserveCreate') $goalAdExtensions = $token->goalAdExtensionsForNotExternalForNotReserveCreate;
->collapse();
foreach (array_chunk($goalAdExtensions->pluck('id')->toArray(), 1000) as $items) { foreach (array_chunk($goalAdExtensions->pluck('id')->toArray(), 1000) as $items) {
AdExtension::whereIn('id', $items) GoalAdExtension::whereIn('id', $items)
->update([ ->update([
'reserve_create_at' => Carbon::now(), 'reserve_create_at' => Carbon::now(),
]); ]);
......
...@@ -59,11 +59,12 @@ class AdvertisementsAdd extends Command ...@@ -59,11 +59,12 @@ class AdvertisementsAdd extends Command
$goalAds = DB::table('goal_advertisements') $goalAds = DB::table('goal_advertisements')
->join('advertisements', 'goal_advertisements.advertisement_id', '=', 'advertisements.id') ->join('advertisements', 'goal_advertisements.advertisement_id', '=', 'advertisements.id')
->leftJoin('goal_advertisement_goal_ad_extensions', 'goal_advertisements.id', '=', 'goal_advertisement_goal_ad_extensions.goal_advertisement_id') ->leftJoin('goal_advertisement_goal_ad_extensions', 'goal_advertisements.id', '=', 'goal_advertisement_goal_ad_extensions.goal_advertisement_id')
->leftJoin('goal_ad_extensions', 'goal_advertisements.id', '=', 'goal_ad_extensions.goal_ad_extension_id') ->leftJoin('goal_ad_extensions', 'goal_advertisement_goal_ad_extensions.goal_ad_extension_id', '=', 'goal_ad_extensions.id')
->whereNotExists(function (Builder $query) { ->whereNotExists(function (Builder $query) {
$query->select(DB::raw(1)) $query->select(DB::raw(1))
->from('goal_advertisement_goal_ad_extensions') ->from('goal_advertisement_goal_ad_extensions')
->join('goal_ad_extensions', 'goal_advertisements.id', '=', 'goal_ad_extensions.goal_ad_extension_id') ->join('goal_ad_extensions', 'goal_advertisement_goal_ad_extensions.goal_ad_extension_id', '=', 'goal_ad_extensions.id')
->whereNull('goal_ad_extensions.external_id')
->whereColumn('goal_advertisements.id', 'goal_advertisement_goal_ad_extensions.goal_advertisement_id'); ->whereColumn('goal_advertisements.id', 'goal_advertisement_goal_ad_extensions.goal_advertisement_id');
}) })
->whereNull('advertisements.deleted_at') ->whereNull('advertisements.deleted_at')
......
...@@ -158,14 +158,16 @@ class DictionaryCampaignsSyncByCampaign extends Command ...@@ -158,14 +158,16 @@ class DictionaryCampaignsSyncByCampaign extends Command
//грузим связь объевлений к расширения которых по какой то причне нет в целевых. //грузим связь объевлений к расширения которых по какой то причне нет в целевых.
DB::insert(" DB::insert("
INSERT INTO goal_advertisement_goal_ad_extensions(goal_advertisement_id, goal_ad_extension_id, created_at, updated_at) INSERT INTO goal_advertisement_goal_ad_extensions(goal_advertisement_id, goal_ad_extension_id, created_at, updated_at)
SELECT ae.id, gae.id, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP SELECT ga.id, gae.id, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
FROM advertisement_ad_extensions aae FROM advertisement_ad_extensions aae
INNER JOIN ad_extensions ae on aae.ad_extension_id = ae.id INNER JOIN ad_extensions ae on aae.ad_extension_id = ae.id and ae.deleted_at is null
INNER JOIN goal_ad_extensions gae on ae.id = gae.ad_extension_id INNER JOIN goal_ad_extensions gae on ae.id = gae.ad_extension_id
INNER JOIN advertisements a on aae.advertisement_id = a.id INNER JOIN advertisements a on aae.advertisement_id = a.id and a.deleted_at is null
INNER JOIN goal_advertisements ga on a.id = ga.advertisement_id INNER JOIN goal_advertisements ga on a.id = ga.advertisement_id
INNER JOIN dictionary_campaigns dc on ga.dictionary_campaign_id = dc.id
INNER JOIN dictionaries d on dc.dictionary_id = d.id and d.token_id = gae.token_id
LEFT JOIN goal_advertisement_goal_ad_extensions gagae on gae.id = gagae.goal_ad_extension_id and ga.id = gagae.goal_advertisement_id LEFT JOIN goal_advertisement_goal_ad_extensions gagae on gae.id = gagae.goal_ad_extension_id and ga.id = gagae.goal_advertisement_id
WHERE gagae.ad_extension_id is null WHERE gagae.goal_ad_extension_id is null
"); ");
//грузим объявления которых по какой то причне нет в целевых. //грузим объявления которых по какой то причне нет в целевых.
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace App\Console; namespace App\Console;
use App\Console\Commands\AdExtensionsAdd;
use App\Console\Commands\AdExtensionsLoad; use App\Console\Commands\AdExtensionsLoad;
use App\Console\Commands\AdGroupsAdd; use App\Console\Commands\AdGroupsAdd;
use App\Console\Commands\AdGroupsLoadUpdated; use App\Console\Commands\AdGroupsLoadUpdated;
...@@ -64,6 +65,7 @@ class Kernel extends ConsoleKernel ...@@ -64,6 +65,7 @@ class Kernel extends ConsoleKernel
$schedule->command(AdGroupsLoadKeywords::class)->hourlyAt(30); $schedule->command(AdGroupsLoadKeywords::class)->hourlyAt(30);
$schedule->command(AdExtensionsLoad::class)->hourlyAt(25); $schedule->command(AdExtensionsLoad::class)->hourlyAt(25);
$schedule->command(AdExtensionsAdd::class)->hourlyAt(30);
$schedule->command(AdGroupsAdd::class)->hourlyAt(30); $schedule->command(AdGroupsAdd::class)->hourlyAt(30);
$schedule->command(AdGroupsUpdate::class)->hourlyAt(30); $schedule->command(AdGroupsUpdate::class)->hourlyAt(30);
......
...@@ -486,16 +486,6 @@ class DictionaryCampaign extends Pivot ...@@ -486,16 +486,6 @@ class DictionaryCampaign extends Pivot
return $this->hasMany(GoalKeywordDelete::class, 'dictionary_campaign_id'); return $this->hasMany(GoalKeywordDelete::class, 'dictionary_campaign_id');
} }
public function adExtensions()
{
return $this->hasMany(GoalAdExtension::class, 'dictionary_campaign_id');
}
public function adExtensionsForNotExternalForNotReserveCreate()
{
return $this->adExtensions()->forNotExternal()->forNotReserveCreate();
}
public function goalAdvertisements() public function goalAdvertisements()
{ {
return $this->hasMany(GoalAdvertisement::class, 'dictionary_campaign_id'); return $this->hasMany(GoalAdvertisement::class, 'dictionary_campaign_id');
......
...@@ -16,6 +16,7 @@ class GoalAdExtension extends Pivot ...@@ -16,6 +16,7 @@ class GoalAdExtension extends Pivot
protected $fillable = [ protected $fillable = [
'external_id', 'external_id',
'ad_extension_id', 'ad_extension_id',
'token_id',
'external_upload_at', 'external_upload_at',
'external_updated_at', 'external_updated_at',
'updated_need', 'updated_need',
...@@ -39,6 +40,7 @@ class GoalAdExtension extends Pivot ...@@ -39,6 +40,7 @@ class GoalAdExtension extends Pivot
'id', 'id',
'external_id', 'external_id',
'ad_extension_id', 'ad_extension_id',
'token_id',
'external_upload_at', 'external_upload_at',
'external_updated_at', 'external_updated_at',
'updated_need', 'updated_need',
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace App\Models; namespace App\Models;
use App\Models\Pivots\DictionaryCampaign; use App\Models\Pivots\DictionaryCampaign;
use App\Models\Pivots\GoalAdExtension;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
...@@ -52,6 +53,8 @@ use Illuminate\Database\Eloquent\Model; ...@@ -52,6 +53,8 @@ use Illuminate\Database\Eloquent\Model;
* @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaignsEnabledForExternalSynchronizedUpdatedChildren * @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaignsEnabledForExternalSynchronizedUpdatedChildren
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Dictionary[] $campaignsForManaged * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Dictionary[] $campaignsForManaged
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Dictionary[] $campaignsNotForManaged * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Dictionary[] $campaignsNotForManaged
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Pivots\GoalAdExtension[] $goalAdExtensions
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Pivots\GoalAdExtension[] $goalAdExtensionsForNotExternalForNotReserveCreate
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Limits[] $limits * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Limits[] $limits
* @property-read int|null $limits_count * @property-read int|null $limits_count
* @property string|null $timestamp * @property string|null $timestamp
...@@ -254,4 +257,15 @@ class Tokens extends Model ...@@ -254,4 +257,15 @@ class Tokens extends Model
{ {
return $this->campaigns()->forManaged(false); return $this->campaigns()->forManaged(false);
} }
public function goalAdExtensions()
{
return $this->hasMany(GoalAdExtension::class, 'token_id');
}
public function goalAdExtensionsForNotExternalForNotReserveCreate()
{
return $this->goalAdExtensions()->forNotExternal()->forNotReserveCreate();
}
} }
...@@ -88,7 +88,7 @@ class AddAdExtensions extends DirectRequest ...@@ -88,7 +88,7 @@ class AddAdExtensions extends DirectRequest
public function failed() public function failed()
{ {
GoalAdGroup::whereIn('id', $this->goalAdExtensions->pluck('id')->toArray()) GoalAdExtension::whereIn('id', $this->goalAdExtensions->pluck('id')->toArray())
->update([ ->update([
'reserve_create_at' => null, 'reserve_create_at' => null,
]); ]);
...@@ -113,7 +113,7 @@ class AddAdExtensions extends DirectRequest ...@@ -113,7 +113,7 @@ class AddAdExtensions extends DirectRequest
$this->setParams([ $this->setParams([
'AdExtensions' => $this->goalAdExtensions->map(function (GoalAdExtension $goalAdExtension) use ($variables, &$lists) { 'AdExtensions' => $this->goalAdExtensions->map(function (GoalAdExtension $goalAdExtension) use ($variables, &$lists) {
if (!isset($lists[$goalAdGroup->dictionary_campaign_id])) { if (!isset($lists[$goalAdExtension->dictionary_campaign_id])) {
$list = Variable::getListVariablesByDictionaryCampaign($goalAdExtension->dictionary_campaign_id, $variables); $list = Variable::getListVariablesByDictionaryCampaign($goalAdExtension->dictionary_campaign_id, $variables);
$lists[$goalAdExtension->dictionary_campaign_id] = $list; $lists[$goalAdExtension->dictionary_campaign_id] = $list;
} else { } else {
......
...@@ -57,7 +57,6 @@ class GetAdExtensions extends DirectRequest ...@@ -57,7 +57,6 @@ class GetAdExtensions extends DirectRequest
'external_id' => $external_id, 'external_id' => $external_id,
'callout_text' => $ad_extension['Callout']['CalloutText'], 'callout_text' => $ad_extension['Callout']['CalloutText'],
'associated' => $ad_extension['Associated'] === 'YES', 'associated' => $ad_extension['Associated'] === 'YES',
'state' => $ad_extension['State'],
'status' => $ad_extension['Status'], 'status' => $ad_extension['Status'],
'status_clarification' => $ad_extension['StatusClarification'], 'status_clarification' => $ad_extension['StatusClarification'],
]; ];
...@@ -94,6 +93,9 @@ class GetAdExtensions extends DirectRequest ...@@ -94,6 +93,9 @@ class GetAdExtensions extends DirectRequest
"Types" => [ "Types" => [
AdExtension::TYPE_CALLOUT, AdExtension::TYPE_CALLOUT,
], ],
"States" => [
AdExtension::STATE_ON,
],
], ],
"FieldNames" => [ "FieldNames" => [
"Id", "Type", "Status", "Id", "Type", "Status",
......
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddGoalAdExtensionsReserveColumn extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('goal_ad_extensions', function (Blueprint $table) {
$table->timestamp('reserve_create_at')->nullable();
$table->timestamp('reserve_update_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!