module_meta_date_changed.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. #include "module_meta_date_changed.h"
  2. #include"area_business_person_attendance.h"
  3. #include"area_business_car_attendance.h"
  4. #include"common_tool.h"
  5. #include "websocket/wsClientMgr.h"
  6. #include"db/db_api/CDBSingletonDefine.h"
  7. #include"event.h"
  8. #include"ant.h"
  9. #include"card.h"
  10. #include"area.h"
  11. #include "forbid_staff_down_mine.h"
  12. #include <boost/format.hpp>
  13. #include <boost/tokenizer.hpp>
  14. #include <boost/algorithm/string/split.hpp>
  15. #include <boost/algorithm/string.hpp>
  16. ///基础数据
  17. void module_meta_date_changed::accept(sio::message::ptr const& data)
  18. {
  19. std::string name="";
  20. if(!tool_map::try_get_value(name, JSON_KEY_NAME, data))
  21. {
  22. log_error("web发来的数据: 基础数据找不到name字段");
  23. return;
  24. }
  25. std::string szParam = "0";
  26. tool_map::try_get_value(szParam,JSON_KEY_ID,data);
  27. int id = std::stoi(szParam);
  28. std::string op_type="";
  29. tool_map::try_get_value(op_type, JSON_KEY_OP_TYPE, data);
  30. if(!szParam.empty() && !op_type.empty())
  31. {
  32. EDIT_TYPE_ID edit_type_id;
  33. if(!try_get_edit_type_id(op_type, edit_type_id))
  34. {
  35. log_error("web发来的数据: 基础数据op_type字段错误:op_type=%s", op_type.c_str());
  36. return;
  37. }
  38. log_info("基础数据 receive meta_data_changed: %s, id=%s, op_type=%d"
  39. , name.c_str(), szParam.c_str() , edit_type_id);
  40. if(JSON_KEY_NAME_VEHICLE == name || JSON_KEY_NAME_VEHICLE_EXTEND == name)
  41. {
  42. deal_call_edit_vehicle_or_staff(szParam, edit_type_id);
  43. }
  44. else if(JSON_KEY_NAME_STAFF == name || JSON_KEY_NAME_STAFF_EXTEND == name)
  45. {
  46. deal_call_edit_vehicle_or_staff(szParam, edit_type_id);
  47. }
  48. else if(JSON_KEY_NAME_CARD == name)
  49. {
  50. deal_call_edit_card(szParam, edit_type_id);
  51. }
  52. else if(JSON_KEY_NAME_AREA == name)
  53. {
  54. deal_call_edit_area(szParam, edit_type_id);
  55. }
  56. else if(JSON_KEY_NAME_READER == name)
  57. {
  58. deal_call_edit_reader(id, edit_type_id);
  59. }
  60. else if ("antenna" == name)
  61. {
  62. deal_call_edit_antenna(id,edit_type_id);
  63. }
  64. else if(JSON_KEY_NAME_PATH == name)
  65. {
  66. deal_call_edit_path(id, edit_type_id);
  67. }
  68. else if(JSON_KEY_NAME_MAP == name)
  69. {
  70. deal_call_edit_map(id, edit_type_id);
  71. }
  72. else if (JSON_KEY_NAME_LIGHT == name)
  73. {
  74. deal_call_edit_light(id,edit_type_id);///待实现
  75. }
  76. else if ("lights_group" == name)
  77. {
  78. deal_call_edit_lights_group(id,edit_type_id);///待实现
  79. }
  80. else if (JSON_KEY_NAME_FORBID_PERSON_DOWN_MINE == name)
  81. {
  82. deal_call_edit_forbid_person_down_mine(szParam,edit_type_id);
  83. }
  84. else
  85. {
  86. log_error("web发来的数据: 基础数据name字段错误:name=%s", name.c_str());
  87. }
  88. }
  89. else
  90. {
  91. if(JSON_KEY_NAME_SETTING == name)
  92. {
  93. //阈值限制接口
  94. init_setting(); ///待实现
  95. }
  96. else
  97. {
  98. log_error("web发来的数据: 基础数据name字段错误:name=%s", name.c_str());
  99. }
  100. }
  101. }
  102. ///3.清除卡相关信息,区域相关,分站相关,考勤相关,清除定时器,
  103. ///速度,状态 呼救 呼叫 告警相关。即保留基础信息,其他的重置。
  104. void module_meta_date_changed::clear_card(const std::shared_ptr<card_location_base>& card_ptr)
  105. {
  106. card_ptr->get_area_tool()->on_leave(card_ptr);
  107. }
  108. /*
  109. * 修改车及卡的数据
  110. * id64 = ;员工ID或者车ID;卡ID=101;0010000001198 分号间隔
  111. */
  112. void module_meta_date_changed::deal_call_edit_vehicle_or_staff(const std::string & id64, EDIT_TYPE_ID edit_type_id)
  113. {
  114. std::vector<std::string> vecSegTag;
  115. boost::split(vecSegTag, id64, boost::is_any_of(";"));
  116. if (vecSegTag.size() == 1) //数据发生错误
  117. {
  118. log_errno("Web Send Data Error!(%s)", id64.c_str());
  119. return;
  120. }
  121. std::string lsz_card_id = vecSegTag[1];
  122. int cid = std::stoi(vecSegTag[0].c_str());
  123. if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
  124. {
  125. if (ET_UPDATE == edit_type_id)
  126. {
  127. auto c = card_list::instance()->get_card_by_cid(cid);
  128. if (nullptr != c && tool_other::type_id_to_str(c->m_type,c->m_id) != lsz_card_id)
  129. {
  130. //如果修改了卡号,则删除原来的卡的数据
  131. remove_card(c);
  132. }
  133. }
  134. int c_type = tool_other::id64_to_type(lsz_card_id);
  135. if (tool_other::is_vehicle(c_type))
  136. {
  137. card_list::instance()->init_vehicle(lsz_card_id);
  138. }
  139. else if (tool_other::is_person(c_type))
  140. {
  141. card_list::instance()->init_staffer(lsz_card_id);
  142. }
  143. }
  144. else
  145. {
  146. remove_card(tool_other::id64_to_id(lsz_card_id), tool_other::id64_to_type(lsz_card_id));
  147. }
  148. }
  149. void module_meta_date_changed::deal_call_edit_card(std::string & id64, EDIT_TYPE_ID edit_type_id)
  150. {
  151. std::string card_id64_str = tool_other::to13str(id64);
  152. int type = tool_other::id64_to_type(card_id64_str);
  153. if(tool_other::is_person(type) || tool_other::is_vehicle(type))
  154. {
  155. auto c = card_list::instance()->get(tool_other::card_id_to_u64(card_id64_str));
  156. if (nullptr == c) {
  157. log_info("web edit card:%s Not bind staff or vehicle. editType:%d", card_id64_str.c_str(), edit_type_id);
  158. return ;
  159. }
  160. std::string lsz = std::to_string(c->m_cid) + ";" + card_id64_str;
  161. deal_call_edit_vehicle_or_staff(lsz,edit_type_id);
  162. }
  163. else
  164. {
  165. log_error("基础数据 删除卡type不对:type=%d", type);
  166. }
  167. }
  168. void module_meta_date_changed::deal_call_edit_area(const std::string& id, EDIT_TYPE_ID edit_type_id)
  169. {
  170. int aid=std::stoi(id);
  171. if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
  172. {
  173. area_list::instance()->init_from_db(aid);
  174. }
  175. else if(ET_DELETE == edit_type_id)
  176. {
  177. area_list::instance()->remove(aid);
  178. // auto area_ptr = area_list::instance()->get(aid);
  179. // if(!area_ptr)
  180. // {
  181. // log_info("区域已经删除:areaid=%d", aid);
  182. // return;
  183. // }
  184. }
  185. }
  186. void module_meta_date_changed::deal_call_edit_reader(int id, EDIT_TYPE_ID edit_type_id)
  187. {
  188. if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
  189. {
  190. sit_list::instance()->init_site(id);
  191. }
  192. else if(ET_DELETE == edit_type_id)
  193. {
  194. sit_list::instance()->remove(id);
  195. }
  196. }
  197. void module_meta_date_changed::deal_call_edit_antenna(int id,EDIT_TYPE_ID edit_type_id)
  198. {
  199. if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
  200. {
  201. sit_list::instance()->read_sit_list(id);
  202. }
  203. else if(ET_DELETE == edit_type_id)
  204. {
  205. delete_antenna(id);
  206. }
  207. }
  208. void module_meta_date_changed::deal_call_edit_path(int id, EDIT_TYPE_ID edit_type_id)
  209. {
  210. if(ET_INSERT == edit_type_id)
  211. {
  212. log_error("path不支持增加操作");
  213. std_debug("path不支持增加操作");
  214. }
  215. else if(ET_UPDATE == edit_type_id)
  216. {
  217. sit_list::instance()->read_ant_path(id);
  218. }
  219. else if(ET_DELETE == edit_type_id)
  220. {
  221. auto sit_ptr = sit_list::instance()->get(id);
  222. if(sit_ptr)
  223. {
  224. sit_ptr->clear_path();
  225. }
  226. log_info("path删除成功,分站id=%d", id);
  227. std_debug("path删除成功,分站id=%d", id);
  228. }
  229. }
  230. void module_meta_date_changed::deal_call_edit_map(int id, EDIT_TYPE_ID edit_type_id)
  231. {
  232. if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
  233. {
  234. if(!is_cur_map(id))
  235. {
  236. log_info("修改的不是当前使用的地图,要修改地图id=%d,当前使用地图id=%d", id, cur_map_id());
  237. return;
  238. }
  239. update_map_info(id);
  240. }
  241. else if(ET_DELETE == edit_type_id)
  242. {
  243. if(is_cur_map(id))
  244. {
  245. log_error("删除了当前使用的地图");
  246. return;
  247. }
  248. }
  249. }
  250. void module_meta_date_changed::deal_call_edit_light(int id, EDIT_TYPE_ID edit_type_id)
  251. {
  252. if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
  253. {
  254. }
  255. else if(ET_DELETE == edit_type_id)
  256. {
  257. }
  258. }
  259. void module_meta_date_changed::deal_call_edit_lights_group(int id, EDIT_TYPE_ID edit_type_id)
  260. {
  261. if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
  262. {
  263. }
  264. else if(ET_DELETE == edit_type_id)
  265. {
  266. }
  267. }
  268. // 禁止指定人员下井 id = (rt_person_forbid_down_mine)数据库中自增长ID
  269. void module_meta_date_changed::deal_call_edit_forbid_person_down_mine(const std::string & lszId,EDIT_TYPE_ID edit_type_id)
  270. {
  271. std::vector<std::string> vecSegTag;
  272. boost::split(vecSegTag, lszId, boost::is_any_of(";"));
  273. if (vecSegTag.size() == 1) //数据发生错误
  274. {
  275. log_errno("Web Send Data Error!(%s)", lszId.c_str());
  276. return;
  277. }
  278. int db_id = std::stoi(vecSegTag[0].c_str());
  279. int staff_id = std::stoi(vecSegTag[1].c_str());
  280. if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
  281. {
  282. forbid_staff_down_mine::instance()->init_forbid_staff(db_id);
  283. }
  284. else if(ET_DELETE == edit_type_id)
  285. {
  286. forbid_staff_down_mine::instance()->del_forbid_data(db_id,staff_id);
  287. }
  288. }
  289. void module_meta_date_changed::init_setting()
  290. {
  291. //pRes = getMysqlRes("select setting_id, name, type, value from dat_setting;");
  292. }
  293. void module_meta_date_changed::remove_card(uint32_t id, int32_t type) {
  294. uint64_t card_id = tool_other::type_id_to_u64(type, id);
  295. auto card_ptr = card_list::instance()->get(card_id);
  296. if (!card_ptr) {
  297. log_error("基础数据, 在全局列表中删除卡,全局列表中已经不存在此卡, id=%d, type=%d", id, type);
  298. return;
  299. }
  300. remove_card(card_ptr);
  301. }
  302. void module_meta_date_changed::remove_card(std::shared_ptr<card_location_base> card_ptr)
  303. {
  304. if (!card_ptr)
  305. {
  306. return;
  307. }
  308. log_info("基础数据, 在全局列表中删除卡成功, id=%d, type=%d",card_ptr->m_type, card_ptr->m_id);
  309. // auto area_hover_ptr = card_ptr->get_area_hover();
  310. // if(area_hover_ptr && 0!=area_hover_ptr->id() && 0!=area_hover_ptr->mapid())
  311. // {
  312. // module_area::on_leave(card_ptr->m_id, area_hover_ptr, card_ptr->m_type);
  313. // }
  314. // if(card_ptr->is_person())
  315. // {
  316. // module_attendance_person::up_mine(card_ptr, nullptr);
  317. // }
  318. // else
  319. // {
  320. // module_attendance_vehicle::save_attendance(card_ptr, nullptr);
  321. // module_meta_date_changed::clear_card(card_ptr);
  322. // }
  323. module_meta_date_changed::clear_card(card_ptr);
  324. // 避免状态重置
  325. uint64_t card_id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
  326. card_list::instance()->remove(card_id);
  327. }
  328. bool module_meta_date_changed::try_get_edit_type_id(const std::string& op_type, EDIT_TYPE_ID& out_edit_type_id)
  329. {
  330. if(JSON_KEY_OP_TYPE_UPDATE == op_type)
  331. {
  332. out_edit_type_id = ET_UPDATE;
  333. }
  334. else if(JSON_KEY_OP_TYPE_DELETE == op_type)
  335. {
  336. out_edit_type_id = ET_DELETE;
  337. }
  338. else if(JSON_KEY_OP_TYPE_INSERT == op_type)
  339. {
  340. out_edit_type_id = ET_INSERT;
  341. }
  342. else
  343. {
  344. return false;
  345. }
  346. return true;
  347. }
  348. ///删除区域所有报警信息
  349. //void module_meta_date_changed::delete_area_event(std::shared_ptr<area> area_ptr)
  350. //{
  351. // for(int i=0; i < CARD_EVENT_COUNT_MAX; i++ )
  352. // {
  353. // event_tool::instance()->handle_event(OT_AREA, static_cast<EVENT_TYPE>(i), area_ptr->id(), 0, 0, false);
  354. // }
  355. //}
  356. void module_meta_date_changed::delete_antenna(int id)
  357. {
  358. int sitid=-8;
  359. auto map = sit_list::instance()->m_map;
  360. for(auto&it:map)
  361. {
  362. auto sit_ptr = it.second;
  363. if(sit_ptr->m_ant[0].m_id == id)
  364. {
  365. sitid=sit_ptr->m_id;
  366. sit_ptr->delete_antenna(0);
  367. break;
  368. }
  369. if(sit_ptr->m_ant[1].m_id == id)
  370. {
  371. sitid=sit_ptr->m_id;
  372. sit_ptr->delete_antenna(1);
  373. break;
  374. }
  375. }
  376. if(sitid==-8)
  377. {
  378. std_debug("天线已经删除了,在分站列表中找不到该天线:天线id=%d", id);
  379. log_info("天线已经删除了,在分站列表中找不到该天线:天线id=%d", id);
  380. }
  381. else
  382. {
  383. std_debug("删除天线成功:天线id=%d,分站id=%d", id, sitid);
  384. log_info("删除天线成功:天线id=%d,分站id=%d", id, sitid);
  385. }
  386. }
  387. bool module_meta_date_changed::is_cur_map(int id)
  388. {
  389. return cur_map_id()==id;
  390. }
  391. int module_meta_date_changed::cur_map_id()
  392. {
  393. auto tmp = area_list::instance()->m_map;
  394. for(auto&it:tmp)
  395. {
  396. if(0!=it.second->m_mapid)
  397. {
  398. return it.second->m_mapid;
  399. }
  400. }
  401. return -1;
  402. }
  403. void module_meta_date_changed::update_map_info(int id)
  404. {
  405. std::string sql = "SELECT scale FROM dat_map WHERE map_id=";
  406. sql.append(std::to_string(id));
  407. sql.append(";");
  408. std_debug("修改地图 sql=%s", sql.c_str());
  409. log_info("修改地图 sql=%s", sql.c_str());
  410. std::string Error;
  411. YADB::CDBResultSet DBRes;
  412. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  413. int nCount = DBRes.GetRecordCount( Error );
  414. if (nCount < 1)
  415. {
  416. log_error("修改地图失败,数据库中找不到: map_id=%d", id);
  417. return ;
  418. }
  419. double scale = 0;
  420. DBRes.GetField( "scale",scale, Error );
  421. auto area = area_list::instance()->m_map;
  422. for(auto& it : area)
  423. {
  424. it.second->m_scale = scale;
  425. }
  426. auto site = sit_list::instance()->m_map;
  427. for(auto& it : site)
  428. {
  429. it.second->m_scale = scale;
  430. }
  431. }