area_business_person_attendance.cpp 6.7 KB

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