# -*- coding: utf-8 -*-
import pyodbc

conn_str = (
    'DRIVER={ODBC Driver 17 for SQL Server};'
    'SERVER=192.168.0.4\\PM2014;'
    'DATABASE=PM_PRES;'
    'UID=sa;'
    'PWD=tmddls214!%(;'
    'TrustServerCertificate=yes;'
    'Connection Timeout=10'
)

conn = pyodbc.connect(conn_str, timeout=10)
cur = conn.cursor()

# 오늘 처방 있는지 확인
cur.execute("""
    SELECT COUNT(*) as cnt, MAX(Indate) as last_date
    FROM PS_main 
    WHERE Indate = CONVERT(VARCHAR, GETDATE(), 112)
""")
row = cur.fetchone()
print(f'오늘(20260307) 처방 수: {row.cnt}')

# 최근 처방일
cur.execute("SELECT MAX(Indate) FROM PS_main")
print(f'최근 처방일: {cur.fetchone()[0]}')

# 어제 처방 약품 중 3명 이하 확인 (테스트용)
cur.execute("""
    SELECT TOP 5 P.DrugCode, M.Paname, M.Indate
    FROM PS_sub_pharm P
    JOIN PS_main M ON P.PreSerial = M.PreSerial
    WHERE M.Indate = '20260306'
    ORDER BY P.DrugCode
""")
print('\n=== 3/6 처방 샘플 ===')
for row in cur.fetchall():
    print(f'{row.DrugCode}: {row.Paname}')
