多数据源中间件标准版1.0

AgentGroup.cpp 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include "StdAfx.h"
  2. #include "AgentGroup.h"
  3. #include "Agent.h"
  4. #include "NetworkAcd.h"
  5. #include "StrategyAD.h"
  6. #include <algorithm>
  7. CAgentGroup::CAgentGroup( void )
  8. {
  9. }
  10. CAgentGroup::~CAgentGroup( void )
  11. {
  12. }
  13. /*****************************************************************
  14. **【函数名称】 add
  15. **【函数功能】 添加座席
  16. **【参数】
  17. **【返回值】
  18. ****************************************************************/
  19. void CAgentGroup::add( CAgent* a_pAgent )
  20. {
  21. ASSERT(a_pAgent != NULL);
  22. m_CsLock4Distribute.Lock();
  23. Add(a_pAgent);
  24. m_CsLock4Distribute.Unlock();
  25. }
  26. /*****************************************************************
  27. **【函数名称】 remove
  28. **【函数功能】 删除座席
  29. **【参数】
  30. **【返回值】
  31. ****************************************************************/
  32. bool CAgentGroup::remove( CAgent* a_pAgent )
  33. {
  34. ASSERT(a_pAgent != NULL);
  35. CSingleLock Lock(&m_CsLock4Distribute, TRUE);
  36. for(int i = 0; i < m_nSize; ++i)
  37. {
  38. if(m_pData[i] == a_pAgent)
  39. {
  40. RemoveAt(i, 1);
  41. TRACE(_T("Succeed @CAgentGroup::remove\r\n"));
  42. return true;
  43. }
  44. }
  45. return false;
  46. }
  47. /*****************************************************************
  48. **【函数名称】 clear
  49. **【函数功能】 释放所有座席
  50. **【参数】
  51. **【返回值】
  52. ****************************************************************/
  53. void CAgentGroup::clear( void )
  54. {
  55. RemoveAll();
  56. }
  57. /*****************************************************************
  58. **【函数名称】 isNoFreeAgent
  59. **【函数功能】 是否座席全忙
  60. **【参数】
  61. **【返回值】
  62. ****************************************************************/
  63. bool CAgentGroup::isNoFreeAgent( void )
  64. {
  65. CSingleLock Lock(&m_CsLock4Distribute, TRUE);
  66. for(int i = 0; i < m_nSize; ++i) // 保证遍历一轮
  67. {
  68. if(m_pData[i]->isFree())
  69. return false;
  70. }
  71. return true;
  72. }
  73. //2018.6.14 for 严格循环
  74. CAgent* CAgentGroup::GetFreeAgentForLoop()
  75. {
  76. CSingleLock Lock(&m_CsLock4Distribute, TRUE);
  77. CAgent *pAgent = NULL;
  78. for (int idx = m_nSize; idx > 0; idx-- ) {
  79. pAgent = m_pData[0];
  80. RemoveAt(0, 1);
  81. Add(pAgent);
  82. if (pAgent->isFree()) {
  83. return pAgent; // ych
  84. }
  85. }
  86. return NULL; // ych
  87. }
  88. CAgent * CAgentGroup::GetFreeAgentForLoopByAgentId()
  89. {
  90. static UINT agentId = 0; // 2020-01-13记录本次分配的座席id
  91. CSingleLock Lock(&m_CsLock4Distribute, TRUE);
  92. CAgent *pAgent = NULL;
  93. bool isExist = false;
  94. std::map<UINT, CAgent*> mAgents;
  95. for (size_t i = 0; i < m_nSize; i++)
  96. {
  97. pAgent = m_pData[i];
  98. mAgents.insert(std::pair<UINT, CAgent*>(pAgent->id(), pAgent));
  99. }
  100. auto it = mAgents.begin();
  101. while (it != mAgents.end())
  102. {
  103. if (it->first > agentId && it->second->isFree())
  104. {
  105. agentId = it->first;
  106. isExist = true;
  107. break;
  108. }
  109. ++it;
  110. }
  111. if (isExist = false)
  112. {
  113. it = mAgents.begin();
  114. while (it != mAgents.end())
  115. {
  116. if (it->first <= agentId && it->second->isFree())
  117. {
  118. agentId = it->first;
  119. isExist = true;
  120. break;
  121. }
  122. if (it->first > agentId)
  123. {
  124. break;
  125. }
  126. ++it;
  127. }
  128. }
  129. if (isExist = true)
  130. {
  131. for (size_t i = 0; i < m_nSize; i++)
  132. {
  133. pAgent = m_pData[i];
  134. if (m_pData[i]->id() == agentId)
  135. return m_pData[i];
  136. }
  137. }
  138. return NULL;
  139. // ----------------------
  140. CArray<CAgent*,CAgent*> *agent = this;
  141. std::sort(this->GetData(), this->GetData() + this->GetSize(), [&](CAgent *a, CAgent *b) mutable ->bool { return a->id() < b->id(); });
  142. for (int i = 0; i < m_nSize; i++)
  143. {
  144. pAgent = m_pData[i];
  145. if (pAgent->isFree()&&pAgent->id()> agentId)
  146. {
  147. agentId = pAgent->id();
  148. return pAgent;
  149. }
  150. }
  151. for (int i = 0; i < m_nSize; i++)
  152. {
  153. pAgent = m_pData[i];
  154. if (pAgent->isFree() && pAgent->id() <= agentId)
  155. {
  156. agentId = pAgent->id();
  157. return pAgent;
  158. }
  159. }
  160. return nullptr;
  161. }
  162. /*****************************************************************
  163. **【函数名称】 distributeAgent
  164. **【函数功能】 分派座席
  165. **【参数】
  166. **【返回值】
  167. ****************************************************************/
  168. QUEUE_AGENT_RESULT CAgentGroup::distributeAgent( CStrategyAD* pStrategy, const CString& InfoEx, UINT& Agent, UINT& Exten )
  169. {
  170. if(m_nSize == 0)
  171. return QUEUE_AGENT_FAILED_NO_AGENT;
  172. // 如果座席全忙
  173. if(isNoFreeAgent())
  174. return QUEUE_AGENT_FAILED_NO_FREE;
  175. CAgent* pAgent = NULL;
  176. CSingleLock Lock(&m_CsLock4Distribute, TRUE);
  177. QUEUE_AGENT_RESULT Result = pStrategy->distributeAgent(*this, InfoEx, pAgent);
  178. if(pAgent != NULL && pAgent->lock()) // 对于无法锁定的座席,默认分配失败
  179. {
  180. Agent = pAgent->id();
  181. Exten = pAgent->assoExten();
  182. return Result;
  183. }
  184. else
  185. {
  186. return QUEUE_AGETN_FAILED_NO_SPEC;
  187. }
  188. }