| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- #include "Agent.h"
- #include "SqlWrite.h"
- #include "JdbcHelper.h"
- Agent::Agent(std::string a_AgentID, std::string a_ExtID) :m_AgentID(a_AgentID), m_Exten(a_ExtID), m_IsRepose(false), m_State(AGENT_STATE_FREE)
- {
- m_TimeLogin = __getCurTime();
- m_LoginTimes = 0;
- m_FreeTimes = 0;
- m_ReposeTimes = 0;
- m_TalkTimes = 0;
- m_ReposeNum = 0;
- m_AnswerNum = 0;
- __login();
- }
- Agent::Agent(std::string a_AgentID, std::string a_ExtID, std::string a_Group) :m_AgentID(a_AgentID), m_Exten(a_ExtID), m_IsRepose(false), m_State(AGENT_STATE_FREE)
- {
- m_TimeLogin = __getCurTime();
- m_LoginTimes = 0;
- m_FreeTimes = 0;
- m_ReposeTimes = 0;
- m_TalkTimes = 0;
- m_ReposeNum = 0;
- m_AnswerNum = 0;
- m_Groups.clear();
- m_Group = a_Group;
- boost::split(m_Groups, a_Group, boost::is_any_of("|"));
- __login();
- }
- Agent::~Agent()
- {
- }
- void Agent::removeAgent(std::string HostAgent)
- {
- __logout();
- setState(AGENT_STATE_LOGOUT);
- m_TimeLogout = __getCurTime();
- m_LoginTimes = __subTime(m_TimeLogin, m_TimeLogout);
- Format fmt("insert into agent_detail(AgentId,ExtId,TimeLogin,TimeLogout,LoginTimes,LogoutHost) values(%s,%s,'%s','%s',%lld,'%s')");
- fmt %m_AgentID %m_Exten %__FormatTime(m_TimeLogin) % __FormatTime(m_TimeLogout) % m_LoginTimes %HostAgent;
- SqlWrite::GetInstance()->addSql(fmt.str());
-
- }
- bool Agent::setState(AGENT_STATE a_AgentState)
- {
- switch (a_AgentState)
- {
- case AGENT_STATE_FREE:
- m_IsRepose = false;
- break;
- case AGENT_STATE_BUSY:
- m_AnswerNum++;
- break;
- case AGENT_STATE_REPOSE:
- m_ReposeNum++;
- m_IsRepose = true;
- break;
- default:
- break;
- }
- if (a_AgentState == AGENT_STATE_FREE || a_AgentState == AGENT_STATE_REPOSE || a_AgentState == AGENT_STATE_LOGING || a_AgentState == AGENT_STATE_LOGOUT)
- {
- Format fmt("insert into agent_state(agent,exten,cur_time,state) values('%s','%s','%s',%d)");
- fmt %m_AgentID %m_Exten %__FormatTime(__getCurTime()) % a_AgentState;
- SqlWrite::GetInstance()->addSql(fmt.str());
- }
-
- m_State = a_AgentState;
- return false;
- }
- std::string Agent::state_s()
- {
- std::string state;
- switch ((int)m_State)
- {
- case AGENT_STATE_FREE: // 空闲
- state = "空闲";
- break;
- case AGENT_STATE_BUSY: // 通话中
- state = "通话中";
- break;
- case AGENT_STATE_REPOSE: // 小休
- state = "小休";
- break;
- case AGENT_STATE_REQUESTED: // 被请求
- state = "振铃中";
- break;
- }
- return state;
- }
- bool Agent::__login()
- {
- setState(AGENT_STATE_LOGING);
- setState(AGENT_STATE_FREE);
-
- /*Format fmt("update agent set exten = '%s' where agent = '%s'");
- fmt %m_Exten %m_AgentID;*/
- Format fmt("insert into agent(agent,exten) values('%s','%s')");
- fmt %m_AgentID %m_Exten;
- std::string sql;
- sql = fmt.str();
- int n = JdbcHelper::GetInstance()->jdbc_executeUpdate(sql, [](sql::PreparedStatement* stmt) {
- }, [sql](sql::SQLException &e) {
- Format fmt("Sql执行失败,错误信息:[%s],Sql[%s]");
- fmt % e.what() % sql;
- LOG_ERROR(fmt.str().c_str());
- return false;
- });
- return n > 0 ? true : false;
- }
- bool Agent::__logout()
- {
- Format fmt("delete from agent where agent = '%s'");
- fmt %m_AgentID;
- /*Format fmt("update agent set exten = '%s' where agent = '%s'");
- fmt %"" %m_AgentID;*/
- std::string sql;
- sql = fmt.str();
- JdbcHelper::GetInstance()->jdbc_executeUpdate(sql, [](sql::PreparedStatement* stmt) {
- }, [sql](sql::SQLException &e) {
- Format fmt("Sql执行失败,错误信息:[%s],Sql[%s]");
- fmt % e.what() % sql;
- LOG_ERROR(fmt.str().c_str());
- return false;
- });
- return true;
- }
- std::string Agent::__FormatTime(ptime time)
- {
- std::string now = to_iso_extended_string(time);
- std::regex reg("T");
- return std::regex_replace(now, reg, " ");
- }
- ptime Agent::__getCurTime()
- {
- return second_clock::local_time();
- }
- int64_t Agent::__subTime(ptime before, ptime after)
- {
- return (after - before).total_seconds();
- }
- //void Agent::onTimer()
- //{
- // switch (m_State)
- // {
- // case AGENT_STATE_FREE:
- // m_FreeTimes++;
- // break;
- // case AGENT_STATE_REPOSE:
- // m_ReposeTimes++;
- // break;
- // case AGENT_STATE_BUSY:
- // m_TalkTimes++;
- // break;
- // default:
- // break;
- // }
- //
- //}
|