1
0

forbid_staff_down_mine.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "forbid_staff_down_mine.h"
  2. #include "log.h"
  3. #include "tool_time.h"
  4. #include "db_api/CDBSingletonDefine.h"
  5. void forbid_staff_down_mine::init_forbid_staff(int id /* = -1*/)
  6. {
  7. std::string sql = "select id,staff_id,start_time,end_time,status,oper_time,lastupdate from rt_person_forbid_down_mine where status = 1 and end_time > now() ";
  8. if (id != -1)
  9. sql += " and id = " + std::to_string(id) + ";";
  10. else
  11. sql += ";";
  12. std::string Error;
  13. YADB::CDBResultSet DBRes;
  14. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  15. int nCount = DBRes.GetRecordCount( Error );
  16. if (nCount < 1)
  17. {
  18. log_error("增加或修改失败,数据库中找不到: sql", sql.c_str());
  19. return ;
  20. }
  21. while ( DBRes.GetNextRecod(Error) )
  22. {
  23. int key = 0;
  24. DBRes.GetField("id", key, Error);
  25. unsigned int staff_id = 0;
  26. DBRes.GetField("staff_id", staff_id, Error);
  27. std::string start_time;
  28. DBRes.GetField("start_time", start_time, Error);
  29. std::string end_time;
  30. DBRes.GetField("end_time", end_time, Error);
  31. int state;
  32. DBRes.GetField("status", state, Error);
  33. std::string create_time;
  34. DBRes.GetField("oper_time", create_time, Error);
  35. std::string update_time;
  36. DBRes.GetField("lastupdate", update_time, Error);
  37. std::shared_ptr<SForbidStaffList> s = forbid_staff_down_mine::instance()->get(staff_id);
  38. //如果存在则查找更新。查找不到则插入
  39. if (s)
  40. {
  41. for (SForbidStaffInfo &info : s->forbidList)
  42. {
  43. //找到记录则更新,停止.找不到,则继续往下走
  44. if (info.db_id == key)
  45. {
  46. info.start_time = tool_time::to_time(start_time);
  47. info.end_time = tool_time::to_time(end_time);
  48. info.state = state;
  49. log_info("Check Forbid_staff_down_mine DBID:%d Staff:%d State:%d Time:%s -> %s . "
  50. ,key,staff_id,state,start_time.c_str(),end_time.c_str());
  51. continue;
  52. }
  53. }
  54. }
  55. else
  56. {
  57. s = std::make_shared<SForbidStaffList>();
  58. forbid_staff_down_mine::instance()->add(staff_id,s);
  59. }
  60. s->staff_id = staff_id;
  61. SForbidStaffInfo info ;
  62. info.staff_id = staff_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. log_info("Init Forbid_staff_down_mine DBID:%d Staff:%d State:%d Time:%s -> %s . "
  68. ,key,staff_id,state,start_time.c_str(),end_time.c_str());
  69. s->forbidList.push_back(info);
  70. }
  71. }
  72. void forbid_staff_down_mine::del_forbid_data(int id,int staff_id)
  73. {
  74. auto s = forbid_staff_down_mine::instance()->get(staff_id);
  75. if (s != nullptr)
  76. {
  77. log_info(" remove Forbid Staff:%d Down mine.DBID=%d",s->staff_id,id);
  78. for(auto it = s->forbidList.begin(); it != s->forbidList.end() ; ++it)
  79. {
  80. if ((*it).db_id == id)
  81. {
  82. s->forbidList.erase(it);
  83. break;
  84. }
  85. }
  86. if(s->forbidList.size() == 0)
  87. {
  88. forbid_staff_down_mine::instance()->remove(staff_id);
  89. }
  90. }
  91. }
  92. //是否禁止状态
  93. bool forbid_staff_down_mine::IsForbid(int staff_id,time_t cur_time)
  94. {
  95. auto flist = m_map;
  96. auto it = flist.find(staff_id);
  97. if (it != flist.end())
  98. {
  99. for (auto &info : it->second->forbidList)
  100. {
  101. if (info.state == 1 && cur_time > info.start_time && cur_time < info.end_time)
  102. {
  103. return true;
  104. }
  105. }
  106. }
  107. return false;
  108. }