This commit is contained in:
aaron 2025-03-22 12:13:04 +08:00
parent ef65140d20
commit 71c114825a
5 changed files with 100 additions and 61 deletions

View File

@ -8,14 +8,14 @@ from app.models.merchant_order import (
MerchantOrderInfo, MerchantOrderInfo,
MerchantOrderStatus MerchantOrderStatus
) )
from app.models.address import AddressDB, AddressType from app.models.address import AddressDB, AddressInfo
from app.models.merchant_product import MerchantProductDB from app.models.merchant_product import MerchantProductDB, MerchantProductInfo
from app.models.database import get_db from app.models.database import get_db
from app.api.deps import get_current_user, get_admin_user, get_merchant_user from app.api.deps import get_current_user, get_admin_user, get_merchant_user
from app.models.user import UserDB from app.models.user import UserDB
from app.core.response import success_response, error_response, ResponseModel from app.core.response import success_response, error_response, ResponseModel
from datetime import datetime, timezone from datetime import datetime, timezone
from app.models.merchant import MerchantDB from app.models.merchant import MerchantDB, MerchantInfo
from app.models.point import PointRecordDB from app.models.point import PointRecordDB
from app.core.account import AccountManager from app.core.account import AccountManager
from app.core.wechat import WeChatClient from app.core.wechat import WeChatClient
@ -64,6 +64,7 @@ async def create_merchant_order(
db_order = MerchantOrderDB( db_order = MerchantOrderDB(
order_id=order_id, order_id=order_id,
user_id=current_user.userid, user_id=current_user.userid,
merchant_id=product.merchant_id, # 从产品中获取商家ID
merchant_product_id=order.merchant_product_id, merchant_product_id=order.merchant_product_id,
unit_price=product.sale_price, unit_price=product.sale_price,
qty=order.qty, qty=order.qty,
@ -92,54 +93,70 @@ async def get_user_orders(
current_user: UserDB = Depends(get_current_user) current_user: UserDB = Depends(get_current_user)
): ):
"""获取用户的订单列表""" """获取用户的订单列表"""
orders = db.query( orders = db.query(MerchantOrderDB).filter(
MerchantOrderDB, MerchantOrderDB.user_id == current_user.userid
MerchantProductDB.name.label('product_name'), ).order_by(
MerchantProductDB.image_url.label('product_image'), MerchantOrderDB.create_time.desc()
MerchantProductDB.tags.label('product_tags'), ).offset(skip).limit(limit).all()
MerchantDB.name.label('merchant_name'),
MerchantDB.latitude.label('merchant_latitude'), result = []
MerchantDB.longitude.label('merchant_longitude'), for order in orders:
MerchantDB.phone.label('merchant_phone'), product = db.query(MerchantProductDB).filter(
MerchantDB.id.label('merchant_id') MerchantProductDB.id == order.merchant_product_id
).join( ).first()
MerchantProductDB, merchant = db.query(MerchantDB).filter(
MerchantOrderDB.merchant_product_id == MerchantProductDB.id MerchantDB.id == product.merchant_id
).join( ).first()
MerchantDB,
MerchantProductDB.merchant_id == MerchantDB.id result.append({
).filter( "order" : MerchantOrderInfo.model_validate(order),
"product" : MerchantProductInfo.model_validate(product),
"merchant" : MerchantInfo.model_validate(merchant)
})
return success_response(data=result)
@router.put("/{order_id}/cancel", response_model=ResponseModel)
async def cancel_order(
order_id: str,
db: Session = Depends(get_db),
current_user: UserDB = Depends(get_current_user)
):
"""取消订单"""
order = db.query(MerchantOrderDB).filter(
MerchantOrderDB.order_id == order_id,
MerchantOrderDB.user_id == current_user.userid MerchantOrderDB.user_id == current_user.userid
).order_by( ).first()
MerchantOrderDB.create_time.desc() if not order:
).offset(skip).limit(limit).all() return error_response(code=404, message="订单不存在")
# 构建返回数据 order.status = MerchantOrderStatus.CANCELLED
order_list = [{ db.commit()
"id": order.MerchantOrderDB.id,
"order_id": order.MerchantOrderDB.order_id, #TODO 退款
"user_id": order.MerchantOrderDB.user_id,
"merchant_product_id": order.MerchantOrderDB.merchant_product_id, return success_response(data=MerchantOrderInfo.model_validate(order))
"order_amount": order.MerchantOrderDB.order_amount,
"status": order.MerchantOrderDB.status, @router.put("/{order_id}/confirm", response_model=ResponseModel)
"order_verify_code": order.MerchantOrderDB.order_verify_code, async def confirm_order(
"verify_time": order.MerchantOrderDB.verify_time, order_id: str,
"verify_user_id": order.MerchantOrderDB.verify_user_id, db: Session = Depends(get_db),
"create_time": order.MerchantOrderDB.create_time, merchant_user: UserDB = Depends(get_merchant_user)
"update_time": order.MerchantOrderDB.update_time, ):
# 商品信息 """商家确认订单"""
"product_name": order.product_name, order = db.query(MerchantOrderDB).filter(
"product_image": process_image(order.product_image).thumbnail(width=800, height=800).format(ImageFormat.WEBP).build(), MerchantOrderDB.order_id == order_id,
"product_tags": order.product_tags, MerchantOrderDB.merchant_id == merchant_user.userid
# 商家信息 ).first()
"merchant_id": order.merchant_id, if not order:
"merchant_name": order.merchant_name, return error_response(code=404, message="订单不存在")
"merchant_latitude": order.merchant_latitude,
"merchant_longitude": order.merchant_longitude, order.status = MerchantOrderStatus.DELIVERING
"merchant_phone": order.merchant_phone db.commit()
} for order in orders]
return success_response(data=MerchantOrderInfo.model_validate(order))
return success_response(data=order_list)
@router.get("/merchant/verify/{verify_code}", response_model=ResponseModel) @router.get("/merchant/verify/{verify_code}", response_model=ResponseModel)
async def query_verify( async def query_verify(
@ -160,7 +177,7 @@ async def query_verify(
MerchantProductDB.merchant_id == MerchantDB.id MerchantProductDB.merchant_id == MerchantDB.id
).filter( ).filter(
MerchantOrderDB.order_verify_code == verify_code, MerchantOrderDB.order_verify_code == verify_code,
MerchantOrderDB.status == MerchantOrderStatus.UNVERIFIED, MerchantOrderDB.status == MerchantOrderStatus.DELIVERING,
MerchantDB.user_id == merchant_user.userid MerchantDB.user_id == merchant_user.userid
).first() ).first()
@ -186,7 +203,7 @@ async def verify_order(
MerchantProductDB.merchant_id == MerchantDB.id MerchantProductDB.merchant_id == MerchantDB.id
).filter( ).filter(
MerchantOrderDB.order_verify_code == request.verify_code, MerchantOrderDB.order_verify_code == request.verify_code,
MerchantOrderDB.status == MerchantOrderStatus.UNVERIFIED, MerchantOrderDB.status == MerchantOrderStatus.DELIVERING,
MerchantDB.user_id == merchant_user.userid MerchantDB.user_id == merchant_user.userid
).first() ).first()
@ -197,7 +214,7 @@ async def verify_order(
# 更新核销时间和核销用户 # 更新核销时间和核销用户
order.MerchantOrderDB.verify_time = datetime.now(timezone.utc) order.MerchantOrderDB.verify_time = datetime.now(timezone.utc)
order.MerchantOrderDB.verify_user_id = merchant_user.userid order.MerchantOrderDB.verify_user_id = merchant_user.userid
order.MerchantOrderDB.status = MerchantOrderStatus.VERIFIED order.MerchantOrderDB.status = MerchantOrderStatus.COMPLETED
# 如果有积分奖励,赠送积分 # 如果有积分奖励,赠送积分
@ -241,7 +258,7 @@ async def apply_refund(
order = db.query(MerchantOrderDB).filter( order = db.query(MerchantOrderDB).filter(
MerchantOrderDB.order_id == order_id, MerchantOrderDB.order_id == order_id,
MerchantOrderDB.user_id == current_user.userid, # 只能申请自己的订单 MerchantOrderDB.user_id == current_user.userid, # 只能申请自己的订单
MerchantOrderDB.status == MerchantOrderStatus.UNVERIFIED # 只有未核销的订单可以退款 MerchantOrderDB.status == MerchantOrderStatus.PENDING # 只有未核销的订单可以退款
).first() ).first()
if not order: if not order:
@ -302,16 +319,16 @@ async def get_order_detail(
order_data = { order_data = {
**order.model_dump(), "order" : MerchantOrderInfo.model_validate(order),
**product.model_dump(), "product" : MerchantProductInfo.model_validate(product),
**merchant.model_dump() "merchant" : MerchantInfo.model_validate(merchant)
} }
if order.address_id: if order.address_id:
address = db.query(AddressDB).filter( address = db.query(AddressDB).filter(
AddressDB.id == order.address_id AddressDB.id == order.address_id
).first() ).first()
order_data["address"] = address.model_dump() order_data["address"] = AddressInfo.model_validate(address)
return success_response(data=order_data) return success_response(data=order_data)
@ -423,13 +440,13 @@ async def get_merchant_order_summary(
# 查询商家昨日、今日订单数量 # 查询商家昨日、今日订单数量
yesterday_total = db.query(MerchantOrderDB).filter( yesterday_total = db.query(MerchantOrderDB).filter(
MerchantOrderDB.user_id == merchant_user.userid, MerchantOrderDB.user_id == merchant_user.userid,
MerchantOrderDB.status == MerchantOrderStatus.VERIFIED, MerchantOrderDB.status == MerchantOrderStatus.COMPLETED,
MerchantOrderDB.create_time.between(yesterday_start, yesterday_end) MerchantOrderDB.create_time.between(yesterday_start, yesterday_end)
).count() ).count()
today_total = db.query(MerchantOrderDB).filter( today_total = db.query(MerchantOrderDB).filter(
MerchantOrderDB.user_id == merchant_user.userid, MerchantOrderDB.user_id == merchant_user.userid,
MerchantOrderDB.status == MerchantOrderStatus.VERIFIED, MerchantOrderDB.status == MerchantOrderStatus.COMPLETED,
MerchantOrderDB.create_time.between(today_start, today_end) MerchantOrderDB.create_time.between(today_start, today_end)
).count() ).count()

View File

@ -60,9 +60,9 @@ class AddressUpdate(BaseModel):
class AddressInfo(BaseModel): class AddressInfo(BaseModel):
id: int id: int
community_id: int community_id: Optional[int] = None
community_name: Optional[str] = None community_name: Optional[str] = None
community_building_id: Optional[int] community_building_id: Optional[int] = None
community_building_name: Optional[str] = None community_building_name: Optional[str] = None
address_detail: str address_detail: str
name: str name: str

View File

@ -26,6 +26,7 @@ class MerchantOrderDB(Base):
order_id = Column(String(15), unique=True, nullable=False) order_id = Column(String(15), unique=True, nullable=False)
qrcode_url = Column(String(200)) # 核销码二维码图片地址 qrcode_url = Column(String(200)) # 核销码二维码图片地址
user_id = Column(Integer, ForeignKey("users.userid"), nullable=False) user_id = Column(Integer, ForeignKey("users.userid"), nullable=False)
merchant_id = Column(Integer, ForeignKey("merchants.id"), nullable=False) # 商家ID
merchant_product_id = Column(Integer, ForeignKey("merchant_products.id"), nullable=False) merchant_product_id = Column(Integer, ForeignKey("merchant_products.id"), nullable=False)
address_id = Column(Integer, ForeignKey("delivery_addresses.id"), nullable=False) # 收货地址ID address_id = Column(Integer, ForeignKey("delivery_addresses.id"), nullable=False) # 收货地址ID
qty = Column(Integer, nullable=False, default=1) # 购买数量 qty = Column(Integer, nullable=False, default=1) # 购买数量
@ -58,6 +59,7 @@ class MerchantOrderInfo(BaseModel):
id: int id: int
order_id: str order_id: str
user_id: int user_id: int
merchant_id: int # 商家ID
merchant_product_id: int merchant_product_id: int
address_id: int # 收货地址ID address_id: int # 收货地址ID
qty: int # 购买数量 qty: int # 购买数量

View File

@ -74,6 +74,25 @@ ADD COLUMN qty INT NOT NULL DEFAULT 1 COMMENT '购买数量' AFTER address_id;
ALTER TABLE merchant_orders ALTER TABLE merchant_orders
ADD COLUMN unit_price DECIMAL(10,2) NOT NULL COMMENT '产品单价' AFTER qty; ADD COLUMN unit_price DECIMAL(10,2) NOT NULL COMMENT '产品单价' AFTER qty;
-- 添加merchant_id字段先不添加外键约束
ALTER TABLE merchant_orders
ADD COLUMN merchant_id INT COMMENT '商家ID' AFTER user_id;
-- 从产品表中获取商家ID更新到订单表
UPDATE merchant_orders mo
INNER JOIN merchant_products mp ON mo.merchant_product_id = mp.id
SET mo.merchant_id = mp.merchant_id;
-- 添加外键约束
ALTER TABLE merchant_orders
ADD CONSTRAINT fk_merchant_orders_merchant
FOREIGN KEY (merchant_id) REFERENCES merchants(id);
-- 修改merchant_id为NOT NULL
ALTER TABLE merchant_orders
MODIFY COLUMN merchant_id INT NOT NULL COMMENT '商家ID';
-- 更新现有记录的address_id为NULL -- 更新现有记录的address_id为NULL
UPDATE merchant_orders UPDATE merchant_orders
SET address_id = NULL SET address_id = NULL
@ -92,3 +111,4 @@ MODIFY COLUMN address_id INT NOT NULL COMMENT '收货地址ID';
ALTER TABLE merchant_orders ALTER TABLE merchant_orders
MODIFY COLUMN status ENUM('CREATED', 'PENDING', 'DELIVERING', 'PICKUP_READY', 'COMPLETED', 'CANCELLED', 'REFUNDING', 'REFUNDED') MODIFY COLUMN status ENUM('CREATED', 'PENDING', 'DELIVERING', 'PICKUP_READY', 'COMPLETED', 'CANCELLED', 'REFUNDING', 'REFUNDED')
NOT NULL DEFAULT 'CREATED' COMMENT '订单状态'; NOT NULL DEFAULT 'CREATED' COMMENT '订单状态';

Binary file not shown.