#include "StdAfx.h" #include "AgentGroup.h" #include "Agent.h" #include "NetworkAcd.h" #include "StrategyAD.h" #include CAgentGroup::CAgentGroup( void ) { } CAgentGroup::~CAgentGroup( void ) { } /***************************************************************** **【函数名称】 add **【函数功能】 添加座席 **【参数】 **【返回值】 ****************************************************************/ void CAgentGroup::add( CAgent* a_pAgent ) { ASSERT(a_pAgent != NULL); m_CsLock4Distribute.Lock(); Add(a_pAgent); m_CsLock4Distribute.Unlock(); } /***************************************************************** **【函数名称】 remove **【函数功能】 删除座席 **【参数】 **【返回值】 ****************************************************************/ bool CAgentGroup::remove( CAgent* a_pAgent ) { ASSERT(a_pAgent != NULL); CSingleLock Lock(&m_CsLock4Distribute, TRUE); for(int i = 0; i < m_nSize; ++i) { if(m_pData[i] == a_pAgent) { RemoveAt(i, 1); TRACE(_T("Succeed @CAgentGroup::remove\r\n")); return true; } } return false; } /***************************************************************** **【函数名称】 clear **【函数功能】 释放所有座席 **【参数】 **【返回值】 ****************************************************************/ void CAgentGroup::clear( void ) { RemoveAll(); } /***************************************************************** **【函数名称】 isNoFreeAgent **【函数功能】 是否座席全忙 **【参数】 **【返回值】 ****************************************************************/ bool CAgentGroup::isNoFreeAgent( void ) { CSingleLock Lock(&m_CsLock4Distribute, TRUE); for(int i = 0; i < m_nSize; ++i) // 保证遍历一轮 { if(m_pData[i]->isFree()) return false; } return true; } //2018.6.14 for 严格循环 CAgent* CAgentGroup::GetFreeAgentForLoop() { CSingleLock Lock(&m_CsLock4Distribute, TRUE); CAgent *pAgent = NULL; for (int idx = m_nSize; idx > 0; idx-- ) { pAgent = m_pData[0]; RemoveAt(0, 1); Add(pAgent); if (pAgent->isFree()) { return pAgent; // ych } } return NULL; // ych } CAgent * CAgentGroup::GetFreeAgentForLoopByAgentId() { static UINT agentId = 0; // 2020-01-13记录本次分配的座席id CSingleLock Lock(&m_CsLock4Distribute, TRUE); CAgent *pAgent = NULL; bool isExist = false; std::map mAgents; for (size_t i = 0; i < m_nSize; i++) { pAgent = m_pData[i]; mAgents.insert(std::pair(pAgent->id(), pAgent)); } auto it = mAgents.begin(); while (it != mAgents.end()) { if (it->first > agentId && it->second->isFree()) { agentId = it->first; isExist = true; break; } ++it; } if (isExist = false) { it = mAgents.begin(); while (it != mAgents.end()) { if (it->first <= agentId && it->second->isFree()) { agentId = it->first; isExist = true; break; } if (it->first > agentId) { break; } ++it; } } if (isExist = true) { for (size_t i = 0; i < m_nSize; i++) { pAgent = m_pData[i]; if (m_pData[i]->id() == agentId) return m_pData[i]; } } return NULL; // ---------------------- CArray *agent = this; std::sort(this->GetData(), this->GetData() + this->GetSize(), [&](CAgent *a, CAgent *b) mutable ->bool { return a->id() < b->id(); }); for (int i = 0; i < m_nSize; i++) { pAgent = m_pData[i]; if (pAgent->isFree()&&pAgent->id()> agentId) { agentId = pAgent->id(); return pAgent; } } for (int i = 0; i < m_nSize; i++) { pAgent = m_pData[i]; if (pAgent->isFree() && pAgent->id() <= agentId) { agentId = pAgent->id(); return pAgent; } } return nullptr; } /***************************************************************** **【函数名称】 distributeAgent **【函数功能】 分派座席 **【参数】 **【返回值】 ****************************************************************/ QUEUE_AGENT_RESULT CAgentGroup::distributeAgent( CStrategyAD* pStrategy, const CString& InfoEx, UINT& Agent, UINT& Exten ) { if(m_nSize == 0) return QUEUE_AGENT_FAILED_NO_AGENT; // 如果座席全忙 if(isNoFreeAgent()) return QUEUE_AGENT_FAILED_NO_FREE; CAgent* pAgent = NULL; CSingleLock Lock(&m_CsLock4Distribute, TRUE); QUEUE_AGENT_RESULT Result = pStrategy->distributeAgent(*this, InfoEx, pAgent); if(pAgent != NULL && pAgent->lock()) // 对于无法锁定的座席,默认分配失败 { Agent = pAgent->id(); Exten = pAgent->assoExten(); return Result; } else { return QUEUE_AGETN_FAILED_NO_SPEC; } }