83 lines
3.0 KiB
Python
83 lines
3.0 KiB
Python
import mysql.connector
|
|
import os
|
|
|
|
db_config = {
|
|
'host': '192.168.1.76',
|
|
'port': 3307,
|
|
'user': 'root',
|
|
'password': 'Vlado9674+',
|
|
'database': 'ordinace'
|
|
}
|
|
|
|
# ROOT_DIR = r'm:\Medicus\DavkyExport\09305000'
|
|
|
|
from pathlib import Path
|
|
# adresář, kde leží tento .py soubor
|
|
SCRIPT_DIR = Path(__file__).resolve().parent
|
|
# projektový root = U:\PycharmProjects1
|
|
PROJECT_ROOT = SCRIPT_DIR.parent
|
|
# cílová složka s texty
|
|
ROOT_DIR = PROJECT_ROOT / "09305000"
|
|
print("Skript je v:", SCRIPT_DIR)
|
|
print("Projekt root:", PROJECT_ROOT)
|
|
print("Data dir:", ROOT_DIR)
|
|
|
|
def hromadny_raw_import():
|
|
try:
|
|
conn = mysql.connector.connect(**db_config)
|
|
cursor = conn.cursor()
|
|
|
|
for root, dirs, files in os.walk(ROOT_DIR):
|
|
for filename in files:
|
|
if filename.upper().startswith('KDAVKA'):
|
|
filepath = os.path.join(root, filename)
|
|
|
|
with open(filepath, 'r', encoding='cp1250') as f:
|
|
first_line = f.readline().strip('\n')
|
|
|
|
if not first_line.startswith('DP'): continue
|
|
|
|
# Unikátní klíč (prvních 30 znaků)
|
|
fingerprint = first_line[:30].strip()
|
|
|
|
# Kontrola duplicity
|
|
cursor.execute("SELECT id_davky FROM davky WHERE identifikator = %s", (fingerprint,))
|
|
if cursor.fetchone():
|
|
continue
|
|
|
|
print(f"Importuji: {filename} -> {fingerprint}")
|
|
|
|
# Vložení dávky
|
|
obdobi = first_line[20:26].strip()
|
|
cursor.execute("INSERT INTO davky (identifikator, obdobi) VALUES (%s, %s)", (fingerprint, obdobi))
|
|
id_davky = cursor.lastrowid
|
|
|
|
# Import všech řádků souboru
|
|
with open(filepath, 'r', encoding='cp1250') as f:
|
|
id_posledni_a = None
|
|
for idx, line in enumerate(f, 1):
|
|
raw = line.strip('\n')
|
|
if not raw: continue
|
|
typ = raw[0]
|
|
|
|
# Uložení věty
|
|
cursor.execute(
|
|
"INSERT INTO vety (id_davky, typ_vety, obsah_radku, poradi_v_souboru) VALUES (%s, %s, %s, %s)",
|
|
(id_davky, typ, raw, idx)
|
|
)
|
|
current_id_vety = cursor.lastrowid
|
|
|
|
# Hierarchie pod 'A'
|
|
if typ == 'A':
|
|
id_posledni_a = current_id_vety
|
|
|
|
if id_posledni_a:
|
|
cursor.execute("UPDATE vety SET id_dokladu_vazba = %s WHERE id_vety = %s",
|
|
(id_posledni_a, current_id_vety))
|
|
conn.commit()
|
|
print("Raw import dokončen.")
|
|
finally:
|
|
conn.close()
|
|
|
|
|
|
hromadny_raw_import() |