area_business_person_attendance.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. //升井json发给web
  97. if(datas.Size() > 0)
  98. {
  99. doc.AddMember(JSON_ROOT_KEY_CMD,JSON_CMD_VALUE_UP_MINE, allocator);
  100. //doc.AddMember(JSON_ROOT_KEY_VERSION,INTERFACE_VERSION, allocator);
  101. doc.AddMember(JSON_ROOT_KEY_DATA, datas, allocator);
  102. swsClientMgr.send(JSON_CMD_VALUE_PUSH, tool_json::doc_to_json(doc));
  103. }
  104. // if(is_web_delete)
  105. // {
  106. // log_info("人卡考勤结束:卡id=%d,卡type=%d,分站id=%d,分站reader_type_id=%d,stat_attendance=%d",
  107. // card_ptr->m_id, card_ptr->m_type,
  108. // site_ptr->m_id,site_ptr->m_reader_type_id,mine_tool_ptr->m_is_attendance);
  109. // }
  110. // else
  111. // {
  112. // log_info("收到web的删除卡命令,人卡考勤结束:卡id=%d,卡type=%d, stat_attendance=%d",
  113. // card_ptr->m_id, card_ptr->m_type, mine_tool_ptr->m_is_attendance);
  114. // }
  115. }
  116. /**
  117. * @brief 手工升井函数
  118. */
  119. void area_business_person_attendance::handle_up_mine(sio::message::ptr const& data)
  120. {
  121. std::vector<sio::message::ptr> card_vec;
  122. if(!tool_map::try_get_value(card_vec, JSON_ROOT_KEY_DATA, data) || card_vec.size() == 0)
  123. {
  124. log_error("手工升井,web发来的数据data字段为空 或者不是数组");
  125. return;
  126. }
  127. rapidjson::Document doc(rapidjson::kObjectType);
  128. rapidjson::Value datas(rapidjson::kArrayType);
  129. rapidjson::Document::AllocatorType& allocator=doc.GetAllocator();
  130. std::vector<sio::message::ptr>::const_iterator it_card = card_vec.begin();
  131. int type = 0;
  132. std::string s_card_id;
  133. for(; it_card != card_vec.end(); ++it_card)
  134. {
  135. if(!tool_map::try_get_value(s_card_id, JSON_KEY_CALL_CARD_CARD_ID, (*it_card))
  136. ||!tool_map::try_get_value(type, JSON_KEY_CALL_CARD_CARD_TYPE_ID, (*it_card)))
  137. {
  138. log_error("手工升井,web发来的数据 card_id 或 card_type格式不对");
  139. continue;
  140. }
  141. uint32_t id = tool_other::id64_to_id(s_card_id);
  142. auto card_ptr = card_list::instance()->get(tool_other::type_id_to_u64(type, id));
  143. if(card_ptr && card_ptr->is_person())
  144. {
  145. std_debug("手工升井,处理,卡id=%d,卡type=%d", id, type);
  146. log_info("手工升井,处理,卡id=%d,卡type=%d", id, type);
  147. auto mine_tool_ptr = card_ptr->get_mine_tool();
  148. if(mine_tool_ptr->m_is_attendance)
  149. {
  150. //考勤结束
  151. mine_tool_ptr->m_is_attendance=false;
  152. //作为一条结束考勤记录保存到数据库
  153. db_tool::save_attendance(card_ptr);
  154. }
  155. //检查井下是否超员--是否需要取消
  156. //CMineCardManager::instance()->OnPersonUp(card_ptr);
  157. auto area_hover_ptr = card_ptr->get_area_hover();
  158. if(area_hover_ptr && 0!=area_hover_ptr->id() && 0!=area_hover_ptr->mapid())
  159. {
  160. module_area::on_leave(card_ptr->m_id, area_hover_ptr, card_ptr->m_type);
  161. }
  162. _to_json_card_up_one(card_ptr, datas, allocator);
  163. module_meta_date_changed::clear_card(card_ptr);
  164. }
  165. else
  166. {
  167. log_error("手工升井,在全局列表中找不到卡,卡id=%d,卡type=%d", id, type);
  168. }
  169. }
  170. //升井json发给web
  171. if(datas.Size() > 0)
  172. {
  173. doc.AddMember(JSON_ROOT_KEY_CMD,JSON_CMD_VALUE_UP_MINE, allocator);
  174. //doc.AddMember(JSON_ROOT_KEY_VERSION,INTERFACE_VERSION, allocator);
  175. doc.AddMember(JSON_ROOT_KEY_DATA, datas, allocator);
  176. swsClientMgr.send(JSON_CMD_VALUE_PUSH, tool_json::doc_to_json(doc));
  177. }
  178. }
  179. ///升井json
  180. void area_business_person_attendance::_to_json_card_up_one(std::shared_ptr<card_location_base> card_ptr,
  181. rapidjson::Value& out_datas, rapidjson::Document::AllocatorType& allocator)
  182. {
  183. if(!card_ptr->m_display)
  184. {
  185. return;
  186. }
  187. auto mine_tool_ptr = card_ptr->get_mine_tool();
  188. rapidjson::Value data(rapidjson::kArrayType);
  189. //卡号
  190. std::string id = tool_other::type_id_to_str(card_ptr->m_type, card_ptr->m_id);
  191. tool_json::push_back(data, id, allocator);
  192. //x,y坐标
  193. data.PushBack(card_ptr->x, allocator);
  194. data.PushBack(card_ptr->y, allocator); //CFunctions::round(card->y_offset_after(),2)
  195. //入井时间戳
  196. uint64_t t = tool_time::to_ms(mine_tool_ptr->m_attendance_start_time); //转为ms
  197. data.PushBack(t, allocator);
  198. //进入区域时间戳
  199. data.PushBack(0, allocator);
  200. //接收时间戳
  201. data.PushBack(0, allocator);
  202. //工作时长
  203. t = tool_time::elapse_ms(mine_tool_ptr->m_attendance_start_time); //转为ms
  204. data.PushBack(t, allocator);
  205. //地图编号
  206. data.PushBack(0, allocator);
  207. //区域编号
  208. data.PushBack(0, allocator);
  209. //部门编号
  210. data.PushBack(card_ptr->m_deptid, allocator);
  211. //状态
  212. data.PushBack(0, allocator);
  213. //运行状态
  214. data.PushBack(card_ptr->m_stat, allocator);
  215. //业务状态
  216. data.PushBack(0, allocator);
  217. //速度
  218. data.PushBack(card_ptr->m_speed, allocator);
  219. out_datas.PushBack(data, allocator);
  220. }