105 lines
3.1 KiB
PHP
105 lines
3.1 KiB
PHP
<?php
|
||
|
||
namespace App\Command;
|
||
|
||
use App\Entity\Filial;
|
||
use Symfony\Component\Console\Attribute\AsCommand;
|
||
use Symfony\Component\Console\Command\Command;
|
||
use Symfony\Component\Console\Input\InputInterface;
|
||
use Symfony\Component\Console\Output\OutputInterface;
|
||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||
use Doctrine\ORM\EntityManagerInterface;
|
||
use App\Service\Translite\Interfaces\TransliteServiceInterface;
|
||
use Psr\Log\LoggerInterface;
|
||
|
||
#[AsCommand(
|
||
name: 'upload:filials',
|
||
description: 'Обнвление филиалов',
|
||
)]
|
||
class UploadFilialsCommand extends Command
|
||
{
|
||
public function __construct(
|
||
private LoggerInterface $logger,
|
||
private EntityManagerInterface $entityManager,
|
||
private HttpClientInterface $client,
|
||
private TransliteServiceInterface $transliteService,
|
||
private string $widgetApiUrl,
|
||
)
|
||
{
|
||
parent::__construct();
|
||
}
|
||
|
||
protected function configure(): void { }
|
||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||
{
|
||
$this->logger->info('Началось обнвление филиалов');
|
||
|
||
$io = new SymfonyStyle($input, $output);
|
||
|
||
$response = $this->client->request('GET', '/filials/list', [
|
||
'verify_peer' => false,
|
||
'verify_host' => false,
|
||
'base_uri' => $this->widgetApiUrl,
|
||
'headers' => [
|
||
'Content-Type' => 'application/json',
|
||
'User-Agent' => 'sovamed_bot'
|
||
],
|
||
]);
|
||
|
||
$response = $response->toArray();
|
||
|
||
$io->info('load:' . count($response['data']));
|
||
foreach ($response['data'] as $item) {
|
||
$filial = $this->entityManager->getRepository(Filial::class)
|
||
->findOneBy(['fid' => $item['id']]);
|
||
|
||
if (is_null($filial)) {
|
||
$filial = new Filial();
|
||
}
|
||
|
||
preg_match('/(ул\.\s*[А-я]+(?:\s+[А-я]+)*)\s*,?\s*д\.\s*(\S+)/u', $item['address'], $matches);
|
||
|
||
if (isset($matches[1]) && isset($matches[2])) {
|
||
$street = $matches[1];
|
||
$house = $matches[2];
|
||
|
||
$filial->setShortName("$street,$house");
|
||
}
|
||
|
||
$filial
|
||
->setFid($item['id'])
|
||
->setName($item['name'])
|
||
->setAddress($item['address'])
|
||
->setActive(true)
|
||
->setRegionId($this->findRegionId($item['address']))
|
||
;
|
||
|
||
$this->entityManager->persist($filial);
|
||
}
|
||
|
||
$this->entityManager->flush();
|
||
$io->success('loaded');
|
||
|
||
return Command::SUCCESS;
|
||
}
|
||
|
||
private function findRegionId($address) {
|
||
$cities = [
|
||
91 => "Саратов",
|
||
92 => "Волгоград",
|
||
93 => "Воронеж",
|
||
94 => "Краснодар",
|
||
];
|
||
|
||
foreach ($cities as $key => $city) {
|
||
if (stripos($address, $city) !== false) {
|
||
return $key;
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
}
|