This commit is contained in:
aaron 2025-03-26 23:52:12 +08:00
parent a12ed5d934
commit d33be5dda7
4 changed files with 56 additions and 24 deletions

View File

@ -35,6 +35,7 @@ from app.models.merchant_product import DeliveryType, DeliveryTimeType
from datetime import timedelta
from fastapi import BackgroundTasks
from app.core.mpmessage import sent_merchant_order_status_change_message
from sqlalchemy.orm import joinedload
router = APIRouter()
@router.post("", response_model=ResponseModel)
@ -120,7 +121,7 @@ async def create_merchant_order(
@router.get("/merchant", response_model=ResponseModel)
async def get_merchant_orders(
skip: int = 0,
limit: int = 100,
limit: int = 20,
delivery_type: Optional[DeliveryType] = None,
delivery_time_type: Optional[DeliveryTimeType] = None,
status: Optional[MerchantOrderStatus] = None,
@ -128,8 +129,19 @@ async def get_merchant_orders(
merchant_user: UserDB = Depends(get_merchant_user)
):
"""获取商家订单列表"""
merchant = db.query(MerchantDB).filter(
MerchantDB.user_id == merchant_user.userid
).first()
if not merchant:
return error_response(code=404, message="商家不存在")
query = db.query(MerchantOrderDB).filter(
MerchantOrderDB.merchant_id == merchant_user.userid
MerchantOrderDB.merchant_id == merchant.id
).options(
joinedload(MerchantOrderDB.merchant_product),
joinedload(MerchantOrderDB.merchant),
joinedload(MerchantOrderDB.address)
).order_by(
MerchantOrderDB.create_time.desc()
)
@ -148,23 +160,21 @@ async def get_merchant_orders(
query = query.filter(
MerchantOrderDB.status == status
)
total = query.count()
orders = query.offset(skip).limit(limit).all()
result = []
for order in orders:
product = db.query(MerchantProductDB).filter(
MerchantProductDB.id == order.merchant_product_id
).first()
merchant = db.query(MerchantDB).filter(
MerchantDB.id == product.merchant_id
).first()
result.append({
"order" : MerchantOrderInfo.model_validate(order),
"product" : MerchantProductInfo.model_validate(product),
"merchant" : MerchantInfo.model_validate(merchant)
"product" : MerchantProductInfo.model_validate(order.merchant_product),
"merchant" : MerchantInfo.model_validate(order.merchant),
"address" : AddressInfo.model_validate(order.address)
})
return success_response(data=result)
return success_response(data={
"total": total,
"items": result
})
@router.get("/user", response_model=ResponseModel)
async def get_user_orders(
@ -174,11 +184,14 @@ async def get_user_orders(
current_user: UserDB = Depends(get_current_user)
):
"""获取用户的订单列表"""
orders = db.query(MerchantOrderDB).filter(
query = db.query(MerchantOrderDB).filter(
MerchantOrderDB.user_id == current_user.userid
).order_by(
MerchantOrderDB.create_time.desc()
).offset(skip).limit(limit).all()
)
total = query.count()
orders = query.offset(skip).limit(limit).all()
result = []
for order in orders:
@ -196,7 +209,10 @@ async def get_user_orders(
})
return success_response(data=result)
return success_response(data={
"total": total,
"items": result
})
@router.put("/{order_id}/user/cancel", response_model=ResponseModel)
async def cancel_order(
@ -245,12 +261,11 @@ async def complete_order(
if not order:
return error_response(code=404, message="订单不存在")
if order.status not in [MerchantOrderStatus.DELIVERING, MerchantOrderStatus.PICKUP_READY]:
if order.status not in [MerchantOrderStatus.DELIVERING]:
return error_response(code=400, message="订单状态不正确")
try:
order.status = MerchantOrderStatus.COMPLETED
db.commit()
# 如果订单有赠送积分,增加积分
if order.gift_points > 0:
@ -270,6 +285,10 @@ async def complete_order(
product = db.query(MerchantProductDB).filter(
MerchantProductDB.id == order.merchant_product_id
).first()
if product:
product.sold_total += order.qty
db.commit()
if user and user.mp_openid:
data={
@ -374,7 +393,7 @@ async def query_verify(
MerchantProductDB.merchant_id == MerchantDB.id
).filter(
MerchantOrderDB.order_verify_code == verify_code,
MerchantOrderDB.status == MerchantOrderStatus.DELIVERING,
MerchantOrderDB.status == MerchantOrderStatus.PICKUP_READY,
MerchantDB.user_id == merchant_user.userid
).first()
@ -400,7 +419,7 @@ async def verify_order(
MerchantProductDB.merchant_id == MerchantDB.id
).filter(
MerchantOrderDB.order_verify_code == request.verify_code,
MerchantOrderDB.status == MerchantOrderStatus.DELIVERING,
MerchantOrderDB.status == MerchantOrderStatus.PICKUP_READY,
MerchantDB.user_id == merchant_user.userid
).first()
@ -434,6 +453,10 @@ async def verify_order(
description=f"团购券核销",
transaction_id=order.MerchantOrderDB.order_id
)
# 更新商品销量
if order.MerchantProductDB:
order.MerchantProductDB.sold_total += order.MerchantOrderDB.qty
db.commit()
return success_response(

View File

@ -18,6 +18,7 @@ from app.models.merchant_product import OperationType, DeliveryType, DeliveryTim
from app.models.merchant import MerchantInfo
from app.models.address import AddressDB, AddressInfo, AddressType
from app.api.deps import get_current_user
from app.core.config import settings
router = APIRouter()
@ -109,10 +110,9 @@ async def list_merchant_products(
product_info = MerchantProductInfo.model_validate(product)
products.append({
**product_info.model_dump(),
"merchant_name": product.merchant.name,
"operation_type_name": "蜂快" if product.operation_type == OperationType.SELF_OPERATED else "业主",
"delivery_type_name": "配送到家" if product.delivery_type == DeliveryType.DELIVERY else "自提",
"delivery_time_type_name": "及时达" if product.delivery_time_type == DeliveryTimeType.IMMEDIATE else "定时达"
"delivery_time_type_name": "小时达" if product.delivery_time_type == DeliveryTimeType.IMMEDIATE else "社区团购",
"pickup_time_format": f"{product.delivery_date.strftime('%m月%d')} {product.pickup_time_from.strftime('%H:%M')}~{product.pickup_time_to.strftime('%H:%M')}" if product.delivery_date and product.pickup_time_from and product.pickup_time_to else None
})
return success_response(data={
@ -141,8 +141,12 @@ async def get_product(
result = {
**product_info.model_dump(),
"gift_points" : int(float(product.gift_points_rate) / 10 * float(product.sale_price)),
"merchant": merchant.model_dump()
"gift_points" : int(float(product.gift_points_rate) / 100 * float(product.sale_price)) * settings.POINT_RATIO,
"merchant": merchant.model_dump(),
"delivery_type_name": "配送到家" if product.delivery_type == DeliveryType.DELIVERY else "自提",
"delivery_time_type_name": "小时达" if product.delivery_time_type == DeliveryTimeType.IMMEDIATE else "社区团购",
"pickup_time_format": f"{product.delivery_date.strftime('%m月%d')} {product.pickup_time_from.strftime('%H:%M')}~{product.pickup_time_to.strftime('%H:%M')}" if product.delivery_date and product.pickup_time_from and product.pickup_time_to else None
}
# 获取默认地址

View File

@ -9,6 +9,7 @@ from .merchant_product import DeliveryType, DeliveryTimeType
import random
import time as time_module
import enum
from sqlalchemy.orm import relationship
class MerchantOrderStatus(str, enum.Enum):
CREATED = "CREATED" # 已创建(待支付)
@ -57,6 +58,10 @@ class MerchantOrderDB(Base):
refund_transaction_id = Column(String(64)) # 微信退款交易号
refund_time = Column(DateTime(timezone=True), nullable=True)
merchant_product = relationship("MerchantProductDB", backref="merchant_orders")
merchant = relationship("MerchantDB", backref="merchant_orders")
address = relationship("AddressDB", backref="merchant_orders")
class MerchantOrderCreate(BaseModel):
merchant_product_id: int

Binary file not shown.