From c196807228cccc3b16f384b3e92982507270431b Mon Sep 17 00:00:00 2001 From: sergei kasianenko Date: Sun, 24 Nov 2024 00:44:19 +0800 Subject: [PATCH 1/6] . --- api/parse_file/views.py | 3 +- .../commands/upload_desription_result.py | 138 ++++++++++++++++++ 2 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 directions/management/commands/upload_desription_result.py diff --git a/api/parse_file/views.py b/api/parse_file/views.py index ff23fa204e..7ea27cbc3a 100644 --- a/api/parse_file/views.py +++ b/api/parse_file/views.py @@ -33,8 +33,7 @@ from slog.models import Log from statistic.views import commercial_offer_xls_save_file, data_xls_save_file, data_xls_save_headers_file from users.models import AssignmentResearches, DoctorProfile -from clients.models import Individual, HarmfulFactor, PatientHarmfullFactor, Card, CardBase, DocumentType, Document -from integration_framework.views import check_enp +from clients.models import Individual, HarmfulFactor, Card, DocumentType, Document from utils.dates import age_for_year, normalize_dots_date from django.views.decorators.csrf import csrf_exempt from django.db.models import Q diff --git a/directions/management/commands/upload_desription_result.py b/directions/management/commands/upload_desription_result.py new file mode 100644 index 0000000000..df0e5aac36 --- /dev/null +++ b/directions/management/commands/upload_desription_result.py @@ -0,0 +1,138 @@ +import datetime +from django.core.management.base import BaseCommand +from django.db.models import Q +from openpyxl import load_workbook +import clients.models as clients +from directory.models import ParaclinicInputGroups, ParaclinicInputField +from django.db import transaction +from django.utils import timezone +import logging + +from users.models import DoctorProfile +import directions.models as directions +from utils.dates import normalize_dots_date + +logger = logging.getLogger("IF") + + +class Command(BaseCommand): + def add_arguments(self, parser): + """ + :param path - файл с пациентами + """ + parser.add_argument('path', type=str) + parser.add_argument('research_id', type=str) + parser.add_argument('doctor_profile_id', type=str) + + def handle(self, *args, **kwargs): + fp = kwargs["path"] + research_id = kwargs["research_id"] + doctor_profile_id = kwargs["doctor_profile_id"] + wb = load_workbook(filename=fp) + ws = wb[wb.sheetnames[0]] + starts = False + base_l2 = None + polis_date_start, born_date, visit_date_star, visit_date_end, visit_type_text = None, None, None, None, None + snils, lastname, name, patronymic, sex, polis = None, None, None, None, None, None + title_fields = [] + + doc_profile = DoctorProfile.objects.filter(pk=doctor_profile_id).first() + financing_source = directions.IstochnikiFinansirovaniya.objects.filter(title="ОМС", base__internal_type=True).first() + + for row in ws.rows: + cells = [str(x.value) for x in row] + if not starts: + if "СНИЛС пациента" in cells and "Полис ОМС" in cells: + starts = True + lastname = cells.index("Фамилия пациента") + name = cells.index("Имя пациента") + patronymic = cells.index("Отчество пациента") + sex = cells.index("Пол пациента") + snils = cells.index("СНИЛС пациента") + polis = cells.index("Полис ОМС") + born_date = cells.index("Дата рождения пациента") + base_l2 = clients.CardBase.objects.filter(internal_type=True)[0] + title_fields = cells.copy() + else: + # если есть индивидуал по документам + ind = clients.Document.objects.filter( + Q(document_type__title__iexact="СНИЛС", number=cells[snils]) + | Q(document_type__title__iexact="Полис ОМС", number=cells[polis]) + ).first() + + if ind: + print("used", ind) + i = ind.individual + if clients.Card.objects.filter(individual=i, base=base_l2).exists(): + card = clients.Card.objects.filter(individual=i, base=base_l2).first() + else: + # создать карту L2 + card = clients.Card.objects.create( + number=clients.Card.next_l2_n(), + base=base_l2, + individual=i, + ) + print('Добавлена карта: \n', c) # noqa: T001 + else: + # создать индивидуал, документы, карты в l2. + ind = clients.Individual.objects.create( + family=cells[lastname], + name=cells[name], + patronymic=cells[patronymic], + birthday=datetime.datetime.strptime(cells[born_date], "%Y-%m-%d %H:%M:%S").date(), + sex=cells[sex][0], + ) + + if cells[snils]: + snils_object = clients.DocumentType.objects.get(title__iexact='СНИЛС') + clients.Document.objects.create(document_type=snils_object, number=cells[snils], individual=ind) + + polis_object = clients.DocumentType.objects.get(title__iexact='Полис ОМС') + document_polis = clients.Document.objects.create(document_type=polis_object, number=cells[polis], individual=ind) if cells[polis] else None + + card = clients.Card.objects.create( + individual=ind, + number=clients.Card.next_l2_n(), + base=base_l2, + ) + print('Добавлена карта: \n', card) # noqa: T001 + print(card) + try: + with transaction.atomic(): + direction = directions.Napravleniya.objects.create( + client=card, + istochnik_f=financing_source, + hospital=doc_profile.hospital, + total_confirmed=True, + last_confirmed_at=timezone.now(), + eds_required_signature_types=['Врач', 'Медицинская организация'], + ) + + iss = directions.Issledovaniya.objects.create( + napravleniye=direction, + research_id=research_id, + time_confirmation=timezone.now(), + time_save=timezone.now(), + doc_confirmation=doc_profile, + doc_save=doc_profile, + doc_confirmation_string=f"{doc_profile.get_fio_parts()}", + ) + for group in ParaclinicInputGroups.objects.filter(research_id=research_id): + for f in ParaclinicInputField.objects.filter(group=group): + print(f.title) + if f.title in title_fields: + print("f.title", f.title, "cells") + current_cells = title_fields.index(f.title) + if f.field_type == 1: + tmp_value = cells[current_cells] + if "." in tmp_value or "-" in tmp_value: + value = tmp_value[:10] + value = normalize_dots_date(value) + else: + value = cells[current_cells] + directions.ParaclinicResult(issledovaniye=iss, field=f, field_type=f.field_type, value=value).save() + + except Exception as e: + logger.exception(e) + message = "Серверная ошибка" + return {"ok": False, "message": message} From 9ea6e4ed56819c1828cccfdd3f5ff6a60bdeede7 Mon Sep 17 00:00:00 2001 From: sergei kasianenko Date: Sun, 24 Nov 2024 00:45:14 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=D0=B0=D1=82=D0=BE=D0=B7=D0=B0=D0=B3=D1=80?= =?UTF-8?q?=D1=83=D0=B7=D0=BA=D0=B0=20=D0=BF=D1=80=D0=BE=D1=82=D0=BE=D0=BA?= =?UTF-8?q?=D0=BE=D0=BB=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- directions/management/commands/upload_desription_result.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/directions/management/commands/upload_desription_result.py b/directions/management/commands/upload_desription_result.py index df0e5aac36..4865b0dadb 100644 --- a/directions/management/commands/upload_desription_result.py +++ b/directions/management/commands/upload_desription_result.py @@ -88,7 +88,7 @@ def handle(self, *args, **kwargs): clients.Document.objects.create(document_type=snils_object, number=cells[snils], individual=ind) polis_object = clients.DocumentType.objects.get(title__iexact='Полис ОМС') - document_polis = clients.Document.objects.create(document_type=polis_object, number=cells[polis], individual=ind) if cells[polis] else None + clients.Document.objects.create(document_type=polis_object, number=cells[polis], individual=ind) if cells[polis] else None card = clients.Card.objects.create( individual=ind, @@ -96,7 +96,6 @@ def handle(self, *args, **kwargs): base=base_l2, ) print('Добавлена карта: \n', card) # noqa: T001 - print(card) try: with transaction.atomic(): direction = directions.Napravleniya.objects.create( @@ -119,9 +118,7 @@ def handle(self, *args, **kwargs): ) for group in ParaclinicInputGroups.objects.filter(research_id=research_id): for f in ParaclinicInputField.objects.filter(group=group): - print(f.title) if f.title in title_fields: - print("f.title", f.title, "cells") current_cells = title_fields.index(f.title) if f.field_type == 1: tmp_value = cells[current_cells] From 0e828b2e7e4f371399effa9e94e0e83ff4712c45 Mon Sep 17 00:00:00 2001 From: Sergei Kasianenko <41939763+urchinpro@users.noreply.github.com> Date: Sun, 24 Nov 2024 01:09:13 +0800 Subject: [PATCH 3/6] Update directions/management/commands/upload_desription_result.py Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- directions/management/commands/upload_desription_result.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/directions/management/commands/upload_desription_result.py b/directions/management/commands/upload_desription_result.py index 4865b0dadb..5a69f08aa1 100644 --- a/directions/management/commands/upload_desription_result.py +++ b/directions/management/commands/upload_desription_result.py @@ -55,10 +55,7 @@ def handle(self, *args, **kwargs): title_fields = cells.copy() else: # если есть индивидуал по документам - ind = clients.Document.objects.filter( - Q(document_type__title__iexact="СНИЛС", number=cells[snils]) - | Q(document_type__title__iexact="Полис ОМС", number=cells[polis]) - ).first() + ind = clients.Document.objects.filter(Q(document_type__title__iexact="СНИЛС", number=cells[snils]) | Q(document_type__title__iexact="Полис ОМС", number=cells[polis])).first() if ind: print("used", ind) From 6ea1abed798e0d599c89408fde99a2f1cae31d2a Mon Sep 17 00:00:00 2001 From: sergei kasianenko Date: Sun, 24 Nov 2024 01:10:27 +0800 Subject: [PATCH 4/6] fix --- directions/management/commands/upload_desription_result.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/directions/management/commands/upload_desription_result.py b/directions/management/commands/upload_desription_result.py index 4865b0dadb..bc59f8db23 100644 --- a/directions/management/commands/upload_desription_result.py +++ b/directions/management/commands/upload_desription_result.py @@ -32,8 +32,7 @@ def handle(self, *args, **kwargs): ws = wb[wb.sheetnames[0]] starts = False base_l2 = None - polis_date_start, born_date, visit_date_star, visit_date_end, visit_type_text = None, None, None, None, None - snils, lastname, name, patronymic, sex, polis = None, None, None, None, None, None + snils, lastname, name, patronymic, sex, polis, born_date = None, None, None, None, None, None, None title_fields = [] doc_profile = DoctorProfile.objects.filter(pk=doctor_profile_id).first() @@ -72,7 +71,7 @@ def handle(self, *args, **kwargs): base=base_l2, individual=i, ) - print('Добавлена карта: \n', c) # noqa: T001 + print('Добавлена карта: \n', card) # noqa: T001 else: # создать индивидуал, документы, карты в l2. ind = clients.Individual.objects.create( From 99b3274f742fb69f0a173e651b293549f1ad6e78 Mon Sep 17 00:00:00 2001 From: sergei kasianenko Date: Sun, 24 Nov 2024 01:10:58 +0800 Subject: [PATCH 5/6] fix --- directions/management/commands/upload_desription_result.py | 1 - 1 file changed, 1 deletion(-) diff --git a/directions/management/commands/upload_desription_result.py b/directions/management/commands/upload_desription_result.py index 8a7d02ce49..ec8a60c186 100644 --- a/directions/management/commands/upload_desription_result.py +++ b/directions/management/commands/upload_desription_result.py @@ -57,7 +57,6 @@ def handle(self, *args, **kwargs): ind = clients.Document.objects.filter(Q(document_type__title__iexact="СНИЛС", number=cells[snils]) | Q(document_type__title__iexact="Полис ОМС", number=cells[polis])).first() if ind: - print("used", ind) i = ind.individual if clients.Card.objects.filter(individual=i, base=base_l2).exists(): card = clients.Card.objects.filter(individual=i, base=base_l2).first() From f6b3bec3486c9d13c5e1700058624872fb622382 Mon Sep 17 00:00:00 2001 From: sergei kasianenko Date: Sun, 24 Nov 2024 01:15:56 +0800 Subject: [PATCH 6/6] fix --- .../management/commands/upload_desription_result.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/directions/management/commands/upload_desription_result.py b/directions/management/commands/upload_desription_result.py index ec8a60c186..d578327b53 100644 --- a/directions/management/commands/upload_desription_result.py +++ b/directions/management/commands/upload_desription_result.py @@ -11,6 +11,7 @@ from users.models import DoctorProfile import directions.models as directions from utils.dates import normalize_dots_date +from sys import stdout logger = logging.getLogger("IF") @@ -53,7 +54,6 @@ def handle(self, *args, **kwargs): base_l2 = clients.CardBase.objects.filter(internal_type=True)[0] title_fields = cells.copy() else: - # если есть индивидуал по документам ind = clients.Document.objects.filter(Q(document_type__title__iexact="СНИЛС", number=cells[snils]) | Q(document_type__title__iexact="Полис ОМС", number=cells[polis])).first() if ind: @@ -61,15 +61,13 @@ def handle(self, *args, **kwargs): if clients.Card.objects.filter(individual=i, base=base_l2).exists(): card = clients.Card.objects.filter(individual=i, base=base_l2).first() else: - # создать карту L2 card = clients.Card.objects.create( number=clients.Card.next_l2_n(), base=base_l2, individual=i, ) - print('Добавлена карта: \n', card) # noqa: T001 + stdout.write(f'Добавлена карта: {card}') else: - # создать индивидуал, документы, карты в l2. ind = clients.Individual.objects.create( family=cells[lastname], name=cells[name], @@ -90,7 +88,7 @@ def handle(self, *args, **kwargs): number=clients.Card.next_l2_n(), base=base_l2, ) - print('Добавлена карта: \n', card) # noqa: T001 + stdout.write(f'Добавлена карта: {card}') try: with transaction.atomic(): direction = directions.Napravleniya.objects.create(