feat: 소수 환자 약품 뱃지 표시

- 1년간 3명 이하 환자만 사용하는 약품에 환자 이름 뱃지 표시
- 조회 기간 내 사용한 환자는 핑크색으로 강조
- 매출액 컬럼명 변경 (약가 → 매출액)
- SUM(DRUPRICE)로 매출액 계산
This commit is contained in:
thug0bin
2026-03-07 00:43:02 +09:00
parent 846883cbfa
commit ee300f80ca
11 changed files with 456 additions and 11 deletions

View File

@@ -4088,6 +4088,50 @@ def api_rx_usage():
mssql_session = db_manager.get_session('PM_PRES')
# 1년간 사용 환자 3명 이하 약품의 환자 목록 조회 + 조회 기간 내 사용 여부
patient_query = text("""
WITH PatientUsage AS (
SELECT DISTINCT
P.DrugCode,
M.Paname,
MAX(CASE WHEN M.Indate >= :start_date AND M.Indate <= :end_date THEN 1 ELSE 0 END) as used_in_period
FROM PS_sub_pharm P
JOIN PS_main M ON P.PreSerial = M.PreSerial
WHERE M.Indate >= CONVERT(VARCHAR, DATEADD(YEAR, -1, GETDATE()), 112)
GROUP BY P.DrugCode, M.Paname
)
SELECT
PU.DrugCode as drug_code,
COUNT(*) as patient_count,
STUFF((
SELECT ', ' + PU2.Paname
FROM PatientUsage PU2
WHERE PU2.DrugCode = PU.DrugCode
ORDER BY PU2.Paname
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)'), 1, 2, '') as patient_names,
STUFF((
SELECT ', ' + PU3.Paname
FROM PatientUsage PU3
WHERE PU3.DrugCode = PU.DrugCode AND PU3.used_in_period = 1
ORDER BY PU3.Paname
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)'), 1, 2, '') as today_patients
FROM PatientUsage PU
GROUP BY PU.DrugCode
HAVING COUNT(*) <= 3
""")
patient_rows = mssql_session.execute(patient_query, {
'start_date': start_date_fmt,
'end_date': end_date_fmt
}).fetchall()
patient_map = {row.drug_code: {
'count': row.patient_count,
'names': row.patient_names,
'today': row.today_patients # 오늘 사용한 환자
} for row in patient_rows}
# 전문의약품 품목별 사용량 집계 쿼리 (현재고: IM_total.IM_QT_sale_debit, 위치: CD_item_position.CD_NM_sale)
rx_query = text("""
SELECT
@@ -4138,6 +4182,9 @@ def api_rx_usage():
amount = float(row.total_amount or 0)
rx_count = int(row.prescription_count or 0)
# 소수 환자 약품인지 확인 (1년간 3명 이하)
patient_info = patient_map.get(drug_code)
items.append({
'drug_code': drug_code,
'product_name': product_name,
@@ -4149,7 +4196,10 @@ def api_rx_usage():
'prescription_count': rx_count,
'current_stock': int(row.current_stock or 0), # 현재고
'location': row.location or '', # 약국 내 위치
'thumbnail': None
'thumbnail': None,
'patient_count': patient_info['count'] if patient_info else None, # 1년간 사용 환자 수 (3명 이하만)
'patient_names': patient_info['names'] if patient_info else None, # 환자 이름 (3명 이하만)
'today_patients': patient_info['today'] if patient_info else None # 오늘 사용한 환자
})
total_qty += qty