linux版本中间件

AutoCall.cpp 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. }
  17. void AutoCall::stopTask()
  18. {
  19. m_IsStop = true;
  20. }
  21. void AutoCall::addTask()
  22. {
  23. std::string sql = "select id,agent,number,type,content from task where state = 0";
  24. JdbcHelper::GetInstance()->jdbc_executeQuery(sql, [=](sql::PreparedStatement* stmt) {
  25. }, [&](sql::ResultSet* result) {
  26. while (result->next())
  27. {
  28. Task task;
  29. task.id = result->getInt("id");
  30. task.agent = result->getString("agent");
  31. task.number = result->getString("number");
  32. task.type = result->getInt("type");
  33. task.content = result->getString("content");
  34. // 判断等待队列是否
  35. volatile bool isExist = false;
  36. if (task.type == 2)
  37. {
  38. std::unique_lock<std::mutex>lock(mutWait);
  39. auto it = m_WaitTasks.begin();
  40. while (it != m_WaitTasks.end())
  41. {
  42. if (it->second.agent == task.agent) // 任务中已存在该坐席呼叫,则加入等待队列
  43. {
  44. m_WaitTasks.insert(std::make_pair(task.id, task));
  45. isExist = true;
  46. break;
  47. }
  48. ++it;
  49. }
  50. }
  51. // 如果等待任务中不存在,再次判断任务中是否存在
  52. if(!isExist)
  53. {
  54. volatile bool isFirst = true;
  55. if (task.type == 2) // 如果指定坐席呼叫,只有第一个进入直接呼叫,之后的放入等待队列
  56. {
  57. {
  58. std::unique_lock<std::mutex>lock(mut);
  59. auto it = m_Tasks.begin();
  60. while (it != m_Tasks.end())
  61. {
  62. if (it->second.agent == task.agent) // 任务中已存在该坐席呼叫,则加入等待队列
  63. {
  64. isFirst = false;
  65. break;
  66. }
  67. ++it;
  68. }
  69. if (isFirst)
  70. m_Tasks.insert(std::make_pair(task.id, task));
  71. }
  72. if(!isFirst)
  73. {
  74. std::unique_lock<std::mutex>lock(mutWait);
  75. m_WaitTasks.insert(std::make_pair(task.id, task));
  76. }
  77. }
  78. if(task.type !=2 )
  79. {
  80. std::unique_lock<std::mutex>lock(mut);
  81. m_Tasks.insert(std::make_pair(task.id, task));
  82. }
  83. }
  84. }
  85. }, NULL);
  86. con.notify_one();
  87. }
  88. void AutoCall::waitTask(std::string agentId)
  89. {
  90. Task task;
  91. {
  92. std::unique_lock<std::mutex>lock(mutWait);
  93. auto it = m_WaitTasks.begin();
  94. while (it != m_WaitTasks.end())
  95. {
  96. if (it->second.agent == agentId)
  97. {
  98. task = it->second;
  99. m_WaitTasks.erase(it);
  100. break;
  101. }
  102. ++it;
  103. }
  104. }
  105. if (this->fun != nullptr && task.id != 0)
  106. this->fun(task);
  107. }
  108. void AutoCall::updateTask(std::string id)
  109. {
  110. if (id.empty()) return;
  111. std::string sql = "update task set state = 1,op_time = now() where id = ";
  112. sql.append(id);
  113. JdbcHelper::GetInstance()->jdbc_executeUpdate(sql,NULL,NULL);
  114. {
  115. std::unique_lock<std::mutex>lock(mut);
  116. m_Tasks.erase(atoi(id.c_str()));
  117. }
  118. {
  119. std::unique_lock<std::mutex>lock(mutWait);
  120. m_WaitTasks.erase(atoi(id.c_str()));
  121. }
  122. }
  123. void AutoCall::__threadFun()
  124. {
  125. while (!m_IsStop)
  126. {
  127. if(!__dealTask())
  128. {
  129. std::unique_lock<std::mutex>lock(mut);
  130. con.wait(lock);
  131. }
  132. }
  133. }
  134. bool AutoCall::__dealTask()
  135. {
  136. Task task;
  137. {
  138. std::unique_lock<std::mutex>lock(mut);
  139. auto it = m_Tasks.begin();
  140. if (it == m_Tasks.end())
  141. return false;
  142. task = it->second;
  143. m_Tasks.erase(it);
  144. }
  145. if (this->fun != nullptr && task.id != 0)
  146. this->fun(task);
  147. return !m_Tasks.empty();
  148. }