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