ai_scheduler/sql/02_advicer_mongo_init.js

61 lines
3.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// ============================================================
// AI 数字销售(个人微信):MongoDB 集合与索引初始化
// 数据库:ai_scheduler(见 config/config.yaml mongo.dataBase)
// 集合:
// 1. advicer_wx_msg —— 微信消息流水(回调接收/AI 发送登记,画像迭代基础数据)
// 2. advicer_activity —— 产品活动(后台发布,AI 主动对话与活动推送素材)
// 3. advicer_proactive_log —— AI 主动触达记录(生日祝福/活动推送,含去重依据)
// 字段变更(无需迁移):
// advicer_client 新增 lastIterateAt(画像迭代游标时间);
// MongoDB 无固定 schema,新字段在写入时自动创建,无需 DDL。
// 说明:
// 1. 本模块数据均存于 MongoDB,MySQL 侧本次无变更;
// 2. 脚本幂等,可重复执行(集合存在则跳过,索引同名自动忽略)。
// 执行方式(任选一种,注意替换账号密码):
// mongosh "mongodb://<user>:<pass>@<host>:<port>/ai_scheduler" --file 02_advicer_mongo_init.js
// 或:mongosh --host <host> --port <port> --username <user> --password <pass> ai_scheduler 02_advicer_mongo_init.js
// ============================================================
db = db.getSiblingDB("ai_scheduler");
// ---------- 1. advicer_wx_msg 微信消息流水 ----------
if (!db.getCollectionNames().includes("advicer_wx_msg")) {
db.createCollection("advicer_wx_msg");
}
// 上游消息 ID 去重(AI 发送登记的消息 msgId 形如 ai-xxxx,稀疏索引兼容空值场景)
db.advicer_wx_msg.createIndex({ msgId: 1 }, { name: "idx_msgId", sparse: true });
// 按客户拉取最近消息(画像迭代/策略生成回复)
db.advicer_wx_msg.createIndex({ wxid: 1, createAt: -1 }, { name: "idx_wxid_createAt" });
// 迭代任务扫描未分析消息(analyzed=false 按时间正序)
db.advicer_wx_msg.createIndex({ analyzed: 1, createAt: 1 }, { name: "idx_analyzed_createAt" });
// ---------- 2. advicer_activity 产品活动 ----------
if (!db.getCollectionNames().includes("advicer_activity")) {
db.createCollection("advicer_activity");
}
// 后台活动列表(按项目 + 更新时间倒序)
db.advicer_activity.createIndex({ projectId: 1, updateAt: -1 }, { name: "idx_project_updateAt" });
// 生效活动扫描(状态 + 开始时间)
db.advicer_activity.createIndex({ status: 1, startAt: -1 }, { name: "idx_status_startAt" });
// ---------- 3. advicer_proactive_log AI 主动触达记录 ----------
if (!db.getCollectionNames().includes("advicer_proactive_log")) {
db.createCollection("advicer_proactive_log");
}
// 触达去重(同一客户对同一活动/生日年度只推送一次)
db.advicer_proactive_log.createIndex({ clientId: 1, type: 1, refId: 1 }, { name: "idx_client_type_ref" });
// 客户触达历史(含当日频率限制判断)
db.advicer_proactive_log.createIndex({ clientId: 1, sendAt: -1 }, { name: "idx_client_sendAt" });
// 按 wxid 排查触达记录
db.advicer_proactive_log.createIndex({ wxid: 1, type: 1, sendAt: -1 }, { name: "idx_wxid_type_sendAt" });
// ---------- 4. advicer_client 客户(补充索引) ----------
// 回调链按 wxid 定位客户
db.advicer_client.createIndex({ wxid: 1 }, { name: "idx_wxid", sparse: true });
// 后台客户列表(按项目 + 销售)
db.advicer_client.createIndex({ projectId: 1, advicerId: 1 }, { name: "idx_project_advicer" });
// 策略任务扫描(按等级 + AI 开关)
db.advicer_client.createIndex({ clientLevel: 1, aiEnabled: 1 }, { name: "idx_level_aiEnabled" });
print("[02_advicer_mongo_init] 集合与索引初始化完成");