From fbe860a014a0e1a3db90974aafd040d440c8dbf4 Mon Sep 17 00:00:00 2001 From: aaron <> Date: Mon, 10 Mar 2025 18:14:22 +0800 Subject: [PATCH] update --- app/api/endpoints/order.py | 21 ++++++++++++++------- app/api/endpoints/order_additional_fee.py | 6 +----- app/api/endpoints/user.py | 23 +---------------------- app/models/user.py | 9 --------- 4 files changed, 16 insertions(+), 43 deletions(-) diff --git a/app/api/endpoints/order.py b/app/api/endpoints/order.py index 4abf0f7..2a68196 100644 --- a/app/api/endpoints/order.py +++ b/app/api/endpoints/order.py @@ -46,6 +46,8 @@ from app.core.coupon_manager import CouponManager from app.core.redis_client import redis_client from app.models.timeperiod import TimePeriodDB from app.models.community_timeperiod import CommunityTimePeriodDB +from app.models.community_profit_sharing import CommunityProfitSharing + router = APIRouter() def calculate_price(price_request: OrderPriceCalculateRequest,user: UserDB,db: Session) -> OrderPriceResult: @@ -147,11 +149,13 @@ def calculate_price(price_request: OrderPriceCalculateRequest,user: UserDB,db: S return result -def calculate_delivery_share(order: ShippingOrderDB, deliveryman: UserDB) -> float: - if deliveryman.delivery_commission_fixed > 0: - return deliveryman.delivery_commission_fixed - elif deliveryman.delivery_commission_rate > 0: - return round(order.original_amount_with_additional_fee * (deliveryman.delivery_commission_rate / 100.0), 2) +def calculate_delivery_share(order: ShippingOrderDB, db: Session) -> float: + # 获取对应小区的分润 + sharing = db.query(CommunityProfitSharing).filter( + CommunityProfitSharing.community_id == order.address_community_id + ).first() + if sharing: + return round(order.original_amount_with_additional_fee * (sharing.delivery_rate / 100.0), 2) else: return 0 @@ -486,6 +490,9 @@ async def get_order_detail( # 计算配送时间 delivery_time = format_delivery_time(order.delivery_date, order.time_period_name) + # 计算配送员配送费用 + deliveryman_share = order.delivery_share if order.delivery_share > 0 else calculate_delivery_share(order, db) + # 构建响应数据 order_data = { "orderid": order.orderid, @@ -500,7 +507,7 @@ async def get_order_detail( "additional_fee_amount": order.additional_fee_amount, "coupon_id": order.coupon_id, "final_amount": order.final_amount, - "deliveryman_share": order.delivery_share if order.delivery_share > 0 else calculate_delivery_share(order, current_user), + "deliveryman_share": deliveryman_share, "status": order.status, "complete_images": order.optimized_complete_images, "packages": package_list, @@ -1273,7 +1280,7 @@ async def deliveryman_receive_order( order.received_time = datetime.now() # 接单就确认收益 - order.delivery_share = calculate_delivery_share(order, deliveryman) + order.delivery_share = calculate_delivery_share(order, db) db.commit() diff --git a/app/api/endpoints/order_additional_fee.py b/app/api/endpoints/order_additional_fee.py index a3d2f88..1ab3bfa 100644 --- a/app/api/endpoints/order_additional_fee.py +++ b/app/api/endpoints/order_additional_fee.py @@ -166,11 +166,7 @@ async def accept_additional_fee( order.final_amount += float(fee_request.additional_fee_amount) # 重新计算配送员配送费用 - deliveryman = db.query(UserDB).filter( - UserDB.userid == order.deliveryman_user_id - ).first() - if deliveryman: - order.delivery_share = calculate_delivery_share(order, deliveryman) + order.delivery_share = calculate_delivery_share(order, db) try: db.commit() diff --git a/app/api/endpoints/user.py b/app/api/endpoints/user.py index d216e16..85512d3 100644 --- a/app/api/endpoints/user.py +++ b/app/api/endpoints/user.py @@ -23,7 +23,7 @@ from app.models.user_auth import UserAuthDB, UserAuthCreate, UserAuthInfo from app.core.qcloud import qcloud_manager from app.models.merchant import MerchantDB from app.models.address import AddressDB, AddressInfo -from app.models.user import UserUpdateRoles, UserUpdateDeliveryCommission +from app.models.user import UserUpdateRoles from app.models.order import ShippingOrderDB, OrderStatus from app.core.redis_client import redis_client import logging @@ -151,28 +151,7 @@ async def update_user_info( except Exception as e: db.rollback() return error_response(code=500, message=f"更新失败: {str(e)}") - -@router.put("/delivery-commission", response_model=ResponseModel) -async def update_delivery_commission( - update_data: UserUpdateDeliveryCommission, - db: Session = Depends(get_db), - current_user: UserDB = Depends(get_admin_user) -): - """更新配送佣金比例""" - user = db.query(UserDB).filter(UserDB.userid == update_data.user_id).first() - if not user: - return error_response(code=404, message="用户不存在") - - if update_data.delivery_commission_rate: - user.delivery_commission_rate = update_data.delivery_commission_rate - - if update_data.delivery_commission_fixed: - user.delivery_commission_fixed = update_data.delivery_commission_fixed - db.commit() - db.refresh(user) - - return success_response(message="配送佣金比例更新成功") @router.put("/roles", response_model=ResponseModel) async def update_user_roles( diff --git a/app/models/user.py b/app/models/user.py index 15d7ed2..f5e7902 100644 --- a/app/models/user.py +++ b/app/models/user.py @@ -52,8 +52,6 @@ class UserDB(Base): update_time = Column(DateTime(timezone=True), onupdate=func.now()) community_id = Column(Integer, ForeignKey("communities.id"), nullable=True) # 归属小区 is_auth = Column(Boolean, nullable=False, default=False) - delivery_commission_rate = Column(Integer, nullable=False, default=0) # 配送佣金比例 - delivery_commission_fixed = Column(DECIMAL(10, 2), nullable=False, default=0) # 配送佣金固定金额 @property def optimized_avatar(self): @@ -86,8 +84,6 @@ class UserInfo(BaseModel): community_id: Optional[int] = None community_name: Optional[str] = None is_auth: bool = False - delivery_commission_rate: Optional[int] = None - delivery_commission_fixed: Optional[float] = None class Config: from_attributes = True @@ -125,11 +121,6 @@ class UserUpdateRoles(BaseModel): user_id: int roles: List[UserRole] -class UserUpdateDeliveryCommission(BaseModel): - user_id: int - delivery_commission_rate: Optional[int] = None - delivery_commission_fixed: Optional[float] = None - def generate_user_code(db=None) -> str: """生成6位大写字母+数字的用户编码""" chars = string.ascii_uppercase + string.digits