37 lines
1.3 KiB
Python
37 lines
1.3 KiB
Python
from datetime import date, timedelta
|
|
from sqlalchemy import select
|
|
|
|
from app.database import async_session_factory
|
|
from app.models.batch import Batch
|
|
from app.models.medicine import Medicine
|
|
from app.config import settings
|
|
|
|
|
|
async def check_expiring_medicines():
|
|
async with async_session_factory() as db:
|
|
today = date.today()
|
|
|
|
for days in settings.EXPIRY_WARNING_DAYS:
|
|
target_date = today + timedelta(days=days)
|
|
|
|
query = select(Batch, Medicine).join(
|
|
Medicine, Batch.medicine_id == Medicine.id
|
|
).where(
|
|
Batch.expiry_date <= target_date,
|
|
Batch.is_expired == False,
|
|
Batch.quantity > 0
|
|
)
|
|
|
|
result = await db.execute(query)
|
|
batches = result.all()
|
|
|
|
if batches:
|
|
from app.notifications.manager import notification_manager
|
|
title = f"药品过期提醒 ({days}天内)"
|
|
content = "以下药品即将过期,请及时处理:\n\n"
|
|
|
|
for batch, medicine in batches:
|
|
content += f"- {medicine.name}: {batch.batch_no or '默认批次'} "
|
|
content += f"(过期日期: {batch.expiry_date})\n"
|
|
|
|
await notification_manager.send_notification(title, content) |