#include "AutoCall.h" #include "JdbcHelper.h" #include "Log.h" #include AutoCall::AutoCall() :m_IsStop(false), fun(nullptr) { } AutoCall::~AutoCall() { m_IsStop = true; } void AutoCall::startTask() { m_IsStop = false; //std::thread(std::bind(&AutoCall::__threadFun, this)).detach(); std::thread(std::bind(&AutoCall::__threadFunV2, this)).detach(); } void AutoCall::stopTask() { m_IsStop = true; } void AutoCall::addTask() { std::unique_locklock(g_mut); // 2020-09-14 std::string sql = "select id,agent,number,type,content from rep_task where state = 0"; JdbcHelper::GetInstance()->jdbc_executeQuery(sql, [=](sql::PreparedStatement* stmt) { }, [&](sql::ResultSet* result) { while (result->next()) { Task task; task.id = result->getInt("id"); task.agent = result->getString("agent"); task.number = result->getString("number"); task.type = result->getInt("type"); task.content = result->getString("content"); // 判断等待队列是否 volatile bool isExist = false; if (task.type == 2) { std::unique_locklock(mutWait); auto it = m_WaitTasks.begin(); while (it != m_WaitTasks.end()) { if (it->second.agent == task.agent) // 任务中已存在该坐席呼叫,则加入等待队列 { m_WaitTasks.insert(std::make_pair(task.id, task)); isExist = true; break; } ++it; } } // 如果等待任务中不存在,再次判断任务中是否存在 if(!isExist) { volatile bool isFirst = true; if (task.type == 2) // 如果指定坐席呼叫,只有第一个进入直接呼叫,之后的放入等待队列 { { std::unique_locklock(mut); auto it = m_Tasks.begin(); while (it != m_Tasks.end()) { if (it->second.agent == task.agent) // 任务中已存在该坐席呼叫,则加入等待队列 { isFirst = false; break; } ++it; } if (isFirst) m_Tasks.insert(std::make_pair(task.id, task)); } if(!isFirst) { std::unique_locklock(mutWait); m_WaitTasks.insert(std::make_pair(task.id, task)); } } if(task.type !=2 ) { std::unique_locklock(mut); m_Tasks.insert(std::make_pair(task.id, task)); } } } }, NULL); con.notify_one(); } void AutoCall::waitTask(std::string agentId) { // 使用定时扫描,该方法暂时不用 return; Task task; { std::unique_locklock(mutWait); auto it = m_WaitTasks.begin(); while (it != m_WaitTasks.end()) { if (it->second.agent == agentId) { task = it->second; m_WaitTasks.erase(it); break; } ++it; } } if (this->fun != nullptr && task.id != 0) this->fun(task); } void AutoCall::updateTask(std::string id) { if (id.empty()) return; std::string sql = "update rep_task set state = 1,op_time = now() where id = "; sql.append(id); JdbcHelper::GetInstance()->jdbc_executeUpdate(sql,NULL,NULL); { std::unique_locklock(mut); m_Tasks.erase(atoi(id.c_str())); } { std::unique_locklock(mutWait); m_WaitTasks.erase(atoi(id.c_str())); } } void AutoCall::__threadFun() { while (!m_IsStop) { if(!__dealTask()) { //std::unique_locklock(mut); std::unique_locklock(g_mut); // 2020-09-14 con.wait(lock); } } } void AutoCall::__threadFunV2() { while (!m_IsStop) { std::this_thread::sleep_for(std::chrono::seconds(60)); std::map tasks; // 坐席-任务 std::map tasksId; // 任务ID-任务 std::string sql = "select id,agent,number,type,content from rep_task where state = 0"; JdbcHelper::GetInstance()->jdbc_executeQuery(sql, [=](sql::PreparedStatement* stmt) { }, [&](sql::ResultSet* result) { while (result->next()) { Task task; task.id = result->getInt("id"); task.agent = result->getString("agent"); task.number = result->getString("number"); task.type = result->getInt("type"); task.content = result->getString("content"); if (task.type == 2) // 坐席外呼 { tasks.insert(std::make_pair(task.agent, task)); } else if (task.type == 1) // 语音外呼 { tasksId.insert(std::make_pair(task.id, task)); } } }, NULL); { auto it = tasksId.begin(); while (it != tasksId.end()) { if (this->fun != nullptr) this->fun(it->second); it = tasksId.erase(it); } } { auto it = tasks.begin(); while (it != tasks.end()) { if (this->fun != nullptr) this->fun(it->second); it = tasks.erase(it); } } } } bool AutoCall::__dealTask() { Task task; { std::unique_locklock(mut); auto it = m_Tasks.begin(); if (it == m_Tasks.end()) return false; task = it->second; m_Tasks.erase(it); } if (this->fun != nullptr && task.id != 0) this->fun(task); return !m_Tasks.empty(); }