linux版本中间件

AutoCall.cpp 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include "AutoCall.h"
  2. #include "JdbcHelper.h"
  3. #include "Log.h"
  4. #include <thread>
  5. AutoCall::AutoCall() :m_IsStop(false), fun(nullptr)
  6. {
  7. }
  8. AutoCall::~AutoCall()
  9. {
  10. m_IsStop = true;
  11. }
  12. void AutoCall::startTask()
  13. {
  14. m_IsStop = false;
  15. //std::thread(std::bind(&AutoCall::__threadFun, this)).detach();
  16. std::thread(std::bind(&AutoCall::__threadFunV2, this)).detach();
  17. }
  18. void AutoCall::stopTask()
  19. {
  20. m_IsStop = true;
  21. }
  22. void AutoCall::addTask()
  23. {
  24. std::unique_lock<std::mutex>lock(g_mut); // 2020-09-14
  25. std::string sql = "select id,agent,number,type,content from rep_task where state = 0";
  26. JdbcHelper::GetInstance()->jdbc_executeQuery(sql, [=](sql::PreparedStatement* stmt) {
  27. }, [&](sql::ResultSet* result) {
  28. while (result->next())
  29. {
  30. Task task;
  31. task.id = result->getInt("id");
  32. task.agent = result->getString("agent");
  33. task.number = result->getString("number");
  34. task.type = result->getInt("type");
  35. task.content = result->getString("content");
  36. // 判断等待队列是否
  37. volatile bool isExist = false;
  38. if (task.type == 2)
  39. {
  40. std::unique_lock<std::mutex>lock(mutWait);
  41. auto it = m_WaitTasks.begin();
  42. while (it != m_WaitTasks.end())
  43. {
  44. if (it->second.agent == task.agent) // 任务中已存在该坐席呼叫,则加入等待队列
  45. {
  46. m_WaitTasks.insert(std::make_pair(task.id, task));
  47. isExist = true;
  48. break;
  49. }
  50. ++it;
  51. }
  52. }
  53. // 如果等待任务中不存在,再次判断任务中是否存在
  54. if(!isExist)
  55. {
  56. volatile bool isFirst = true;
  57. if (task.type == 2) // 如果指定坐席呼叫,只有第一个进入直接呼叫,之后的放入等待队列
  58. {
  59. {
  60. std::unique_lock<std::mutex>lock(mut);
  61. auto it = m_Tasks.begin();
  62. while (it != m_Tasks.end())
  63. {
  64. if (it->second.agent == task.agent) // 任务中已存在该坐席呼叫,则加入等待队列
  65. {
  66. isFirst = false;
  67. break;
  68. }
  69. ++it;
  70. }
  71. if (isFirst)
  72. m_Tasks.insert(std::make_pair(task.id, task));
  73. }
  74. if(!isFirst)
  75. {
  76. std::unique_lock<std::mutex>lock(mutWait);
  77. m_WaitTasks.insert(std::make_pair(task.id, task));
  78. }
  79. }
  80. if(task.type !=2 )
  81. {
  82. std::unique_lock<std::mutex>lock(mut);
  83. m_Tasks.insert(std::make_pair(task.id, task));
  84. }
  85. }
  86. }
  87. }, NULL);
  88. con.notify_one();
  89. }
  90. void AutoCall::waitTask(std::string agentId)
  91. {
  92. // 使用定时扫描,该方法暂时不用
  93. return;
  94. Task task;
  95. {
  96. std::unique_lock<std::mutex>lock(mutWait);
  97. auto it = m_WaitTasks.begin();
  98. while (it != m_WaitTasks.end())
  99. {
  100. if (it->second.agent == agentId)
  101. {
  102. task = it->second;
  103. m_WaitTasks.erase(it);
  104. break;
  105. }
  106. ++it;
  107. }
  108. }
  109. if (this->fun != nullptr && task.id != 0)
  110. this->fun(task);
  111. }
  112. void AutoCall::updateTask(std::string id)
  113. {
  114. if (id.empty()) return;
  115. std::string sql = "update rep_task set state = 1,op_time = now() where id = ";
  116. sql.append(id);
  117. JdbcHelper::GetInstance()->jdbc_executeUpdate(sql,NULL,NULL);
  118. {
  119. std::unique_lock<std::mutex>lock(mut);
  120. m_Tasks.erase(atoi(id.c_str()));
  121. }
  122. {
  123. std::unique_lock<std::mutex>lock(mutWait);
  124. m_WaitTasks.erase(atoi(id.c_str()));
  125. }
  126. }
  127. void AutoCall::__threadFun()
  128. {
  129. while (!m_IsStop)
  130. {
  131. if(!__dealTask())
  132. {
  133. //std::unique_lock<std::mutex>lock(mut);
  134. std::unique_lock<std::mutex>lock(g_mut); // 2020-09-14
  135. con.wait(lock);
  136. }
  137. }
  138. }
  139. void AutoCall::__threadFunV2()
  140. {
  141. while (!m_IsStop)
  142. {
  143. std::this_thread::sleep_for(std::chrono::seconds(60));
  144. std::map<std::string, Task> tasks; // 坐席-任务
  145. std::map<int, Task> tasksId; // 任务ID-任务
  146. std::string sql = "select id,agent,number,type,content from rep_task where state = 0";
  147. JdbcHelper::GetInstance()->jdbc_executeQuery(sql, [=](sql::PreparedStatement* stmt) {
  148. }, [&](sql::ResultSet* result) {
  149. while (result->next())
  150. {
  151. Task task;
  152. task.id = result->getInt("id");
  153. task.agent = result->getString("agent");
  154. task.number = result->getString("number");
  155. task.type = result->getInt("type");
  156. task.content = result->getString("content");
  157. if (task.type == 2) // 坐席外呼
  158. {
  159. tasks.insert(std::make_pair(task.agent, task));
  160. }
  161. else if (task.type == 1) // 语音外呼
  162. {
  163. tasksId.insert(std::make_pair(task.id, task));
  164. }
  165. }
  166. }, NULL);
  167. {
  168. auto it = tasksId.begin();
  169. while (it != tasksId.end())
  170. {
  171. if (this->fun != nullptr)
  172. this->fun(it->second);
  173. it = tasksId.erase(it);
  174. }
  175. }
  176. {
  177. auto it = tasks.begin();
  178. while (it != tasks.end())
  179. {
  180. if (this->fun != nullptr)
  181. this->fun(it->second);
  182. it = tasks.erase(it);
  183. }
  184. }
  185. }
  186. }
  187. bool AutoCall::__dealTask()
  188. {
  189. Task task;
  190. {
  191. std::unique_lock<std::mutex>lock(mut);
  192. auto it = m_Tasks.begin();
  193. if (it == m_Tasks.end())
  194. return false;
  195. task = it->second;
  196. m_Tasks.erase(it);
  197. }
  198. if (this->fun != nullptr && task.id != 0)
  199. this->fun(task);
  200. return !m_Tasks.empty();
  201. }