feat: PostgreSQL에서 image_url 직접 조회 (바코드=APC 케이스 지원)

This commit is contained in:
thug0bin 2026-02-28 11:45:27 +09:00
parent f438f42d15
commit 912679b137

View File

@ -2704,6 +2704,7 @@ def _get_animal_drug_rag(apc_codes):
result = conn.execute(text(f"""
SELECT apc, product_name, main_ingredient,
component_name_ko,
image_url1,
llm_pharm->>'사용가능 동물' as target_animals,
llm_pharm->>'분류' as category,
llm_pharm->>'체중/부위' as dosage_weight,
@ -2728,7 +2729,8 @@ def _get_animal_drug_rag(apc_codes):
'owner_caution': row.owner_caution or '',
'main_ingredient': row.component_name_ko or row.main_ingredient_detail or row.main_ingredient or '',
'description': row.description or '',
'usage_for': row.usage_for or ''
'usage_for': row.usage_for or '',
'image_url': row.image_url1 or ''
}
return rag_data
except Exception as e:
@ -2767,18 +2769,17 @@ def _get_animal_drugs():
rows = drug_session.execute(query).fetchall()
result = []
apc_list = []
for r in rows:
apc = r.APC_CODE if hasattr(r, 'APC_CODE') else None
barcode = r.BARCODE or ''
image_url = None
# APC가 없으면 바코드를 APC로 사용 (PostgreSQL에서 바코드=APC인 경우)
if not apc and barcode:
apc = barcode
if apc and apc.startswith('023'):
# 023으로 시작하는 APC만 이미지 URL 생성
image_url = f"https://ani.0bin.in/img/{apc}_F.jpg"
if apc:
apc_list.append(apc)
result.append({
'code': r.DrugCode,
@ -2786,9 +2787,27 @@ def _get_animal_drugs():
'price': float(r.Saleprice) if r.Saleprice else 0,
'barcode': barcode,
'apc': apc,
'image_url': image_url
'image_url': None # PostgreSQL에서 가져옴
})
# PostgreSQL에서 이미지 URL 가져오기
if apc_list:
try:
from sqlalchemy import create_engine
pg_engine = create_engine('postgresql://admin:trajet6640@192.168.0.87:5432/apdb_master')
with pg_engine.connect() as conn:
placeholders = ','.join([f"'{a}'" for a in apc_list])
img_result = conn.execute(text(f"""
SELECT apc, image_url1 FROM apc WHERE apc IN ({placeholders})
"""))
img_map = {row.apc: row.image_url1 for row in img_result}
for item in result:
if item['apc'] and item['apc'] in img_map:
item['image_url'] = img_map[item['apc']]
except Exception as e:
logging.warning(f"PostgreSQL 이미지 URL 조회 실패: {e}")
return result
except Exception as e:
logging.warning(f"동물약 목록 조회 실패: {e}")