area_business_person_attendance.cpp 8.8 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. #include "module_meta_date_changed.h"
  13. struct person_attendance_data:business_data
  14. {
  15. person_attendance_data():m_is_attendance(false)
  16. ,m_attendance_start_time(std::chrono::seconds(0))
  17. {
  18. }
  19. ///考勤状态 0初始状态 1 没在考勤 2 考勤;参看ATTENDANCE_STATUS
  20. bool m_is_attendance;
  21. ///考勤开始时间
  22. std::chrono::system_clock::time_point m_attendance_start_time;
  23. };
  24. area_business_person_attendance::area_business_person_attendance()
  25. {
  26. }
  27. //记录进入时间等信息,结束考勤,根据离开的时间和距离,判断是否记录一条新的考勤记录
  28. void area_business_person_attendance::on_enter(const std::shared_ptr<area_hover>&a,
  29. const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data>&ptr)
  30. {
  31. if(!card_ptr->is_person())
  32. {
  33. return;
  34. }
  35. auto mine_tool_ptr = card_ptr->get_mine_tool();
  36. if(!mine_tool_ptr->m_is_attendance)
  37. {
  38. //考勤开始
  39. mine_tool_ptr->m_is_attendance=true;
  40. mine_tool_ptr->m_attendance_start_time=std::chrono::system_clock::now();
  41. //作为一条开始考勤记录保存到数据库
  42. db_tool::save_attendance(card_ptr);
  43. // log_info("人卡考勤开始:卡id=%d,卡type=%d,分站id=%d,分站reader_type_id=%d,stat_attendance=%d",
  44. // card_ptr->m_id, card_ptr->m_type,
  45. // site_ptr->m_id,site_ptr->m_reader_type_id,mine_tool_ptr->m_is_attendance);
  46. }
  47. // auto ptr_temp = std::make_shared<person_attendance_data>();
  48. // ptr = ptr_temp;
  49. // ptr_temp->m_is_attendance = true;
  50. // ptr_temp->m_attendance_start_time = std::chrono::system_clock::now();
  51. // //作为一条开始考勤记录保存到数据库
  52. // db_tool::save_attendance(card_ptr, ptr_temp->m_is_attendance, ptr_temp->m_attendance_start_time);
  53. }
  54. 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)
  55. {
  56. }
  57. //记录离开考勤区域信息,开始考勤
  58. void area_business_person_attendance::on_leave(const std::shared_ptr<area_hover>&a,
  59. const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data> ptr)
  60. {
  61. if(!card_ptr->is_person())
  62. {
  63. return;
  64. }
  65. up_mine(card_ptr, false);
  66. // if(nullptr == ptr)
  67. // {
  68. // log_error("结束考勤失败:nullptr == ptr");
  69. // return;
  70. // }
  71. // auto ptr_temp = static_cast<person_attendance_data*>(ptr.get());
  72. // if(ptr_temp->m_is_attendance)
  73. // {
  74. // ptr_temp->m_is_attendance = false;
  75. // //作为一条开始考勤记录保存到数据库
  76. // db_tool::save_attendance(card_ptr, ptr_temp->m_is_attendance, ptr_temp->m_attendance_start_time);
  77. // }
  78. }
  79. ///升井或收到web的删除卡命令 site_ptr==nullptr表示收到web的删除卡命令
  80. /// 保存考勤记录,发升井json,清理卡
  81. void area_business_person_attendance::up_mine(std::shared_ptr<card_location_base> card_ptr, bool is_web_delete)
  82. {
  83. auto mine_tool_ptr = card_ptr->get_mine_tool();
  84. if(!mine_tool_ptr->m_is_attendance)
  85. {
  86. return;
  87. }
  88. //考勤结束
  89. mine_tool_ptr->m_is_attendance=false;
  90. //作为一条结束考勤记录保存到数据库
  91. db_tool::save_attendance(card_ptr);
  92. rapidjson::Document doc(rapidjson::kObjectType);
  93. rapidjson::Value datas(rapidjson::kArrayType);
  94. rapidjson::Document::AllocatorType& allocator=doc.GetAllocator();
  95. _to_json_card_up_one(card_ptr, datas, allocator);
  96. //module_meta_date_changed::clear_card(card_ptr);
  97. auto rea_tool = card_ptr->get_area_tool();
  98. rea_tool->on_leave(card_ptr);
  99. card_ptr->clear();
  100. //升井json发给web
  101. if(datas.Size() > 0)
  102. {
  103. doc.AddMember(JSON_ROOT_KEY_CMD,JSON_CMD_VALUE_UP_MINE, allocator);
  104. //doc.AddMember(JSON_ROOT_KEY_VERSION,INTERFACE_VERSION, allocator);
  105. doc.AddMember(JSON_ROOT_KEY_DATA, datas, allocator);
  106. swsClientMgr.send(JSON_CMD_VALUE_PUSH, tool_json::doc_to_json(doc));
  107. }
  108. // if(is_web_delete)
  109. // {
  110. // log_info("人卡考勤结束:卡id=%d,卡type=%d,分站id=%d,分站reader_type_id=%d,stat_attendance=%d",
  111. // card_ptr->m_id, card_ptr->m_type,
  112. // site_ptr->m_id,site_ptr->m_reader_type_id,mine_tool_ptr->m_is_attendance);
  113. // }
  114. // else
  115. // {
  116. // log_info("收到web的删除卡命令,人卡考勤结束:卡id=%d,卡type=%d, stat_attendance=%d",
  117. // card_ptr->m_id, card_ptr->m_type, mine_tool_ptr->m_is_attendance);
  118. // }
  119. }
  120. /**
  121. * @brief 手工升井函数
  122. */
  123. void area_business_person_attendance::handle_up_mine(sio::message::ptr const& data)
  124. {
  125. std::vector<sio::message::ptr> card_vec;
  126. if(!tool_map::try_get_value(card_vec, JSON_ROOT_KEY_DATA, data) || card_vec.size() == 0)
  127. {
  128. log_error("手工升井,web发来的数据data字段为空 或者不是数组");
  129. return;
  130. }
  131. rapidjson::Document doc(rapidjson::kObjectType);
  132. rapidjson::Value datas(rapidjson::kArrayType);
  133. rapidjson::Document::AllocatorType& allocator=doc.GetAllocator();
  134. std::vector<sio::message::ptr>::const_iterator it_card = card_vec.begin();
  135. int type = 0;
  136. std::string s_card_id;
  137. for(; it_card != card_vec.end(); ++it_card)
  138. {
  139. if(!tool_map::try_get_value(s_card_id, JSON_KEY_CALL_CARD_CARD_ID, (*it_card))
  140. ||!tool_map::try_get_value(type, JSON_KEY_CALL_CARD_CARD_TYPE_ID, (*it_card)))
  141. {
  142. log_error("手工升井,web发来的数据 card_id 或 card_type格式不对");
  143. continue;
  144. }
  145. uint32_t id = tool_other::id64_to_id(s_card_id);
  146. auto card_ptr = card_list::instance()->get(tool_other::type_id_to_u64(type, id));
  147. if(card_ptr && card_ptr->is_person())
  148. {
  149. std_debug("手工升井,处理,卡id=%d,卡type=%d", id, type);
  150. log_info("手工升井,处理,卡id=%d,卡type=%d", id, type);
  151. auto mine_tool_ptr = card_ptr->get_mine_tool();
  152. if(mine_tool_ptr->m_is_attendance)
  153. {
  154. //考勤结束
  155. mine_tool_ptr->m_is_attendance=false;
  156. //作为一条结束考勤记录保存到数据库
  157. db_tool::save_attendance(card_ptr);
  158. }
  159. //检查井下是否超员--是否需要取消
  160. //CMineCardManager::instance()->OnPersonUp(card_ptr);
  161. auto rea_tool = card_ptr->get_area_tool();
  162. rea_tool->on_leave(card_ptr);
  163. card_ptr->clear();
  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. }