Commit 9029a151 by Vladislav

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

1 parent 4d090b7f
...@@ -24,7 +24,7 @@ class AdExtensionsAdd extends Command ...@@ -24,7 +24,7 @@ class AdExtensionsAdd extends Command
* *
* @var string * @var string
*/ */
protected $description = 'Добавление не расширений с целевого аккаунта'; protected $description = 'Добавление не созданных расширений с целевого аккаунта';
/** /**
* Create a new command instance. * Create a new command instance.
......
<?php
namespace App\Console\Commands;
use App\Models\Pivots\GoalAdExtension;
use App\Models\Pivots\GoalNegativeKeywordSharedSet;
use App\Models\Tokens;
use App\Service\API\API;
use App\Service\Requests\APIRequest;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Relations\HasMany;
class NegativeKeywordSharedSetsAdd extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'negativekeywordsharedsets:add';
/**
* 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()
{
$tokens = Tokens::whereHas('goalNegativeKeywordSharedSetsForNotExternalForNotReserveCreate.negativeKeywordSharedSet')
->where('type', '!=', Tokens::MAIN)
->get();
foreach ($tokens as $token) {
$token->load([
'goalNegativeKeywordSharedSetsForNotExternalForNotReserveCreate' => function (HasMany $query) {
return $query->has('negativeKeywordSharedSet');
},
'goalNegativeKeywordSharedSetsForNotExternalForNotReserveCreate.negativeKeywordSharedSet',
]);
$factory = APIRequest::getInstance(API::YANDEX);
$factory->setToken($token);
$goalNegativeKeywordSharedSets = $token->goalNegativeKeywordSharedSetsForNotExternalForNotReserveCreate;
foreach (array_chunk($goalNegativeKeywordSharedSets->pluck('id')->toArray(), 1000) as $items) {
GoalNegativeKeywordSharedSet::whereIn('id', $items)
->update([
'reserve_create_at' => Carbon::now(),
]);
}
$factory->getRequest('NegativeKeywordSharedSets', 'add')
->call([
'goalNegativeKeywordSharedSets' => $goalNegativeKeywordSharedSets,
]);
}
return 0;
}
}
...@@ -4,6 +4,7 @@ namespace App\Models; ...@@ -4,6 +4,7 @@ namespace App\Models;
use App\Models\Pivots\DictionaryCampaign; use App\Models\Pivots\DictionaryCampaign;
use App\Models\Pivots\GoalAdExtension; use App\Models\Pivots\GoalAdExtension;
use App\Models\Pivots\GoalNegativeKeywordSharedSet;
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;
...@@ -55,6 +56,8 @@ use Illuminate\Database\Eloquent\Model; ...@@ -55,6 +56,8 @@ use Illuminate\Database\Eloquent\Model;
* @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[] $goalAdExtensions
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Pivots\GoalAdExtension[] $goalAdExtensionsForNotExternalForNotReserveCreate * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Pivots\GoalAdExtension[] $goalAdExtensionsForNotExternalForNotReserveCreate
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Pivots\GoalNegativeKeywordSharedSet[] $goalNegativeKeywordSharedSets
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Pivots\GoalNegativeKeywordSharedSet[] $goalNegativeKeywordSharedSetsForNotExternalForNotReserveCreate
* @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
...@@ -268,4 +271,14 @@ class Tokens extends Model ...@@ -268,4 +271,14 @@ class Tokens extends Model
return $this->goalAdExtensions()->forNotExternal()->forNotReserveCreate(); return $this->goalAdExtensions()->forNotExternal()->forNotReserveCreate();
} }
public function goalNegativeKeywordSharedSets()
{
return $this->hasMany(GoalNegativeKeywordSharedSet::class, 'token_id');
}
public function goalNegativeKeywordSharedSetsForNotExternalForNotReserveCreate()
{
return $this->goalNegativeKeywordSharedSets()->forNotExternal()->forNotReserveCreate();
}
} }
<?php
namespace App\Service\Requests\Direct;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\NegativeKeywordSharedSet;
use App\Models\Pivots\GoalNegativeKeywordSharedSet;
use App\Models\Variable;
use App\Service\Contract\APIRequest;
use App\Service\Requests\DirectRequest;
use App\Service\StrReplaceByVariables;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Log;
class AddNegativeKeywordSharedSets extends DirectRequest
{
protected $max_count = 30;
protected $timestamp;
/* @var Collection|NegativeKeywordSharedSet[] */
protected $goalNegativeKeywordSharedSets;
public function call($params = null)
{
$this->requestPrepare($params);
$process = new ProcessCallLimitedAPI($this);
dispatch($process)->onQueue('limits');
}
public function getObjectsCount()
{
return count($this->getParams()['NegativeKeywordSharedSets']);
}
public function slice($maxObjects): ?APIRequest
{
$splinter = $this->sliceByKey($maxObjects, 'NegativeKeywordSharedSets');
$splinter->putParams([
'goalNegativeKeywordSharedSets' => $this->goalNegativeKeywordSharedSets->slice($maxObjects)->values(),
]);
$this->putParams([
'goalNegativeKeywordSharedSets' => $this->goalNegativeKeywordSharedSets->slice(0, $maxObjects),
]);
return $splinter;
}
public function handle($response)
{
try {
if (!isset($response['result']['AddResults'])) {
return;
}
foreach ($response['result']['AddResults'] as $key => $add_result) {
$goalNegativeKeywordSharedSet = $this->goalNegativeKeywordSharedSets->get($key);
if (!isset($add_result['Id'])) {
Log::debug("AddNegativeKeywordSharedSet, empty Id");
Log::debug($add_result);
Log::debug($this->getParams()['NegativeKeywordSharedSets'][$key]);
$goalNegativeKeywordSharedSet->update([
'reserve_create_at' => null,
]);
continue;
}
$external_id = (string)$add_result['Id'];
$goalNegativeKeywordSharedSet->update([
'external_id' => $external_id,
'external_upload_at' => Carbon::now(),
'reserve_create_at' => null,
]);
}
} catch (\Exception $e) {
Log::debug($e);
throw $e;
}
}
public function failed()
{
GoalNegativeKeywordSharedSet::whereIn('id', $this->goalNegativeKeywordSharedSets->pluck('id')->toArray())
->update([
'reserve_create_at' => null,
]);
}
public function putParams($params)
{
$this->goalNegativeKeywordSharedSets = $params['goalNegativeKeywordSharedSets'];
}
private function requestPrepare($params)
{
$this->setService('NegativeKeywordSharedSets');
$this->setMethod('add');
$this->putParams($params);
$variables = Variable::all();
$lists = [];
$this->setParams([
'NegativeKeywordSharedSets' => $this->goalNegativeKeywordSharedSets->map(function (GoalNegativeKeywordSharedSet $goalNegativeKeywordSharedSet) use ($variables, &$lists) {
if (!isset($lists[$goalNegativeKeywordSharedSet->dictionary_campaign_id])) {
$list = Variable::getListVariablesByDictionaryCampaign($goalNegativeKeywordSharedSet->dictionary_campaign_id, $variables);
$lists[$goalNegativeKeywordSharedSet->dictionary_campaign_id] = $list;
} else {
$list = $lists[$goalNegativeKeywordSharedSet->dictionary_campaign_id];
}
return [
'Name' => StrReplaceByVariables::getInstance($goalNegativeKeywordSharedSet->negativeKeywordSharedSet->name, $list)->get(),
'NegativeKeywords' => array_map(function ($negative_keyword) use ($list44) {
return StrReplaceByVariables::getInstance($negative_keyword, $list)->get();
}, $goalNegativeKeywordSharedSet->negativeKeywordSharedSet->negative_keywords),
];
})->toArray(),
]);
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!