dana-medical-site/migrations/import/export_doctor_experience.py
2026-10-14 13:33:13 +00:00

48 lines
2.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
"""
Переносит стаж врачей в practice_start_year.
При первом переносе текст «Стаж работы по специальности» (свойство 328) попал
в поле quote — цитату для карточки, а practice_start_year остался пустым у всех.
В итоге стаж, который макет показывает на каждой карточке, не отдавался.
Год начала практики = год последней правки карточки на старом сайте − стаж.
Считать от сегодняшнего дня нельзя: «10 лет» было верно на момент правки.
Голое число («13») считается годами — поле называлось «Стаж работы».
Из quote стираем только сам текст стажа, настоящие цитаты не трогаем.
"""
import sqlread, io, re
P = 'D:/medkor-local/tmp/dana/db.sql'
OUT = io.open('seed_doctor_experience.sql', 'w', encoding='utf-8')
work = {}
for r in sqlread.rows(P, 'b_iblock_element_property'):
if r.get('IBLOCK_PROPERTY_ID') == '328' and r.get('VALUE') not in (None, '', 'NULL'):
work[r['IBLOCK_ELEMENT_ID']] = r['VALUE']
OUT.write('BEGIN;\n')
done = skipped = 0
for r in sqlread.rows(P, 'b_iblock_element'):
if r['IBLOCK_ID'] != '29' or r.get('ACTIVE') != 'Y':
continue
text = work.get(r['ID'], '')
m = re.search(r'(\d+)', text)
ts = r.get('TIMESTAMP_X') or ''
if not m or not re.match(r'\d{4}', ts):
skipped += 1
continue
years = int(m.group(1))
if not 0 < years < 70:
skipped += 1
continue
start = int(ts[:4]) - years
OUT.write(
"UPDATE doctors SET practice_start_year=%d, "
"quote = CASE WHEN quote ~ '^\\s*(более\\s+)?\\d+\\s*(год|года|лет)?\\s*$' THEN '' ELSE quote END, "
"updated_at=now() WHERE legacy_id=%s;\n" % (start, r['ID']))
done += 1
OUT.write('COMMIT;\n')
OUT.close()
print('стаж проставлен:', done, '| пропущено (нет стажа или не разобран):', skipped)