Commit bb0c1463 by Vladislav

#19458 Обновление справочника городов.

1 parent b94fb42e
......@@ -6,9 +6,8 @@ use App\Models\Tokens;
use App\Service\API\API;
use App\Service\Direct\CheckDictionaries;
use App\Service\Direct\GetCampaigns;
use App\Service\Requests\Direct\GetDictionaries;
use App\Service\Requests\APIRequest;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Bus;
class DictionariesLoad extends Command
{
......@@ -39,18 +38,22 @@ class DictionariesLoad extends Command
/**
* Execute the console command.
*
* @return void
* @return int
* @throws \Exception
*/
public function handle()
{
$token = Tokens::where('type', Tokens::MAIN)->first();
if (!$token){
if (!$token) {
throw new \Exception('Не найден токен блин');
}
GetDictionaries::getInstance(API::YANDEX)
->setToken($token)
$factory = APIRequest::getInstance(API::YANDEX);
$factory->setToken($token);
$factory->getRequest('dictionaries', 'get')
->call();
return 0;
}
}
......@@ -3,7 +3,9 @@
namespace App\Service\Requests\Direct;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Dictionary;
use App\Service\Requests\DirectRequest;
use Illuminate\Support\Facades\Log;
class GetDictionaries extends DirectRequest
{
......@@ -19,8 +21,34 @@ class GetDictionaries extends DirectRequest
function handle($response)
{
if ($this->next) {
$this->next->call(null, $response);
try {
$data = collect();
foreach ($response['result']['GeoRegions'] as $region) {
$data->push([
'region_id' => $region['GeoRegionId'],
'parent_id' => $region['ParentId'],
'name' => $region['GeoRegionName'],
'type' => $region['GeoRegionType'],
]);
}
foreach ($data->chunk(100) as $list) {
Dictionary::upsert($list->toArray(), [
'region_id'
], [
'parent_id',
'name',
'type',
]);
}
if ($this->next) {
$this->next->call(null, $response);
}
} catch (\Exception $e) {
Log::debug($e);
}
}
......@@ -28,5 +56,11 @@ class GetDictionaries extends DirectRequest
{
$this->setService('dictionaries');
$this->setMethod('get');
$params = [
'DictionaryNames' => [
"GeoRegions",
],
];
$this->setParams($params);
}
}
<?php
namespace App\Service\Requests;
use App\Models\Tokens;
/**
* Class DirectRequest
* @package App\Service\Requests
......
......@@ -26,7 +26,8 @@
"nunomaduro/collision": "^5.0",
"phpunit/phpunit": "^9.3",
"reinink/remember-query-strings": "^0.1.0",
"tightenco/ziggy": "^0.8.0"
"tightenco/ziggy": "^0.8.0",
"ext-json": "*"
},
"config": {
"optimize-autoloader": true,
......
......@@ -15,7 +15,10 @@ class CreateDictionariesTable extends Migration
{
Schema::create('dictionaries', function (Blueprint $table) {
$table->id();
$table->bigInteger('region_id')->unique();
$table->bigInteger('parent_id')->nullable();
$table->string('name', 255);
$table->string('type', 255);
$table->timestamps();
});
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!