1
0

forbid_staff_down_mine.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. m_map.clear();
  15. sql += " and id = " + std::to_string(id) + ";";
  16. }
  17. std::string Error;
  18. YADB::CDBResultSet DBRes;
  19. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  20. int nCount = DBRes.GetRecordCount( Error );
  21. if (nCount < 1)
  22. {
  23. log_error("增加或修改失败,数据库中找不到: sql", sql.c_str());
  24. return ;
  25. }
  26. while ( DBRes.GetNextRecod(Error) )
  27. {
  28. int key = 0;
  29. DBRes.GetField("id", key, Error);
  30. unsigned int s_id = 0;
  31. DBRes.GetField("staff_id", s_id, Error);
  32. std::string start_time;
  33. DBRes.GetField("start_time", start_time, Error);
  34. std::string end_time;
  35. DBRes.GetField("end_time", end_time, Error);
  36. int state;
  37. DBRes.GetField("status", state, Error);
  38. if (id != -1)
  39. {
  40. auto s = get(id);
  41. if (s != nullptr)
  42. {
  43. s->staff_id = s_id;
  44. s->start_time = tool_time::to_time(start_time);
  45. s->end_time = tool_time::to_time(end_time);
  46. s->state = state;
  47. continue;
  48. }
  49. }
  50. std::shared_ptr<SForbidStaffInfo> s = std::make_shared<SForbidStaffInfo>();
  51. s->staff_id = s_id;
  52. s->start_time = tool_time::to_time(start_time);
  53. s->end_time = tool_time::to_time(end_time);
  54. s->state = state;
  55. forbid_staff_down_mine::instance()->add(key,s);
  56. m_forbidlist[s_id].push_back(key);
  57. }
  58. }
  59. void forbid_staff_down_mine::del_forbid_staff(int staff_id)
  60. {
  61. auto it = m_forbidlist.find(staff_id);
  62. if (it != m_forbidlist.end()) {
  63. for (auto key : it->second) {
  64. forbid_staff_down_mine::instance()->remove(key);
  65. }
  66. m_forbidlist.erase(it);
  67. }
  68. }
  69. void forbid_staff_down_mine::del_forbid_data(int id)
  70. {
  71. auto s = forbid_staff_down_mine::instance()->get(id);
  72. if (s != nullptr)
  73. {
  74. log_info(" remove Forbid Staff:%d Down mine.",s->staff_id);
  75. auto it = m_forbidlist.find(s->staff_id);
  76. if (it != m_forbidlist.end())
  77. {
  78. for (auto key : it->second)
  79. {
  80. if(key == id)
  81. {
  82. it->second.remove(id);
  83. break;
  84. }
  85. }
  86. if (it->second.size() == 0)
  87. {
  88. m_forbidlist.erase(it);
  89. }
  90. }
  91. }
  92. forbid_staff_down_mine::instance()->remove(id);
  93. }
  94. //是否禁止状态
  95. bool forbid_staff_down_mine::IsForbid(int staff_id,time_t cur_time)
  96. {
  97. auto flist = m_forbidlist;
  98. auto it = flist.find(staff_id);
  99. if (it != flist.end())
  100. {
  101. for (auto key : it->second)
  102. {
  103. std::shared_ptr<SForbidStaffInfo> s = get(key);
  104. if (nullptr == s) {
  105. continue;
  106. }
  107. if (s->state == 1 && cur_time > s->start_time && cur_time < s->end_time)
  108. {
  109. return true;
  110. }
  111. }
  112. }
  113. //del_forbid_staff(staff_id); //容易引起多线程问题 已过禁止时间 或者记录失效 删除
  114. return false;
  115. }