linux版本中间件

DepttelextOffice.cpp 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "DepttelextOffice.h"
  2. #include <iostream>
  3. #include <json/json.h>
  4. #include "Log.h"
  5. #include "JdbcHelper.h"
  6. CDepttelextOffice::~CDepttelextOffice()
  7. {
  8. std::unique_lock<std::mutex>lock(mut);
  9. auto it = m_DeptTelextmap.begin();
  10. DeptTelext *pDeptTelext = nullptr;
  11. while (it != m_DeptTelextmap.end())
  12. {
  13. pDeptTelext = it->second;
  14. if (pDeptTelext) {
  15. delete pDeptTelext;
  16. pDeptTelext = nullptr;
  17. }
  18. ++it;
  19. }
  20. }
  21. bool CDepttelextOffice::InitDepttelext()
  22. {
  23. LOG_INFO("督办分机号码列表开始初始化");
  24. std::string strSQL = "SELECT id,Name,Telephone,Extension FROM rep_video_depttelext";
  25. std::unique_lock<std::mutex>lock(mut);
  26. JdbcHelper::GetInstance()->jdbc_executeQuery(strSQL, NULL, [this](sql::ResultSet* result) {
  27. while (result->next()){
  28. DeptTelext *pDeptTelext = new DeptTelext();
  29. pDeptTelext->Name = result->getString("Name");
  30. pDeptTelext->Telephone = result->getString("Telephone");
  31. pDeptTelext->Extension = result->getString("Extension");
  32. pDeptTelext->State = 0;// 默认不可用
  33. auto extenId = std::stoul(pDeptTelext->Extension);
  34. m_DeptTelextmap.insert(std::make_pair(extenId, pDeptTelext));
  35. }
  36. }, NULL);
  37. LOG_INFO("督办分机号码列表结束初始化");
  38. return true;
  39. }
  40. bool CDepttelextOffice::UpdateDepttelext(uint32_t extno, int state)
  41. {
  42. std::unique_lock<std::mutex>lock(mut);
  43. auto it = m_DeptTelextmap.find(extno);
  44. if (it == m_DeptTelextmap.end()) return false;
  45. if (it->second) {
  46. it->second->UpdateState(state);
  47. }
  48. return true;
  49. }
  50. std::string CDepttelextOffice::GetAllDeptTelext()
  51. {
  52. Json::Value root;
  53. Json::Value person;
  54. Json::FastWriter writer;
  55. person["Type"] = "";
  56. person["Result"] = true;
  57. std::unique_lock<std::mutex>lock(mut);
  58. auto it = m_DeptTelextmap.begin();
  59. DeptTelext *pDeptTelext = nullptr;
  60. while (it != m_DeptTelextmap.end()) {
  61. pDeptTelext = it->second;
  62. if (pDeptTelext) {
  63. Json::Value body;
  64. body["Name"] = pDeptTelext->Name;
  65. body["Telephone"] = pDeptTelext->Telephone;
  66. body["AgentExten"] = pDeptTelext->Extension;
  67. body["State"] = pDeptTelext->State;
  68. person["ExtenList"].append(body);
  69. }
  70. ++it;
  71. }
  72. lock.unlock();
  73. root.append(person);
  74. return writer.write(root);
  75. }