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