forbid_staff_down_mine.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 ";
  8. sql += " where status = 1 and end_time > now() ";
  9. if (id != -1)
  10. sql += " and id = " + std::to_string(id) + ";";
  11. else
  12. sql += ";";
  13. std::string Error;
  14. YADB::CDBResultSet DBRes;
  15. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  16. int nCount = DBRes.GetRecordCount( Error );
  17. if (nCount < 1)
  18. {
  19. log_error("增加或修改失败,数据库中找不到: sql", sql.c_str());
  20. return ;
  21. }
  22. while ( DBRes.GetNextRecod(Error) )
  23. {
  24. int key = 0;
  25. DBRes.GetField("id", key, Error);
  26. unsigned int staff_id = 0;
  27. DBRes.GetField("staff_id", staff_id, Error);
  28. std::string start_time;
  29. DBRes.GetField("start_time", start_time, Error);
  30. std::string end_time;
  31. DBRes.GetField("end_time", end_time, Error);
  32. int state;
  33. DBRes.GetField("status", state, Error);
  34. std::string create_time;
  35. DBRes.GetField("oper_time", create_time, Error);
  36. std::string update_time;
  37. DBRes.GetField("lastupdate", update_time, Error);
  38. std::shared_ptr<SForbidStaffList> s = forbid_staff_down_mine::instance()->get(staff_id);
  39. //如果存在则查找更新。查找不到则插入
  40. if (s)
  41. {
  42. bool f = false; // 是否修改过记录
  43. for (SForbidStaffInfo &info : s->forbidList)
  44. {
  45. //找到记录则更新,停止.找不到,则继续往下走
  46. if (info.db_id == key)
  47. {
  48. f = true;
  49. info.start_time = tool_time::to_time(start_time);
  50. info.end_time = tool_time::to_time(end_time);
  51. info.state = state;
  52. log_info("Check Forbid_staff_down_mine DBID:%d Staff:%d State:%d Time:%s -> %s . "
  53. ,key,staff_id,state,start_time.c_str(),end_time.c_str());
  54. break;
  55. }
  56. if (f) continue;
  57. }
  58. }
  59. else
  60. {
  61. s = std::make_shared<SForbidStaffList>();
  62. forbid_staff_down_mine::instance()->add(staff_id,s);
  63. }
  64. s->staff_id = staff_id;
  65. SForbidStaffInfo info ;
  66. info.staff_id = staff_id;
  67. info.db_id = key;
  68. info.start_time = tool_time::to_time(start_time);
  69. info.end_time = tool_time::to_time(end_time);
  70. info.state = state;
  71. log_info("Init Forbid_staff_down_mine DBID:%d Staff:%d State:%d Time:%s -> %s . "
  72. ,key,staff_id,state,start_time.c_str(),end_time.c_str());
  73. s->forbidList.push_back(info);
  74. }
  75. }
  76. void forbid_staff_down_mine::del_forbid_data(int id,int staff_id)
  77. {
  78. auto s = forbid_staff_down_mine::instance()->get(staff_id);
  79. if (s != nullptr)
  80. {
  81. log_info(" remove Forbid Staff:%d Down mine.DBID=%d",s->staff_id,id);
  82. for(auto it = s->forbidList.begin(); it != s->forbidList.end() ; ++it)
  83. {
  84. if ((*it).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. }