forbid_staff_down_mine.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. bool f=false;
  42. for (SForbidStaffInfo &info : s->forbidList)
  43. {
  44. //找到记录则更新,停止.找不到,则继续往下走
  45. if (info.db_id == key)
  46. {
  47. f=true;
  48. info.start_time = tool_time::to_time(start_time);
  49. info.end_time = tool_time::to_time(end_time);
  50. info.state = state;
  51. log_info("Check Forbid_staff_down_mine DBID:%d Staff:%d State:%d Time:%s -> %s . "
  52. ,key,staff_id,state,start_time.c_str(),end_time.c_str());
  53. break;
  54. }
  55. }
  56. if(f)continue;
  57. }
  58. else
  59. {
  60. s = std::make_shared<SForbidStaffList>();
  61. forbid_staff_down_mine::instance()->add(staff_id,s);
  62. }
  63. s->staff_id = staff_id;
  64. SForbidStaffInfo info ;
  65. info.staff_id = staff_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. log_info("Init Forbid_staff_down_mine DBID:%d Staff:%d State:%d Time:%s -> %s . "
  71. ,key,staff_id,state,start_time.c_str(),end_time.c_str());
  72. s->forbidList.push_back(info);
  73. }
  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. if ((*it).db_id == id)
  84. {
  85. s->forbidList.erase(it);
  86. break;
  87. }
  88. }
  89. if(s->forbidList.size() == 0)
  90. {
  91. forbid_staff_down_mine::instance()->remove(staff_id);
  92. }
  93. }
  94. }
  95. //是否禁止状态
  96. bool forbid_staff_down_mine::IsForbid(int staff_id,time_t cur_time)
  97. {
  98. auto flist = forbid_staff_down_mine::instance()->m_map;
  99. auto it = flist.find(staff_id);
  100. if (it != flist.end())
  101. {
  102. for (auto &info : it->second->forbidList)
  103. {
  104. if (info.state == 1 && cur_time > info.start_time && cur_time < info.end_time)
  105. {
  106. return true;
  107. }
  108. }
  109. }
  110. return false;
  111. }