| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- #include "AutoCall.h"
- #include "JdbcHelper.h"
- #include "Log.h"
- #include <thread>
- 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_lock<std::mutex>lock(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_lock<std::mutex>lock(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_lock<std::mutex>lock(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_lock<std::mutex>lock(mutWait);
- m_WaitTasks.insert(std::make_pair(task.id, task));
- }
- }
-
- if(task.type !=2 )
- {
- std::unique_lock<std::mutex>lock(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_lock<std::mutex>lock(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_lock<std::mutex>lock(mut);
- m_Tasks.erase(atoi(id.c_str()));
- }
- {
- std::unique_lock<std::mutex>lock(mutWait);
- m_WaitTasks.erase(atoi(id.c_str()));
- }
- }
- void AutoCall::__threadFun()
- {
- while (!m_IsStop)
- {
- if(!__dealTask())
- {
- //std::unique_lock<std::mutex>lock(mut);
- std::unique_lock<std::mutex>lock(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<std::string, Task> tasks; // 坐席-任务
- std::map<int, Task> 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_lock<std::mutex>lock(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();
- }
|