Commit 5e2f8c4b by Vladislav

#19495 Первоначальная загрузка объявлений.

1 parent 8c3a693c
<?php
namespace App\Models;
use App\Models\Pivots\GoalAdGroup;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;
class Advertisement extends Model
{
use SoftDeletes;
const STATUS_ACCEPTED = 'ACCEPTED';
const STATUS_DRAFT = 'DRAFT';
const STATUS_MODERATION = 'MODERATION';
const STATUS_PREACCEPTED = 'PREACCEPTED';
const STATUS_REJECTED = 'REJECTED';
const STATUS_UNKNOWN = 'UNKNOWN';
const STATE_OFF = 'OFF';
const STATE_ON = 'ON';
const STATE_SUSPENDED = 'SUSPENDED';
const STATE_OFF_BY_MONITORING = 'OFF_BY_MONITORING';
const STATE_ARCHIVED = 'ARCHIVED';
const STATE_UNKNOWN = 'UNKNOWN';
const AGE_LABEL_0 = 'AGE_0';
const AGE_LABEL_6 = 'AGE_6';
const AGE_LABEL_12 = 'AGE_12';
const AGE_LABEL_16 = 'AGE_16';
const AGE_LABEL_18 = 'AGE_18';
const AGE_LABEL_MONTHS_0 = 'MONTHS_0';
const AGE_LABEL_MONTHS_1 = 'MONTHS_1';
const AGE_LABEL_MONTHS_2 = 'MONTHS_2';
const AGE_LABEL_MONTHS_3 = 'MONTHS_3';
const AGE_LABEL_MONTHS_4 = 'MONTHS_4';
const AGE_LABEL_MONTHS_5 = 'MONTHS_5';
const AGE_LABEL_MONTHS_6 = 'MONTHS_6';
const AGE_LABEL_MONTHS_7 = 'MONTHS_7';
const AGE_LABEL_MONTHS_8 = 'MONTHS_8';
const AGE_LABEL_MONTHS_9 = 'MONTHS_9';
const AGE_LABEL_MONTHS_10 = 'MONTHS_10';
const AGE_LABEL_MONTHS_11 = 'MONTHS_11';
const AGE_LABEL_MONTHS_12 = 'MONTHS_12';
const TYPE_TEXT_AD = 'TEXT_AD';
const TYPE_MOBILE_APP_AD = 'MOBILE_APP_AD';
const TYPE_DYNAMIC_TEXT_AD = 'DYNAMIC_TEXT_AD';
const TYPE_IMAGE_AD = 'IMAGE_AD';
const TYPE_CPC_VIDEO_AD = 'CPC_VIDEO_AD';
const TYPE_CPM_BANNER_AD = 'CPM_BANNER_AD';
const TYPE_CPM_VIDEO_AD = 'CPM_VIDEO_AD';
const TYPE_SMART_AD = 'SMART_AD';
const SUB_TYPE_NONE = 'NONE';
const SUB_TYPE_TEXT_IMAGE_AD = 'TEXT_IMAGE_AD';
const SUB_TYPE_MOBILE_APP_IMAGE_AD = 'MOBILE_APP_IMAGE_AD';
const SUB_TYPE_MOBILE_APP_CPC_VIDEO_AD_BUILDER_AD = 'MOBILE_APP_CPC_VIDEO_AD_BUILDER_AD';
const SUB_TYPE_TEXT_AD_BUILDER_AD = 'TEXT_AD_BUILDER_AD';
const SUB_TYPE_MOBILE_APP_AD_BUILDER_AD = 'MOBILE_APP_AD_BUILDER_AD';
protected $fillable = [
'external_id',
'campaign_external_id',
'ad_group_external_id',
'campaign_id',
'ad_group_id',
'status',
'state',
'status_clarification',
'ad_categories',
'age_label',
'title',
'title2',
'text',
'href',
'mobile',
'display_domain',
'display_url_path',
'v_card_id',
'ad_image_hash',
'site_link_set_id',
'display_url_path_moderation',
'v_card_moderation',
'site_links_moderation',
'ad_image_moderation',
'ad_extensions',
'video_extension',
'price_extension',
'turbo_page_id',
'turbo_page_moderation',
'business_id',
'prefer_v_card_over_business',
];
protected $casts = [
'ad_categories' => 'array',
'mobile' => 'boolean',
'display_url_path_moderation' => 'json',
'v_card_moderation' => 'json',
'site_links_moderation' => 'json',
'ad_image_moderation' => 'json',
'ad_extensions' => 'json',
'video_extension' => 'json',
'price_extension' => 'json',
'turbo_page_moderation' => 'json',
'prefer_v_card_over_business' => 'boolean',
];
/**
* @return Collection
*/
static public function getPropertiesWatch()
{
return collect([
]);
}
public static function boot()
{
parent::boot();
static::created(function (Advertisement $advertisement) {
//
});
static::updated(function (Advertisement $advertisement) {
//
});
}
/**
* @param Builder $query
* @return Builder
*/
public function scopeForUpdatedSelf($query)
{
return $query->whereNotNull("{$query->getModel()->getTable()}.updated_self");
}
public function groups()
{
return $this->hasMany(AdGroup::class, 'ad_group_id');
}
public function campaign()
{
return $this->belongsTo(Campaigns::class, 'campaign_id');
}
}
<?php
namespace App\Service\Requests\Direct;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\AdGroup;
use App\Models\Advertisement;
use App\Service\Contract\APIRequest;
use App\Service\Requests\DirectRequest;
use Illuminate\Support\Facades\Log;
class GetAds extends DirectRequest
{
protected $max_count = -1;
protected $max_count_CampaignIds = 10;
protected $max_count_AdGroupIds = 1000;
protected $max_count_Ids = 10000;
function call($params = null)
{
$this->requestPrepare($params);
$process = new ProcessCallLimitedAPI($this);
dispatch($process)->onQueue('limits');
}
public function getObjectsCount()
{
$params = $this->getParams();
if (isset($params['SelectionCriteria']['CampaignIds'])) {
return count($params['SelectionCriteria']['CampaignIds']);
}
if (isset($params['SelectionCriteria']['AdGroupIds'])) {
return count($params['SelectionCriteria']['AdGroupIds']);
}
if (isset($params['SelectionCriteria']['Ids'])) {
return count($params['SelectionCriteria']['Ids']);
}
return -1;
}
public function slice($maxObjects): ?APIRequest
{
$params = $this->getParams();
if (isset($params['SelectionCriteria']['CampaignIds'])) {
return $this->sliceByKey($maxObjects, ['SelectionCriteria', 'CampaignIds']);
}
if (isset($params['SelectionCriteria']['AdGroupIds'])) {
return $this->sliceByKey($maxObjects, ['SelectionCriteria', 'AdGroupIds']);
}
if (isset($params['SelectionCriteria']['Ids'])) {
return $this->sliceByKey($maxObjects, ['SelectionCriteria', 'Ids']);
}
return null;
}
function handle($response)
{
try {
$external_ids = [];
if (!isset($response['result']['Ads'])) {
return;
}
foreach ($response['result']['Ads'] as $ad) {
$adGroupId = (string)$ad['AdGroupId'];
if (isset($external_ids[$adGroupId])) {
continue;
}
$external_ids[$adGroupId] = true;
}
if (!count($external_ids)) {
return;
}
if ($this->getToken()->isMain()) {
$ad_groups = AdGroup::whereIn('external_id', array_keys($external_ids))
->get()
->keyBy('external_id');
} else {
//
}
$ids = [];
foreach ($response['result']['Ads'] as $ad) {
$ad_group = $ad_groups->get((string)$ad['AdGroupId']);
if (!$ad_group)
continue;
$external_id = (string)$ad['Id'];
if ($this->getToken()->isMain()) {
$data = [
'external_id' => $external_id,
'campaign_external_id' => $ad_group->campaign->external_id,
'ad_group_external_id' => $ad_group->external_id,
'ad_group_id' => $ad_group->getKey(),
'campaign_id' => $ad_group->campaign->getKey(),
'status' => $ad['Status'],
'state' => $ad['State'],
'status_clarification' => $ad['StatusClarification'],
'ad_categories' => $ad['AdCategories']['Items'] ?? [],
'age_label' => $ad['AgeLabel'],
'type' => $ad['Type'],
'sub_type' => $ad['Subtype'],
'title' => $ad['TextAd']['Title'],
'title2' => $ad['TextAd']['Title2'],
'text' => $ad['TextAd']['Text'],
'href' => $ad['TextAd']['Href'],
'mobile' => $ad['TextAd']['Mobile'] === 'YES',
'display_domain' => $ad['TextAd']['DisplayDomain'],
'display_url_path' => $ad['TextAd']['DisplayUrlPath'],
'v_card_id' => $ad['TextAd']['VCardId'],
'ad_image_hash' => $ad['TextAd']['AdImageHash'],
'site_link_set_id' => $ad['TextAd']['SitelinkSetId'],
'display_url_path_moderation' => $ad['TextAd']['DisplayUrlPathModeration'],
'v_card_moderation' => $ad['TextAd']['VCardModeration'],
'site_links_moderation' => $ad['TextAd']['SitelinksModeration'],
'ad_image_moderation' => $ad['TextAd']['AdImageModeration'],
'ad_extensions' => $ad['TextAd']['AdExtensions'],
'video_extension' => $ad['TextAd']['VideoExtension'],
'price_extension' => $ad['TextAd']['PriceExtension'],
'turbo_page_id' => $ad['TextAd']['TurboPageId'],
'turbo_page_moderation' => $ad['TextAd']['TurboPageModeration'],
'business_id' => $ad['TextAd']['BusinessId'],
'prefer_v_card_over_business' => $ad['TextAd']['PreferVCardOverBusiness'],
];
Advertisement::updateOrCreate([
'external_id' => $external_id
], $data);
} else {
//
}
}
} catch (\Exception $e) {
Log::debug($e);
throw $e;
}
}
private function requestPrepare($filter)
{
$this->setService('Ads');
$this->setMethod('get');
$params = [
"FieldNames" => [
"AdCategories", "AgeLabel", "AdGroupId",
"CampaignId", "Id", "State", "Status",
"StatusClarification", "Type", "Subtype"
],
];
if (isset($filter['CampaignIds'])) {
$this->max_count = $this->max_count_CampaignIds;
$params['SelectionCriteria'] = [
'CampaignIds' => $filter['CampaignIds'],
];
}
if (isset($filter['AdGroupIds'])) {
$this->max_count = $this->max_count_AdGroupIds;
$params['SelectionCriteria'] = [
'AdGroupIds' => $filter['AdGroupIds'],
];
}
if (isset($filter['Ids'])) {
$this->max_count = $this->max_count_Ids;
$params['SelectionCriteria'] = [
'Ids' => $filter['Ids'],
];
}
$this->setParams($params);
}
}
<?php
use App\Models\Advertisement;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateAdvertisementsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('advertisements', function (Blueprint $table) {
$table->id();
$table->bigInteger('external_id')->nullable();
$table->bigInteger('campaign_external_id')->nullable();
$table->bigInteger('ad_group_external_id')->nullable();
$table->bigInteger('campaign_id')->unsigned();
$table->bigInteger('ad_group_id')->unsigned();
$table->enum('status', [
Advertisement::STATUS_ACCEPTED,
Advertisement::STATUS_DRAFT,
Advertisement::STATUS_MODERATION,
Advertisement::STATUS_PREACCEPTED,
Advertisement::STATUS_REJECTED,
Advertisement::STATUS_UNKNOWN,
])->nullable();
$table->enum('state', [
Advertisement::STATE_OFF,
Advertisement::STATE_ON,
Advertisement::STATE_SUSPENDED,
Advertisement::STATE_OFF_BY_MONITORING,
Advertisement::STATE_ARCHIVED,
Advertisement::STATE_UNKNOWN,
])->nullable();
$table->string('status_clarification')->nullable();
$table->json('ad_categories')->nullable();
$table->enum('age_label', [
Advertisement::AGE_LABEL_0,
Advertisement::AGE_LABEL_6,
Advertisement::AGE_LABEL_12,
Advertisement::AGE_LABEL_16,
Advertisement::AGE_LABEL_18,
Advertisement::AGE_LABEL_MONTHS_0,
Advertisement::AGE_LABEL_MONTHS_1,
Advertisement::AGE_LABEL_MONTHS_2,
Advertisement::AGE_LABEL_MONTHS_3,
Advertisement::AGE_LABEL_MONTHS_4,
Advertisement::AGE_LABEL_MONTHS_5,
Advertisement::AGE_LABEL_MONTHS_6,
Advertisement::AGE_LABEL_MONTHS_7,
Advertisement::AGE_LABEL_MONTHS_8,
Advertisement::AGE_LABEL_MONTHS_9,
Advertisement::AGE_LABEL_MONTHS_10,
Advertisement::AGE_LABEL_MONTHS_11,
Advertisement::AGE_LABEL_MONTHS_12,
])->nullable();
$table->enum('type', [
Advertisement::TYPE_TEXT_AD,
Advertisement::TYPE_MOBILE_APP_AD,
Advertisement::TYPE_DYNAMIC_TEXT_AD,
Advertisement::TYPE_IMAGE_AD,
Advertisement::TYPE_CPC_VIDEO_AD,
Advertisement::TYPE_CPM_BANNER_AD,
Advertisement::TYPE_CPM_VIDEO_AD,
Advertisement::TYPE_SMART_AD,
])->nullable();
$table->enum('sub_type', [
Advertisement::SUB_TYPE_NONE,
Advertisement::SUB_TYPE_TEXT_IMAGE_AD,
Advertisement::SUB_TYPE_MOBILE_APP_IMAGE_AD,
Advertisement::SUB_TYPE_MOBILE_APP_CPC_VIDEO_AD_BUILDER_AD,
Advertisement::SUB_TYPE_TEXT_AD_BUILDER_AD,
Advertisement::SUB_TYPE_MOBILE_APP_AD_BUILDER_AD,
])->nullable();
$table->string('title')->nullable();
$table->string('title2')->nullable();
$table->string('text')->nullable();
$table->string('href')->nullable();
$table->boolean('mobile')->nullable();
$table->string('display_domain')->nullable();
$table->string('display_url_path')->nullable();
$table->bigInteger('v_card_id')->nullable();
$table->string('ad_image_hash')->nullable();
$table->string('site_link_set_id')->nullable();
$table->json('display_url_path_moderation')->nullable();
$table->json('v_card_moderation')->nullable();
$table->json('site_links_moderation')->nullable();
$table->json('ad_image_moderation')->nullable();
$table->json('ad_extensions')->nullable();
$table->json('video_extension')->nullable();
$table->json('price_extension')->nullable();
$table->bigInteger('turbo_page_id')->nullable();
$table->bigInteger('turbo_page_moderation')->nullable();
$table->bigInteger('business_id')->nullable();
$table->boolean('prefer_v_card_over_business')->nullable();
$table->timestamp('updated_self')->nullable();
$table->softDeletes();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('advertisements');
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!