Browse Source

修改禁止人员下井功能。。解决可能多线程导致的问题

chensongchao 6 years ago
parent
commit
5820dcb5dc

+ 5 - 2
card_path.cpp

@@ -439,7 +439,10 @@ static std::vector<base_path> init_path(std::vector<base_path> & ret,vertex_list
 		p0[i].erase(it,p0[i].end());
 
 		for(int j=1,cnt=p0[i].size();j<cnt;j++)
-			ret2.push_back(base_path(p0[i][j-1],p0[i][j]));
+		{
+			ret2.push_back(base_path(p0[i][j - 1], p0[i][j]));
+			ret2.back().sid = ret[i].sid;
+		}
 	}
 
 	ret2.erase(std::remove_if(ret2.begin(),ret2.end(),[&v](base_path&p){
@@ -648,7 +651,7 @@ struct graph
 
 			line_v lv(v[from],v[to]);
 
-			log_info("line:%s\n",lv.to_string().c_str());
+			log_info("graph::init() site:%d line:%s\n",p.sid, lv.to_string().c_str());
 
 			double cos=lv.cos_k();
 			double sin=lv.sin_k();

+ 41 - 45
forbid_staff_down_mine.cpp

@@ -13,9 +13,12 @@ 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) + ";";
     }
+    else
+    {
+        m_map.clear();
+    }
     std::string Error;
     YADB::CDBResultSet DBRes;
     sDBConnPool.Query(sql.c_str(),DBRes,Error);
@@ -41,84 +44,77 @@ void forbid_staff_down_mine::init_forbid_staff(int id /* = -1*/)
 
         if (id != -1)
         {
-            auto s = get(id);
+            auto s = forbid_staff_down_mine::instance()->get(s_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;
+                for (SForbidStaffInfo &info : s->forbidList)
+                {
+                    if (info.db_id == key) //已有记录,修改完成
+                    {
+                        info.start_time = tool_time::to_time(start_time);
+                        info.end_time = tool_time::to_time(end_time);
+                        info.state = state;
+                        break;
+                    }
+                }
+                return;
             }
         }
 
-        std::shared_ptr<SForbidStaffInfo> s = std::make_shared<SForbidStaffInfo>();
+        std::shared_ptr<SForbidStaffList> s = std::make_shared<SForbidStaffList>();
         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);
+        SForbidStaffInfo info ;
+        info.staff_id = s_id;
+        info.db_id = key;
+        info.start_time = tool_time::to_time(start_time);
+        info.end_time = tool_time::to_time(end_time);
+        info.state = state;
+        s->forbidList.push_back(info);
+        forbid_staff_down_mine::instance()->add(s_id,s);
     }
 }
 
 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);
-    }
+    forbid_staff_down_mine::instance()->remove(staff_id);
 }
 
-void forbid_staff_down_mine::del_forbid_data(int id)
+void forbid_staff_down_mine::del_forbid_data(int id,int staff_id)
 {
-    auto s = forbid_staff_down_mine::instance()->get(id);
+    auto s = forbid_staff_down_mine::instance()->get(staff_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())
+        log_info(" remove Forbid Staff:%d Down mine.DBID=%d",s->staff_id,id);
+        for(auto it = s->forbidList.begin(); it != s->forbidList.end() ; ++it)
         {
-            for (auto key : it->second)
-            {
-                if(key == id)
-                {
-                    it->second.remove(id);
-                    break;
-                }
-            }
-            if (it->second.size() == 0)
+            SForbidStaffInfo & info = *it;
+            if (info.db_id == id)
             {
-                m_forbidlist.erase(it);
+                s->forbidList.erase(it);
+                break;
             }
         }
+        if(s->forbidList.size() == 0)
+        {
+            forbid_staff_down_mine::instance()->remove(staff_id);
+        }
     }
-    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 flist = m_map;
     auto it = flist.find(staff_id);
     if (it != flist.end())
     {
-        for (auto key : it->second)
+        for (auto &info : it->second->forbidList)
         {
-            std::shared_ptr<SForbidStaffInfo> s = get(key);
-            if (nullptr == s) {
-                continue;
-            }
-            if (s->state == 1 && cur_time > s->start_time && cur_time < s->end_time)
+            if (info.state == 1 && cur_time > info.start_time && cur_time < info.end_time)
             {
                 return true;
             }
         }
     }
-    //del_forbid_staff(staff_id);  //容易引起多线程问题 已过禁止时间 或者记录失效 删除
     return false;
 }

+ 10 - 6
forbid_staff_down_mine.h

@@ -14,11 +14,13 @@
 struct SForbidStaffInfo
 {
     int staff_id;
+    int db_id;
     time_t start_time;
     time_t end_time;
     int state;          //状态 0 = 无效 1 = 有效
     SForbidStaffInfo()
     {
+        db_id = 0;
         staff_id = 0;
         start_time = 0;
         end_time = 0;
@@ -26,23 +28,25 @@ struct SForbidStaffInfo
     }
 };
 
+struct SForbidStaffList
+{
+    int staff_id ;
+    std::list<SForbidStaffInfo> forbidList;
+};
+
 struct forbid_staff_down_mine
-        :single_base<forbid_staff_down_mine,int,std::shared_ptr<SForbidStaffInfo>>
+        :single_base<forbid_staff_down_mine,int,std::shared_ptr<SForbidStaffList>>
 {
 public:
     // 根据数据库中的自增长id
     void init_forbid_staff(int id = -1);
     // 根据数据库中的自增长id
-    void del_forbid_data(int id);
+    void del_forbid_data(int id,int staff_id);
     // 根据员工ID
     void del_forbid_staff(int staff_id);
 
     //是否禁止状态
     bool IsForbid(int staff_id,time_t cur_time);
-
-private:
-    typedef std::map <int ,std::list<int>> ForbidStaffList;
-    ForbidStaffList m_forbidlist;
 };
 
 

+ 14 - 5
module_service/module_meta_date_changed.cpp

@@ -34,7 +34,6 @@ void module_meta_date_changed::accept(sio::message::ptr const& data)
 
     std::string op_type="";
     tool_map::try_get_value(op_type, JSON_KEY_OP_TYPE, data);
-
     if((-1 != id || szParam != "0") && !op_type.empty())
     {
         EDIT_TYPE_ID edit_type_id;
@@ -88,7 +87,7 @@ void module_meta_date_changed::accept(sio::message::ptr const& data)
         }
         else if (JSON_KEY_NAME_FORBID_PERSON_DOWN_MINE == name)
         {
-            deal_call_edit_forbid_person_down_mine(id,edit_type_id);
+            deal_call_edit_forbid_person_down_mine(szParam,edit_type_id);
         }
         else
         {
@@ -324,15 +323,25 @@ void module_meta_date_changed::deal_call_edit_lights_group(int id, EDIT_TYPE_ID
 }
 
 // 禁止指定人员下井 id = (rt_person_forbid_down_mine)数据库中自增长ID
-void module_meta_date_changed::deal_call_edit_forbid_person_down_mine(int id,EDIT_TYPE_ID edit_type_id)
+void module_meta_date_changed::deal_call_edit_forbid_person_down_mine(const std::string & lszId,EDIT_TYPE_ID edit_type_id)
 {
+    std::vector<std::string> vecSegTag;
+    boost::split(vecSegTag, lszId, boost::is_any_of(";"));
+    if (vecSegTag.size() == 1)  //数据发生错误
+    {
+        log_errno("Web Send Data Error!(%s)", lszId.c_str());
+        return;
+    }
+    int db_id = std::stoi(vecSegTag[0].c_str());
+    int staff_id = std::stoi(vecSegTag[1].c_str());
+
     if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
     {
-        forbid_staff_down_mine::instance()->init_forbid_staff(id);
+        forbid_staff_down_mine::instance()->init_forbid_staff(db_id);
     }
     else if(ET_DELETE == edit_type_id)
     {
-        forbid_staff_down_mine::instance()->del_forbid_data(id);
+        forbid_staff_down_mine::instance()->del_forbid_data(db_id,staff_id);
     }
 }
 

+ 1 - 1
module_service/module_meta_date_changed.h

@@ -70,7 +70,7 @@ private:
     void deal_call_edit_lights_group(int id,EDIT_TYPE_ID edit_type_id);
 
     // 禁止指定人员下井
-    void deal_call_edit_forbid_person_down_mine(int id,EDIT_TYPE_ID edit_type_id);
+    void deal_call_edit_forbid_person_down_mine(const std::string & lszId,EDIT_TYPE_ID edit_type_id);
 
     ///待实现
     void init_setting();