增加 时段 模型
This commit is contained in:
parent
3a746e0bf5
commit
5614b8aa85
144
app/api/endpoints/timeperiod.py
Normal file
144
app/api/endpoints/timeperiod.py
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from app.models.database import get_db
|
||||||
|
from app.models.user import UserDB, UserRole
|
||||||
|
from app.api.deps import get_current_user, get_admin_user
|
||||||
|
from app.core.response import success_response, error_response, ResponseModel
|
||||||
|
from app.models.timeperiod import (
|
||||||
|
TimePeriodDB,
|
||||||
|
TimePeriodCreate,
|
||||||
|
TimePeriodUpdate,
|
||||||
|
TimePeriodInfo
|
||||||
|
)
|
||||||
|
from typing import List, Optional
|
||||||
|
import logging
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.post("", response_model=ResponseModel)
|
||||||
|
async def create_time_period(
|
||||||
|
time_period: TimePeriodCreate,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
admin: UserDB = Depends(get_admin_user)
|
||||||
|
):
|
||||||
|
"""创建配送时段"""
|
||||||
|
try:
|
||||||
|
# 检查时间段是否合理
|
||||||
|
if time_period.from_time >= time_period.to_time:
|
||||||
|
return error_response(code=400, message="开始时间必须早于结束时间")
|
||||||
|
|
||||||
|
# 创建新的时间段
|
||||||
|
db_time_period = TimePeriodDB(
|
||||||
|
name=time_period.name,
|
||||||
|
from_time=time_period.from_time,
|
||||||
|
to_time=time_period.to_time,
|
||||||
|
)
|
||||||
|
|
||||||
|
db.add(db_time_period)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_time_period)
|
||||||
|
|
||||||
|
return success_response(message="配送时段创建成功", data=TimePeriodInfo.model_validate(db_time_period))
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
db.rollback()
|
||||||
|
logging.exception(f"创建配送时段失败: {str(e)}")
|
||||||
|
return error_response(code=500, message="创建配送时段失败,请稍后重试")
|
||||||
|
|
||||||
|
@router.get("", response_model=ResponseModel)
|
||||||
|
async def get_time_periods(
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
"""获取配送时段列表"""
|
||||||
|
try:
|
||||||
|
query = db.query(TimePeriodDB)
|
||||||
|
|
||||||
|
# 按开始时间排序
|
||||||
|
query = query.order_by(TimePeriodDB.from_time.asc())
|
||||||
|
|
||||||
|
time_periods = query.all()
|
||||||
|
|
||||||
|
return success_response(data=[TimePeriodInfo.model_validate(tp) for tp in time_periods])
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logging.exception(f"获取配送时段列表失败: {str(e)}")
|
||||||
|
return error_response(code=500, message="获取配送时段列表失败,请稍后重试")
|
||||||
|
|
||||||
|
@router.get("/{time_period_id}", response_model=ResponseModel)
|
||||||
|
async def get_time_period(
|
||||||
|
time_period_id: int,
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
"""获取配送时段详情"""
|
||||||
|
try:
|
||||||
|
time_period = db.query(TimePeriodDB).filter(TimePeriodDB.id == time_period_id).first()
|
||||||
|
|
||||||
|
if not time_period:
|
||||||
|
return error_response(code=404, message="配送时段不存在")
|
||||||
|
|
||||||
|
return success_response(data=TimePeriodInfo.model_validate(time_period))
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logging.exception(f"获取配送时段详情失败: {str(e)}")
|
||||||
|
return error_response(code=500, message="获取配送时段详情失败,请稍后重试")
|
||||||
|
|
||||||
|
@router.put("/{time_period_id}", response_model=ResponseModel)
|
||||||
|
async def update_time_period(
|
||||||
|
time_period_id: int,
|
||||||
|
time_period: TimePeriodUpdate,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
admin: UserDB = Depends(get_admin_user)
|
||||||
|
):
|
||||||
|
"""更新配送时段"""
|
||||||
|
try:
|
||||||
|
db_time_period = db.query(TimePeriodDB).filter(TimePeriodDB.id == time_period_id).first()
|
||||||
|
|
||||||
|
if not db_time_period:
|
||||||
|
return error_response(code=404, message="配送时段不存在")
|
||||||
|
|
||||||
|
# 更新字段
|
||||||
|
if time_period.name is not None:
|
||||||
|
db_time_period.name = time_period.name
|
||||||
|
|
||||||
|
if time_period.from_time is not None:
|
||||||
|
db_time_period.from_time = time_period.from_time
|
||||||
|
|
||||||
|
if time_period.to_time is not None:
|
||||||
|
db_time_period.to_time = time_period.to_time
|
||||||
|
|
||||||
|
# 检查时间段是否合理
|
||||||
|
if db_time_period.from_time >= db_time_period.to_time:
|
||||||
|
return error_response(code=400, message="开始时间必须早于结束时间")
|
||||||
|
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_time_period)
|
||||||
|
|
||||||
|
return success_response(message="配送时段更新成功", data=TimePeriodInfo.model_validate(db_time_period))
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
db.rollback()
|
||||||
|
logging.exception(f"更新配送时段失败: {str(e)}")
|
||||||
|
return error_response(code=500, message="更新配送时段失败,请稍后重试")
|
||||||
|
|
||||||
|
@router.delete("/{time_period_id}", response_model=ResponseModel)
|
||||||
|
async def delete_time_period(
|
||||||
|
time_period_id: int,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
admin: UserDB = Depends(get_admin_user)
|
||||||
|
):
|
||||||
|
"""删除配送时段"""
|
||||||
|
try:
|
||||||
|
db_time_period = db.query(TimePeriodDB).filter(TimePeriodDB.id == time_period_id).first()
|
||||||
|
|
||||||
|
if not db_time_period:
|
||||||
|
return error_response(code=404, message="配送时段不存在")
|
||||||
|
|
||||||
|
db.delete(db_time_period)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
return success_response(message="配送时段删除成功")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
db.rollback()
|
||||||
|
logging.exception(f"删除配送时段失败: {str(e)}")
|
||||||
|
return error_response(code=500, message="删除配送时段失败,请稍后重试")
|
||||||
@ -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,merchant_pay_order, message, bank_card, withdraw, mp, point_product, point_product_order, coupon_activity, ocr, dashboard, wecom
|
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, message, bank_card, withdraw, mp, point_product, point_product_order, coupon_activity, ocr, dashboard, wecom, feedback, timeperiod
|
||||||
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
|
||||||
@ -76,6 +76,7 @@ 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=["系统日志"])
|
||||||
app.include_router(ocr.router, prefix="/api/ai/ocr", tags=["图像识别"])
|
app.include_router(ocr.router, prefix="/api/ai/ocr", tags=["图像识别"])
|
||||||
app.include_router(feedback.router, prefix="/api/feedback", tags=["反馈"])
|
app.include_router(feedback.router, prefix="/api/feedback", tags=["反馈"])
|
||||||
|
app.include_router(timeperiod.router, prefix="/api/time-periods", tags=["配送时段"])
|
||||||
|
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
async def root():
|
async def root():
|
||||||
|
|||||||
40
app/models/timeperiod.py
Normal file
40
app/models/timeperiod.py
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
from sqlalchemy import Column, Integer, String, Time, Boolean, DateTime, ForeignKey
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
from pydantic import BaseModel, Field
|
||||||
|
from typing import Optional
|
||||||
|
from datetime import time, datetime
|
||||||
|
from .database import Base
|
||||||
|
|
||||||
|
# 数据库模型
|
||||||
|
class TimePeriodDB(Base):
|
||||||
|
"""配送时段模型"""
|
||||||
|
__tablename__ = "time_periods"
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||||
|
name = Column(String(50), nullable=False) # 时段名称,例如"上午"、"下午"、"晚上"
|
||||||
|
from_time = Column(Time, nullable=False) # 开始时间
|
||||||
|
to_time = Column(Time, nullable=False) # 结束时间
|
||||||
|
create_time = Column(DateTime(timezone=True), server_default=func.now())
|
||||||
|
update_time = Column(DateTime(timezone=True), onupdate=func.now())
|
||||||
|
|
||||||
|
# Pydantic 模型
|
||||||
|
class TimePeriodCreate(BaseModel):
|
||||||
|
name: str = Field(..., max_length=50, description="时段名称")
|
||||||
|
from_time: time = Field(..., description="开始时间")
|
||||||
|
to_time: time = Field(..., description="结束时间")
|
||||||
|
|
||||||
|
class TimePeriodUpdate(BaseModel):
|
||||||
|
name: Optional[str] = Field(None, max_length=50, description="时段名称")
|
||||||
|
from_time: Optional[time] = Field(None, description="开始时间")
|
||||||
|
to_time: Optional[time] = Field(None, description="结束时间")
|
||||||
|
|
||||||
|
class TimePeriodInfo(BaseModel):
|
||||||
|
id: int
|
||||||
|
name: str
|
||||||
|
from_time: time
|
||||||
|
to_time: time
|
||||||
|
create_time: datetime
|
||||||
|
update_time: Optional[datetime] = None
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
from_attributes = True
|
||||||
Loading…
Reference in New Issue
Block a user