#!/usr/bin/env python3 """ 월비탕 단계별 처방 추가 스크립트 월비탕 1차부터 4차까지 단계별로 처방을 등록합니다. 각 단계마다 약재의 용량이 다릅니다. """ import sqlite3 import json from datetime import datetime def add_wolbitang_prescriptions(): """월비탕 단계별 처방 추가""" # 월비탕 단계별 데이터 wolbitang_data = { "월비탕 1차": { "마황": 4, "석고": 3, "감초": 3, "진피": 3.333, "복령": 4, "갈근": 3.333, "건지황": 3.333, "창출": 3.333 }, "월비탕 2차": { "마황": 5, "석고": 4, "감초": 3, "진피": 3.75, "복령": 4, "갈근": 3.333, "건지황": 3.333, "창출": 3.333 }, "월비탕 3차": { "마황": 6, "석고": 4.17, "감초": 3, "진피": 4.17, "복령": 4.17, "갈근": 3.75, "건지황": 3.75, "창출": 3.333 }, "월비탕 4차": { "마황": 7, "석고": 5, "감초": 3, "진피": 4.17, "복령": 5, "갈근": 3.75, "건지황": 4, "창출": 3.333 } } conn = sqlite3.connect('kdrug.db') cursor = conn.cursor() try: # 약재명-코드 매핑 herb_code_mapping = { "마황": "H004", "석고": "H025", "감초": "H001", "진피": "H022", "복령": "H010", "갈근": "H024", "건지황": "H026", "창출": "H014" } # 각 단계별로 처방 추가 for prescription_name, herbs in wolbitang_data.items(): print(f"\n{'='*50}") print(f"{prescription_name} 추가 중...") # 1. 처방 기본 정보 추가 cursor.execute(""" INSERT INTO prescriptions ( name, description, source, category, created_at ) VALUES (?, ?, ?, ?, ?) """, ( prescription_name, f"{prescription_name} - 월비탕의 단계별 처방", "임상처방", "단계별처방", datetime.now().isoformat() )) prescription_id = cursor.lastrowid print(f" 처방 ID {prescription_id}로 등록됨") # 2. 처방 구성 약재 추가 ingredients = [] for herb_name, amount in herbs.items(): herb_code = herb_code_mapping.get(herb_name) if not herb_code: print(f" ⚠️ {herb_name}의 코드를 찾을 수 없습니다.") continue # prescription_ingredients 테이블에 추가 cursor.execute(""" INSERT INTO prescription_ingredients ( prescription_id, ingredient_code, amount, unit ) VALUES (?, ?, ?, ?) """, (prescription_id, herb_code, amount, 'g')) ingredients.append({ 'code': herb_code, 'name': herb_name, 'amount': amount, 'unit': 'g' }) print(f" - {herb_name}({herb_code}): {amount}g 추가됨") # 3. prescription_details 테이블에 JSON 형태로도 저장 cursor.execute(""" INSERT INTO prescription_details ( prescription_id, ingredients_json, total_herbs, default_packets, preparation_method ) VALUES (?, ?, ?, ?, ?) """, ( prescription_id, json.dumps(ingredients, ensure_ascii=False), len(ingredients), 20, # 기본 첩수 "1일 2회, 1회 1포" )) print(f" ✅ {prescription_name} 처방 추가 완료 (총 {len(ingredients)}개 약재)") conn.commit() print(f"\n{'='*50}") print("✅ 월비탕 1차~4차 처방이 모두 성공적으로 추가되었습니다!") # 추가된 처방 확인 print("\n📊 추가된 처방 목록:") cursor.execute(""" SELECT p.id, p.name, pd.total_herbs FROM prescriptions p LEFT JOIN prescription_details pd ON p.id = pd.prescription_id WHERE p.name LIKE '월비탕%' ORDER BY p.id """) for row in cursor.fetchall(): print(f" ID {row[0]}: {row[1]} - {row[2]}개 약재") except sqlite3.Error as e: print(f"❌ 데이터베이스 오류: {e}") conn.rollback() return False finally: conn.close() return True if __name__ == "__main__": print("🌿 월비탕 단계별 처방 추가 프로그램") print("="*50) if add_wolbitang_prescriptions(): print("\n✅ 월비탕 처방 추가 작업이 완료되었습니다.") else: print("\n❌ 처방 추가 중 오류가 발생했습니다.")