#!/usr/bin/env python3 """ 월비탕 단계별 처방 추가 스크립트 월비탕 1차부터 4차까지 단계별로 처방을 등록합니다. 각 단계마다 약재의 용량이 다릅니다. """ import sqlite3 from datetime import datetime def add_wolbitang_formulas(): """월비탕 단계별 처방 추가""" # 약재 성분 코드 매핑 herb_codes = { "마황": "3147H1AHM", "석고": "3265H1AHM", "감초": "3007H1AHM", "진피": "3632H1AHM", "복령": "3215H1AHM", "갈근": "3002H1AHM", "지황": "3463H1AHM", # 건지황 대신 지황 사용 "창출": "3472H1AHM" } # 월비탕 단계별 처방 데이터 wolbitang_prescriptions = [ { 'formula_code': 'WBT001-1', 'formula_name': '월비탕 1차', 'formula_type': 'CUSTOM', 'base_cheop': 1, 'base_pouches': 1, 'description': '월비탕 1차 - 단계별 처방의 첫 번째 단계', 'ingredients': [ {'herb': '마황', 'amount': 4.0, 'notes': '발한해표'}, {'herb': '석고', 'amount': 3.0, 'notes': '청열사화'}, {'herb': '감초', 'amount': 3.0, 'notes': '조화제약'}, {'herb': '진피', 'amount': 3.333, 'notes': '리기화담'}, {'herb': '복령', 'amount': 4.0, 'notes': '건비이수'}, {'herb': '갈근', 'amount': 3.333, 'notes': '승진해기'}, {'herb': '지황', 'amount': 3.333, 'notes': '보음청열'}, {'herb': '창출', 'amount': 3.333, 'notes': '건비조습'} ] }, { 'formula_code': 'WBT001-2', 'formula_name': '월비탕 2차', 'formula_type': 'CUSTOM', 'base_cheop': 1, 'base_pouches': 1, 'description': '월비탕 2차 - 단계별 처방의 두 번째 단계', 'ingredients': [ {'herb': '마황', 'amount': 5.0, 'notes': '발한해표'}, {'herb': '석고', 'amount': 4.0, 'notes': '청열사화'}, {'herb': '감초', 'amount': 3.0, 'notes': '조화제약'}, {'herb': '진피', 'amount': 3.75, 'notes': '리기화담'}, {'herb': '복령', 'amount': 4.0, 'notes': '건비이수'}, {'herb': '갈근', 'amount': 3.333, 'notes': '승진해기'}, {'herb': '지황', 'amount': 3.333, 'notes': '보음청열'}, {'herb': '창출', 'amount': 3.333, 'notes': '건비조습'} ] }, { 'formula_code': 'WBT001-3', 'formula_name': '월비탕 3차', 'formula_type': 'CUSTOM', 'base_cheop': 1, 'base_pouches': 1, 'description': '월비탕 3차 - 단계별 처방의 세 번째 단계', 'ingredients': [ {'herb': '마황', 'amount': 6.0, 'notes': '발한해표'}, {'herb': '석고', 'amount': 4.17, 'notes': '청열사화'}, {'herb': '감초', 'amount': 3.0, 'notes': '조화제약'}, {'herb': '진피', 'amount': 4.17, 'notes': '리기화담'}, {'herb': '복령', 'amount': 4.17, 'notes': '건비이수'}, {'herb': '갈근', 'amount': 3.75, 'notes': '승진해기'}, {'herb': '지황', 'amount': 3.75, 'notes': '보음청열'}, {'herb': '창출', 'amount': 3.333, 'notes': '건비조습'} ] }, { 'formula_code': 'WBT001-4', 'formula_name': '월비탕 4차', 'formula_type': 'CUSTOM', 'base_cheop': 1, 'base_pouches': 1, 'description': '월비탕 4차 - 단계별 처방의 네 번째 단계', 'ingredients': [ {'herb': '마황', 'amount': 7.0, 'notes': '발한해표'}, {'herb': '석고', 'amount': 5.0, 'notes': '청열사화'}, {'herb': '감초', 'amount': 3.0, 'notes': '조화제약'}, {'herb': '진피', 'amount': 4.17, 'notes': '리기화담'}, {'herb': '복령', 'amount': 5.0, 'notes': '건비이수'}, {'herb': '갈근', 'amount': 3.75, 'notes': '승진해기'}, {'herb': '지황', 'amount': 4.0, 'notes': '보음청열'}, {'herb': '창출', 'amount': 3.333, 'notes': '건비조습'} ] } ] conn = sqlite3.connect('database/kdrug.db') cursor = conn.cursor() try: # 기존 월비탕 처방 확인 cursor.execute(""" SELECT formula_code, formula_name FROM formulas WHERE formula_code LIKE 'WBT%' ORDER BY formula_code """) existing = cursor.fetchall() if existing: print("⚠️ 기존 월비탕 처방 발견:") for code, name in existing: print(f" - {code}: {name}") print() # 각 처방 추가 for prescription in wolbitang_prescriptions: print(f"\n{'='*60}") print(f"📝 {prescription['formula_name']} 추가 중...") # 1. formulas 테이블에 처방 추가 cursor.execute(""" INSERT INTO formulas ( formula_code, formula_name, formula_type, base_cheop, base_pouches, description, is_active, created_at, updated_at ) VALUES (?, ?, ?, ?, ?, ?, 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP) """, ( prescription['formula_code'], prescription['formula_name'], prescription['formula_type'], prescription['base_cheop'], prescription['base_pouches'], prescription['description'] )) formula_id = cursor.lastrowid print(f" ✅ 처방 기본 정보 등록 (ID: {formula_id})") # 2. formula_ingredients 테이블에 약재 추가 sort_order = 0 for ingredient in prescription['ingredients']: herb_name = ingredient['herb'] ingredient_code = herb_codes[herb_name] cursor.execute(""" INSERT INTO formula_ingredients ( formula_id, ingredient_code, grams_per_cheop, notes, sort_order, created_at ) VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP) """, ( formula_id, ingredient_code, ingredient['amount'], ingredient['notes'], sort_order )) sort_order += 1 print(f" - {herb_name}: {ingredient['amount']}g ({ingredient['notes']})") print(f" ✅ {prescription['formula_name']} 추가 완료!") conn.commit() print(f"\n{'='*60}") print("🎉 월비탕 1차~4차 모든 처방이 성공적으로 추가되었습니다!") # 추가된 처방 확인 print("\n📊 추가된 월비탕 처방 목록:") print("-"*60) cursor.execute(""" SELECT f.formula_id, f.formula_code, f.formula_name, COUNT(fi.ingredient_id) as herb_count, SUM(fi.grams_per_cheop) as total_grams FROM formulas f LEFT JOIN formula_ingredients fi ON f.formula_id = fi.formula_id WHERE f.formula_code LIKE 'WBT%' GROUP BY f.formula_id ORDER BY f.formula_code """) for row in cursor.fetchall(): print(f"ID {row[0]}: {row[1]} - {row[2]}") print(f" 약재 {row[3]}개, 총 {row[4]:.3f}g") except sqlite3.IntegrityError as e: print(f"❌ 중복 오류: {e}") print(" 이미 동일한 처방 코드가 존재합니다.") conn.rollback() return False except sqlite3.Error as e: print(f"❌ 데이터베이스 오류: {e}") conn.rollback() return False finally: conn.close() return True if __name__ == "__main__": print("🌿 월비탕 단계별 처방 추가 프로그램") print("="*60) if add_wolbitang_formulas(): print("\n✅ 월비탕 처방 추가 작업이 완료되었습니다.") else: print("\n❌ 처방 추가 중 오류가 발생했습니다.")