a
This commit is contained in:
@@ -0,0 +1,203 @@
|
||||
"""
|
||||
潜在客户管理 API
|
||||
"""
|
||||
from typing import Optional, List
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import or_, desc
|
||||
from pydantic import BaseModel
|
||||
|
||||
from database import get_db
|
||||
from auth import get_current_user
|
||||
from models import PotentialCustomer, PotentialContact
|
||||
|
||||
router = APIRouter(prefix="/potential-customers", tags=["potential-customers"])
|
||||
|
||||
|
||||
class CustomerFilter(BaseModel):
|
||||
keyword: Optional[str] = None
|
||||
country: Optional[str] = None
|
||||
industry: Optional[str] = None
|
||||
has_contacts: Optional[bool] = None
|
||||
|
||||
|
||||
@router.get("/")
|
||||
def list_potential_customers(
|
||||
keyword: Optional[str] = Query(None),
|
||||
country: Optional[str] = Query(None),
|
||||
industry: Optional[str] = Query(None),
|
||||
has_contacts: Optional[bool] = Query(None),
|
||||
page: int = Query(1, ge=1),
|
||||
page_size: int = Query(20, ge=1, le=100),
|
||||
db: Session = Depends(get_db),
|
||||
current_user: dict = Depends(get_current_user)
|
||||
):
|
||||
"""获取潜在客户列表(类似发现模块的展示方式)"""
|
||||
query = db.query(PotentialCustomer)
|
||||
|
||||
# 关键词搜索(公司名称、域名、描述)
|
||||
if keyword:
|
||||
search = f"%{keyword}%"
|
||||
query = query.filter(
|
||||
or_(
|
||||
PotentialCustomer.name.ilike(search),
|
||||
PotentialCustomer.domain.ilike(search),
|
||||
PotentialCustomer.description.ilike(search)
|
||||
)
|
||||
)
|
||||
|
||||
# 国家筛选
|
||||
if country:
|
||||
query = query.filter(PotentialCustomer.country == country)
|
||||
|
||||
# 行业筛选
|
||||
if industry:
|
||||
query = query.filter(PotentialCustomer.industry.ilike(f"%{industry}%"))
|
||||
|
||||
# 是否有联系人筛选
|
||||
if has_contacts is not None:
|
||||
if has_contacts:
|
||||
query = query.filter(PotentialCustomer.contact_count > 0)
|
||||
else:
|
||||
query = query.filter(PotentialCustomer.contact_count == 0)
|
||||
|
||||
# 统计总数
|
||||
total = query.count()
|
||||
|
||||
# 分页查询
|
||||
customers = query.order_by(
|
||||
desc(PotentialCustomer.source_score)
|
||||
).offset((page - 1) * page_size).limit(page_size).all()
|
||||
|
||||
return {
|
||||
"total": total,
|
||||
"page": page,
|
||||
"page_size": page_size,
|
||||
"results": [c.to_dict() for c in customers]
|
||||
}
|
||||
|
||||
|
||||
@router.get("/countries")
|
||||
def get_customer_countries(
|
||||
db: Session = Depends(get_db),
|
||||
current_user: dict = Depends(get_current_user)
|
||||
):
|
||||
"""获取所有客户所在国家列表(用于筛选)"""
|
||||
countries = db.query(
|
||||
PotentialCustomer.country,
|
||||
PotentialCustomer.country_cn,
|
||||
PotentialCustomer.country_en
|
||||
).distinct().filter(
|
||||
PotentialCustomer.country.isnot(None)
|
||||
).all()
|
||||
|
||||
return [
|
||||
{
|
||||
"code": c.country,
|
||||
"cn_name": c.country_cn,
|
||||
"en_name": c.country_en
|
||||
}
|
||||
for c in countries if c.country
|
||||
]
|
||||
|
||||
|
||||
@router.get("/industries")
|
||||
def get_customer_industries(
|
||||
db: Session = Depends(get_db),
|
||||
current_user: dict = Depends(get_current_user)
|
||||
):
|
||||
"""获取所有行业列表(用于筛选)"""
|
||||
industries = db.query(
|
||||
PotentialCustomer.industry
|
||||
).distinct().filter(
|
||||
PotentialCustomer.industry.isnot(None)
|
||||
).all()
|
||||
|
||||
return [i.industry for i in industries if i.industry]
|
||||
|
||||
|
||||
@router.get("/{customer_id}")
|
||||
def get_customer_detail(
|
||||
customer_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: dict = Depends(get_current_user)
|
||||
):
|
||||
"""获取客户详情"""
|
||||
customer = db.query(PotentialCustomer).filter(
|
||||
PotentialCustomer.id == customer_id
|
||||
).first()
|
||||
|
||||
if not customer:
|
||||
raise HTTPException(status_code=404, detail="客户不存在")
|
||||
|
||||
# 获取联系人
|
||||
contacts = db.query(PotentialContact).filter(
|
||||
PotentialContact.customer_id == customer_id
|
||||
).order_by(desc(PotentialContact.score)).all()
|
||||
|
||||
result = customer.to_dict()
|
||||
result["contacts"] = [c.to_dict() for c in contacts]
|
||||
|
||||
return result
|
||||
|
||||
|
||||
@router.get("/{customer_id}/contacts")
|
||||
def get_customer_contacts(
|
||||
customer_id: int,
|
||||
page: int = Query(1, ge=1),
|
||||
page_size: int = Query(20, ge=1, le=100),
|
||||
db: Session = Depends(get_db),
|
||||
current_user: dict = Depends(get_current_user)
|
||||
):
|
||||
"""获取客户的联系人列表"""
|
||||
# 验证客户存在
|
||||
customer = db.query(PotentialCustomer).filter(
|
||||
PotentialCustomer.id == customer_id
|
||||
).first()
|
||||
|
||||
if not customer:
|
||||
raise HTTPException(status_code=404, detail="客户不存在")
|
||||
|
||||
# 查询联系人
|
||||
query = db.query(PotentialContact).filter(
|
||||
PotentialContact.customer_id == customer_id
|
||||
)
|
||||
|
||||
total = query.count()
|
||||
|
||||
contacts = query.order_by(
|
||||
desc(PotentialContact.score)
|
||||
).offset((page - 1) * page_size).limit(page_size).all()
|
||||
|
||||
return {
|
||||
"total": total,
|
||||
"page": page,
|
||||
"page_size": page_size,
|
||||
"contacts": [c.to_dict() for c in contacts]
|
||||
}
|
||||
|
||||
|
||||
@router.delete("/{customer_id}")
|
||||
def delete_customer(
|
||||
customer_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: dict = Depends(get_current_user)
|
||||
):
|
||||
"""删除客户(同时删除关联的联系人)"""
|
||||
customer = db.query(PotentialCustomer).filter(
|
||||
PotentialCustomer.id == customer_id
|
||||
).first()
|
||||
|
||||
if not customer:
|
||||
raise HTTPException(status_code=404, detail="客户不存在")
|
||||
|
||||
# 删除联系人
|
||||
db.query(PotentialContact).filter(
|
||||
PotentialContact.customer_id == customer_id
|
||||
).delete()
|
||||
|
||||
# 删除客户
|
||||
db.delete(customer)
|
||||
db.commit()
|
||||
|
||||
return {"success": True, "message": "删除成功"}
|
||||
Reference in New Issue
Block a user