card.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. #include <memory>
  2. #include "common_tool.h"
  3. #include "message.h"
  4. #include "loc_message.h"
  5. #include "config_file.h"
  6. #include "card_person.h"
  7. #include "card_car.h"
  8. #include "db/db_card.h"
  9. #include "db/db_api/CDBSingletonDefine.h"
  10. #include "db/db_api/CDBResultSet.h"
  11. #include "tool_time.h"
  12. #include "mine.h"
  13. #include "log.h"
  14. #include "area.h"
  15. #include "card.h"
  16. #include "mine_business.h"
  17. #include "worker.h"
  18. #include "sys_setting.h"
  19. extern config_file config;
  20. int three_rates_flag = 0;
  21. int traffic_light_flag = 0;
  22. static void request(uint64_t id,uint32_t hash_id,uint16_t dis)
  23. {
  24. task * t = task::alloc<message_change_card_display>();
  25. auto&mcb=t->body<message_change_card_display>();
  26. t->m_hash_id=hash_id;
  27. t->m_cmd_code=0x10002;
  28. mcb.m_card_id=id;
  29. mcb.m_display=dis;
  30. worker::instance()->request(t);
  31. }
  32. void card_list::init_staffer(const std::string & lszId64)
  33. {
  34. std::string strategy = config.get("person.strategy","PS_1");
  35. std::unordered_map<uint64_t,std::shared_ptr<card_location_base>>
  36. map=std::move(db_card::load_person(lszId64,strategy));
  37. if(map.empty())
  38. {
  39. log_error("增加或修改失败,数据库中找不到: card_id=%", lszId64.c_str());
  40. return ;
  41. }
  42. if("" == lszId64)
  43. {
  44. log_info( "init_staffer. The record count=%d\n", map.size() );
  45. card_list::instance()->add(map);
  46. }
  47. else
  48. {
  49. auto db_person=map.begin()->second;
  50. uint64_t id64 = map.begin()->first;
  51. auto card_ptr = card_list::instance()->get(id64);
  52. if(card_ptr)
  53. {
  54. person* tmp_ptr= static_cast<person*>(card_ptr.get());
  55. person* db_person_ptr= static_cast<person*>(db_person.get());
  56. card_ptr->set_base_data(db_person_ptr->m_cid,db_person_ptr->m_type,db_person_ptr->m_deptid,db_person_ptr->m_level_id);
  57. //group_id
  58. //occ_id
  59. tmp_ptr->m_workLine = db_person_ptr->m_workLine;
  60. tmp_ptr->m_stafferName = db_person_ptr->m_stafferName;
  61. tmp_ptr->m_deptName = db_person_ptr->m_deptName;
  62. tmp_ptr->m_worktype_id = db_person_ptr->m_worktype_id;
  63. if(db_person_ptr->m_display!=tmp_ptr->m_display){
  64. request(id64,tmp_ptr->m_id,db_person_ptr->m_display);
  65. }
  66. }
  67. else
  68. {
  69. card_list::instance()->add(id64, db_person);
  70. }
  71. log_info("基础数据 增加或修改人卡成功:卡id:%ld, dept_id:%d,need_display:%d,occupation_level_id:%d",
  72. id64,db_person->m_deptid,db_person->m_display,db_person->m_level_id);
  73. }
  74. }
  75. void card_list::init_vehicle(const std::string & lszId64)
  76. {
  77. std::unordered_map<uint64_t,std::shared_ptr<card_location_base>>
  78. map=std::move(db_card::load_car(lszId64));
  79. if(map.empty())
  80. {
  81. log_error("增加或修改失败,数据库中找不到: card_id=%s", lszId64.c_str());
  82. return ;
  83. }
  84. if("" == lszId64)
  85. {
  86. log_info( "init_vehicle. The record count=%d\n", map.size() );
  87. card_list::instance()->add(map);
  88. }
  89. else
  90. {
  91. uint64_t id64 = map.begin()->first;
  92. auto card_ptr = card_list::instance()->get(id64);
  93. car* db_car= static_cast<car*>(map.begin()->second.get());
  94. if(card_ptr)
  95. {
  96. car* tmp_ptr= static_cast<car*>(card_ptr.get());
  97. card_ptr->set_base_data(db_car->m_cid,db_car->m_type,db_car->m_deptid,db_car->m_level_id);
  98. tmp_ptr->m_vehicle_category_id = db_car->m_vehicle_category_id;
  99. tmp_ptr->m_vehicle_type_id = db_car->m_vehicle_type_id;
  100. if(db_car->m_display!=tmp_ptr->m_display){
  101. request(id64,tmp_ptr->m_id,db_car->m_display);
  102. }
  103. }
  104. else
  105. {
  106. card_list::instance()->add(id64, map.begin()->second);
  107. }
  108. log_info("基础数据 增加或修改车卡成功:卡id:%ld, dept_id:%d,need_display:%d,categoryid:%d",
  109. id64,db_car->m_deptid,db_car->m_display,db_car->m_vehicle_category_id);
  110. }
  111. }
  112. void card_list::init_card_from_db()
  113. {
  114. init_staffer("");
  115. init_vehicle("");
  116. }
  117. void card_list::load_his_card_postion_from_db()
  118. {
  119. load_his_card_postion_vehicle();
  120. load_his_card_postion_staff();
  121. }
  122. void card_list::load_his_card_postion_vehicle()
  123. {
  124. const char *sql = "select l.card_id,l.site_id, l.cur_time, l.x, l.y, l.z, l.area_info, l.state, \
  125. att.start_time, att.end_time \
  126. from rt_location l, rt_att_vehicle att, dat_card c, dat_vehicle_extend v \
  127. where l.card_id = c.card_id and l.card_id = att.card_id and l.card_id=v.card_id \
  128. and c.state_id=0;";
  129. std::string Error;
  130. YADB::CDBResultSet DBRes;
  131. sDBConnPool.Query(sql,DBRes,Error);
  132. if(!Error.empty())
  133. log_error("load_his_card_postion_vehicle列表 Error,%s",Error.c_str());
  134. uint64_t nCount = DBRes.GetRecordCount( Error );
  135. if (nCount > 0)
  136. {
  137. log_info( "load_his_card_postion_vehicle. The record count=%ld\n", nCount );
  138. while ( DBRes.GetNextRecod(Error) )
  139. {
  140. std::string card_id = "";
  141. DBRes.GetField( "card_id",card_id, Error );
  142. uint64_t c_id = tool_other::card_id_to_u64(card_id);
  143. auto card_ptr = card_list::instance()->get(c_id);
  144. if(!card_ptr)
  145. {
  146. log_error("全局卡列表中找不到卡,%s",card_id.c_str());
  147. continue;
  148. }
  149. int site_id = -1;
  150. DBRes.GetField( "site_id",site_id, Error );
  151. double x = 0;
  152. DBRes.GetField( "x",x, Error );
  153. double y = 0;
  154. DBRes.GetField( "y",y, Error );
  155. double z = 0;
  156. DBRes.GetField( "z",z, Error );
  157. std::string strAreaInfo = "";
  158. DBRes.GetField( "area_info",strAreaInfo, Error );
  159. int state = 0;
  160. DBRes.GetField( "state",state, Error );
  161. double speed = 0;
  162. DBRes.GetField( "speed",speed, Error );
  163. std::string start_time = "";
  164. DBRes.GetField( "start_time",start_time, Error );
  165. std::string end_time = "";
  166. DBRes.GetField( "end_time",end_time, Error );
  167. std::string cur_time = "";
  168. DBRes.GetField( "cur_time",cur_time, Error );
  169. card_ptr->m_time = tool_time::to_time(cur_time)*1000;
  170. card_ptr->set(x,y,z);
  171. //考勤结束也要初始化该变量,用于统计今天出勤过得车卡数量
  172. auto mine_tool_ptr = card_ptr->get_mine_tool();
  173. mine_tool_ptr->m_attendance_start_time = tool_time::to_time_ex(start_time);
  174. if(end_time.empty())
  175. {
  176. mine_tool_ptr->m_is_attendance = true;
  177. log_info("vehicle加载考勤,考勤开始时间: %s, 卡id=%s", start_time.c_str(), card_id.c_str());
  178. }
  179. auto area_tool=card_ptr->get_area_tool();
  180. auto sit_ptr=sit_list::instance()->get(site_id);
  181. if(sit_ptr)
  182. {
  183. auto area_hover_ptr = std::make_shared<area_hover>(sit_ptr->get_area(),*card_ptr);
  184. area_tool->m_hover_list.push_back(area_hover_ptr);
  185. area_tool->set_site(sit_ptr);
  186. }
  187. //解析area_info
  188. const char * split = "|";
  189. char *p = strtok(const_cast<char *>(strAreaInfo.c_str()), split);
  190. while (p != NULL)
  191. {
  192. int area_id = 0;
  193. uint64_t enter_time = 0;
  194. sscanf(p, "%d,%lu", &area_id, &enter_time);
  195. p = strtok(NULL, split);
  196. if (area_id < 0)
  197. continue;
  198. auto area = area_list::instance()->get(area_id);
  199. if(area == nullptr)
  200. {
  201. log_error("load_vehicle...area_list 找不到区域:区域id=%d", area_id);
  202. continue;
  203. }
  204. log_info("load_vehicle_area:%s,%d,%lu",card_id.c_str(),area_id,enter_time);
  205. //要放到最后
  206. auto area_hover_ptr = std::make_shared<area_hover>();
  207. area_hover_ptr->m_area = area;
  208. area_hover_ptr->m_enter_time = enter_time;
  209. area_tool->m_hover_list.push_back(area_hover_ptr);
  210. area->on_load_his(area_hover_ptr, card_ptr);
  211. }
  212. }
  213. }
  214. }
  215. void card_list::load_his_card_postion_staff()
  216. {
  217. std::string init_limit_hour = config.get("service.init_limit_hour","48");
  218. std::string sql = "select l.card_id,l.site_id, l.cur_time, l.x, l.y, l.z, l.area_info, l.state, \
  219. att.start_time, att.end_time \
  220. from rt_location l, rt_att_staff att, dat_card c, dat_staff_extend s \
  221. where l.card_id = c.card_id and l.card_id = att.card_id and l.card_id=s.card_id\
  222. and c.state_id=0 and end_time is null ";
  223. sql += " and timestampdiff(hour ,att.start_time,now()) <= " + init_limit_hour + ";";
  224. log_info("load_his_card_postion_staff:%s ",sql.c_str());
  225. std::string Error;
  226. YADB::CDBResultSet DBRes;
  227. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  228. if(!Error.empty())
  229. log_error("load_his_card_postion_staff列表 Error,%s",Error.c_str());
  230. uint64_t nCount = DBRes.GetRecordCount( Error );
  231. if (nCount > 0)
  232. {
  233. log_info( "load_his_card_postion_staff. The record count=%ld\n", nCount );
  234. while ( DBRes.GetNextRecod(Error) )
  235. {
  236. std::string end_time = "";
  237. DBRes.GetField( "end_time",end_time, Error );
  238. if(!end_time.empty()) // 已升井,不再读入
  239. continue;
  240. std::string card_id = "";
  241. DBRes.GetField( "card_id",card_id, Error );
  242. uint64_t c_id = tool_other::card_id_to_u64(card_id);
  243. auto card_ptr = card_list::instance()->get(c_id);
  244. if(!card_ptr)
  245. {
  246. log_error("全局卡列表中找不到卡,%s", card_id.c_str());
  247. continue;
  248. }
  249. int site_id = -1;
  250. DBRes.GetField( "site_id",site_id, Error );
  251. double x = 0;
  252. DBRes.GetField( "x",x, Error );
  253. double y = 0;
  254. DBRes.GetField( "y",y, Error );
  255. double z = 0;
  256. DBRes.GetField( "z",z, Error );
  257. std::string strAreaInfo = "";
  258. DBRes.GetField( "area_info",strAreaInfo, Error );
  259. int state = 0;
  260. DBRes.GetField( "state",state, Error );
  261. double speed = 0;
  262. DBRes.GetField( "speed",speed, Error );
  263. std::string start_time = "";
  264. DBRes.GetField( "start_time",start_time, Error );
  265. std::string cur_time = "";
  266. DBRes.GetField( "cur_time",cur_time, Error );
  267. card_ptr->set(x,y,z);
  268. card_ptr->m_time = tool_time::to_time(cur_time)*1000;
  269. auto mine_tool_ptr = card_ptr->get_mine_tool();
  270. mine_tool_ptr->m_attendance_start_time = tool_time::to_time_ex(start_time);
  271. mine_tool_ptr->m_is_attendance = true;
  272. log_info("person加载考勤,考勤开始时间:%s,[%s]cur_time:%s 卡id=%s areainfo=%s",start_time.c_str(),tool_time::to_str(mine_tool_ptr->m_attendance_start_time).c_str(),cur_time.c_str(),card_id.c_str(),strAreaInfo.c_str());
  273. //分站设置
  274. auto area_tool=card_ptr->get_area_tool();
  275. auto sit_ptr=sit_list::instance()->get(site_id);
  276. if(sit_ptr)
  277. {
  278. auto area_hover_ptr = std::make_shared<area_hover>(sit_ptr->get_area(),*card_ptr);
  279. area_tool->m_hover_list.push_back(area_hover_ptr);
  280. area_tool->set_site(sit_ptr);
  281. }
  282. //解析area_info
  283. const char * split = "|";
  284. char *p = strtok(const_cast<char *>(strAreaInfo.c_str()), split);
  285. while (p != NULL)
  286. {
  287. int area_id = 0;
  288. uint64_t enter_time = 0;
  289. sscanf(p, "%d,%lu", &area_id, &enter_time);
  290. p = strtok(NULL, split);
  291. if (area_id < 0)
  292. continue;
  293. auto area = area_list::instance()->get(area_id);
  294. if(area == nullptr)
  295. {
  296. log_error("area_list 找不到区域:区域id=%d", area_id);
  297. continue;
  298. }
  299. log_info("load_person_area:%s,%d,%lu",card_id.c_str(),area_id,enter_time);
  300. //要放到最后
  301. auto area_hover_ptr = std::make_shared<area_hover>();
  302. area_hover_ptr->m_area = area;
  303. area_hover_ptr->m_enter_time = enter_time;
  304. area_tool->m_hover_list.push_back(area_hover_ptr);
  305. area->on_load_his(area_hover_ptr, card_ptr);
  306. }
  307. }
  308. }
  309. }
  310. void card_list::on_message(zloop<task*> *loop, message_locinfo&loc,bool is_history)
  311. {
  312. uint64_t cardid = tool_other::type_id_to_u64(loc.m_card_type,loc.m_card_id);
  313. const auto &c=card_list::instance()->get(cardid);
  314. if(!c)
  315. {
  316. log_warn("数据库中未定义该卡的信息,card_id=%d, card_type=%d, cardid:%lld",loc.m_card_id,loc.m_card_type,cardid);
  317. return;
  318. }
  319. double dist_tof=loc.m_tof*15.65*2.996*1e-4;
  320. logn_info(3,"card_message:site=%d,ant=%d,type=%lu,card=%d,ct=%d,tof=%lld(%.2lfm),rav=%02X,acc=%02X,rssi=%d,bat=%02X,stamp=%llu",
  321. loc.m_site_id,loc.m_ant_id,loc.m_card_type,loc.m_card_id,loc.m_card_ct,loc.m_tof,dist_tof,
  322. (loc.m_card_type==1?loc.m_callinfo:loc.m_rav),
  323. loc.m_acc,loc.m_rssi,loc.m_batty_status,loc.m_time_stamp);
  324. if(loc.m_card_type != CT_PERSON && CYaSetting::m_sys_setting.check_rav(loc.m_card_type,loc.m_card_id))
  325. loc.set_rav(0);
  326. c->on_message(loop,loc,is_history);
  327. if(tool_other::is_driving(loc.m_card_type,c->get_vehicle_type_id()))
  328. mine_business::inst()->make_reverse_condition(loc.m_card_type,loc.m_card_id,loc.m_ant_id,loc.m_card_ct,loc.m_tof,loc.m_site_id);
  329. }
  330. void card_list::on_message(zloop<task*>* loop, message_tdoa_locinfo& loc, bool is_history)
  331. {
  332. uint64_t cardid = tool_other::type_id_to_u64(loc.m_card_msg.m_type,loc.m_card_msg.m_id);
  333. const auto &c=card_list::instance()->get(cardid);
  334. if(!c)
  335. {
  336. log_warn("[tdoa] 数据库中未定义该卡的信息,card_id=%d, card_type=%d, cardid:%lld",loc.m_card_msg.m_id,loc.m_card_msg.m_type, cardid);
  337. return;
  338. }
  339. c->on_message(loop, loc, is_history);
  340. }
  341. void card_list::on_message(zloop<task*>* loop, message_pdoa_locinfo& loc, bool is_history)
  342. {
  343. uint64_t cardid = tool_other::type_id_to_u64(loc.m_card_type,loc.m_card_id);
  344. const auto &c = card_list::instance()->get(cardid);
  345. if(!c)
  346. {
  347. log_warn("[pdoa] 数据库中未定义该卡的信息,card_id=%d, card_type=%d, cardid:%lld",loc.m_card_id,loc.m_card_type,cardid);
  348. return;
  349. }
  350. c->on_message(loop, loc, is_history);
  351. }
  352. //获取卡数据 //标识id 人staff_id 车 vehicle_id
  353. const std::shared_ptr<card_location_base> card_list::get_card_by_cid(int cid)
  354. {
  355. auto t_map = card_list::instance()->m_map;
  356. for(const std::pair<int,std::shared_ptr<card_location_base>> &me:t_map)
  357. if ((int)me.second->m_cid == cid)
  358. return me.second;
  359. return nullptr;
  360. }
  361. bool card_list_visit::visit(std::shared_ptr<card_location_base> c)
  362. {
  363. c->get_card(_flag);
  364. return true;
  365. }
  366. //template<> std::shared_ptr<card_list> single_base<card_list, uint64_t, std::shared_ptr<card_location_base>>::m_instance=std::make_shared<card_list>();