- 模块一: 出海案例作品集、博客动态发布、人名发音语音条、RTL阿语多语言切换 - 模块二: AI顾问人设配置、行业术语库润色、RAG切片沙盒调试、流式会话与意向留资卡片 - 模块三: 全球访客雷达大屏、多维加权意向算分、外部CRM对接推送、团队公海池流转 - 模块四: 移动端NFC安全挂失、NFC动态路由规则配置、动态防伪验签、展会制卡Zip批量导出 - 模块五: 组织架构与名片分配、移动端个人中心设置、离职交接与历史线索迁移、安全审计日志 - 模块六: Stripe国际信用卡订阅Webhook、独立二级域名与SSL检测、坐席配额预警、多币种发票 - 模块七: 纸质名片OCR解析入库、国际商务日程RFC 5545 ICS日历、微官网离线SWR预加载、展会实时排行榜大屏 - 模块八: 全球出海SEO OpenGraph元标签注入与社交爬虫探测、30点端到端全链路业务流集成测试套件
222 行
6.5 KiB
TypeScript
222 行
6.5 KiB
TypeScript
import { Provide } from '@midwayjs/core';
|
|
import { BaseService, CoolCommException } from '@cool-midway/core';
|
|
import { InjectEntityModel } from '@midwayjs/typeorm';
|
|
import { Repository } from 'typeorm';
|
|
import { CrmLeadEntity } from '../entity/lead';
|
|
import { CrmLeadAuditEntity } from '../entity/audit';
|
|
|
|
/**
|
|
* 自动回收执行结果接口
|
|
*/
|
|
export interface AutoRecycleResult {
|
|
recycledCount: number;
|
|
leadIds: number[];
|
|
}
|
|
|
|
/**
|
|
* 团队线索公海池流转与自动分配服务
|
|
* 负责线索公海回收、超时自动扫描回收、销售原子防并发抢单、管理员指派及全链路审计追踪
|
|
*/
|
|
@Provide()
|
|
export class CrmLeadPoolService extends BaseService {
|
|
@InjectEntityModel(CrmLeadEntity)
|
|
crmLeadEntity: Repository<CrmLeadEntity>;
|
|
|
|
@InjectEntityModel(CrmLeadAuditEntity)
|
|
crmLeadAuditEntity: Repository<CrmLeadAuditEntity>;
|
|
|
|
/**
|
|
* 将线索退回/手动置入公海池
|
|
* @param leadId 线索主键 ID
|
|
* @param reason 退回公海原因说明
|
|
*/
|
|
async recycleToPool(
|
|
leadId: number,
|
|
reason?: string
|
|
): Promise<CrmLeadEntity> {
|
|
const id = Number(leadId);
|
|
if (!id) {
|
|
throw new CoolCommException('缺少线索 ID', 400);
|
|
}
|
|
|
|
const lead = await this.crmLeadEntity.findOne({ where: { id } });
|
|
if (!lead) {
|
|
throw new CoolCommException('潜客线索不存在', 404);
|
|
}
|
|
|
|
const oldUserId = lead.userId ?? null;
|
|
lead.isPool = true;
|
|
lead.userId = null;
|
|
const saved = await this.crmLeadEntity.save(lead);
|
|
|
|
const audit = new CrmLeadAuditEntity();
|
|
audit.leadId = id;
|
|
audit.action = 'recycle';
|
|
audit.fromUserId = oldUserId;
|
|
audit.toUserId = null;
|
|
audit.reason = reason || '手动退回公海池';
|
|
await this.crmLeadAuditEntity.save(audit);
|
|
|
|
return saved;
|
|
}
|
|
|
|
/**
|
|
* 定时扫描超过指定天数未跟进的线索,自动批量归入公海池
|
|
* @param inactivityDays 未跟进超期天数阈值
|
|
*/
|
|
async scanAndAutoRecycle(inactivityDays: number): Promise<AutoRecycleResult> {
|
|
if (!inactivityDays || inactivityDays <= 0) {
|
|
return { recycledCount: 0, leadIds: [] };
|
|
}
|
|
|
|
const thresholdMs = inactivityDays * 24 * 60 * 60 * 1000;
|
|
const now = Date.now();
|
|
|
|
const allLeads = await this.crmLeadEntity.find();
|
|
const toRecycle: CrmLeadEntity[] = [];
|
|
|
|
for (const lead of allLeads) {
|
|
// 仅扫描私海线索,且排除已赢单 (won) 和已归档 (archived)
|
|
if (lead.isPool) continue;
|
|
if (lead.status === 'won' || lead.status === 'archived') continue;
|
|
|
|
const followUp = lead.lastFollowUpTime || lead.updateTime || lead.createTime;
|
|
if (!followUp) continue;
|
|
|
|
const diff = now - new Date(followUp).getTime();
|
|
if (diff >= thresholdMs) {
|
|
toRecycle.push(lead);
|
|
}
|
|
}
|
|
|
|
const recycledIds: number[] = [];
|
|
for (const lead of toRecycle) {
|
|
const oldUserId = lead.userId ?? null;
|
|
lead.isPool = true;
|
|
lead.userId = null;
|
|
await this.crmLeadEntity.save(lead);
|
|
recycledIds.push(Number(lead.id));
|
|
|
|
const audit = new CrmLeadAuditEntity();
|
|
audit.leadId = Number(lead.id);
|
|
audit.action = 'auto_recycle';
|
|
audit.fromUserId = oldUserId;
|
|
audit.toUserId = null;
|
|
audit.reason = `超过 ${inactivityDays} 天未跟进自动回收至公海池`;
|
|
await this.crmLeadAuditEntity.save(audit);
|
|
}
|
|
|
|
return { recycledCount: recycledIds.length, leadIds: recycledIds };
|
|
}
|
|
|
|
/**
|
|
* 销售成员从公海池抢单/领用 (CAS 原子性防并发保护)
|
|
* @param leadId 线索 ID
|
|
* @param userId 认领销售人员 ID
|
|
*/
|
|
async claimLead(leadId: number, userId: number): Promise<CrmLeadEntity> {
|
|
const id = Number(leadId);
|
|
const targetUid = Number(userId);
|
|
|
|
if (!id) {
|
|
throw new CoolCommException('缺少线索 ID', 400);
|
|
}
|
|
if (!targetUid) {
|
|
throw new CoolCommException('缺少领用销售人员 ID', 400);
|
|
}
|
|
|
|
// 检查线索是否存在
|
|
const existing = await this.crmLeadEntity.findOne({ where: { id } });
|
|
if (!existing) {
|
|
throw new CoolCommException('潜客线索不存在', 404);
|
|
}
|
|
|
|
if (!existing.isPool || existing.userId) {
|
|
throw new CoolCommException('该线索已被其他销售认领或不在公海池', 409);
|
|
}
|
|
|
|
// 原子性条件更新:仅在线索仍在公海时方可更新
|
|
const updateResult = await this.crmLeadEntity.update(
|
|
{ id, isPool: true },
|
|
{ isPool: false, userId: targetUid }
|
|
);
|
|
|
|
if (!updateResult || updateResult.affected === 0) {
|
|
throw new CoolCommException('该线索已被其他销售认领或不在公海池', 409);
|
|
}
|
|
|
|
const updated = await this.crmLeadEntity.findOne({ where: { id } });
|
|
const resultLead = updated || existing;
|
|
resultLead.isPool = false;
|
|
resultLead.userId = targetUid;
|
|
|
|
// 记录领用审计日志
|
|
const audit = new CrmLeadAuditEntity();
|
|
audit.leadId = id;
|
|
audit.action = 'claim';
|
|
audit.operatorId = targetUid;
|
|
audit.fromUserId = null;
|
|
audit.toUserId = targetUid;
|
|
audit.reason = '销售公海抢单领用';
|
|
await this.crmLeadAuditEntity.save(audit);
|
|
|
|
return resultLead;
|
|
}
|
|
|
|
/**
|
|
* 管理员指定派发分配线索
|
|
* @param leadId 线索 ID
|
|
* @param targetUserId 目标销售成员 ID
|
|
* @param assignerId 管理员/操作人 ID
|
|
*/
|
|
async assignLead(
|
|
leadId: number,
|
|
targetUserId: number,
|
|
assignerId?: number
|
|
): Promise<CrmLeadEntity> {
|
|
const id = Number(leadId);
|
|
const targetUid = Number(targetUserId);
|
|
|
|
if (!id) {
|
|
throw new CoolCommException('缺少线索 ID', 400);
|
|
}
|
|
if (!targetUid) {
|
|
throw new CoolCommException('缺少目标销售人员 ID', 400);
|
|
}
|
|
|
|
const lead = await this.crmLeadEntity.findOne({ where: { id } });
|
|
if (!lead) {
|
|
throw new CoolCommException('潜客线索不存在', 404);
|
|
}
|
|
|
|
const oldUserId = lead.userId ?? null;
|
|
lead.userId = targetUid;
|
|
lead.isPool = false;
|
|
const saved = await this.crmLeadEntity.save(lead);
|
|
|
|
const audit = new CrmLeadAuditEntity();
|
|
audit.leadId = id;
|
|
audit.action = 'assign';
|
|
audit.operatorId = assignerId ? Number(assignerId) : null;
|
|
audit.fromUserId = oldUserId;
|
|
audit.toUserId = targetUid;
|
|
audit.reason = '管理员指定派发线索';
|
|
await this.crmLeadAuditEntity.save(audit);
|
|
|
|
return saved;
|
|
}
|
|
|
|
/**
|
|
* 查询指定线索的全生命周期流转审计追踪日志
|
|
* @param leadId 线索 ID
|
|
*/
|
|
async getAuditLogs(leadId: number): Promise<CrmLeadAuditEntity[]> {
|
|
const id = Number(leadId);
|
|
if (!id) return [];
|
|
|
|
return await this.crmLeadAuditEntity.find({
|
|
where: { leadId: id },
|
|
});
|
|
}
|
|
}
|