189 lines
5.4 KiB
Python
189 lines
5.4 KiB
Python
from fastapi import APIRouter, Depends, Query, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from pydantic import BaseModel
|
|
from typing import Optional
|
|
from datetime import datetime
|
|
from app.core.database import get_db
|
|
from app.core.auth import get_current_user
|
|
from app.models.seek_info import SeekInfo
|
|
|
|
router = APIRouter(prefix="/api/seek", tags=["寻配号"])
|
|
|
|
# ============ Schema ============
|
|
class SeekInfoCreate(BaseModel):
|
|
title: str
|
|
content: Optional[str] = None
|
|
expect_category: Optional[str] = None
|
|
expect_version: Optional[str] = None
|
|
expect_packaging: Optional[str] = None
|
|
expect_number: Optional[str] = None
|
|
expect_price_min: Optional[float] = None
|
|
expect_price_max: Optional[float] = None
|
|
|
|
class SeekInfoUpdate(BaseModel):
|
|
title: Optional[str] = None
|
|
content: Optional[str] = None
|
|
expect_category: Optional[str] = None
|
|
expect_version: Optional[str] = None
|
|
expect_packaging: Optional[str] = None
|
|
expect_number: Optional[str] = None
|
|
expect_price_min: Optional[float] = None
|
|
expect_price_max: Optional[float] = None
|
|
status: Optional[str] = None
|
|
|
|
class SeekInfoResponse(BaseModel):
|
|
id: str
|
|
user_id: str
|
|
title: str
|
|
content: Optional[str]
|
|
expect_category: Optional[str]
|
|
expect_version: Optional[str]
|
|
expect_packaging: Optional[str]
|
|
expect_number: Optional[str]
|
|
expect_price_min: Optional[float]
|
|
expect_price_max: Optional[float]
|
|
status: str
|
|
is_matched: Optional[str]
|
|
matched_user_id: Optional[str]
|
|
matched_contact: Optional[str]
|
|
view_count: int
|
|
contact_count: int
|
|
created_at: Optional[datetime]
|
|
updated_at: Optional[datetime]
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
# ============ API ============
|
|
@router.get("/list", response_model=list[SeekInfoResponse])
|
|
def get_seek_list(
|
|
status: str = Query("active"),
|
|
page: int = Query(1, ge=1),
|
|
page_size: int = Query(20, ge=1, le=1000),
|
|
user_only: bool = Query(False),
|
|
current_user: Optional = Depends(get_current_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""获取寻配号列表"""
|
|
query = db.query(SeekInfo).filter(SeekInfo.status == status)
|
|
|
|
# 我的寻配号:只查看自己的
|
|
if user_only and current_user:
|
|
query = query.filter(SeekInfo.user_id == current_user.f99_90_id)
|
|
|
|
# 排序
|
|
query = query.order_by(SeekInfo.created_at.desc())
|
|
|
|
# 分页
|
|
offset = (page - 1) * page_size
|
|
items = query.offset(offset).limit(page_size).all()
|
|
|
|
return items
|
|
|
|
@router.get("/stats")
|
|
def get_seek_stats(
|
|
current_user = Depends(get_current_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""获取寻配号统计"""
|
|
total = db.query(SeekInfo).filter(SeekInfo.status == "active").count()
|
|
matched = db.query(SeekInfo).filter(SeekInfo.is_matched == "true").count()
|
|
|
|
return {
|
|
"total": total,
|
|
"matched": matched,
|
|
"unmatched": total - matched
|
|
}
|
|
|
|
@router.post("", response_model=SeekInfoResponse)
|
|
def create_seek(
|
|
data: SeekInfoCreate,
|
|
current_user = Depends(get_current_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""创建寻配号"""
|
|
if not current_user:
|
|
raise HTTPException(status_code=401, detail="请先登录")
|
|
|
|
seek = SeekInfo(
|
|
user_id=current_user.f99_90_id,
|
|
title=data.title,
|
|
content=data.content,
|
|
expect_category=data.expect_category,
|
|
expect_version=data.expect_version,
|
|
expect_packaging=data.expect_packaging,
|
|
expect_number=data.expect_number,
|
|
expect_price_min=data.expect_price_min,
|
|
expect_price_max=data.expect_price_max,
|
|
status="active"
|
|
)
|
|
db.add(seek)
|
|
db.commit()
|
|
db.refresh(seek)
|
|
return seek
|
|
|
|
@router.get("/{seek_id}", response_model=SeekInfoResponse)
|
|
def get_seek(
|
|
seek_id: str,
|
|
current_user = Depends(get_current_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""获取寻配号详情"""
|
|
seek = db.query(SeekInfo).filter(SeekInfo.id == seek_id).first()
|
|
if not seek:
|
|
raise HTTPException(status_code=404, detail="寻配号不存在")
|
|
|
|
# 增加浏览数
|
|
seek.view_count += 1
|
|
db.commit()
|
|
|
|
return seek
|
|
|
|
@router.put("/{seek_id}", response_model=SeekInfoResponse)
|
|
def update_seek(
|
|
seek_id: str,
|
|
data: SeekInfoUpdate,
|
|
current_user = Depends(get_current_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""更新寻配号"""
|
|
if not current_user:
|
|
raise HTTPException(status_code=401, detail="请先登录")
|
|
|
|
seek = db.query(SeekInfo).filter(
|
|
SeekInfo.id == seek_id,
|
|
SeekInfo.user_id == current_user.f99_90_id
|
|
).first()
|
|
|
|
if not seek:
|
|
raise HTTPException(status_code=404, detail="寻配号不存在")
|
|
|
|
for key, value in data.model_dump(exclude_unset=True).items():
|
|
setattr(seek, key, value)
|
|
|
|
db.commit()
|
|
db.refresh(seek)
|
|
return seek
|
|
|
|
@router.delete("/{seek_id}")
|
|
def delete_seek(
|
|
seek_id: str,
|
|
current_user = Depends(get_current_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""删除寻配号"""
|
|
if not current_user:
|
|
raise HTTPException(status_code=401, detail="请先登录")
|
|
|
|
seek = db.query(SeekInfo).filter(
|
|
SeekInfo.id == seek_id,
|
|
SeekInfo.user_id == current_user.f99_90_id
|
|
).first()
|
|
|
|
if not seek:
|
|
raise HTTPException(status_code=404, detail="寻配号不存在")
|
|
|
|
seek.status = "deleted"
|
|
db.commit()
|
|
|
|
return {"message": "删除成功"} |