card_tool.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <cmath>
  2. #include "card_tool.h"
  3. #include "log.h"
  4. #include "db/db_tool.h"
  5. #include "sstream"
  6. card_tool::card_tool()
  7. :m_staff_num(0)
  8. ,m_record_staffer_timeval(0)
  9. {}
  10. card_tool* card_tool::inst()
  11. {
  12. static card_tool ct;
  13. return &ct;
  14. }
  15. void card_tool::fetch_add()
  16. {
  17. m_staff_num++;
  18. }
  19. void card_tool::reset()
  20. {
  21. m_staff_num=0;
  22. }
  23. void card_tool::record_stffer_num()
  24. {
  25. static uint32_t min_num=0,max_num=0;
  26. std::time_t t = time(NULL);
  27. char ti[64] = { 0 };
  28. strftime(ti,sizeof(ti),"%Y/%m/%d %H:%M:%S",localtime(&t));
  29. std::string sti(ti);
  30. int minute = atoi(sti.substr(sti.find_first_of(':')+1,2).c_str());
  31. if(m_record_staffer_timeval==0)
  32. {
  33. min_num=max_num=m_staff_num;
  34. m_record_staffer_timeval = t;
  35. }
  36. if(t-m_record_staffer_timeval>=120 && minute%2 == 0)
  37. {
  38. std::stringstream ss;
  39. ss<< "INSERT INTO his_staff_number(max_num,min_num,ave_num) VALUES("<<max_num<<','<<min_num<<','<<std::lround((max_num+min_num)/2)<<");";
  40. logn_info(2,"staff_number:%s",ss.str().c_str());
  41. db_tool::PushAsync(ss.str().c_str());
  42. min_num=max_num=m_staff_num;
  43. m_record_staffer_timeval = t;
  44. }
  45. else
  46. {
  47. if (min_num>m_staff_num) min_num = m_staff_num;
  48. if (max_num < m_staff_num) max_num = m_staff_num;
  49. }
  50. reset();
  51. return;
  52. }