forbid_staff_down_mine.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // Created by Administrator on 2019/3/5.
  3. //
  4. #include "forbid_staff_down_mine.h"
  5. #include "log.h"
  6. #include "tool_time.h"
  7. #include "db_api/CDBSingletonDefine.h"
  8. std::string forbid_staff_down_mine::m_updateTime = "";
  9. void forbid_staff_down_mine::init_forbid_staff(int id /* = -1*/)
  10. {
  11. std::string sql = "select id,staff_id,start_time,end_time,status,oper_time,lastupdate from rt_person_forbid_down_mine ";
  12. sql += " where status = 1 and end_time > now() ";
  13. if (id != -1)
  14. {
  15. sql += " and id = " + std::to_string(id) + ";";
  16. }
  17. else
  18. {
  19. sql += ";";
  20. m_map.clear();
  21. }
  22. std::string Error;
  23. YADB::CDBResultSet DBRes;
  24. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  25. int nCount = DBRes.GetRecordCount( Error );
  26. if (nCount < 1)
  27. {
  28. log_error("增加或修改失败,数据库中找不到: sql", sql.c_str());
  29. return ;
  30. }
  31. forbid_staff_down_mine::instance()->update_forbid_data(DBRes,true);
  32. }
  33. //更新数据
  34. void forbid_staff_down_mine::update_forbid_data(YADB::CDBResultSet & DBRes,bool bInsert)
  35. {
  36. std::string Error;
  37. while ( DBRes.GetNextRecod(Error) )
  38. {
  39. int key = 0;
  40. DBRes.GetField("id", key, Error);
  41. unsigned int staff_id = 0;
  42. DBRes.GetField("staff_id", staff_id, Error);
  43. std::string start_time;
  44. DBRes.GetField("start_time", start_time, Error);
  45. std::string end_time;
  46. DBRes.GetField("end_time", end_time, Error);
  47. int state;
  48. DBRes.GetField("status", state, Error);
  49. std::string create_time;
  50. DBRes.GetField("oper_time", create_time, Error);
  51. std::string update_time;
  52. DBRes.GetField("lastupdate", update_time, Error);
  53. forbid_staff_down_mine::updatetime(create_time);
  54. forbid_staff_down_mine::updatetime(update_time);
  55. std::shared_ptr<SForbidStaffList> s = forbid_staff_down_mine::instance()->get(staff_id);
  56. if (s != nullptr)
  57. {
  58. if(!bInsert)
  59. {
  60. for (SForbidStaffInfo &info : s->forbidList)
  61. {
  62. if (info.db_id == key) //已有记录,修改完成
  63. {
  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("Check 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. continue;
  70. }
  71. }
  72. }
  73. }
  74. else
  75. {
  76. s = std::make_shared<SForbidStaffList>();
  77. forbid_staff_down_mine::instance()->add(staff_id,s);
  78. }
  79. s->staff_id = staff_id;
  80. SForbidStaffInfo info ;
  81. info.staff_id = staff_id;
  82. info.db_id = key;
  83. info.start_time = tool_time::to_time(start_time);
  84. info.end_time = tool_time::to_time(end_time);
  85. info.state = state;
  86. log_info("Init Forbid_staff_down_mine DBID:%d Staff:%d State:%d Time:%s -> %s . "
  87. ,key,staff_id,state,start_time.c_str(),end_time.c_str());
  88. s->forbidList.push_back(info);
  89. }
  90. }
  91. //重新加载数据更新时间> m_updateTime
  92. void forbid_staff_down_mine::on_load_data()
  93. {
  94. std::string sql = "select id,staff_id,start_time,end_time,status,oper_time,lastupdate from rt_person_forbid_down_mine";
  95. sql += " where status = 1 and (oper_time > '" + m_updateTime + "' or lastupdate > '" + m_updateTime + "') ;";
  96. std::string Error;
  97. YADB::CDBResultSet DBRes;
  98. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  99. int nCount = DBRes.GetRecordCount( Error );
  100. if (nCount < 1)
  101. {
  102. return ;
  103. }
  104. forbid_staff_down_mine::instance()->update_forbid_data(DBRes,false);
  105. }
  106. void forbid_staff_down_mine::del_forbid_staff(int staff_id)
  107. {
  108. forbid_staff_down_mine::instance()->remove(staff_id);
  109. }
  110. void forbid_staff_down_mine::del_forbid_data(int id,int staff_id)
  111. {
  112. auto s = forbid_staff_down_mine::instance()->get(staff_id);
  113. if (s != nullptr)
  114. {
  115. log_info(" remove Forbid Staff:%d Down mine.DBID=%d",s->staff_id,id);
  116. for(auto it = s->forbidList.begin(); it != s->forbidList.end() ; ++it)
  117. {
  118. SForbidStaffInfo & info = *it;
  119. if (info.db_id == id)
  120. {
  121. s->forbidList.erase(it);
  122. break;
  123. }
  124. }
  125. if(s->forbidList.size() == 0)
  126. {
  127. forbid_staff_down_mine::instance()->remove(staff_id);
  128. }
  129. }
  130. }
  131. //是否禁止状态
  132. bool forbid_staff_down_mine::IsForbid(int staff_id,time_t cur_time)
  133. {
  134. auto flist = m_map;
  135. auto it = flist.find(staff_id);
  136. if (it != flist.end())
  137. {
  138. for (auto &info : it->second->forbidList)
  139. {
  140. if (info.state == 1 && cur_time > info.start_time && cur_time < info.end_time)
  141. {
  142. return true;
  143. }
  144. }
  145. }
  146. return false;
  147. }
  148. void forbid_staff_down_mine::updatetime(const std::string & t)
  149. {
  150. if (t.length() > 0 && t > m_updateTime)
  151. {
  152. log_info("forbid_staff_down_mine::updatetime :LastTime: %s -> t:%s ",t.c_str(),m_updateTime.c_str());
  153. m_updateTime = t;
  154. }
  155. }