添加在线买单接口
This commit is contained in:
parent
38cbb8695e
commit
24354b9373
138
app/api/endpoints/merchant_pay_order.py
Normal file
138
app/api/endpoints/merchant_pay_order.py
Normal file
@ -0,0 +1,138 @@
|
|||||||
|
from fastapi import APIRouter, Depends
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from typing import Optional
|
||||||
|
from app.models.merchant_pay_order import (
|
||||||
|
MerchantPayOrderDB,
|
||||||
|
MerchantPayOrderCreate,
|
||||||
|
MerchantPayOrderInfo,
|
||||||
|
MerchantPayOrderStatus,
|
||||||
|
generate_pay_order_id
|
||||||
|
)
|
||||||
|
from app.models.merchant import MerchantDB
|
||||||
|
from app.models.database import get_db
|
||||||
|
from app.api.deps import get_current_user
|
||||||
|
from app.models.user import UserDB
|
||||||
|
from app.core.response import success_response, error_response, ResponseModel
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.post("", response_model=ResponseModel)
|
||||||
|
async def create_pay_order(
|
||||||
|
order: MerchantPayOrderCreate,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: UserDB = Depends(get_current_user)
|
||||||
|
):
|
||||||
|
"""创建支付订单"""
|
||||||
|
# 检查商家是否存在
|
||||||
|
merchant = db.query(MerchantDB).filter(
|
||||||
|
MerchantDB.id == order.merchant_id
|
||||||
|
).first()
|
||||||
|
if not merchant:
|
||||||
|
return error_response(code=404, message="商家不存在")
|
||||||
|
|
||||||
|
# 生成订单号
|
||||||
|
order_id = generate_pay_order_id()
|
||||||
|
|
||||||
|
# 创建订单
|
||||||
|
db_order = MerchantPayOrderDB(
|
||||||
|
order_id=order_id,
|
||||||
|
merchant_id=order.merchant_id,
|
||||||
|
user_id=current_user.userid,
|
||||||
|
amount=order.amount,
|
||||||
|
gift_points=order.gift_points
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
db.add(db_order)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_order)
|
||||||
|
|
||||||
|
# 获取完整订单信息
|
||||||
|
order = db.query(MerchantPayOrderDB).join(
|
||||||
|
MerchantDB,
|
||||||
|
MerchantPayOrderDB.merchant_id == MerchantDB.id
|
||||||
|
).filter(
|
||||||
|
MerchantPayOrderDB.id == db_order.id
|
||||||
|
).first()
|
||||||
|
|
||||||
|
# 获取商家名称
|
||||||
|
merchant_name = db.query(MerchantDB.name).filter(
|
||||||
|
MerchantDB.id == order.merchant_id
|
||||||
|
).scalar()
|
||||||
|
|
||||||
|
return success_response(data=MerchantPayOrderInfo(
|
||||||
|
id=order.id,
|
||||||
|
order_id=order.order_id,
|
||||||
|
merchant_id=order.merchant_id,
|
||||||
|
user_id=order.user_id,
|
||||||
|
amount=float(order.amount),
|
||||||
|
gift_points=float(order.gift_points),
|
||||||
|
status=order.status,
|
||||||
|
create_time=order.create_time,
|
||||||
|
update_time=order.update_time,
|
||||||
|
pay_time=order.pay_time,
|
||||||
|
merchant_name=merchant_name
|
||||||
|
))
|
||||||
|
except Exception as e:
|
||||||
|
db.rollback()
|
||||||
|
return error_response(code=500, message=f"创建订单失败: {str(e)}")
|
||||||
|
|
||||||
|
@router.get("", response_model=ResponseModel)
|
||||||
|
async def list_pay_orders(
|
||||||
|
status: Optional[MerchantPayOrderStatus] = None,
|
||||||
|
skip: int = 0,
|
||||||
|
limit: int = 20,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: UserDB = Depends(get_current_user)
|
||||||
|
):
|
||||||
|
"""获取支付订单列表"""
|
||||||
|
# 构建基础查询
|
||||||
|
query = db.query(MerchantPayOrderDB).join(
|
||||||
|
MerchantDB,
|
||||||
|
MerchantPayOrderDB.merchant_id == MerchantDB.id
|
||||||
|
).filter(
|
||||||
|
MerchantPayOrderDB.user_id == current_user.userid
|
||||||
|
)
|
||||||
|
|
||||||
|
# 添加状态筛选
|
||||||
|
if status:
|
||||||
|
query = query.filter(MerchantPayOrderDB.status == status)
|
||||||
|
|
||||||
|
# 获取总数
|
||||||
|
total = query.count()
|
||||||
|
|
||||||
|
# 获取分页数据
|
||||||
|
orders = query.order_by(
|
||||||
|
MerchantPayOrderDB.create_time.desc()
|
||||||
|
).offset(skip).limit(limit).all()
|
||||||
|
|
||||||
|
# 获取商家名称映射
|
||||||
|
merchant_ids = [order.merchant_id for order in orders]
|
||||||
|
merchant_names = dict(
|
||||||
|
db.query(MerchantDB.id, MerchantDB.name).filter(
|
||||||
|
MerchantDB.id.in_(merchant_ids)
|
||||||
|
).all()
|
||||||
|
)
|
||||||
|
|
||||||
|
# 处理返回数据
|
||||||
|
order_list = [
|
||||||
|
MerchantPayOrderInfo(
|
||||||
|
id=order.id,
|
||||||
|
order_id=order.order_id,
|
||||||
|
merchant_id=order.merchant_id,
|
||||||
|
user_id=order.user_id,
|
||||||
|
amount=float(order.amount),
|
||||||
|
gift_points=float(order.gift_points),
|
||||||
|
status=order.status,
|
||||||
|
create_time=order.create_time,
|
||||||
|
update_time=order.update_time,
|
||||||
|
pay_time=order.pay_time,
|
||||||
|
merchant_name=merchant_names.get(order.merchant_id)
|
||||||
|
) for order in orders
|
||||||
|
]
|
||||||
|
|
||||||
|
return success_response(data={
|
||||||
|
"total": total,
|
||||||
|
"items": order_list
|
||||||
|
})
|
||||||
@ -1,6 +1,6 @@
|
|||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
from app.api.endpoints import wechat,user, address, community, station, order, coupon, community_building, upload, merchant, merchant_product, merchant_order, point, config, merchant_category, log, account
|
from app.api.endpoints import wechat,user, address, community, station, order, coupon, community_building, upload, merchant, merchant_product, merchant_order, point, config, merchant_category, log, account,merchant_pay_order
|
||||||
from app.models.database import Base, engine
|
from app.models.database import Base, engine
|
||||||
from fastapi.exceptions import RequestValidationError
|
from fastapi.exceptions import RequestValidationError
|
||||||
from fastapi.responses import JSONResponse
|
from fastapi.responses import JSONResponse
|
||||||
@ -44,6 +44,7 @@ app.include_router(merchant.router, prefix="/api/merchant", tags=["商家"])
|
|||||||
app.include_router(merchant_category.router, prefix="/api/merchant-categories", tags=["商家分类"])
|
app.include_router(merchant_category.router, prefix="/api/merchant-categories", tags=["商家分类"])
|
||||||
app.include_router(merchant_product.router, prefix="/api/merchant/product", tags=["商家产品"])
|
app.include_router(merchant_product.router, prefix="/api/merchant/product", tags=["商家产品"])
|
||||||
app.include_router(merchant_order.router, prefix="/api/merchant/order", tags=["商家订单"])
|
app.include_router(merchant_order.router, prefix="/api/merchant/order", tags=["商家订单"])
|
||||||
|
app.include_router(merchant_pay_order.router,prefix="/api/merchant-pay",tags=["商家在线买单"])
|
||||||
app.include_router(upload.router, prefix="/api/upload", tags=["文件上传"])
|
app.include_router(upload.router, prefix="/api/upload", tags=["文件上传"])
|
||||||
app.include_router(config.router, prefix="/api/config", tags=["系统配置"])
|
app.include_router(config.router, prefix="/api/config", tags=["系统配置"])
|
||||||
app.include_router(log.router, prefix="/api/logs", tags=["系统日志"])
|
app.include_router(log.router, prefix="/api/logs", tags=["系统日志"])
|
||||||
|
|||||||
56
app/models/merchant_pay_order.py
Normal file
56
app/models/merchant_pay_order.py
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
from sqlalchemy import Column, String, Integer, DateTime, ForeignKey, Enum, DECIMAL
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
from pydantic import BaseModel, Field
|
||||||
|
from typing import Optional
|
||||||
|
from datetime import datetime
|
||||||
|
from .database import Base
|
||||||
|
import time
|
||||||
|
import enum
|
||||||
|
|
||||||
|
class MerchantPayOrderStatus(str, enum.Enum):
|
||||||
|
UNPAID = "UNPAID" # 未支付
|
||||||
|
PAID = "PAID" # 已支付
|
||||||
|
|
||||||
|
class MerchantPayOrderDB(Base):
|
||||||
|
__tablename__ = "merchant_pay_orders"
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||||
|
order_id = Column(String(20), unique=True, nullable=False)
|
||||||
|
merchant_id = Column(Integer, ForeignKey("merchants.id"), nullable=False)
|
||||||
|
user_id = Column(Integer, ForeignKey("users.userid"), nullable=False)
|
||||||
|
amount = Column(DECIMAL(10,2), nullable=False)
|
||||||
|
gift_points = Column(DECIMAL(10,1), nullable=False, default=0)
|
||||||
|
status = Column(Enum(MerchantPayOrderStatus), nullable=False, default=MerchantPayOrderStatus.UNPAID)
|
||||||
|
create_time = Column(DateTime(timezone=True), server_default=func.now())
|
||||||
|
update_time = Column(DateTime(timezone=True), onupdate=func.now())
|
||||||
|
pay_time = Column(DateTime(timezone=True), nullable=True)
|
||||||
|
|
||||||
|
class MerchantPayOrderCreate(BaseModel):
|
||||||
|
merchant_id: int
|
||||||
|
amount: float = Field(..., gt=0)
|
||||||
|
gift_points: float = Field(0, ge=0)
|
||||||
|
|
||||||
|
class MerchantPayOrderInfo(BaseModel):
|
||||||
|
id: int
|
||||||
|
order_id: str
|
||||||
|
merchant_id: int
|
||||||
|
user_id: int
|
||||||
|
amount: float
|
||||||
|
gift_points: float
|
||||||
|
status: MerchantPayOrderStatus
|
||||||
|
create_time: datetime
|
||||||
|
update_time: Optional[datetime]
|
||||||
|
pay_time: Optional[datetime]
|
||||||
|
merchant_name: Optional[str] = None
|
||||||
|
merchant_logo: Optional[str] = None
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
from_attributes = True
|
||||||
|
|
||||||
|
def generate_pay_order_id() -> str:
|
||||||
|
"""生成支付订单号:P + 8位日期 + 7位时间戳"""
|
||||||
|
now = datetime.now()
|
||||||
|
date_str = now.strftime('%Y%m%d')
|
||||||
|
# 取时间戳后7位
|
||||||
|
timestamp = str(int(time.time() * 1000))[-7:]
|
||||||
|
return f"P{date_str}{timestamp}"
|
||||||
Loading…
Reference in New Issue
Block a user