Files
shanxia2.0/backend/app/crud/crm/customer_pool.py
T
2026-06-01 08:21:21 +08:00

129 lines
3.6 KiB
Python

"""CRM 公海池记录 CRUD"""
from __future__ import annotations
import math
from datetime import datetime
from typing import Any, Dict, List, Optional, Tuple
from sqlalchemy import or_
from sqlalchemy.orm import Session
from app.core.utils import generate_uuid
from app.models.crm_models import CrmCustomerPool
_FMT = "%Y-%m-%d %H:%M:%S"
def total_pages(total: int, page_size: int) -> int:
if page_size <= 0:
return 0
return math.ceil(total / page_size)
def _row_to_dict(row: CrmCustomerPool) -> Dict[str, Any]:
return {
"id": row.id,
"customerId": row.customer_id or "",
"customerName": row.customer_name or "",
"operationType": row.operation_type or "",
"fromUserId": row.from_user_id or "",
"fromUserName": row.from_user_name or "",
"toUserId": row.to_user_id or "",
"toUserName": row.to_user_name or "",
"reason": row.reason or "",
"createByName": row.create_by_name or "",
"createTime": row.create_time.strftime(_FMT) if row.create_time else "",
}
def create_pool_record(
db: Session,
*,
tenant_no: str,
user_id: Optional[str] = None,
customer_id: str,
customer_name: str,
operation_type: str,
from_user_id: Optional[str] = None,
from_user_name: Optional[str] = None,
to_user_id: Optional[str] = None,
to_user_name: Optional[str] = None,
reason: Optional[str] = None,
) -> CrmCustomerPool:
from app.models.models import SysUser
create_by_name = ""
if user_id:
u = db.query(SysUser).filter(SysUser.id == user_id, SysUser.del_flag == 0).first()
if u:
create_by_name = (u.nick_name or "").strip()
row = CrmCustomerPool(
id=generate_uuid(),
customer_id=customer_id,
customer_name=customer_name,
operation_type=operation_type,
from_user_id=from_user_id,
from_user_name=from_user_name,
to_user_id=to_user_id,
to_user_name=to_user_name,
reason=reason,
tenant_no=tenant_no,
create_by=user_id,
create_by_name=create_by_name,
create_time=datetime.now(),
update_time=datetime.now(),
)
db.add(row)
db.flush()
return row
def list_pool_records(
db: Session,
tenant_no: str,
*,
page: int = 1,
page_size: int = 10,
customer_id: Optional[str] = None,
operation_type: Optional[str] = None,
keyword: Optional[str] = None,
) -> Tuple[List[Dict[str, Any]], int]:
q = db.query(CrmCustomerPool).filter(
CrmCustomerPool.tenant_no == tenant_no,
CrmCustomerPool.del_flag == 0,
)
if customer_id:
q = q.filter(CrmCustomerPool.customer_id == customer_id)
if operation_type:
q = q.filter(CrmCustomerPool.operation_type == operation_type)
if keyword:
like = f"%{keyword}%"
q = q.filter(
or_(
CrmCustomerPool.customer_name.like(like),
CrmCustomerPool.from_user_name.like(like),
CrmCustomerPool.to_user_name.like(like),
CrmCustomerPool.reason.like(like),
)
)
total = q.count()
q = q.order_by(CrmCustomerPool.create_time.desc())
offset = (page - 1) * page_size
rows = q.offset(offset).limit(page_size).all()
return [_row_to_dict(r) for r in rows], total
def get_pool_record(db: Session, record_id: str, tenant_no: str) -> Optional[CrmCustomerPool]:
return (
db.query(CrmCustomerPool)
.filter(
CrmCustomerPool.id == record_id,
CrmCustomerPool.tenant_no == tenant_no,
CrmCustomerPool.del_flag == 0,
)
.first()
)