customer_pool.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. """CRM 公海池记录 CRUD"""
  2. from __future__ import annotations
  3. import math
  4. from datetime import datetime
  5. from typing import Any, Dict, List, Optional, Tuple
  6. from sqlalchemy import or_
  7. from sqlalchemy.orm import Session
  8. from app.core.utils import generate_uuid
  9. from app.models.crm_models import CrmCustomerPool
  10. _FMT = "%Y-%m-%d %H:%M:%S"
  11. def total_pages(total: int, page_size: int) -> int:
  12. if page_size <= 0:
  13. return 0
  14. return math.ceil(total / page_size)
  15. def _row_to_dict(row: CrmCustomerPool) -> Dict[str, Any]:
  16. return {
  17. "id": row.id,
  18. "customerId": row.customer_id or "",
  19. "customerName": row.customer_name or "",
  20. "operationType": row.operation_type or "",
  21. "fromUserId": row.from_user_id or "",
  22. "fromUserName": row.from_user_name or "",
  23. "toUserId": row.to_user_id or "",
  24. "toUserName": row.to_user_name or "",
  25. "reason": row.reason or "",
  26. "createByName": row.create_by_name or "",
  27. "createTime": row.create_time.strftime(_FMT) if row.create_time else "",
  28. }
  29. def create_pool_record(
  30. db: Session,
  31. *,
  32. tenant_no: str,
  33. user_id: Optional[str] = None,
  34. customer_id: str,
  35. customer_name: str,
  36. operation_type: str,
  37. from_user_id: Optional[str] = None,
  38. from_user_name: Optional[str] = None,
  39. to_user_id: Optional[str] = None,
  40. to_user_name: Optional[str] = None,
  41. reason: Optional[str] = None,
  42. ) -> CrmCustomerPool:
  43. from app.models.models import SysUser
  44. create_by_name = ""
  45. if user_id:
  46. u = db.query(SysUser).filter(SysUser.id == user_id, SysUser.del_flag == 0).first()
  47. if u:
  48. create_by_name = (u.nick_name or "").strip()
  49. row = CrmCustomerPool(
  50. id=generate_uuid(),
  51. customer_id=customer_id,
  52. customer_name=customer_name,
  53. operation_type=operation_type,
  54. from_user_id=from_user_id,
  55. from_user_name=from_user_name,
  56. to_user_id=to_user_id,
  57. to_user_name=to_user_name,
  58. reason=reason,
  59. tenant_no=tenant_no,
  60. create_by=user_id,
  61. create_by_name=create_by_name,
  62. create_time=datetime.now(),
  63. update_time=datetime.now(),
  64. )
  65. db.add(row)
  66. db.flush()
  67. return row
  68. def list_pool_records(
  69. db: Session,
  70. tenant_no: str,
  71. *,
  72. page: int = 1,
  73. page_size: int = 10,
  74. customer_id: Optional[str] = None,
  75. operation_type: Optional[str] = None,
  76. keyword: Optional[str] = None,
  77. ) -> Tuple[List[Dict[str, Any]], int]:
  78. q = db.query(CrmCustomerPool).filter(
  79. CrmCustomerPool.tenant_no == tenant_no,
  80. CrmCustomerPool.del_flag == 0,
  81. )
  82. if customer_id:
  83. q = q.filter(CrmCustomerPool.customer_id == customer_id)
  84. if operation_type:
  85. q = q.filter(CrmCustomerPool.operation_type == operation_type)
  86. if keyword:
  87. like = f"%{keyword}%"
  88. q = q.filter(
  89. or_(
  90. CrmCustomerPool.customer_name.like(like),
  91. CrmCustomerPool.from_user_name.like(like),
  92. CrmCustomerPool.to_user_name.like(like),
  93. CrmCustomerPool.reason.like(like),
  94. )
  95. )
  96. total = q.count()
  97. q = q.order_by(CrmCustomerPool.create_time.desc())
  98. offset = (page - 1) * page_size
  99. rows = q.offset(offset).limit(page_size).all()
  100. return [_row_to_dict(r) for r in rows], total
  101. def get_pool_record(db: Session, record_id: str, tenant_no: str) -> Optional[CrmCustomerPool]:
  102. return (
  103. db.query(CrmCustomerPool)
  104. .filter(
  105. CrmCustomerPool.id == record_id,
  106. CrmCustomerPool.tenant_no == tenant_no,
  107. CrmCustomerPool.del_flag == 0,
  108. )
  109. .first()
  110. )