feat(drug-usage): 환자 뱃지 + limit 5000 + 현재고 IM_total

- 조제목록에 환자 뱃지 표시 (3명 이하: 전체, 3명 초과: 최근 3명 + 외 N명)
- API에 unique_patients, recent_patients 필드 추가
- limit 1000 → 5000 증가
- 현재고: 계산 방식 → IM_total.IM_QT_sale_debit (실제 DB 값)
This commit is contained in:
thug0bin
2026-03-11 20:25:42 +09:00
parent 6db31785fa
commit 88a23c26c1
2 changed files with 79 additions and 11 deletions

View File

@@ -8786,14 +8786,17 @@ def api_drug_usage():
rx_info = rx_data.get(code, {})
import_info = import_data.get(code, {})
import_qty = import_info.get('import_total_qty', 0)
rx_qty = rx_info.get('rx_total_qty', 0)
item = {
'drug_code': code,
'goods_name': rx_info.get('goods_name', ''),
'category': rx_info.get('category', ''),
'rx_count': rx_info.get('rx_count', 0),
'rx_total_qty': rx_info.get('rx_total_qty', 0),
'rx_total_qty': rx_qty,
'import_count': import_info.get('import_count', 0),
'import_total_qty': import_info.get('import_total_qty', 0)
'import_total_qty': import_qty,
'current_stock': 0 # IM_total에서 나중에 채움
}
# 약품명이 없으면 (입고만 있는 경우) PM_DRUG에서 조회
@@ -8807,6 +8810,20 @@ def api_drug_usage():
items.append(item)
# IM_total에서 현재 재고 조회
if items:
drug_codes = [item['drug_code'] for item in items]
placeholders = ','.join([f"'{c}'" for c in drug_codes])
stock_result = drug_session.execute(text(f"""
SELECT DrugCode, IM_QT_sale_debit
FROM IM_total
WHERE DrugCode IN ({placeholders})
"""))
stock_map = {row.DrugCode: float(row.IM_QT_sale_debit) if row.IM_QT_sale_debit else 0
for row in stock_result}
for item in items:
item['current_stock'] = stock_map.get(item['drug_code'], 0)
# 조제 건수 기준 정렬
items.sort(key=lambda x: x['rx_count'], reverse=True)
@@ -8915,14 +8932,25 @@ def api_drug_usage_prescriptions(drug_code):
"""), {'drug_code': drug_code, 'start_date': start_date, 'end_date': end_date})
items = []
seen_patients = set()
recent_patients = [] # 최근 조제받은 환자 (중복 제외, 최대 3명)
for row in result:
dosage = float(row.dosage) if row.dosage else 0
freq = float(row.frequency) if row.frequency else 0
days = int(row.days) if row.days else 0
patient = row.patient_name or ''
# 중복 제외 환자 목록 (최근순, 최대 3명)
if patient and patient not in seen_patients:
seen_patients.add(patient)
if len(recent_patients) < 3:
recent_patients.append(patient)
items.append({
'rx_date': row.rx_date or '',
'expiry_date': row.expiry_date or '',
'patient_name': row.patient_name or '',
'patient_name': patient,
'institution_name': row.institution_name or '',
'dosage': dosage,
'frequency': freq,
@@ -8934,6 +8962,8 @@ def api_drug_usage_prescriptions(drug_code):
'success': True,
'drug_code': drug_code,
'total_count': len(items),
'unique_patients': len(seen_patients),
'recent_patients': recent_patients,
'items': items
})
except Exception as e: