db_tool.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "log.h"
  2. #include "db_api/CDBSingletonDefine.h"
  3. #include "card_base.h"
  4. #include "mine.h"
  5. #include "area.h"
  6. #include "tool_time.h"
  7. #include "db_tool.h"
  8. namespace db_tool
  9. {
  10. void PushAsync(const char* sql)
  11. {
  12. logn_info(2,"sql:[%s]",sql);
  13. if(!sDBConnPool.PushAsync(sql))
  14. {
  15. logn_error(2,"PushAsync记录到队列中失败");
  16. }
  17. }
  18. static std::string type_id_to_str(int32_t type,uint32_t id)
  19. {
  20. char sql[15] = {0};
  21. snprintf(sql, 15,"%03d%010d", type, id);
  22. return std::string(sql);
  23. }
  24. // 保存考勤开始或结束数据到数据库
  25. void save_attendance(const std::shared_ptr<card_location_base>& card_ptr,
  26. const std::shared_ptr<area_hover>& area_hover_ptr)
  27. {
  28. char sql[LENGTH_SQL] = {0};
  29. std::string call(card_ptr->is_person()?"add_att_staff":"add_att_vehicle");
  30. auto mine_tool_ptr = card_ptr->get_mine_tool();
  31. auto start = mine_tool_ptr->m_attendance_start_time;
  32. auto end = mine_tool_ptr->m_attendance_start_time;
  33. if(!mine_tool_ptr->m_is_attendance)//考勤结束时间
  34. {
  35. end = std::chrono::system_clock::now();
  36. }
  37. std::string start_str = tool_time::to_str(start);
  38. std::string end_str = tool_time::to_str(end);
  39. auto map_area_info = card_ptr->get_area_tool()->m_area_info;
  40. int landmarkid=0,landmarkdirect=0;
  41. double landmarkdist=0;
  42. if(!map_area_info.empty())
  43. {
  44. auto lm = map_area_info.begin()->second;
  45. landmarkid = std::get<1>(lm);
  46. landmarkdirect=std::get<2>(lm);
  47. landmarkdist=std::get<3>(lm);
  48. }
  49. sprintf(sql, "CALL %s(%s, %d, '%s', '%s', %d, %d, %.3lf);", call.c_str(),
  50. type_id_to_str(card_ptr->m_type,card_ptr->m_id).c_str(),
  51. card_ptr->m_cid, start_str.c_str(), end_str.c_str(),
  52. landmarkid, landmarkdirect, landmarkdist);
  53. log_info("attendance():%s",sql);
  54. PushAsync(sql);
  55. }
  56. }