forbid_staff_down_mine.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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,lastupdate 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_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. 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. if (f) continue;
  58. }
  59. if(f)continue;
  60. //ET_UPDATE
  61. if(id !=-1 && etype ==2)
  62. {
  63. time_t now= time(0);
  64. if(state!=1 || now>etime)
  65. continue;
  66. }
  67. }
  68. else
  69. {
  70. s = std::make_shared<SForbidStaffList>();
  71. forbid_staff_down_mine::instance()->add(staff_id,s);
  72. }
  73. s->staff_id = staff_id;
  74. SForbidStaffInfo info ;
  75. info.staff_id = staff_id;
  76. info.db_id = key;
  77. info.start_time = stime;
  78. info.end_time =etime;
  79. info.state = state;
  80. log_info("Init Forbid_staff_down_mine DBID:%d Staff:%d State:%d Time:%s -> %s . "
  81. ,key,staff_id,state,start_time.c_str(),end_time.c_str());
  82. s->forbidList.push_back(info);
  83. }
  84. }
  85. void forbid_staff_down_mine::del_forbid_data(int id,int staff_id)
  86. {
  87. auto s = forbid_staff_down_mine::instance()->get(staff_id);
  88. if (s != nullptr)
  89. {
  90. log_info(" remove Forbid Staff:%d Down mine.DBID=%d",s->staff_id,id);
  91. for(auto it = s->forbidList.begin(); it != s->forbidList.end() ; ++it)
  92. {
  93. if ((*it).db_id == id)
  94. {
  95. s->forbidList.erase(it);
  96. break;
  97. }
  98. }
  99. if(s->forbidList.size() == 0)
  100. {
  101. forbid_staff_down_mine::instance()->remove(staff_id);
  102. }
  103. }
  104. }
  105. //是否禁止状态
  106. bool forbid_staff_down_mine::IsForbid(int staff_id,time_t cur_time)
  107. {
  108. auto flist = forbid_staff_down_mine::instance()->m_map;
  109. auto it = flist.find(staff_id);
  110. if (it != flist.end())
  111. {
  112. for (auto &info : it->second->forbidList)
  113. {
  114. if (info.state == 1 && cur_time > info.start_time && cur_time < info.end_time)
  115. {
  116. return true;
  117. }
  118. }
  119. }
  120. return false;
  121. }