diff --git a/add_missing_herbs.py b/add_missing_herbs.py new file mode 100644 index 0000000..0a497fa --- /dev/null +++ b/add_missing_herbs.py @@ -0,0 +1,135 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +누락된 약재 추가 스크립트 +""" + +import sqlite3 + +def add_missing_herbs(): + conn = sqlite3.connect('database/kdrug.db') + cursor = conn.cursor() + + try: + # 누락된 약재 추가 + missing_herbs = [ + ('A001100', '당귀', '보혈'), # 보혈약 + ('A001200', '백작약', '보혈'), # 보혈약 + ('A001300', '인삼', '대보원기'), # 대보원기약 + ('A001400', '생강', '온중'), # 온중약 + ] + + print("=== 누락된 약재 추가 ===") + for code, name, efficacy in missing_herbs: + # 약재가 이미 있는지 확인 + cursor.execute("SELECT herb_item_id FROM herb_items WHERE herb_name = ?", (name,)) + if cursor.fetchone(): + print(f" ⚠️ {name} - 이미 존재함") + continue + + # 약재 추가 + cursor.execute(""" + INSERT INTO herb_items (insurance_code, herb_name, specification, default_unit, is_active) + VALUES (?, ?, ?, 'g', 1) + """, (code, name, '')) + + herb_id = cursor.lastrowid + print(f" ✅ {name} (ID: {herb_id}) 추가 완료") + + # 효능 태그 연결 + cursor.execute("SELECT tag_id FROM herb_efficacy_tags WHERE tag_name = ?", (efficacy,)) + tag_result = cursor.fetchone() + + if tag_result: + tag_id = tag_result[0] + cursor.execute(""" + INSERT OR IGNORE INTO herb_item_tags (herb_item_id, tag_id) + VALUES (?, ?) + """, (herb_id, tag_id)) + print(f" → 효능 '{efficacy}' 연결") + + conn.commit() + + # 쌍화탕 처방 재구성 시도 + print("\n=== 쌍화탕 처방 재구성 ===") + + # 쌍화탕 처방 ID 가져오기 + cursor.execute("SELECT formula_id FROM formulas WHERE formula_name = '쌍화탕'") + formula_result = cursor.fetchone() + + if formula_result: + formula_id = formula_result[0] + + # 현재 없는 약재들 다시 추가 시도 + missing_ingredients = [ + ('당귀', 6.0, '보혈'), + ('백작약', 6.0, '보혈'), + ('인삼', 4.0, '대보원기'), + ('생강', 5.0, '온중'), + ] + + for herb_name, grams, notes in missing_ingredients: + # 이미 등록되었는지 확인 + cursor.execute(""" + SELECT fi.ingredient_id + FROM formula_ingredients fi + JOIN herb_items h ON fi.herb_item_id = h.herb_item_id + WHERE fi.formula_id = ? AND h.herb_name = ? + """, (formula_id, herb_name)) + + if cursor.fetchone(): + print(f" ⚠️ {herb_name} - 이미 처방에 포함됨") + continue + + # 약재 ID 찾기 + cursor.execute("SELECT herb_item_id FROM herb_items WHERE herb_name = ?", (herb_name,)) + herb_result = cursor.fetchone() + + if herb_result: + herb_id = herb_result[0] + cursor.execute(""" + INSERT INTO formula_ingredients ( + formula_id, herb_item_id, grams_per_cheop, notes + ) VALUES (?, ?, ?, ?) + """, (formula_id, herb_id, grams, notes)) + print(f" ✅ {herb_name}: {grams}g ({notes}) 추가") + + conn.commit() + + # 최종 쌍화탕 구성 확인 + print("\n=== 최종 쌍화탕 구성 확인 ===") + cursor.execute(""" + SELECT + h.herb_name, + fi.grams_per_cheop, + fi.notes, + GROUP_CONCAT(et.tag_name) as efficacy_tags + FROM formula_ingredients fi + JOIN herb_items h ON fi.herb_item_id = h.herb_item_id + LEFT JOIN herb_item_tags hit ON h.herb_item_id = hit.herb_item_id + LEFT JOIN herb_efficacy_tags et ON hit.tag_id = et.tag_id + WHERE fi.formula_id = (SELECT formula_id FROM formulas WHERE formula_name = '쌍화탕') + GROUP BY h.herb_name, fi.grams_per_cheop, fi.notes + ORDER BY fi.grams_per_cheop DESC + """) + + total_grams = 0 + for herb_name, grams, notes, tags in cursor.fetchall(): + total_grams += grams + tags_display = f" [{tags}]" if tags else "" + print(f" - {herb_name}: {grams}g ({notes}){tags_display}") + + print(f"\n 📊 총 {cursor.rowcount}종 약재") + print(f" 💊 1첩 총량: {total_grams}g") + print(f" 📦 20첩 총량: {total_grams * 20}g") + + print("\n✅ 약재 추가 및 처방 재구성 완료!") + + except Exception as e: + print(f"❌ 오류 발생: {e}") + conn.rollback() + finally: + conn.close() + +if __name__ == "__main__": + add_missing_herbs() \ No newline at end of file diff --git a/insert_ssanghwa.py b/insert_ssanghwa.py new file mode 100644 index 0000000..9655333 --- /dev/null +++ b/insert_ssanghwa.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +쌍화탕 처방 등록 스크립트 +""" + +import sqlite3 +from datetime import datetime + +def insert_ssanghwa_formula(): + conn = sqlite3.connect('database/kdrug.db') + cursor = conn.cursor() + + try: + # 1. 쌍화탕 처방 등록 + cursor.execute(""" + INSERT INTO formulas ( + formula_code, + formula_name, + formula_type, + base_cheop, + base_pouches, + description + ) VALUES (?, ?, ?, ?, ?, ?) + """, ( + 'SST001', + '쌍화탕', + 'CUSTOM', + 20, + 30, + '기혈을 보하고 원기를 회복시키는 대표적인 보약 처방' + )) + + formula_id = cursor.lastrowid + print(f"✅ 쌍화탕 처방 등록 완료 (ID: {formula_id})") + + # 2. 쌍화탕 구성 약재 등록 + # 전형적인 쌍화탕 구성 (1첩 기준 용량) + ingredients = [ + ('숙지황', 8.0, '보혈'), + ('당귀', 6.0, '보혈'), + ('백작약', 6.0, '보혈'), + ('천궁', 4.0, '활혈'), + ('황기', 6.0, '보기'), + ('인삼', 4.0, '대보원기'), + ('백출', 4.0, '보기건비'), + ('감초', 3.0, '조화제약'), + ('생강', 5.0, '온중'), + ('대추', 3.0, '보중익기'), + ('육계', 2.0, '온양'), + ('건강', 2.0, '온중') # 건강 포함! + ] + + for herb_name, grams, notes in ingredients: + # 약재 ID 찾기 + cursor.execute(""" + SELECT herb_item_id FROM herb_items + WHERE herb_name = ? + """, (herb_name,)) + + result = cursor.fetchone() + if result: + herb_id = result[0] + cursor.execute(""" + INSERT INTO formula_ingredients ( + formula_id, + herb_item_id, + grams_per_cheop, + notes + ) VALUES (?, ?, ?, ?) + """, (formula_id, herb_id, grams, notes)) + print(f" - {herb_name}: {grams}g ({notes})") + else: + print(f" ⚠️ {herb_name} 약재를 찾을 수 없음") + + conn.commit() + print("\n✅ 쌍화탕 처방 구성 완료!") + + # 3. 등록 확인 + cursor.execute(""" + SELECT + f.formula_name, + COUNT(fi.ingredient_id) as ingredient_count, + SUM(fi.grams_per_cheop) as total_grams_per_cheop + FROM formulas f + LEFT JOIN formula_ingredients fi ON f.formula_id = fi.formula_id + WHERE f.formula_id = ? + GROUP BY f.formula_id + """, (formula_id,)) + + result = cursor.fetchone() + if result: + print(f"\n📋 등록 결과:") + print(f" 처방명: {result[0]}") + print(f" 구성 약재: {result[1]}종") + print(f" 1첩 총량: {result[2]}g") + print(f" 20첩 총량: {result[2] * 20}g") + + except sqlite3.IntegrityError as e: + print(f"⚠️ 이미 등록된 처방이거나 오류 발생: {e}") + conn.rollback() + except Exception as e: + print(f"❌ 오류 발생: {e}") + conn.rollback() + finally: + conn.close() + +if __name__ == "__main__": + insert_ssanghwa_formula() \ No newline at end of file