- time_utils 新增 to_beijing(统一naive按UTC转北京时间) + working_duration_hours(排除周末/节假日) - Holiday 模型 + alembic 迁移建 holidays 表 - GET/POST/DELETE /holidays 节假日管理接口,可随时配置放假日期
61 lines
1.9 KiB
Python
61 lines
1.9 KiB
Python
"""节假日管理 — 工作日时长计算所需的放假日期配置"""
|
|
from datetime import date
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from pydantic import BaseModel, Field
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.database import get_db
|
|
from app.models.holiday import Holiday
|
|
from app.services.auth_service import get_current_user
|
|
|
|
router = APIRouter(prefix="/holidays", tags=["节假日管理"])
|
|
|
|
|
|
class HolidayCreate(BaseModel):
|
|
day: date = Field(..., description="放假日期")
|
|
name: str | None = Field(None, max_length=100, description="放假说明")
|
|
|
|
|
|
@router.get("/")
|
|
async def list_holidays(
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
"""获取全部放假日期(按日期正序)"""
|
|
result = await db.execute(select(Holiday).order_by(Holiday.day.asc()))
|
|
return [
|
|
{"id": h.id, "date": h.day.isoformat(), "name": h.name}
|
|
for h in result.scalars().all()
|
|
]
|
|
|
|
|
|
@router.post("/", status_code=201)
|
|
async def create_holiday(
|
|
data: HolidayCreate,
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: dict = Depends(get_current_user),
|
|
):
|
|
"""添加一个放假日期"""
|
|
exists = await db.scalar(select(Holiday).where(Holiday.day == data.day))
|
|
if exists:
|
|
raise HTTPException(status_code=400, detail=f"{data.day} 已在节假日列表中")
|
|
h = Holiday(day=data.day, name=data.name)
|
|
db.add(h)
|
|
await db.commit()
|
|
await db.refresh(h)
|
|
return {"id": h.id, "date": h.day.isoformat(), "name": h.name}
|
|
|
|
|
|
@router.delete("/{holiday_id}", status_code=204)
|
|
async def delete_holiday(
|
|
holiday_id: int,
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: dict = Depends(get_current_user),
|
|
):
|
|
"""删除一个放假日期"""
|
|
h = await db.get(Holiday, holiday_id)
|
|
if not h:
|
|
raise HTTPException(status_code=404, detail="节假日不存在")
|
|
await db.delete(h)
|
|
await db.commit()
|