jiachenlong/backend/app/routers/operations.py

211 lines
4.3 KiB
Python
Raw Normal View History

# operations - 运营操作路由
# Version: 1.2.70
# 更新:
2026-03-23 11:08:52 +08:00
from typing import List, Optional
# 更新:
2026-03-23 11:08:52 +08:00
from fastapi import APIRouter, Depends, HTTPException, status, Query
# 更新:
2026-03-23 11:08:52 +08:00
from sqlalchemy.orm import Session
# 更新:
2026-03-23 11:08:52 +08:00
from app.core.database import get_db
# 更新:
2026-03-23 11:08:52 +08:00
from app.core.auth import get_current_user
# 更新:
2026-03-23 11:08:52 +08:00
from app.models.models import User, Collection, Operation
# 更新:
2026-03-23 11:08:52 +08:00
from app.schemas.schemas import OperationCreate, OperationResponse
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
router = APIRouter(prefix="/api", tags=["操作"])
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
@router.get("/operations", response_model=List[OperationResponse])
# 更新:
2026-03-23 11:08:52 +08:00
def get_operations(
# 更新:
2026-03-23 11:08:52 +08:00
collection_id: Optional[str] = None,
# 更新:
2026-03-23 11:08:52 +08:00
page: int = Query(1, ge=1),
# 更新:
2026-03-23 11:08:52 +08:00
limit: int = Query(50, ge=1, le=100),
# 更新:
2026-03-23 11:08:52 +08:00
current_user: User = Depends(get_current_user),
# 更新:
2026-03-23 11:08:52 +08:00
db: Session = Depends(get_db)
# 更新:
2026-03-23 11:08:52 +08:00
):
# 更新:
2026-03-23 11:08:52 +08:00
"""获取操作历史"""
# 更新:
2026-03-23 11:08:52 +08:00
query = db.query(Operation).filter(Operation.user_id == current_user.id)
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
if collection_id:
# 更新:
2026-03-23 11:08:52 +08:00
query = query.filter(Operation.collection_id == collection_id)
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
operations = query.order_by(Operation.created_at.desc()) \
# 更新:
2026-03-23 11:08:52 +08:00
.offset((page - 1) * limit) \
# 更新:
2026-03-23 11:08:52 +08:00
.limit(limit) \
# 更新:
2026-03-23 11:08:52 +08:00
.all()
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
return operations
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
@router.get("/operations/history")
# 更新:
2026-03-23 11:08:52 +08:00
def get_operation_history(
# 更新:
2026-03-23 11:08:52 +08:00
collection_id: Optional[str] = None,
# 更新:
2026-03-23 11:08:52 +08:00
type: Optional[str] = None,
# 更新:
2026-03-23 11:08:52 +08:00
start_date: Optional[str] = None,
# 更新:
2026-03-23 11:08:52 +08:00
end_date: Optional[str] = None,
# 更新:
2026-03-23 11:08:52 +08:00
page: int = Query(1, ge=1),
# 更新:
2026-03-23 11:08:52 +08:00
limit: int = Query(50, ge=1, le=100),
# 更新:
2026-03-23 11:08:52 +08:00
current_user: User = Depends(get_current_user),
# 更新:
2026-03-23 11:08:52 +08:00
db: Session = Depends(get_db)
# 更新:
2026-03-23 11:08:52 +08:00
):
# 更新:
2026-03-23 11:08:52 +08:00
"""获取操作历史(带统计)"""
# 更新:
2026-03-23 11:08:52 +08:00
query = db.query(Operation).filter(Operation.user_id == current_user.id)
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
if collection_id:
# 更新:
2026-03-23 11:08:52 +08:00
query = query.filter(Operation.collection_id == collection_id)
# 更新:
2026-03-23 11:08:52 +08:00
if type:
# 更新:
2026-03-23 11:08:52 +08:00
query = query.filter(Operation.type == type)
# 更新:
2026-03-23 11:08:52 +08:00
if start_date:
# 更新:
2026-03-23 11:08:52 +08:00
query = query.filter(Operation.created_at >= start_date)
# 更新:
2026-03-23 11:08:52 +08:00
if end_date:
# 更新:
2026-03-23 11:08:52 +08:00
query = query.filter(Operation.created_at <= end_date)
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
total = query.count()
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
data = query.order_by(Operation.created_at.desc()) \
# 更新:
2026-03-23 11:08:52 +08:00
.offset((page - 1) * limit) \
# 更新:
2026-03-23 11:08:52 +08:00
.limit(limit) \
# 更新:
2026-03-23 11:08:52 +08:00
.all()
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
return {
# 更新:
2026-03-23 11:08:52 +08:00
"data": data,
# 更新:
2026-03-23 11:08:52 +08:00
"pagination": {
# 更新:
2026-03-23 11:08:52 +08:00
"page": page,
# 更新:
2026-03-23 11:08:52 +08:00
"limit": limit,
# 更新:
2026-03-23 11:08:52 +08:00
"total": total,
# 更新:
2026-03-23 11:08:52 +08:00
"pages": (total + limit - 1) // limit
# 更新:
2026-03-23 11:08:52 +08:00
}
# 更新:
2026-03-23 11:08:52 +08:00
}
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
@router.post("/operations", response_model=OperationResponse)
# 更新:
2026-03-23 11:08:52 +08:00
def create_operation(
# 更新:
2026-03-23 11:08:52 +08:00
operation_data: OperationCreate,
# 更新:
2026-03-23 11:08:52 +08:00
current_user: User = Depends(get_current_user),
# 更新:
2026-03-23 11:08:52 +08:00
db: Session = Depends(get_db)
# 更新:
2026-03-23 11:08:52 +08:00
):
# 更新:
2026-03-23 11:08:52 +08:00
"""创建操作记录"""
# 更新:
2026-03-23 11:08:52 +08:00
# 验证藏品存在
# 更新:
2026-03-23 11:08:52 +08:00
collection = db.query(Collection).filter(
# 更新:
2026-03-23 11:08:52 +08:00
Collection.id == operation_data.collection_id,
# 更新:
2026-03-23 11:08:52 +08:00
Collection.user_id == current_user.id
# 更新:
2026-03-23 11:08:52 +08:00
).first()
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
if not collection:
# 更新:
2026-03-23 11:08:52 +08:00
raise HTTPException(status_code=404, detail="藏品不存在")
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
operation = Operation(
# 更新:
2026-03-23 11:08:52 +08:00
collection_id=operation_data.collection_id,
# 更新:
2026-03-23 11:08:52 +08:00
user_id=current_user.id,
# 更新:
2026-03-23 11:08:52 +08:00
type=operation_data.type,
# 更新:
2026-03-23 11:08:52 +08:00
price=operation_data.price,
# 更新:
2026-03-23 11:08:52 +08:00
note=operation_data.note
# 更新:
2026-03-23 11:08:52 +08:00
)
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
db.add(operation)
# 更新:
2026-03-23 11:08:52 +08:00
db.commit()
# 更新:
2026-03-23 11:08:52 +08:00
db.refresh(operation)
# 更新:
2026-03-23 11:08:52 +08:00
# 更新:
2026-03-23 11:08:52 +08:00
return operation
# 更新: