84 lines
2.6 KiB
Python
84 lines
2.6 KiB
Python
"""
|
|
公司黑名单路由:拉黑后 RPA 不再投递该公司的任何职位
|
|
"""
|
|
from datetime import timedelta
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
from sqlalchemy.orm import Session
|
|
|
|
from ..auth import get_current_user
|
|
from ..database import get_db
|
|
from ..models import User, CompanyBlacklist
|
|
from ..schemas import BlacklistIn, BlacklistOut, Message
|
|
|
|
router = APIRouter(prefix="/api/blacklist", tags=["公司黑名单"])
|
|
|
|
|
|
@router.get("", response_model=dict)
|
|
def list_blacklist(
|
|
page: int = Query(1, ge=1, description="页码(1 起)"),
|
|
page_size: int = Query(20, ge=1, le=500, description="每页条数"),
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""查询当前用户的黑名单列表(按拉黑时间倒序,分页返回)"""
|
|
query = db.query(CompanyBlacklist).filter(
|
|
CompanyBlacklist.user_id == current_user.id,
|
|
)
|
|
total = query.count()
|
|
rows = query.order_by(CompanyBlacklist.created_at.desc()) \
|
|
.offset((page - 1) * page_size).limit(page_size).all()
|
|
items = [{
|
|
"id": r.id,
|
|
"company": r.company,
|
|
"created_at": (r.created_at + timedelta(hours=8)).strftime("%Y-%m-%d %H:%M:%S"),
|
|
} for r in rows]
|
|
return {
|
|
"items": items, "total": total,
|
|
"page": page, "page_size": page_size,
|
|
"pages": (total + page_size - 1) // page_size if page_size else 1,
|
|
}
|
|
|
|
|
|
@router.post("", response_model=BlacklistOut)
|
|
def add_blacklist(
|
|
req: BlacklistIn,
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""拉黑一家公司(重复拉黑幂等返回已有记录)"""
|
|
company = req.company.strip()
|
|
if not company:
|
|
raise HTTPException(status_code=422, detail="公司名不能为空")
|
|
|
|
existing = db.query(CompanyBlacklist).filter(
|
|
CompanyBlacklist.user_id == current_user.id,
|
|
CompanyBlacklist.company == company,
|
|
).first()
|
|
if existing:
|
|
return existing
|
|
|
|
item = CompanyBlacklist(user_id=current_user.id, company=company)
|
|
db.add(item)
|
|
db.commit()
|
|
db.refresh(item)
|
|
return item
|
|
|
|
|
|
@router.delete("/{item_id}", response_model=Message)
|
|
def remove_blacklist(
|
|
item_id: int,
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""移除一条黑名单记录"""
|
|
item = db.query(CompanyBlacklist).filter(
|
|
CompanyBlacklist.id == item_id,
|
|
CompanyBlacklist.user_id == current_user.id,
|
|
).first()
|
|
if not item:
|
|
raise HTTPException(status_code=404, detail="黑名单记录不存在")
|
|
db.delete(item)
|
|
db.commit()
|
|
return Message(msg="已移除")
|