277 lines
8.3 KiB
Python
277 lines
8.3 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 func
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.delete_guard import assert_row_deletable_by_creator
|
|
from app.core.utils import generate_uuid
|
|
from app.crud.crm.clue import get_clue, _ensure_clue_in_scope
|
|
from app.crud.crm.data_scope_helpers import apply_crm_create_by_scope, ensure_crm_create_by_row_in_scope
|
|
from app.models.crm_models import CrmClueFollow
|
|
|
|
|
|
def total_pages(total: int, page_size: int) -> int:
|
|
if page_size <= 0:
|
|
return 0
|
|
return math.ceil(total / page_size)
|
|
|
|
|
|
_FMT = "%Y-%m-%d %H:%M:%S"
|
|
|
|
|
|
def _row_to_dict(row: CrmClueFollow) -> Dict[str, Any]:
|
|
return {
|
|
"id": row.id,
|
|
"clueId": row.clue_id or "",
|
|
"clueName": row.clue_name or "",
|
|
"followType": row.follow_type or "",
|
|
"followContent": row.follow_content or "",
|
|
"nextTime": row.next_time.strftime(_FMT) if row.next_time else "",
|
|
"attachments": row.attachments or "",
|
|
"createByName": row.create_by_name or "",
|
|
"orderNum": row.order_num if row.order_num is not None else 0,
|
|
"remark": row.remark or "",
|
|
"createBy": row.create_by or "",
|
|
"createTime": row.create_time.strftime(_FMT) if row.create_time else "",
|
|
"updateBy": row.update_by or "",
|
|
"updateTime": row.update_time.strftime(_FMT) if row.update_time else "",
|
|
}
|
|
|
|
|
|
def create_clue_follow(
|
|
db: Session,
|
|
tenant_no: str,
|
|
data: Any,
|
|
user_id: str,
|
|
user_name: str,
|
|
user_info=None,
|
|
) -> Dict[str, Any]:
|
|
"""创建线索跟进记录,并级联更新父线索的跟进相关字段"""
|
|
clue = get_clue(db, data.clue_id)
|
|
if not clue:
|
|
raise ValueError("线索不存在")
|
|
if clue.tenant_no != tenant_no:
|
|
raise ValueError("线索不存在")
|
|
_ensure_clue_in_scope(db, clue, user_info, tenant_no)
|
|
if clue.is_converted == 1:
|
|
raise ValueError("已转化的线索不允许添加跟进记录")
|
|
|
|
now = datetime.now()
|
|
row = CrmClueFollow(
|
|
id=generate_uuid(),
|
|
clue_id=data.clue_id,
|
|
clue_name=clue.clue_name or "",
|
|
follow_type=data.follow_type,
|
|
follow_content=data.follow_content,
|
|
next_time=datetime.strptime(data.next_time, _FMT) if data.next_time else None,
|
|
attachments=data.attachments or "",
|
|
create_by_name=user_name,
|
|
tenant_no=tenant_no,
|
|
order_num=0,
|
|
remark=data.remark or "",
|
|
create_by=user_id,
|
|
create_dept_id="",
|
|
create_time=now,
|
|
update_by=user_id,
|
|
update_time=now,
|
|
del_flag=0,
|
|
)
|
|
db.add(row)
|
|
|
|
clue.last_time = now
|
|
clue.follow_count = (clue.follow_count or 0) + 1
|
|
if data.next_time:
|
|
clue.next_time = datetime.strptime(data.next_time, _FMT)
|
|
if clue.status == "new":
|
|
clue.status = "follow"
|
|
clue.update_by = user_id
|
|
clue.update_time = now
|
|
|
|
db.commit()
|
|
db.refresh(row)
|
|
return _row_to_dict(row)
|
|
|
|
|
|
def update_clue_follow(
|
|
db: Session,
|
|
follow_id: str,
|
|
data: Any,
|
|
user_id: str,
|
|
user_info=None,
|
|
tenant_no: str = None,
|
|
) -> Dict[str, Any]:
|
|
"""更新线索跟进记录,必要时同步线索 next_time"""
|
|
from app.models.crm_models import CrmClue
|
|
|
|
row = db.query(CrmClueFollow).filter(
|
|
CrmClueFollow.id == follow_id,
|
|
CrmClueFollow.del_flag == 0,
|
|
).first()
|
|
if not row:
|
|
raise ValueError("跟进记录不存在")
|
|
if tenant_no and row.tenant_no != tenant_no:
|
|
raise ValueError("跟进记录不存在")
|
|
ensure_crm_create_by_row_in_scope(
|
|
db, row, user_info, row.tenant_no, message="无权访问该跟进记录"
|
|
)
|
|
|
|
if data.follow_type is not None:
|
|
row.follow_type = data.follow_type
|
|
if data.follow_content is not None:
|
|
row.follow_content = data.follow_content
|
|
if data.attachments is not None:
|
|
row.attachments = data.attachments
|
|
if data.remark is not None:
|
|
row.remark = data.remark
|
|
if data.next_time is not None:
|
|
row.next_time = datetime.strptime(data.next_time, _FMT) if data.next_time else None
|
|
clue = db.query(CrmClue).filter(CrmClue.id == row.clue_id).first()
|
|
if clue and row.next_time:
|
|
clue.next_time = row.next_time
|
|
clue.update_by = user_id
|
|
clue.update_time = datetime.now()
|
|
|
|
row.update_by = user_id
|
|
row.update_time = datetime.now()
|
|
db.commit()
|
|
db.refresh(row)
|
|
return _row_to_dict(row)
|
|
|
|
|
|
def list_clue_follows(
|
|
db: Session,
|
|
clue_id: str,
|
|
page: int = 1,
|
|
page_size: int = 10,
|
|
user_info=None,
|
|
tenant_no: str = None,
|
|
) -> Tuple[List[Dict[str, Any]], int]:
|
|
"""分页查询线索跟进记录"""
|
|
clue = get_clue(db, clue_id)
|
|
if not clue:
|
|
raise ValueError("线索不存在")
|
|
if tenant_no and clue.tenant_no != tenant_no:
|
|
raise ValueError("线索不存在")
|
|
_ensure_clue_in_scope(db, clue, user_info, clue.tenant_no)
|
|
|
|
q = db.query(CrmClueFollow).filter(
|
|
CrmClueFollow.clue_id == clue_id,
|
|
CrmClueFollow.del_flag == 0,
|
|
)
|
|
if user_info:
|
|
q = apply_crm_create_by_scope(q, CrmClueFollow, user_info, db, clue.tenant_no)
|
|
|
|
total = q.count()
|
|
rows = (
|
|
q.order_by(CrmClueFollow.create_time.desc())
|
|
.offset((page - 1) * page_size)
|
|
.limit(page_size)
|
|
.all()
|
|
)
|
|
return [_row_to_dict(r) for r in rows], total
|
|
|
|
|
|
def get_clue_follow(
|
|
db: Session,
|
|
follow_id: str,
|
|
user_info=None,
|
|
tenant_no: str = None,
|
|
) -> Optional[Dict[str, Any]]:
|
|
"""获取单条跟进记录"""
|
|
row = db.query(CrmClueFollow).filter(
|
|
CrmClueFollow.id == follow_id,
|
|
CrmClueFollow.del_flag == 0,
|
|
).first()
|
|
if not row:
|
|
return None
|
|
if tenant_no and row.tenant_no != tenant_no:
|
|
return None
|
|
ensure_crm_create_by_row_in_scope(
|
|
db, row, user_info, row.tenant_no, message="无权访问该跟进记录"
|
|
)
|
|
return _row_to_dict(row)
|
|
|
|
|
|
def delete_clue_follow(
|
|
db: Session,
|
|
follow_id: str,
|
|
user_info=None,
|
|
tenant_no: str = None,
|
|
) -> bool:
|
|
"""软删除跟进记录,并回写线索 follow_count / last_time"""
|
|
from app.models.crm_models import CrmClue
|
|
|
|
row = db.query(CrmClueFollow).filter(
|
|
CrmClueFollow.id == follow_id,
|
|
CrmClueFollow.del_flag == 0,
|
|
).first()
|
|
if not row:
|
|
return False
|
|
if tenant_no and row.tenant_no != tenant_no:
|
|
return False
|
|
ensure_crm_create_by_row_in_scope(
|
|
db, row, user_info, row.tenant_no, message="无权访问该跟进记录"
|
|
)
|
|
assert_row_deletable_by_creator(row, user_info)
|
|
|
|
clue_id = row.clue_id
|
|
row.del_flag = 1
|
|
row.update_time = datetime.now()
|
|
|
|
clue = db.query(CrmClue).filter(CrmClue.id == clue_id, CrmClue.del_flag == 0).first()
|
|
if clue:
|
|
remaining = (
|
|
db.query(CrmClueFollow)
|
|
.filter(CrmClueFollow.clue_id == clue_id, CrmClueFollow.del_flag == 0)
|
|
.count()
|
|
)
|
|
clue.follow_count = remaining
|
|
latest = (
|
|
db.query(CrmClueFollow)
|
|
.filter(CrmClueFollow.clue_id == clue_id, CrmClueFollow.del_flag == 0)
|
|
.order_by(CrmClueFollow.create_time.desc())
|
|
.first()
|
|
)
|
|
clue.last_time = latest.create_time if latest else None
|
|
clue.update_time = datetime.now()
|
|
|
|
db.commit()
|
|
return True
|
|
|
|
|
|
def get_follow_statistics(
|
|
db: Session,
|
|
clue_id: str,
|
|
user_info=None,
|
|
tenant_no: str = None,
|
|
) -> Dict[str, Any]:
|
|
"""统计线索跟进次数和最后跟进时间"""
|
|
clue = get_clue(db, clue_id)
|
|
if not clue:
|
|
raise ValueError("线索不存在")
|
|
if tenant_no and clue.tenant_no != tenant_no:
|
|
raise ValueError("线索不存在")
|
|
_ensure_clue_in_scope(db, clue, user_info, clue.tenant_no)
|
|
|
|
q = db.query(
|
|
func.count(CrmClueFollow.id).label("total"),
|
|
func.max(CrmClueFollow.create_time).label("lastFollowTime"),
|
|
).filter(
|
|
CrmClueFollow.clue_id == clue_id,
|
|
CrmClueFollow.del_flag == 0,
|
|
)
|
|
if user_info:
|
|
q = apply_crm_create_by_scope(q, CrmClueFollow, user_info, db, clue.tenant_no)
|
|
|
|
result = q.first()
|
|
|
|
return {
|
|
"total": result.total or 0,
|
|
"lastFollowTime": result.lastFollowTime.strftime(_FMT) if result.lastFollowTime else "",
|
|
}
|