module_meta_date_changed.cpp 14 KB

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