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

View File

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

View File

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

View File

@ -74,6 +74,25 @@ ADD COLUMN qty INT NOT NULL DEFAULT 1 COMMENT '购买数量' AFTER address_id;
ALTER TABLE merchant_orders
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
UPDATE merchant_orders
SET address_id = NULL
@ -91,4 +110,5 @@ MODIFY COLUMN address_id INT NOT NULL COMMENT '收货地址ID';
-- 修改status字段的枚举类型
ALTER TABLE merchant_orders
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.