1
0

forbid_staff_down_mine.cpp 4.6 KB

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