1
This commit is contained in:
183
api/middleware/schedule/tasks/autoChatTask.js
Normal file
183
api/middleware/schedule/tasks/autoChatTask.js
Normal file
@@ -0,0 +1,183 @@
|
||||
const BaseTask = require('./baseTask');
|
||||
const db = require('../../dbProxy');
|
||||
const config = require('../config');
|
||||
|
||||
/**
|
||||
* 自动沟通任务
|
||||
* 自动回复HR消息,保持活跃度
|
||||
*/
|
||||
class AutoChatTask extends BaseTask {
|
||||
constructor() {
|
||||
super('auto_chat', {
|
||||
defaultInterval: 15, // 默认15分钟
|
||||
defaultPriority: 6, // 中等优先级
|
||||
requiresLogin: true, // 需要登录
|
||||
conflictsWith: [] // 不与其他任务冲突(可以在投递/搜索间隙执行)
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证任务参数
|
||||
*/
|
||||
validateParams(params) {
|
||||
if (!params.platform) {
|
||||
return {
|
||||
valid: false,
|
||||
reason: '缺少必要参数: platform'
|
||||
};
|
||||
}
|
||||
return { valid: true };
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取任务名称
|
||||
*/
|
||||
getTaskName(params) {
|
||||
return `自动沟通 - ${params.name || '默认'}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行自动沟通任务
|
||||
*/
|
||||
async execute(sn_code, params) {
|
||||
console.log(`[自动沟通] 设备 ${sn_code} 开始执行沟通任务`);
|
||||
|
||||
// 1. 获取未读消息列表
|
||||
const unreadMessages = await this.getUnreadMessages(sn_code, params.platform);
|
||||
|
||||
if (!unreadMessages || unreadMessages.length === 0) {
|
||||
console.log(`[自动沟通] 设备 ${sn_code} 没有未读消息`);
|
||||
return {
|
||||
success: true,
|
||||
repliedCount: 0,
|
||||
message: '没有未读消息'
|
||||
};
|
||||
}
|
||||
|
||||
console.log(`[自动沟通] 设备 ${sn_code} 找到 ${unreadMessages.length} 条未读消息`);
|
||||
|
||||
// 2. 智能回复(这里需要调用实际的AI回复逻辑)
|
||||
const replyResult = {
|
||||
success: true,
|
||||
repliedCount: unreadMessages.length,
|
||||
messages: unreadMessages.map(m => ({
|
||||
id: m.id,
|
||||
from: m.hr_name,
|
||||
company: m.company_name
|
||||
}))
|
||||
};
|
||||
|
||||
return replyResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取未读消息
|
||||
*/
|
||||
async getUnreadMessages(sn_code, platform) {
|
||||
// TODO: 从数据库或缓存获取未读消息
|
||||
// 这里返回空数组作为示例
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加沟通任务到队列
|
||||
*/
|
||||
async addToQueue(sn_code, taskQueue, customParams = {}) {
|
||||
const now = new Date();
|
||||
console.log(`[自动沟通] ${now.toLocaleString()} 尝试为设备 ${sn_code} 添加任务`);
|
||||
|
||||
try {
|
||||
// 1. 获取账号信息
|
||||
const { pla_account } = db.models;
|
||||
const account = await pla_account.findOne({
|
||||
where: {
|
||||
sn_code: sn_code,
|
||||
is_delete: 0,
|
||||
is_enabled: 1
|
||||
}
|
||||
});
|
||||
|
||||
if (!account) {
|
||||
console.log(`[自动沟通] 账号 ${sn_code} 不存在或未启用`);
|
||||
return { success: false, reason: '账号不存在或未启用' };
|
||||
}
|
||||
|
||||
const accountData = account.toJSON();
|
||||
|
||||
// 2. 检查是否开启了自动沟通
|
||||
if (!accountData.auto_chat) {
|
||||
console.log(`[自动沟通] 设备 ${sn_code} 未开启自动沟通`);
|
||||
return { success: false, reason: '未开启自动沟通' };
|
||||
}
|
||||
|
||||
// 3. 获取沟通策略配置
|
||||
let chatStrategy = {};
|
||||
if (accountData.chat_strategy) {
|
||||
chatStrategy = typeof accountData.chat_strategy === 'string'
|
||||
? JSON.parse(accountData.chat_strategy)
|
||||
: accountData.chat_strategy;
|
||||
}
|
||||
|
||||
// 4. 检查时间范围
|
||||
if (chatStrategy.time_range) {
|
||||
const timeCheck = this.checkTimeRange(chatStrategy.time_range);
|
||||
if (!timeCheck.allowed) {
|
||||
console.log(`[自动沟通] 设备 ${sn_code} ${timeCheck.reason}`);
|
||||
return { success: false, reason: timeCheck.reason };
|
||||
}
|
||||
}
|
||||
|
||||
// 5. 执行所有层级的冲突检查
|
||||
const conflictCheck = await this.canExecuteTask(sn_code, taskQueue);
|
||||
if (!conflictCheck.allowed) {
|
||||
console.log(`[自动沟通] 设备 ${sn_code} 冲突检查未通过: ${conflictCheck.reason}`);
|
||||
return { success: false, reason: conflictCheck.reason };
|
||||
}
|
||||
|
||||
// 6. 检查沟通间隔
|
||||
const chat_interval = chatStrategy.chat_interval || this.config.defaultInterval;
|
||||
const intervalCheck = await this.checkExecutionInterval(sn_code, chat_interval);
|
||||
|
||||
if (!intervalCheck.allowed) {
|
||||
console.log(`[自动沟通] 设备 ${sn_code} ${intervalCheck.reason}`);
|
||||
return { success: false, reason: intervalCheck.reason };
|
||||
}
|
||||
|
||||
// 7. 构建任务参数
|
||||
const taskParams = {
|
||||
platform: accountData.platform_type || 'boss',
|
||||
name: accountData.name || '默认',
|
||||
...customParams
|
||||
};
|
||||
|
||||
// 8. 验证参数
|
||||
const validation = this.validateParams(taskParams);
|
||||
if (!validation.valid) {
|
||||
return { success: false, reason: validation.reason };
|
||||
}
|
||||
|
||||
// 9. 添加任务到队列
|
||||
await taskQueue.addTask(sn_code, {
|
||||
taskType: this.taskType,
|
||||
taskName: this.getTaskName(taskParams),
|
||||
taskParams: taskParams,
|
||||
priority: this.config.defaultPriority
|
||||
});
|
||||
|
||||
console.log(`[自动沟通] 已为设备 ${sn_code} 添加沟通任务,间隔: ${chat_interval} 分钟`);
|
||||
|
||||
// 10. 释放任务锁
|
||||
this.releaseTaskLock(sn_code);
|
||||
|
||||
return { success: true };
|
||||
|
||||
} catch (error) {
|
||||
console.error(`[自动沟通] 添加任务失败:`, error);
|
||||
this.releaseTaskLock(sn_code);
|
||||
return { success: false, reason: error.message };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 导出单例
|
||||
module.exports = new AutoChatTask();
|
||||
Reference in New Issue
Block a user