77 lines
2.2 KiB
PHP
77 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Command;
|
|
|
|
use App\Entity\PriceDepartment;
|
|
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;
|
|
|
|
#[AsCommand(
|
|
name: 'upload:priceDep',
|
|
description: 'Обновление отделений для услуг',
|
|
)]
|
|
class UploadPriceDepCommand extends Command
|
|
{
|
|
public function __construct(
|
|
private EntityManagerInterface $entityManager,
|
|
private HttpClientInterface $client,
|
|
private string $widgetApiUrl,
|
|
)
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
protected function configure(): void { }
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
{
|
|
$io = new SymfonyStyle($input, $output);
|
|
|
|
$response = $this->client->request('GET', '/pricelist/departments', [
|
|
'verify_peer' => false,
|
|
'verify_host' => false,
|
|
'base_uri' => $this->widgetApiUrl,
|
|
'headers' => [
|
|
'Content-Type' => 'application/json',
|
|
'User-Agent' => 'sovamed_bot'
|
|
],
|
|
]);
|
|
|
|
$response = $response->toArray();
|
|
|
|
foreach ($response['data'] as $item) {
|
|
$department = $this->entityManager->getRepository(PriceDepartment::class)
|
|
->findOneBy([
|
|
'groupId' => $item['id']
|
|
]);
|
|
|
|
if (is_null($department)) {
|
|
$department = new PriceDepartment();
|
|
}
|
|
|
|
if (empty($item['viewInWeb'])) {
|
|
$item['viewInWeb'] = 0;
|
|
}
|
|
|
|
$department
|
|
->setGroupId($item['id'])
|
|
->setName($item['name'])
|
|
->setViewInWeb($item['viewInWeb'])
|
|
->setDoctCount($item['schCount'])
|
|
;
|
|
|
|
$this->entityManager->persist($department);
|
|
$this->entityManager->flush();
|
|
|
|
$io->success('load: '. $department->getId());
|
|
}
|
|
|
|
return Command::SUCCESS;
|
|
}
|
|
}
|