issues/27: prod baseline without task branch
This commit is contained in:
@@ -2,26 +2,206 @@
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Entity\Disease;
|
||||
use App\Repository\DiseaseRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
/**
|
||||
* Импорт заболеваний из материализованного представления (Bitrix view).
|
||||
*
|
||||
* См. DiseaseController + CrudResponder для CRUD; этот сервис — только syncFromView*.
|
||||
*/
|
||||
final class DiseaseCrudService
|
||||
{
|
||||
public function __construct(
|
||||
private EntityManagerInterface $em,
|
||||
private DiseaseRepository $diseaseRepository,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{data: Disease[], total: int, page: int, per_page: int}
|
||||
*/
|
||||
public function getPaginatedList(int $page, int $perPage, ?int $regionId = null): array
|
||||
{
|
||||
$page = max(1, $page);
|
||||
$perPage = min(max(1, $perPage), 500);
|
||||
|
||||
$qb = $this->diseaseRepository->createQueryBuilder('d')
|
||||
->orderBy('d.id', 'ASC');
|
||||
|
||||
if ($regionId !== null) {
|
||||
$qb->andWhere('d.regionId = :regionId')
|
||||
->setParameter('regionId', $regionId);
|
||||
}
|
||||
|
||||
$countQb = $this->diseaseRepository->createQueryBuilder('d')
|
||||
->select('COUNT(d.id)');
|
||||
if ($regionId !== null) {
|
||||
$countQb->andWhere('d.regionId = :regionId')
|
||||
->setParameter('regionId', $regionId);
|
||||
}
|
||||
$total = (int) $countQb->getQuery()->getSingleScalarResult();
|
||||
|
||||
$qb->setFirstResult(($page - 1) * $perPage)
|
||||
->setMaxResults($perPage);
|
||||
|
||||
$data = $qb->getQuery()->getResult();
|
||||
|
||||
return [
|
||||
'data' => $data,
|
||||
'total' => $total,
|
||||
'page' => $page,
|
||||
'per_page' => $perPage,
|
||||
];
|
||||
}
|
||||
|
||||
public function getShow(int $id): ?Disease
|
||||
{
|
||||
return $this->diseaseRepository->find($id);
|
||||
}
|
||||
|
||||
public function create(array $data): Disease
|
||||
{
|
||||
if (!array_key_exists('id', $data) || $data['id'] === null || $data['id'] === '') {
|
||||
throw new \InvalidArgumentException('Поле id обязательно.');
|
||||
}
|
||||
|
||||
$disease = new Disease();
|
||||
$this->updateEntity($disease, $data);
|
||||
|
||||
$this->em->persist($disease);
|
||||
$this->em->flush();
|
||||
|
||||
return $disease;
|
||||
}
|
||||
|
||||
public function update(Disease $disease, array $data): Disease
|
||||
{
|
||||
unset($data['id']);
|
||||
$this->updateEntity($disease, $data);
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
return $disease;
|
||||
}
|
||||
|
||||
public function delete(Disease $disease): void
|
||||
{
|
||||
$this->em->remove($disease);
|
||||
$this->em->flush();
|
||||
}
|
||||
|
||||
private function updateEntity(Disease $disease, array $data): void
|
||||
{
|
||||
if (array_key_exists('id', $data) && $data['id'] !== null && $data['id'] !== '') {
|
||||
$disease->setId((int) $data['id']);
|
||||
}
|
||||
|
||||
if (array_key_exists('name', $data)) {
|
||||
$disease->setName($data['name']);
|
||||
}
|
||||
|
||||
if (array_key_exists('previewPicture', $data) || array_key_exists('preview_picture', $data)) {
|
||||
$disease->setPreviewPicture($data['previewPicture'] ?? $data['preview_picture']);
|
||||
}
|
||||
|
||||
if (array_key_exists('active', $data)) {
|
||||
$disease->setActive($data['active']);
|
||||
}
|
||||
|
||||
if (array_key_exists('regionId', $data) || array_key_exists('region_id', $data)) {
|
||||
$v = $data['regionId'] ?? $data['region_id'];
|
||||
$disease->setRegionId($v === null || $v === '' ? null : (int) $v);
|
||||
}
|
||||
|
||||
if (array_key_exists('alias', $data)) {
|
||||
$disease->setAlias($data['alias']);
|
||||
}
|
||||
|
||||
if (array_key_exists('anons', $data)) {
|
||||
$disease->setAnons($data['anons']);
|
||||
}
|
||||
|
||||
if (array_key_exists('updateAt', $data) || array_key_exists('update_at', $data)) {
|
||||
$raw = $data['updateAt'] ?? $data['update_at'];
|
||||
if ($raw === null || $raw === '') {
|
||||
$disease->setUpdateAt(null);
|
||||
} elseif ($raw instanceof \DateTimeInterface) {
|
||||
$disease->setUpdateAt($raw);
|
||||
} elseif (is_string($raw)) {
|
||||
$disease->setUpdateAt(new \DateTimeImmutable($raw));
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists('hidePicture', $data) || array_key_exists('hide_picture', $data)) {
|
||||
$disease->setHidePicture($data['hidePicture'] ?? $data['hide_picture']);
|
||||
}
|
||||
|
||||
if (array_key_exists('readTime', $data) || array_key_exists('read_time', $data)) {
|
||||
$disease->setReadTime($data['readTime'] ?? $data['read_time']);
|
||||
}
|
||||
|
||||
if (array_key_exists('diseasesName', $data) || array_key_exists('diseases_name', $data)) {
|
||||
$disease->setDiseasesName($data['diseasesName'] ?? $data['diseases_name']);
|
||||
}
|
||||
|
||||
if (array_key_exists('tagsImportant', $data) || array_key_exists('tags_important', $data)) {
|
||||
$disease->setTagsImportant($data['tagsImportant'] ?? $data['tags_important']);
|
||||
}
|
||||
|
||||
if (array_key_exists('tags', $data)) {
|
||||
$disease->setTags($data['tags']);
|
||||
}
|
||||
|
||||
if (array_key_exists('diseasesOtherName', $data) || array_key_exists('diseases_other_name', $data)) {
|
||||
$disease->setDiseasesOtherName($data['diseasesOtherName'] ?? $data['diseases_other_name']);
|
||||
}
|
||||
|
||||
if (array_key_exists('symptom', $data)) {
|
||||
$disease->setSymptom($data['symptom']);
|
||||
}
|
||||
|
||||
if (array_key_exists('staff', $data)) {
|
||||
$disease->setStaff($data['staff']);
|
||||
}
|
||||
|
||||
if (array_key_exists('linkServices', $data) || array_key_exists('link_services', $data)) {
|
||||
$disease->setLinkServices($data['linkServices'] ?? $data['link_services']);
|
||||
}
|
||||
|
||||
if (array_key_exists('staffList', $data) || array_key_exists('staff_list', $data)) {
|
||||
$disease->setStaffList($data['staffList'] ?? $data['staff_list']);
|
||||
}
|
||||
|
||||
if (array_key_exists('staffPost', $data) || array_key_exists('staff_post', $data)) {
|
||||
$disease->setStaffPost($data['staffPost'] ?? $data['staff_post']);
|
||||
}
|
||||
|
||||
if (array_key_exists('staffPostExclude', $data) || array_key_exists('staff_post_exclude', $data)) {
|
||||
$disease->setStaffPostExclude($data['staffPostExclude'] ?? $data['staff_post_exclude']);
|
||||
}
|
||||
|
||||
if (array_key_exists('linkFaq', $data) || array_key_exists('link_faq', $data)) {
|
||||
$disease->setLinkFaq($data['linkFaq'] ?? $data['link_faq']);
|
||||
}
|
||||
|
||||
if (array_key_exists('bibliography', $data)) {
|
||||
$disease->setBibliography($data['bibliography']);
|
||||
}
|
||||
|
||||
if (array_key_exists('staffCheck', $data) || array_key_exists('staff_check', $data)) {
|
||||
$disease->setStaffCheck($data['staffCheck'] ?? $data['staff_check']);
|
||||
}
|
||||
|
||||
if (array_key_exists('content', $data)) {
|
||||
$disease->setContent($data['content']);
|
||||
}
|
||||
}
|
||||
|
||||
public function syncFromViewDisease(string $viewName = 'public.view_disease'): int
|
||||
{
|
||||
if (!preg_match('/^[A-Za-z0-9_\.]+$/', $viewName)) {
|
||||
throw new \InvalidArgumentException('Invalid view name');
|
||||
}
|
||||
|
||||
$connection = $this->em->getConnection();
|
||||
|
||||
$sql = sprintf(
|
||||
'INSERT INTO disease (
|
||||
id,
|
||||
@@ -102,6 +282,6 @@ final class DiseaseCrudService
|
||||
$viewName
|
||||
);
|
||||
|
||||
return (int) $this->em->getConnection()->executeStatement($sql);
|
||||
return (int) $connection->executeStatement($sql);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user