forbid_staff_down_mine.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // Created by Administrator on 2019/3/5.
  3. //
  4. #include "forbid_staff_down_mine.h"
  5. #include "log.h"
  6. #include "tool_time.h"
  7. #include "db_api/CDBResultSet.h"
  8. #include "db_api/CDBSingletonDefine.h"
  9. void forbid_staff_down_mine::init_forbid_staff(int id /* = -1*/)
  10. {
  11. std::string sql = "select id,staff_id,start_time,end_time,status from rt_person_forbid_down_mine where status = 1";
  12. if (id != -1)
  13. {
  14. sql += " and id = " + std::to_string(id) + ";";
  15. }
  16. else
  17. {
  18. m_map.clear();
  19. }
  20. std::string Error;
  21. YADB::CDBResultSet DBRes;
  22. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  23. int nCount = DBRes.GetRecordCount( Error );
  24. if (nCount < 1)
  25. {
  26. log_error("增加或修改失败,数据库中找不到: sql", sql.c_str());
  27. return ;
  28. }
  29. while ( DBRes.GetNextRecod(Error) )
  30. {
  31. int key = 0;
  32. DBRes.GetField("id", key, Error);
  33. unsigned int s_id = 0;
  34. DBRes.GetField("staff_id", s_id, Error);
  35. std::string start_time;
  36. DBRes.GetField("start_time", start_time, Error);
  37. std::string end_time;
  38. DBRes.GetField("end_time", end_time, Error);
  39. int state;
  40. DBRes.GetField("status", state, Error);
  41. std::shared_ptr<SForbidStaffList> s = nullptr;
  42. if (id != -1)
  43. {
  44. s = forbid_staff_down_mine::instance()->get(s_id);
  45. if (s != nullptr)
  46. {
  47. for (SForbidStaffInfo &info : s->forbidList)
  48. {
  49. if (info.db_id == key) //已有记录,修改完成
  50. {
  51. info.start_time = tool_time::to_time(start_time);
  52. info.end_time = tool_time::to_time(end_time);
  53. info.state = state;
  54. return;
  55. }
  56. }
  57. }
  58. }
  59. if (s == nullptr)
  60. {
  61. s = std::make_shared<SForbidStaffList>();
  62. }
  63. s->staff_id = s_id;
  64. SForbidStaffInfo info ;
  65. info.staff_id = s_id;
  66. info.db_id = key;
  67. info.start_time = tool_time::to_time(start_time);
  68. info.end_time = tool_time::to_time(end_time);
  69. info.state = state;
  70. s->forbidList.push_back(info);
  71. forbid_staff_down_mine::instance()->add(s_id,s);
  72. }
  73. }
  74. void forbid_staff_down_mine::del_forbid_staff(int staff_id)
  75. {
  76. forbid_staff_down_mine::instance()->remove(staff_id);
  77. }
  78. void forbid_staff_down_mine::del_forbid_data(int id,int staff_id)
  79. {
  80. auto s = forbid_staff_down_mine::instance()->get(staff_id);
  81. if (s != nullptr)
  82. {
  83. log_info(" remove Forbid Staff:%d Down mine.DBID=%d",s->staff_id,id);
  84. for(auto it = s->forbidList.begin(); it != s->forbidList.end() ; ++it)
  85. {
  86. SForbidStaffInfo & info = *it;
  87. if (info.db_id == id)
  88. {
  89. s->forbidList.erase(it);
  90. break;
  91. }
  92. }
  93. if(s->forbidList.size() == 0)
  94. {
  95. forbid_staff_down_mine::instance()->remove(staff_id);
  96. }
  97. }
  98. }
  99. //是否禁止状态
  100. bool forbid_staff_down_mine::IsForbid(int staff_id,time_t cur_time)
  101. {
  102. auto flist = m_map;
  103. auto it = flist.find(staff_id);
  104. if (it != flist.end())
  105. {
  106. for (auto &info : it->second->forbidList)
  107. {
  108. if (info.state == 1 && cur_time > info.start_time && cur_time < info.end_time)
  109. {
  110. return true;
  111. }
  112. }
  113. }
  114. return false;
  115. }