Commit 4d090b7f by Vladislav

#20346 Загрузка наборов минус фраз

1 parent 71665f50
......@@ -110,6 +110,16 @@ class DictionaryCampaignsSyncByCampaign extends Command
");
}
//грузим наборы минус-фраз которых по какой то причне нет в целевых.
DB::insert("
INSERT INTO goal_negative_keyword_shared_sets(negative_keyword_shared_set_id, token_id, created_at, updated_at)
SELECT nkss.id, t.id, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
FROM negative_keyword_shared_sets nkss
INNER JOIN tokens t on t.type != '" . Tokens::MAIN . "'
LEFT JOIN goal_negative_keyword_shared_sets gnkss on nkss.id = gnkss.negative_keyword_shared_set_id and t.id = gnkss.token_id
WHERE gnkss.id is null and nkss.deleted_at is null
");
//грузим группы которых по какой то причне нет в целевых.
DB::insert("
INSERT INTO goal_ad_groups(ad_group_id, dictionary_campaign_external_id, dictionary_campaign_id, name, negative_keywords, created_at, updated_at)
......
<?php
namespace App\Console\Commands;
use App\Models\Campaigns;
use App\Models\Tokens;
use App\Service\API\API;
use App\Service\Requests\APIRequest;
use Illuminate\Console\Command;
class NegativeKeywordSharedSetsLoad extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'negativekeywordsharedsets:load';
/**
* 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
* @throws \Exception
*/
public function handle()
{
$token = Tokens::where('type', Tokens::MAIN)->first();
if (!$token) {
throw new \Exception('Не найден токен блин');
}
$factory = APIRequest::getInstance(API::YANDEX);
$factory->setToken($token);
$factory->getRequest('NegativeKeywordSharedSets', 'get')
->call();
return 0;
}
}
<?php
namespace App\Models;
use App\Models\Pivots\GoalNegativeKeywordSharedSet;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;
class NegativeKeywordSharedSet extends Model
{
use SoftDeletes;
protected $table = 'negative_keyword_shared_sets';
protected $fillable = [
'external_id',
'name',
'negative_keywords',
'associated',
];
protected $casts = [
'negative_keywords' => 'array',
'associated' => 'boolean',
];
/**
* @return Collection
*/
static public function getPropertiesWatch()
{
return collect([
'name',
'negative_keywords',
]);
}
public function goalNegativeKeywordSharedSet()
{
return $this->hasMany(GoalNegativeKeywordSharedSet::class, 'negative_keyword_shared_set_id');
}
}
<?php
namespace App\Models\Pivots;
use App\Models\AdExtension;
use App\Models\NegativeKeywordSharedSet;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\SoftDeletes;
class GoalNegativeKeywordSharedSet extends Pivot
{
use SoftDeletes;
protected $table = 'goal_negative_keyword_shared_sets';
protected $fillable = [
'external_id',
'negative_keyword_shared_set_id',
'token_id',
'external_upload_at',
'external_updated_at',
'updated_need',
'reserve_create_at',
'reserve_update_at',
];
protected $casts = [
'external_upload_at' => 'datetime',
'external_updated_at' => 'datetime',
'updated_need' => 'datetime',
'reserve_create_at' => 'datetime',
'reserve_update_at' => 'datetime',
];
public $incrementing = true;
static public function getWithPivot()
{
return [
'id',
'external_id',
'negative_keyword_shared_set_id',
'token_id',
'external_upload_at',
'external_updated_at',
'updated_need',
'reserve_create_at',
'reserve_update_at',
];
}
/**
* @param Builder $query
* @return Builder
*/
public function scopeForExternal($query)
{
return $query->whereNotNull('external_id');
}
/**
* @param Builder $query
* @return Builder
*/
public function scopeForNotExternal($query)
{
return $query->whereNull('external_id');
}
/**
* @param Builder $query
* @return Builder
*/
public function scopeForNotReserveCreate($query)
{
return $query->whereNull('reserve_create_at');
}
/**
* @param Builder $query
* @return Builder
*/
public function scopeForNotReserveUpdate($query)
{
return $query->whereNull('reserve_update_at');
}
/**
* @param Builder $query
* @return Builder
*/
public function scopeNeedUpdated($query)
{
return $query->whereNotNull('updated_need');
}
public function negativeKeywordSharedSet()
{
return $this->belongsTo(NegativeKeywordSharedSet::class, 'negative_keyword_shared_set_id');
}
}
<?php
namespace App\Service\Requests\Direct;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\AdExtension;
use App\Models\AdGroup;
use App\Models\Advertisement;
use App\Models\NegativeKeywordSharedSet;
use App\Service\Contract\APIRequest;
use App\Service\Requests\DirectRequest;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
class GetNegativeKeywordSharedSets extends DirectRequest
{
protected $max_count = -1;
protected $max_count_Ids = 30;
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']['Ids'])) {
return count($params['SelectionCriteria']['Ids']);
}
return -1;
}
public function slice($maxObjects): ?APIRequest
{
$params = $this->getParams();
if (isset($params['SelectionCriteria']['Ids'])) {
return $this->sliceByKey($maxObjects, ['SelectionCriteria', 'Ids']);
}
return null;
}
function handle($response)
{
try {
dd($response);
if (isset($response['result']['NegativeKeywordSharedSets'])) {
foreach ($response['result']['NegativeKeywordSharedSets'] as $negative_keyword_shared_sets) {
$external_id = (string)$negative_keyword_shared_sets['Id'];
if ($this->getToken()->isMain()) {
$data = [
'external_id' => $external_id,
'name' => $negative_keyword_shared_sets['Name'],
'negative_keywords' => $negative_keyword_shared_sets['NegativeKeywords'],
'associated' => $negative_keyword_shared_sets['Associated'] === 'YES',
];
$adExtension = NegativeKeywordSharedSet::updateOrCreate([
'external_id' => $external_id
], $data);
if ($adExtension->wasChanged($adExtension::getPropertiesWatch()->toArray())) {
$adExtension->goalNegativeKeywordSharedSet()->forExternal()->update([
'updated_need' => Carbon::now(),
]);
}
} else {
//
}
}
}
} catch (\Exception $e) {
Log::debug($e);
throw $e;
}
}
private function requestPrepare($filter)
{
$this->setService('NegativeKeywordSharedSets');
$this->setMethod('get');
$params = [
"FieldNames" => [
"Id", "Name", "NegativeKeywords",
"Associated",
],
];
if (isset($filter['Ids'])) {
$this->max_count = $this->max_count_Ids;
$params['SelectionCriteria'] = [
'Ids' => $filter['Ids'],
];
}
$this->setParams($params);
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateNegativeKeywordSharedSetsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('negative_keyword_shared_sets', function (Blueprint $table) {
$table->id();
$table->bigInteger('external_id');
$table->string('name');
$table->json('negative_keywords');
$table->boolean('associated');
$table->softDeletes();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('negative_keyword_shared_sets');
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateGoalNegativeKeywordSharedSetsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('goal_negative_keyword_shared_sets', function (Blueprint $table) {
$table->id();
$table->bigInteger('external_id')->nullable();
$table->bigInteger('negative_keyword_shared_set_id')->unsigned();
$table->bigInteger('token_id')->unsigned();
$table->timestamp('external_upload_at')->nullable();
$table->timestamp('external_updated_at')->nullable();
$table->timestamp('updated_need')->nullable();
$table->timestamp('reserve_create_at')->nullable();
$table->timestamp('reserve_update_at')->nullable();
$table->foreign('negative_keyword_shared_set_id', '`gnkss_nkss_id_foreign')->references('id')->on('negative_keyword_shared_sets')
->cascadeOnDelete();
$table->foreign('token_id')->references('id')->on('tokens')
->cascadeOnDelete();
$table->softDeletes();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('goal_negative_keyword_shared_sets');
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!