107 lines
3.5 KiB
JavaScript
107 lines
3.5 KiB
JavaScript
const BaseHandler = require('./baseHandler');
|
||
const ConfigManager = require('../services/configManager');
|
||
const command = require('../core/command');
|
||
const config = require('../infrastructure/config');
|
||
|
||
/**
|
||
* 自动搜索处理器
|
||
* 负责搜索职位列表
|
||
*/
|
||
class SearchHandler extends BaseHandler {
|
||
/**
|
||
* 处理自动搜索任务
|
||
* @param {object} task - 任务对象
|
||
* @returns {Promise<object>} 执行结果
|
||
*/
|
||
async handle(task) {
|
||
return await this.execute(task, async () => {
|
||
return await this.doSearch(task);
|
||
}, {
|
||
checkAuth: true,
|
||
checkOnline: true,
|
||
recordDeviceMetrics: true
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 执行搜索逻辑
|
||
*/
|
||
async doSearch(task) {
|
||
const { sn_code, taskParams } = task;
|
||
const { keyword, platform = 'boss', pageCount = 3 } = taskParams;
|
||
|
||
console.log(`[自动搜索] 开始 - 设备: ${sn_code}, 关键词: ${keyword}`);
|
||
|
||
// 1. 获取账户配置
|
||
const accountConfig = await this.getAccountConfig(sn_code, ['keyword', 'platform_type', 'search_config']);
|
||
|
||
if (!accountConfig) {
|
||
return {
|
||
jobsFound: 0,
|
||
message: '未找到账户配置'
|
||
};
|
||
}
|
||
|
||
// 2. 解析搜索配置
|
||
const searchConfig = ConfigManager.parseSearchConfig(accountConfig.search_config);
|
||
|
||
// 3. 检查搜索时间范围
|
||
const timeRange = ConfigManager.getTimeRange(searchConfig);
|
||
if (timeRange) {
|
||
const timeRangeValidator = require('../services/timeRangeValidator');
|
||
const timeCheck = timeRangeValidator.checkTimeRange(timeRange);
|
||
|
||
if (!timeCheck.allowed) {
|
||
return {
|
||
jobsFound: 0,
|
||
message: timeCheck.reason
|
||
};
|
||
}
|
||
}
|
||
|
||
// 4. 从 resume_info 取 deliver_tab_label,下发给 get_job_list 用于切换期望 tab
|
||
const platformType = platform || accountConfig.platform_type || 'boss';
|
||
let tabLabel = '';
|
||
try {
|
||
const db = require('../../dbProxy');
|
||
const resume_info = db.getModel('resume_info');
|
||
const resume = await resume_info.findOne({
|
||
where: { sn_code, platform: platformType, isActive: true },
|
||
order: [['last_modify_time', 'DESC']],
|
||
attributes: ['deliver_tab_label']
|
||
});
|
||
if (resume && resume.deliver_tab_label) {
|
||
tabLabel = String(resume.deliver_tab_label).trim();
|
||
}
|
||
} catch (e) {
|
||
console.warn('[自动搜索] 读取 resume_info.deliver_tab_label 失败:', e.message);
|
||
}
|
||
|
||
const commandParams = {
|
||
sn_code,
|
||
platform: platformType,
|
||
pageCount: pageCount || searchConfig.page_count || 3,
|
||
...(tabLabel ? { tabLabel } : {})
|
||
};
|
||
|
||
const searchCommand = {
|
||
command_type: 'get_job_list',
|
||
command_name: '获取职位列表',
|
||
command_params: JSON.stringify(commandParams),
|
||
priority: config.getTaskPriority('auto_search') || 8
|
||
};
|
||
|
||
// 5. 执行搜索指令
|
||
const result = await command.executeCommands(task.id, [searchCommand], this.mqttClient);
|
||
|
||
console.log(`[自动搜索] 完成 - 设备: ${sn_code}, 结果: ${JSON.stringify(result)}`);
|
||
|
||
return {
|
||
jobsFound: result.jobCount || 0,
|
||
message: '搜索完成'
|
||
};
|
||
}
|
||
}
|
||
|
||
module.exports = SearchHandler;
|