worktime_rate.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include "worktime_rate.h"
  2. #include <utility>
  3. #include<memory>
  4. #include<string>
  5. #include "db_api/CDBSingletonDefine.h"
  6. #include <stdio.h>
  7. #include<chrono>
  8. #include"log.h"
  9. worktime::worktime()
  10. {
  11. }
  12. worktime::~worktime()
  13. {
  14. }
  15. void worktime::enter_area(std::shared_ptr<card_pos> &card_ptr, std::shared_ptr<area_data> &area_ptr)
  16. {
  17. if (area_ptr->is_work_area && card_ptr->work_line == 1 && card_ptr->identifier_id != 0 && area_ptr->enter_time)
  18. {
  19. time_t cur_time = static_cast<time_t>(area_ptr->enter_time);
  20. std::string time_str = to_str(cur_time);
  21. int shift_id = _get_shift_id(cur_time);
  22. char strsql[LENGTH_SQL]={0};
  23. snprintf(strsql, sizeof(strsql), "INSERT IGNORE INTO \
  24. his_worktime_detail(staff_id, start_work_time,end_work_time,real_work_time,schedule_work_time,work_area_id,shift_id) \
  25. VALUES(%d, '%s', NULL, %.3f, %.3f, %d, %d);",
  26. card_ptr->identifier_id, time_str.c_str(), 0.0, 8.0, area_ptr->area_id, shift_id);
  27. std::string Error;
  28. if(static_cast<int>(sDBConnPool.ExecuteSql(strsql,Error)) < 0)
  29. {
  30. logn_error(2,"worktime执行sql失败:%s, %s", Error.c_str(), strsql);
  31. }
  32. }
  33. }
  34. void worktime::leave_area(std::shared_ptr<card_pos> &card_ptr, std::shared_ptr<area_data> &area_ptr)
  35. {
  36. if (area_ptr->is_work_area && card_ptr->work_line == 1&& card_ptr->identifier_id != 0 && area_ptr->leave_time)
  37. {
  38. time_t start_time;
  39. if(!_get_start_worktime(card_ptr->identifier_id, start_time))
  40. {
  41. logn_error(2,"worktime get_start_worktime==false");
  42. return;
  43. }
  44. double real_work_time = (area_ptr->leave_time - static_cast<uint64_t>(start_time))/3600.0;
  45. if(real_work_time<0)
  46. {
  47. logn_error(2,"worktime (card_ptr->leave_time - start_time)/3600=%.3f", real_work_time);
  48. return;
  49. }
  50. char strsql[LENGTH_SQL]={0};
  51. snprintf(strsql, sizeof(strsql),
  52. "UPDATE his_worktime_detail SET end_work_time='%s',proc_tag=1,real_work_time= %.3f \
  53. where staff_id=%d and start_work_time='%s';",
  54. to_str(static_cast<time_t>(area_ptr->leave_time)).c_str(), real_work_time,card_ptr->identifier_id, to_str(start_time).c_str());
  55. std::string Error;
  56. if(static_cast<int>(sDBConnPool.ExecuteSql(strsql,Error)) < 0)
  57. {
  58. logn_error(2,"worktime执行sql失败:%s, %s", Error.c_str(), strsql);
  59. }
  60. }
  61. }
  62. void worktime::init_shift()
  63. {
  64. char strsql[LENGTH_SQL]={0};
  65. snprintf(strsql,sizeof(strsql), "select shift_id, start_time, end_time, shift_type_id from dat_shift;");
  66. YADB::CDBResultSet DBRes;
  67. std::string Error;
  68. if(!sDBConnPool.Query(strsql,DBRes,Error))
  69. {
  70. logn_error(2,"worktime::init_shift 失败");
  71. return;
  72. }
  73. int nCount = static_cast<int>(DBRes.GetRecordCount(Error));
  74. if(nCount < 0)
  75. {
  76. logn_error(2,"worktime::init_shift 失败");
  77. return;
  78. }
  79. _shitf_map.clear();
  80. while (DBRes.GetNextRecod(Error))
  81. {
  82. int shift_id=0;
  83. DBRes.GetField("shift_id", shift_id,Error);
  84. std::string start_time;
  85. DBRes.GetField("start_time",start_time,Error);
  86. std::string end_time;
  87. DBRes.GetField("end_time",end_time,Error);
  88. int shift_type_id=0;
  89. DBRes.GetField("shift_type_id", shift_type_id, Error);
  90. auto ptr = std::make_shared<BanShift>(shift_id, shift_type_id, to_time_short(start_time), to_time_short(end_time));
  91. _shitf_map.insert(std::make_pair(ptr->shift_id, ptr));
  92. }
  93. for(auto&a:_shitf_map)
  94. {
  95. std_debug("shift_id=%d, start=%d, end=%d", a.second->shift_id, a.second->start_time, a.second->end_time);
  96. }
  97. }
  98. bool worktime::_get_start_worktime(int staff_id, time_t& out_time)
  99. {
  100. char strsql[LENGTH_SQL] = {0};
  101. std::string Error = "";
  102. snprintf(strsql,sizeof(strsql),"select start_work_time from his_worktime_detail \
  103. where staff_id=%d and proc_tag=0 order by start_work_time desc limit 1", staff_id);
  104. YADB::CDBResultSet DBRes;
  105. if(!sDBConnPool.Query(strsql,DBRes,Error))
  106. {
  107. logn_error(2,"worktime执行sql失败:%s, %s", Error.c_str(), strsql);
  108. return false;
  109. }
  110. int nCount = static_cast<int>(DBRes.GetRecordCount(Error));
  111. if (nCount > 0 && DBRes.GetNextRecod(Error))
  112. {
  113. std::string start_work_time;
  114. DBRes.GetField("start_work_time",start_work_time, Error);
  115. out_time = to_time(start_work_time);
  116. return true;
  117. }
  118. return false;
  119. }
  120. int worktime::_get_shift_id( time_t time)
  121. {
  122. uint32_t cur_time = to_time_short(time);
  123. auto iter = _shitf_map.begin();
  124. for(; iter != _shitf_map.end(); ++iter)
  125. {
  126. if(iter->second->end_time < iter->second->start_time)
  127. {
  128. if(cur_time >= iter->second->start_time || cur_time < iter->second->end_time)
  129. {
  130. return iter->first;
  131. }
  132. }
  133. else
  134. {
  135. if(cur_time >= iter->second->start_time && cur_time < iter->second->end_time)
  136. {
  137. return iter->first;
  138. }
  139. }
  140. }
  141. return 1;
  142. }