diff --git a/app/api/endpoints/coupon_activity.py b/app/api/endpoints/coupon_activity.py index 082d5ab..9a21b9a 100644 --- a/app/api/endpoints/coupon_activity.py +++ b/app/api/endpoints/coupon_activity.py @@ -14,9 +14,10 @@ from app.api.deps import get_current_user, get_admin_user from app.models.user import UserDB from app.core.response import success_response, error_response, ResponseModel from typing import Optional, List -from datetime import datetime, time +from datetime import datetime, time, timedelta from app.core.coupon_manager import CouponManager + router = APIRouter() @router.post("", response_model=ResponseModel) @@ -137,13 +138,12 @@ async def get_coupon_activities( "items": activities_data }) -@router.post("/{activity_id}/receive", response_model=ResponseModel) -async def receive_coupons( +async def check_activity_can_receive( activity_id: int, db: Session = Depends(get_db), current_user: UserDB = Depends(get_current_user) ): - """领取优惠券""" + """检查活动是否可以领取""" # 查询活动 activity = db.query(CouponActivityDB).filter( CouponActivityDB.id == activity_id, @@ -151,12 +151,12 @@ async def receive_coupons( ).first() if not activity: - return error_response(code=404, message="活动不存在或已结束") + return False, "活动不存在或已结束", None # 检查领取时间 current_time = datetime.now().time() if current_time < activity.daily_start_time or current_time > activity.daily_end_time: - return error_response(code=400, message="不在领取时间范围内") + return False, "不在领取时间范围内", activity # 检查总领取次数 total_receive_count = db.query(func.count(CouponReceiveRecordDB.id)).filter( @@ -165,7 +165,7 @@ async def receive_coupons( ).scalar() if activity.total_limit > 0 and total_receive_count >= activity.total_limit: - return error_response(code=400, message=f"您只能领取 {activity.total_limit} 次") + return False, f"您只能领取 {activity.total_limit} 次", activity # 检查今日领取次数 today = datetime.now().date() @@ -177,15 +177,29 @@ async def receive_coupons( ).scalar() if today_receive_count >= activity.daily_limit: - return error_response(code=400, message="今日领取次数已达上限") - + return False, "今日领取次数已达上限", activity + + return True, "可领取", activity + +@router.post("/{activity_id}/receive", response_model=ResponseModel) +async def receive_coupons( + activity_id: int, + db: Session = Depends(get_db), + current_user: UserDB = Depends(get_current_user) +): + """领取优惠券""" + can_receive, message, activity = await check_activity_can_receive(activity_id, db, current_user) + if not can_receive: + return error_response(code=400, message=message) try: # 发放优惠券 for coupon_id, count in activity.coupon_config.items(): coupon = db.query(CouponDB).filter(CouponDB.id == coupon_id).first() if coupon: - expire_time = datetime.combine(today, datetime.max.time()) + today = datetime.now().date() + #过期时间:30 天 + expire_time = datetime.combine(today, datetime.max.time()) + timedelta(days=30) manager = CouponManager(db) manager.add_coupon( user_id=current_user.userid, @@ -218,6 +232,22 @@ async def receive_coupons( db.rollback() return error_response(code=500, message=f"领取失败: {str(e)}") +@router.get("/{activity_id}/check_receive", response_model=ResponseModel) +async def check_receive( + activity_id: int, + db: Session = Depends(get_db), + current_user: UserDB = Depends(get_current_user) +): + """检查是否领取过优惠券""" + can_receive, message, activity = await check_activity_can_receive(activity_id, db, current_user) + if not can_receive: + return error_response(code=400, message=message) + + return success_response(data={ + "can_receive": can_receive, + "message": message + }) + @router.put("/{activity_id}", response_model=ResponseModel) async def update_coupon_activity( activity_id: int, diff --git a/app/api/endpoints/wecom.py b/app/api/endpoints/wecom.py index 3cfe7fe..1d86d5a 100644 --- a/app/api/endpoints/wecom.py +++ b/app/api/endpoints/wecom.py @@ -137,7 +137,7 @@ async def wechat_corp_callback( if update_detail == 'add_member': print(f"发送欢迎消息") # 发送欢迎消息 - await wecom_client.send_welcome_message(chat_id) + # await wecom_client.send_welcome_message(chat_id) return Response(content="success", media_type="text/plain")