forbid_staff_down_mine.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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*/,int etype)
  6. {
  7. std::string sql = "select id,staff_id,start_time,end_time,status,oper_time,last_update from rt_person_forbid_down_mine where ";
  8. if (id != -1)
  9. sql += " id = " + std::to_string(id) + ";";
  10. else
  11. sql += "status = 1 and end_time > now();";
  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_info("增加或修改失败,数据库中没有记录: sql=%s", 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("last_update", update_time, Error);
  37. time_t stime = tool_time::to_time(start_time);
  38. time_t etime = tool_time::to_time(end_time);
  39. std::shared_ptr<SForbidStaffList> s = forbid_staff_down_mine::instance()->get(staff_id);
  40. //如果存在则查找更新。查找不到则插入
  41. if (s)
  42. {
  43. bool f = false; // 是否修改过记录
  44. for (SForbidStaffInfo &info : s->forbidList)
  45. {
  46. //找到记录则更新,停止.找不到,则继续往下走
  47. if (info.db_id == key)
  48. {
  49. f=true;
  50. info.start_time =stime;
  51. info.end_time =etime;
  52. info.state = state;
  53. log_info("Check Forbid_staff_down_mine DBID:%d Staff:%d State:%d Time:%s -> %s . "
  54. ,key,staff_id,state,start_time.c_str(),end_time.c_str());
  55. break;
  56. }
  57. }
  58. if(f)continue;
  59. //ET_UPDATE
  60. if(id !=-1 && etype ==2)
  61. {
  62. time_t now= time(0);
  63. if(state!=1 || now>etime)
  64. continue;
  65. }
  66. }
  67. else
  68. {
  69. s = std::make_shared<SForbidStaffList>();
  70. forbid_staff_down_mine::instance()->add(staff_id,s);
  71. }
  72. s->staff_id = staff_id;
  73. SForbidStaffInfo info ;
  74. info.staff_id = staff_id;
  75. info.db_id = key;
  76. info.start_time = stime;
  77. info.end_time =etime;
  78. info.state = state;
  79. log_info("Init Forbid_staff_down_mine DBID:%d Staff:%d State:%d Time:%s -> %s . "
  80. ,key,staff_id,state,start_time.c_str(),end_time.c_str());
  81. s->forbidList.push_back(info);
  82. }
  83. }
  84. void forbid_staff_down_mine::del_forbid_data(int id,int staff_id)
  85. {
  86. auto s = forbid_staff_down_mine::instance()->get(staff_id);
  87. if (s != nullptr)
  88. {
  89. log_info(" remove Forbid Staff:%d Down mine.DBID=%d",s->staff_id,id);
  90. for(auto it = s->forbidList.begin(); it != s->forbidList.end() ; ++it)
  91. {
  92. if ((*it).db_id == id)
  93. {
  94. s->forbidList.erase(it);
  95. break;
  96. }
  97. }
  98. if(s->forbidList.size() == 0)
  99. {
  100. forbid_staff_down_mine::instance()->remove(staff_id);
  101. }
  102. }
  103. }
  104. //是否禁止状态
  105. bool forbid_staff_down_mine::IsForbid(int staff_id,time_t cur_time)
  106. {
  107. auto flist = forbid_staff_down_mine::instance()->m_map;
  108. auto it = flist.find(staff_id);
  109. if (it != flist.end())
  110. {
  111. for (auto &info : it->second->forbidList)
  112. {
  113. if (info.state == 1 && cur_time > info.start_time && cur_time < info.end_time)
  114. {
  115. return true;
  116. }
  117. }
  118. }
  119. return false;
  120. }