forbid_staff_down_mine.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. if (id != -1)
  42. {
  43. auto s = forbid_staff_down_mine::instance()->get(s_id);
  44. if (s != nullptr)
  45. {
  46. for (SForbidStaffInfo &info : s->forbidList)
  47. {
  48. if (info.db_id == key) //已有记录,修改完成
  49. {
  50. info.start_time = tool_time::to_time(start_time);
  51. info.end_time = tool_time::to_time(end_time);
  52. info.state = state;
  53. break;
  54. }
  55. }
  56. return;
  57. }
  58. }
  59. std::shared_ptr<SForbidStaffList> s = std::make_shared<SForbidStaffList>();
  60. s->staff_id = s_id;
  61. SForbidStaffInfo info ;
  62. info.staff_id = s_id;
  63. info.db_id = key;
  64. info.start_time = tool_time::to_time(start_time);
  65. info.end_time = tool_time::to_time(end_time);
  66. info.state = state;
  67. s->forbidList.push_back(info);
  68. forbid_staff_down_mine::instance()->add(s_id,s);
  69. }
  70. }
  71. void forbid_staff_down_mine::del_forbid_staff(int staff_id)
  72. {
  73. forbid_staff_down_mine::instance()->remove(staff_id);
  74. }
  75. void forbid_staff_down_mine::del_forbid_data(int id,int staff_id)
  76. {
  77. auto s = forbid_staff_down_mine::instance()->get(staff_id);
  78. if (s != nullptr)
  79. {
  80. log_info(" remove Forbid Staff:%d Down mine.DBID=%d",s->staff_id,id);
  81. for(auto it = s->forbidList.begin(); it != s->forbidList.end() ; ++it)
  82. {
  83. SForbidStaffInfo & info = *it;
  84. if (info.db_id == id)
  85. {
  86. s->forbidList.erase(it);
  87. break;
  88. }
  89. }
  90. if(s->forbidList.size() == 0)
  91. {
  92. forbid_staff_down_mine::instance()->remove(staff_id);
  93. }
  94. }
  95. }
  96. //是否禁止状态
  97. bool forbid_staff_down_mine::IsForbid(int staff_id,time_t cur_time)
  98. {
  99. auto flist = m_map;
  100. auto it = flist.find(staff_id);
  101. if (it != flist.end())
  102. {
  103. for (auto &info : it->second->forbidList)
  104. {
  105. if (info.state == 1 && cur_time > info.start_time && cur_time < info.end_time)
  106. {
  107. return true;
  108. }
  109. }
  110. }
  111. return false;
  112. }