area_business_person_attendance.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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"area.h"
  7. #include"common_tool.h"
  8. #include"mine.h"
  9. #include"websocket/constdef.h"
  10. #include "websocket/wsClientMgr.h"
  11. #include"tool_time.h"
  12. struct person_attendance_data:business_data
  13. {
  14. person_attendance_data():m_is_attendance(false)
  15. ,m_attendance_start_time(std::chrono::seconds(0))
  16. {
  17. }
  18. ///考勤状态 0初始状态 1 没在考勤 2 考勤;参看ATTENDANCE_STATUS
  19. bool m_is_attendance;
  20. ///考勤开始时间
  21. std::chrono::system_clock::time_point m_attendance_start_time;
  22. };
  23. area_business_person_attendance::area_business_person_attendance()
  24. {
  25. }
  26. //记录进入时间等信息,结束考勤,根据离开的时间和距离,判断是否记录一条新的考勤记录
  27. void area_business_person_attendance::on_enter(const std::shared_ptr<area_hover>&a,
  28. const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data>&ptr)
  29. {
  30. if(!card_ptr->is_person())
  31. {
  32. return;
  33. }
  34. auto mine_tool_ptr = card_ptr->get_mine_tool();
  35. if(!mine_tool_ptr->m_is_attendance)
  36. {
  37. //考勤开始
  38. mine_tool_ptr->m_is_attendance=true;
  39. mine_tool_ptr->m_attendance_start_time=std::chrono::system_clock::now();
  40. //作为一条开始考勤记录保存到数据库
  41. db_tool::save_attendance(card_ptr);
  42. // log_info("人卡考勤开始:卡id=%d,卡type=%d,分站id=%d,分站reader_type_id=%d,stat_attendance=%d",
  43. // card_ptr->m_id, card_ptr->m_type,
  44. // site_ptr->m_id,site_ptr->m_reader_type_id,mine_tool_ptr->m_is_attendance);
  45. }
  46. // auto ptr_temp = std::make_shared<person_attendance_data>();
  47. // ptr = ptr_temp;
  48. // ptr_temp->m_is_attendance = true;
  49. // ptr_temp->m_attendance_start_time = std::chrono::system_clock::now();
  50. // //作为一条开始考勤记录保存到数据库
  51. // db_tool::save_attendance(card_ptr, ptr_temp->m_is_attendance, ptr_temp->m_attendance_start_time);
  52. }
  53. 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)
  54. {
  55. }
  56. //记录离开考勤区域信息,开始考勤
  57. void area_business_person_attendance::on_leave(const std::shared_ptr<area_hover>&a,
  58. const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data> ptr)
  59. {
  60. if(!card_ptr->is_person())
  61. {
  62. return;
  63. }
  64. up_mine(card_ptr, false);
  65. // if(nullptr == ptr)
  66. // {
  67. // log_error("结束考勤失败:nullptr == ptr");
  68. // return;
  69. // }
  70. // auto ptr_temp = static_cast<person_attendance_data*>(ptr.get());
  71. // if(ptr_temp->m_is_attendance)
  72. // {
  73. // ptr_temp->m_is_attendance = false;
  74. // //作为一条开始考勤记录保存到数据库
  75. // db_tool::save_attendance(card_ptr, ptr_temp->m_is_attendance, ptr_temp->m_attendance_start_time);
  76. // }
  77. }
  78. ///升井或收到web的删除卡命令 site_ptr==nullptr表示收到web的删除卡命令
  79. /// 保存考勤记录,发升井json,清理卡
  80. void area_business_person_attendance::up_mine(std::shared_ptr<card_location_base> card_ptr, bool is_web_delete)
  81. {
  82. auto mine_tool_ptr = card_ptr->get_mine_tool();
  83. if(!mine_tool_ptr->m_is_attendance)
  84. {
  85. return;
  86. }
  87. //考勤结束
  88. mine_tool_ptr->m_is_attendance=false;
  89. //作为一条结束考勤记录保存到数据库
  90. db_tool::save_attendance(card_ptr);
  91. rapidjson::Document doc(rapidjson::kObjectType);
  92. rapidjson::Value datas(rapidjson::kArrayType);
  93. rapidjson::Document::AllocatorType& allocator=doc.GetAllocator();
  94. _to_json_card_up_one(card_ptr, datas, allocator);
  95. //module_meta_date_changed::clear_card(card_ptr);
  96. auto rea_tool = card_ptr->get_area_tool();
  97. rea_tool->on_leave(card_ptr);
  98. card_ptr->clear();
  99. //升井json发给web
  100. if(datas.Size() > 0)
  101. {
  102. doc.AddMember(JSON_ROOT_KEY_CMD,JSON_CMD_VALUE_UP_MINE, allocator);
  103. //doc.AddMember(JSON_ROOT_KEY_VERSION,INTERFACE_VERSION, allocator);
  104. doc.AddMember(JSON_ROOT_KEY_DATA, datas, allocator);
  105. swsClientMgr.send(JSON_CMD_VALUE_PUSH, tool_json::doc_to_json(doc));
  106. }
  107. // if(is_web_delete)
  108. // {
  109. // log_info("人卡考勤结束:卡id=%d,卡type=%d,分站id=%d,分站reader_type_id=%d,stat_attendance=%d",
  110. // card_ptr->m_id, card_ptr->m_type,
  111. // site_ptr->m_id,site_ptr->m_reader_type_id,mine_tool_ptr->m_is_attendance);
  112. // }
  113. // else
  114. // {
  115. // log_info("收到web的删除卡命令,人卡考勤结束:卡id=%d,卡type=%d, stat_attendance=%d",
  116. // card_ptr->m_id, card_ptr->m_type, mine_tool_ptr->m_is_attendance);
  117. // }
  118. }
  119. /**
  120. * @brief 手工升井函数
  121. */
  122. void area_business_person_attendance::handle_up_mine(sio::message::ptr const& data)
  123. {
  124. std::vector<sio::message::ptr> card_vec;
  125. if(!tool_map::try_get_value(card_vec, JSON_ROOT_KEY_DATA, data) || card_vec.size() == 0)
  126. {
  127. log_error("手工升井,web发来的数据data字段为空 或者不是数组");
  128. return;
  129. }
  130. rapidjson::Document doc(rapidjson::kObjectType);
  131. rapidjson::Value datas(rapidjson::kArrayType);
  132. rapidjson::Document::AllocatorType& allocator=doc.GetAllocator();
  133. std::vector<sio::message::ptr>::const_iterator it_card = card_vec.begin();
  134. int type = 0;
  135. std::string s_card_id;
  136. for(; it_card != card_vec.end(); ++it_card)
  137. {
  138. if(!tool_map::try_get_value(s_card_id, JSON_KEY_CALL_CARD_CARD_ID, (*it_card))
  139. ||!tool_map::try_get_value(type, JSON_KEY_CALL_CARD_CARD_TYPE_ID, (*it_card)))
  140. {
  141. log_error("手工升井,web发来的数据 card_id 或 card_type格式不对");
  142. continue;
  143. }
  144. uint32_t id = tool_other::id64_to_id(s_card_id);
  145. auto card_ptr = card_list::instance()->get(tool_other::type_id_to_u64(type, id));
  146. if(card_ptr && card_ptr->is_person())
  147. {
  148. std_debug("手工升井,处理,卡id=%d,卡type=%d", id, type);
  149. log_info("手工升井,处理,卡id=%d,卡type=%d", id, type);
  150. auto mine_tool_ptr = card_ptr->get_mine_tool();
  151. if(mine_tool_ptr->m_is_attendance)
  152. {
  153. //考勤结束
  154. mine_tool_ptr->m_is_attendance=false;
  155. //作为一条结束考勤记录保存到数据库
  156. db_tool::save_attendance(card_ptr);
  157. }
  158. //检查井下是否超员--是否需要取消
  159. //CMineCardManager::instance()->OnPersonUp(card_ptr);
  160. auto area_hover_ptr = card_ptr->get_area_hover();
  161. if(area_hover_ptr && 0!=area_hover_ptr->id() && 0!=area_hover_ptr->mapid())
  162. {
  163. module_area::on_leave(card_ptr->m_id, area_hover_ptr, card_ptr->m_type);
  164. }
  165. _to_json_card_up_one(card_ptr, datas, allocator);
  166. module_meta_date_changed::clear_card(card_ptr);
  167. }
  168. else
  169. {
  170. log_error("手工升井,在全局列表中找不到卡,卡id=%d,卡type=%d", id, type);
  171. }
  172. }
  173. //升井json发给web
  174. if(datas.Size() > 0)
  175. {
  176. doc.AddMember(JSON_ROOT_KEY_CMD,JSON_CMD_VALUE_UP_MINE, allocator);
  177. //doc.AddMember(JSON_ROOT_KEY_VERSION,INTERFACE_VERSION, allocator);
  178. doc.AddMember(JSON_ROOT_KEY_DATA, datas, allocator);
  179. swsClientMgr.send(JSON_CMD_VALUE_PUSH, tool_json::doc_to_json(doc));
  180. }
  181. }
  182. ///升井json
  183. void area_business_person_attendance::_to_json_card_up_one(std::shared_ptr<card_location_base> card_ptr,
  184. rapidjson::Value& out_datas, rapidjson::Document::AllocatorType& allocator)
  185. {
  186. if(!card_ptr->m_display)
  187. {
  188. return;
  189. }
  190. auto mine_tool_ptr = card_ptr->get_mine_tool();
  191. rapidjson::Value data(rapidjson::kArrayType);
  192. //卡号
  193. std::string id = tool_other::type_id_to_str(card_ptr->m_type, card_ptr->m_id);
  194. tool_json::push_back(data, id, allocator);
  195. //x,y坐标
  196. data.PushBack(card_ptr->x, allocator);
  197. data.PushBack(card_ptr->y, allocator); //CFunctions::round(card->y_offset_after(),2)
  198. //入井时间戳
  199. uint64_t t = tool_time::to_ms(mine_tool_ptr->m_attendance_start_time); //转为ms
  200. data.PushBack(t, allocator);
  201. //进入区域时间戳
  202. data.PushBack(0, allocator);
  203. //接收时间戳
  204. data.PushBack(0, allocator);
  205. //工作时长
  206. t = tool_time::elapse_ms(mine_tool_ptr->m_attendance_start_time); //转为ms
  207. data.PushBack(t, allocator);
  208. //地图编号
  209. data.PushBack(0, allocator);
  210. //区域编号
  211. data.PushBack(0, allocator);
  212. //部门编号
  213. data.PushBack(card_ptr->m_deptid, allocator);
  214. //状态
  215. data.PushBack(0, allocator);
  216. //运行状态
  217. data.PushBack(card_ptr->m_stat, allocator);
  218. //业务状态
  219. data.PushBack(0, allocator);
  220. //速度
  221. data.PushBack(card_ptr->m_speed, allocator);
  222. out_datas.PushBack(data, allocator);
  223. }