area_business_person_attendance.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include "area_business_person_attendance.h"
  2. #include "db/db_tool.h"
  3. #include"db/db_api/CDBSingletonDefine.h"
  4. #include"log.h"
  5. #include"card.h"
  6. #include "card_person.h"
  7. #include"area.h"
  8. #include"common_tool.h"
  9. #include"mine.h"
  10. #include"websocket/constdef.h"
  11. #include "websocket/wsClientMgr.h"
  12. #include "websocket/wsTimerThread.h"
  13. #include"tool_time.h"
  14. #include "module_meta_date_changed.h"
  15. #include "his_location.h"
  16. //记录进入时间等信息,结束考勤,根据离开的时间和距离,判断是否记录一条新的考勤记录
  17. void area_business_person_attendance::on_enter(const std::shared_ptr<area_hover>&area_hover_ptr,
  18. const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data>&ptr)
  19. {
  20. log_info("on_enter:person_att:%d",card_ptr->m_id);
  21. if(!card_ptr->is_person())
  22. return;
  23. auto mine_tool_ptr = card_ptr->get_mine_tool();
  24. if(!mine_tool_ptr->m_is_attendance)
  25. {
  26. mine_tool_ptr->m_is_attendance=true;
  27. mine_tool_ptr->m_attendance_start_time=
  28. std::chrono::system_clock::time_point(std::chrono::milliseconds(area_hover_ptr->m_enter_time));
  29. //作为一条开始考勤记录保存到数据库
  30. db_tool::save_attendance(card_ptr, area_hover_ptr);
  31. }
  32. }
  33. void area_business_person_attendance::on_hover(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr)
  34. {
  35. }
  36. //记录离开考勤区域信息,开始考勤
  37. void area_business_person_attendance::on_leave(const std::shared_ptr<area_hover>&area_hover_ptr,const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data> ptr)
  38. {
  39. if(!card_ptr->is_person())return;
  40. auto mine_tool_ptr = card_ptr->get_mine_tool();
  41. if(!mine_tool_ptr->m_is_attendance)return;
  42. log_info("on_leave_person_att:%d",card_ptr->m_id);
  43. //考勤结束
  44. mine_tool_ptr->m_is_attendance=false;
  45. if(card_ptr->upmine_flag()){
  46. auto start = mine_tool_ptr->m_attendance_start_time;
  47. std::string start_time = tool_time::to_str(start);
  48. std::string card_id = tool_other::type_id_to_str(card_ptr->m_type,card_ptr->m_id);
  49. char nsql[512]{0};
  50. const char *sql = "update rpt_att_staff set is_auto=%d where card_id=%s and start_time='%s';";
  51. snprintf(nsql,512,sql,card_ptr->upmine_flag(),card_id.c_str(),start_time.c_str());
  52. db_tool::PushAsync(nsql);
  53. }
  54. //作为一条结束考勤记录保存到数据库
  55. db_tool::save_attendance(card_ptr, area_hover_ptr);
  56. YA::_CARD_POS_ cp;
  57. cp.Type=card_ptr->m_type;
  58. cp.ID = card_ptr->m_id;
  59. swsTimerThrd.del_card_pos(cp);
  60. rapidjson::Document doc(rapidjson::kObjectType);
  61. rapidjson::Value datas(rapidjson::kArrayType);
  62. rapidjson::Document::AllocatorType& allocator=doc.GetAllocator();
  63. _to_json_card_up_one(card_ptr, datas, allocator);
  64. //升井json发给web
  65. if(datas.Size() > 0)
  66. {
  67. doc.AddMember(JSON_ROOT_KEY_CMD,JSON_CMD_VALUE_UP_MINE, allocator);
  68. //doc.AddMember(JSON_ROOT_KEY_VERSION,INTERFACE_VERSION, allocator);
  69. doc.AddMember(JSON_ROOT_KEY_DATA, datas, allocator);
  70. swsClientMgr.send(JSON_CMD_VALUE_PUSH, tool_json::doc_to_json(doc));
  71. }
  72. auto per = std::dynamic_pointer_cast<person>(card_ptr);
  73. per->clear();
  74. }
  75. /**
  76. * @brief 手工升井函数
  77. */
  78. void area_business_person_attendance::handle_up_mine(sio::message::ptr const& data)
  79. {
  80. std::vector<sio::message::ptr> card_vec;
  81. if(!tool_map::try_get_value(card_vec, JSON_ROOT_KEY_DATA, data) || card_vec.size() == 0)
  82. {
  83. log_error("手工升井,web发来的数据data字段为空 或者不是数组");
  84. return;
  85. }
  86. std::vector<sio::message::ptr>::const_iterator it_card = card_vec.begin();
  87. int type = 0;
  88. std::string s_card_id;
  89. for(; it_card != card_vec.end(); ++it_card)
  90. {
  91. if(!tool_map::try_get_value(s_card_id, JSON_KEY_CALL_CARD_CARD_ID, (*it_card))
  92. ||!tool_map::try_get_value(type, JSON_KEY_CALL_CARD_CARD_TYPE_ID, (*it_card))
  93. )
  94. {
  95. log_error("手工升井,web发来的数据 card_id 或 card_type格式不对");
  96. continue;
  97. }
  98. log_info("handle_up_card:%s,接收到手动升井的请求",s_card_id.c_str());
  99. if(!tool_other::is_person(type))
  100. continue;
  101. auto card_ptr = card_list::instance()->get(tool_other::card_id_to_u64(s_card_id));
  102. if(card_ptr && STATUS_LOST == card_ptr->m_biz_stat)
  103. {
  104. log_info("handle_up_card:%s 手工升井,处理",s_card_id.c_str());
  105. card_ptr->inc_upmine_flag(1);
  106. }
  107. else
  108. log_warn("handle_up_card:%s,手动升井的卡找不到,或者该卡不在盲区",s_card_id.c_str());
  109. }
  110. }
  111. ///升井json
  112. void area_business_person_attendance::_to_json_card_up_one(std::shared_ptr<card_location_base> card_ptr,
  113. rapidjson::Value& out_datas, rapidjson::Document::AllocatorType& allocator)
  114. {
  115. auto mine_tool_ptr = card_ptr->get_mine_tool();
  116. int map_id=0;
  117. if(auto area_tool_ptr = card_ptr->get_area_tool())
  118. map_id=area_tool_ptr->get_mapid();
  119. rapidjson::Value data(rapidjson::kArrayType);
  120. //卡号
  121. std::string id = tool_other::type_id_to_str(card_ptr->m_type, card_ptr->m_id);
  122. tool_json::push_back(data, id, allocator);
  123. //x,y坐标
  124. data.PushBack(card_ptr->x, allocator);
  125. data.PushBack(card_ptr->y, allocator); //CFunctions::round(card->y_offset_after(),2)
  126. //入井时间戳
  127. uint64_t t = tool_time::to_ms(mine_tool_ptr->m_attendance_start_time); //转为ms
  128. data.PushBack(t, allocator);
  129. //进入区域时间戳
  130. data.PushBack(0, allocator);
  131. //接收时间戳
  132. data.PushBack(0, allocator);
  133. //工作时长
  134. t = tool_time::elapse_ms(mine_tool_ptr->m_attendance_start_time); //转为ms
  135. data.PushBack(t, allocator);
  136. //地图编号
  137. data.PushBack(map_id, allocator);
  138. //区域编号
  139. data.PushBack(0, allocator);
  140. //部门编号
  141. data.PushBack(card_ptr->m_deptid, allocator);
  142. //状态
  143. data.PushBack(card_ptr->m_pwr_stat, allocator);
  144. //运行状态
  145. data.PushBack(card_ptr->m_stat, allocator);
  146. //业务状态
  147. data.PushBack(card_ptr->m_biz_stat, allocator);
  148. //速度
  149. data.PushBack(card_ptr->m_speed, allocator);
  150. out_datas.PushBack(data, allocator);
  151. }