28 lines
987 B
Python
28 lines
987 B
Python
"""订单支付超时自动关闭任务。"""
|
|
import logging
|
|
from datetime import datetime, timedelta
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.crud.order import order_crud
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def close_timeout_online_orders(db: Session) -> int:
|
|
"""关闭超时未支付的线上订单(超时分钟数来自系统设置 auto_cancel_minutes)。"""
|
|
from app.api.system_config import get_auto_cancel_minutes_for_job
|
|
|
|
timeout_minutes = get_auto_cancel_minutes_for_job(db)
|
|
before_time = datetime.now() - timedelta(minutes=timeout_minutes)
|
|
timeout_note = f"超过未支付自动取消时限({timeout_minutes} 分钟)"
|
|
return order_crud.close_unpaid_online_orders(
|
|
db, before_time=before_time, timeout_note=timeout_note
|
|
)
|
|
|
|
|
|
def run_close_timeout_online_orders(db: Session):
|
|
"""定时任务包装。"""
|
|
count = close_timeout_online_orders(db)
|
|
logger.info(f"线上支付超时订单自动关闭完成: {count} 条")
|