// // Created by Administrator on 2019/3/5. // #include "forbid_staff_down_mine.h" #include "log.h" #include "tool_time.h" #include "db_api/CDBResultSet.h" #include "db_api/CDBSingletonDefine.h" void forbid_staff_down_mine::init_forbid_staff(int id /* = -1*/) { std::string sql = "select id,staff_id,start_time,end_time,status from rt_person_forbid_down_mine where status = 1"; if (id != -1) { m_map.clear(); sql += " and id = " + std::to_string(id) + ";"; } std::string Error; YADB::CDBResultSet DBRes; sDBConnPool.Query(sql.c_str(),DBRes,Error); int nCount = DBRes.GetRecordCount( Error ); if (nCount < 1) { log_error("增加或修改失败,数据库中找不到: sql", sql.c_str()); return ; } while ( DBRes.GetNextRecod(Error) ) { int key = 0; DBRes.GetField("id", key, Error); unsigned int s_id = 0; DBRes.GetField("staff_id", s_id, Error); std::string start_time; DBRes.GetField("start_time", start_time, Error); std::string end_time; DBRes.GetField("end_time", end_time, Error); int state; DBRes.GetField("status", state, Error); if (id != -1) { auto s = get(id); if (s != nullptr) { s->staff_id = s_id; s->start_time = tool_time::to_time(start_time); s->end_time = tool_time::to_time(end_time); s->state = state; continue; } } std::shared_ptr s = std::make_shared(); s->staff_id = s_id; s->start_time = tool_time::to_time(start_time); s->end_time = tool_time::to_time(end_time); s->state = state; forbid_staff_down_mine::instance()->add(key,s); m_forbidlist[s_id].push_back(key); } } void forbid_staff_down_mine::del_forbid_staff(int staff_id) { auto it = m_forbidlist.find(staff_id); if (it != m_forbidlist.end()) { for (auto key : it->second) { forbid_staff_down_mine::instance()->remove(key); } m_forbidlist.erase(it); } } void forbid_staff_down_mine::del_forbid_data(int id) { auto s = forbid_staff_down_mine::instance()->get(id); if (s != nullptr) { log_info(" remove Forbid Staff:%d Down mine.",s->staff_id); auto it = m_forbidlist.find(s->staff_id); if (it != m_forbidlist.end()) { for (auto key : it->second) { if(key == id) { it->second.remove(id); break; } } if (it->second.size() == 0) { m_forbidlist.erase(it); } } } forbid_staff_down_mine::instance()->remove(id); } //是否禁止状态 bool forbid_staff_down_mine::IsForbid(int staff_id,time_t cur_time) { auto flist = m_forbidlist; auto it = flist.find(staff_id); if (it != flist.end()) { for (auto key : it->second) { std::shared_ptr s = get(key); if (nullptr == s) { continue; } if (s->state == 1 && cur_time > s->start_time && cur_time < s->end_time) { return true; } } } //del_forbid_staff(staff_id); //容易引起多线程问题 已过禁止时间 或者记录失效 删除 return false; }