From 5448a04be496ff08f5da32fa13820389ea5e720b Mon Sep 17 00:00:00 2001 From: aaron <> Date: Sat, 29 Mar 2025 22:40:21 +0800 Subject: [PATCH] update --- app/api/endpoints/merchant_order.py | 78 +++++++++++++------------- app/api/endpoints/merchant_product.py | 33 ++++++++++- jobs.sqlite | Bin 24576 -> 24576 bytes 3 files changed, 70 insertions(+), 41 deletions(-) diff --git a/app/api/endpoints/merchant_order.py b/app/api/endpoints/merchant_order.py index 28972e1..8a855b8 100644 --- a/app/api/endpoints/merchant_order.py +++ b/app/api/endpoints/merchant_order.py @@ -310,6 +310,9 @@ async def complete_order( if not order: return error_response(code=404, message="订单不存在") + if merchant_user.userid != order.merchant.user_id: + return error_response(code=403, message="不是你的订单,无权限完成") + if order.status not in [MerchantOrderStatus.DELIVERING, MerchantOrderStatus.PICKUP_READY]: return error_response(code=400, message="订单状态不正确") @@ -326,24 +329,31 @@ async def complete_order( order_id = order.order_id ) + # 对商家进行结算 + account_manager = AccountManager(db) + settlement_amount = float(order.MerchantProductDB.settlement_amount) * order.MerchantOrderDB.qty + if settlement_amount > 0: + account_manager.change_balance( + user_id=order.merchant.user_id, + amount=settlement_amount, + description=order.merchant_product.name, + transaction_id=order.order_id + ) - user = db.query(UserDB).filter( - UserDB.userid == order.user_id - ).first() - - product = db.query(MerchantProductDB).filter( - MerchantProductDB.id == order.merchant_product_id - ).first() - if product: - product.sold_total += order.qty + # 更新商品销量 + if order.merchant_product: + order.merchant_product.sold_total += order.qty db.commit() # 发送商家订单完成消息 + user = db.query(UserDB).filter( + UserDB.userid == order.user_id + ).first() if user and user.mp_openid: data={ "character_string7": order_id, - "thing5": product.name, + "thing5": order.merchant_product.name, "character_string24": order.qty, "time10": datetime.now().strftime("%Y-%m-%d %H:%M:%S") } @@ -467,61 +477,53 @@ async def verify_order( ): """核销订单""" # 查询订单及相关信息 - order = db.query( - MerchantOrderDB, - MerchantProductDB, - MerchantDB - ).join( - MerchantProductDB, - MerchantOrderDB.merchant_product_id == MerchantProductDB.id - ).join( - MerchantDB, - MerchantProductDB.merchant_id == MerchantDB.id - ).filter( + order = db.query(MerchantOrderDB).filter( MerchantOrderDB.order_verify_code == request.verify_code, - MerchantOrderDB.status == MerchantOrderStatus.PICKUP_READY, - MerchantDB.user_id == merchant_user.userid + MerchantOrderDB.status == MerchantOrderStatus.PICKUP_READY ).first() if not order: return error_response(code=404, message="订单不存在或已核销") + if merchant_user.userid != order.merchant.user_id: + return error_response(code=403, message="不是你的订单,无权限核销") + try: # 更新核销时间和核销用户 - order.MerchantOrderDB.verify_time = datetime.now(timezone.utc) - order.MerchantOrderDB.verify_user_id = merchant_user.userid - order.MerchantOrderDB.status = MerchantOrderStatus.COMPLETED + order.verify_time = datetime.now(timezone.utc) + order.verify_user_id = merchant_user.userid + order.status = MerchantOrderStatus.COMPLETED # 如果有积分奖励,赠送积分 - if order.MerchantOrderDB.gift_points > 0: + if order.gift_points > 0: point_manager = PointManager(db) point_manager.add_points( - user_id=order.MerchantOrderDB.user_id, - points=order.MerchantOrderDB.gift_points, - description=f"团购券核销奖励", - order_id=order.MerchantOrderDB.order_id + user_id=order.user_id, + points=order.gift_points, + description=order.merchant_product.name, + order_id=order.order_id ) # 对商家进行结算 account_manager = AccountManager(db) - settlement_amount = float(order.MerchantProductDB.settlement_amount) * order.MerchantOrderDB.qty + settlement_amount = float(order.settlement_amount) * order.qty if settlement_amount > 0: account_manager.change_balance( - user_id=order.MerchantDB.user_id, + user_id=order.merchant.user_id, amount=settlement_amount, - description=f"团购券核销", - transaction_id=order.MerchantOrderDB.order_id + description=order.merchant_product.name, + transaction_id=order.order_id ) # 更新商品销量 - if order.MerchantProductDB: - order.MerchantProductDB.sold_total += order.MerchantOrderDB.qty + if order.merchant_product: + order.merchant_product.sold_total += order.qty db.commit() return success_response( message="核销成功", - data=MerchantOrderInfo.model_validate(order.MerchantOrderDB) + data=MerchantOrderInfo.model_validate(order) ) except Exception as e: db.rollback() diff --git a/app/api/endpoints/merchant_product.py b/app/api/endpoints/merchant_product.py index cc6bdc8..900f64f 100644 --- a/app/api/endpoints/merchant_product.py +++ b/app/api/endpoints/merchant_product.py @@ -8,7 +8,7 @@ from app.models.merchant_product import ( MerchantProductInfo ) from app.models.database import get_db -from app.api.deps import get_admin_user +from app.api.deps import get_admin_user, get_merchant_user from app.models.user import UserDB from app.core.response import success_response, error_response, ResponseModel from app.models.merchant import MerchantDB @@ -77,15 +77,42 @@ async def update_product( db.rollback() return error_response(code=500, message=f"更新失败: {str(e)}") +@router.get("/merchant_product_list", response_model=ResponseModel) +async def merchant_product_list( + db: Session = Depends(get_db), + current_user: UserDB = Depends(get_merchant_user) +): + """获取商家产品列表(商家端)""" + + merchant = db.query(MerchantDB).filter(MerchantDB.user_id == current_user.userid).first() + if not merchant: + return error_response(code=404, message="商家不存在") + + products = db.query(MerchantProductDB).filter(MerchantProductDB.merchant_id == merchant.id).all() + + product_list = [] + for product in products: + product_list.append( + { + **MerchantProductInfo.model_validate(product).model_dump(), + "merchant": MerchantInfo.model_validate(merchant).model_dump(), + "total_sales_amount": 0, + "total_profit_amount": 0 + } + ) + + return success_response(data=product_list) + + @router.get("/list", response_model=ResponseModel) async def list_merchant_products( user_id: Optional[int] = None, community_id: Optional[int] = None, skip: int = 0, limit: int = 20, - db: Session = Depends(get_db) + db: Session = Depends(get_db), ): - """获取商品列表""" + """获取商品列表(用户端)""" # 联表查询商家信息 query = db.query(MerchantProductDB).options( joinedload(MerchantProductDB.merchant) diff --git a/jobs.sqlite b/jobs.sqlite index 70eb5f2cb16aace87c4ed0a9240de61428a3c9a8..9902dba2428be45f7451274c7ab58c6985492600 100644 GIT binary patch delta 21 bcmZoTz}Rqrae@>Rr~E`2Cm^{oVL?0qN=F8O delta 21 bcmZoTz}Rqrae@>R$J>cAPC#;F!h(1JR}}~v