123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- #include "worktime_rate.h"
- #include <utility>
- #include<memory>
- #include<string>
- #include "db_api/CDBSingletonDefine.h"
- #include <stdio.h>
- #include<chrono>
- #include"log.h"
- worktime::worktime()
- {
- }
- worktime::~worktime()
- {
- }
- void worktime::enter_area(std::shared_ptr<card_pos> &card_ptr, std::shared_ptr<area_data> &area_ptr)
- {
- if (area_ptr->is_work_area && card_ptr->work_line == 1 && card_ptr->identifier_id != 0 && area_ptr->enter_time)
- {
- time_t cur_time = static_cast<time_t>(area_ptr->enter_time);
- std::string time_str = to_str(cur_time);
- int shift_id = _get_shift_id(cur_time);
- char strsql[LENGTH_SQL]={0};
- snprintf(strsql, sizeof(strsql), "INSERT IGNORE INTO \
- his_worktime_detail(staff_id, start_work_time,end_work_time,real_work_time,schedule_work_time,work_area_id,shift_id) \
- VALUES(%d, '%s', NULL, %.3f, %.3f, %d, %d);",
- card_ptr->identifier_id, time_str.c_str(), 0.0, 8.0, area_ptr->area_id, shift_id);
- std::string Error;
- if(static_cast<int>(sDBConnPool.ExecuteSql(strsql,Error)) < 0)
- {
- logn_error(2,"worktime执行sql失败:%s, %s", Error.c_str(), strsql);
- }
- }
- }
- void worktime::leave_area(std::shared_ptr<card_pos> &card_ptr, std::shared_ptr<area_data> &area_ptr)
- {
- if (area_ptr->is_work_area && card_ptr->work_line == 1&& card_ptr->identifier_id != 0 && area_ptr->leave_time)
- {
- time_t start_time;
- if(!_get_start_worktime(card_ptr->identifier_id, start_time))
- {
- logn_error(2,"worktime get_start_worktime==false");
- return;
- }
- double real_work_time = (area_ptr->leave_time - static_cast<uint64_t>(start_time))/3600.0;
- if(real_work_time<0)
- {
- logn_error(2,"worktime (card_ptr->leave_time - start_time)/3600=%.3f", real_work_time);
- return;
- }
- char strsql[LENGTH_SQL]={0};
- snprintf(strsql, sizeof(strsql),
- "UPDATE his_worktime_detail SET end_work_time='%s',proc_tag=1,real_work_time= %.3f \
- where staff_id=%d and start_work_time='%s';",
- 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());
- std::string Error;
- if(static_cast<int>(sDBConnPool.ExecuteSql(strsql,Error)) < 0)
- {
- logn_error(2,"worktime执行sql失败:%s, %s", Error.c_str(), strsql);
- }
- }
- }
- void worktime::init_shift()
- {
- char strsql[LENGTH_SQL]={0};
- snprintf(strsql,sizeof(strsql), "select shift_id, start_time, end_time, shift_type_id from dat_shift;");
- YADB::CDBResultSet DBRes;
- std::string Error;
- if(!sDBConnPool.Query(strsql,DBRes,Error))
- {
- logn_error(2,"worktime::init_shift 失败");
- return;
- }
- int nCount = static_cast<int>(DBRes.GetRecordCount(Error));
- if(nCount < 0)
- {
- logn_error(2,"worktime::init_shift 失败");
- return;
- }
- _shitf_map.clear();
- while (DBRes.GetNextRecod(Error))
- {
- int shift_id=0;
- DBRes.GetField("shift_id", shift_id,Error);
- std::string start_time;
- DBRes.GetField("start_time",start_time,Error);
- std::string end_time;
- DBRes.GetField("end_time",end_time,Error);
- int shift_type_id=0;
- DBRes.GetField("shift_type_id", shift_type_id, Error);
- auto ptr = std::make_shared<BanShift>(shift_id, shift_type_id, to_time_short(start_time), to_time_short(end_time));
- _shitf_map.insert(std::make_pair(ptr->shift_id, ptr));
- }
- for(auto&a:_shitf_map)
- {
- std_debug("shift_id=%d, start=%d, end=%d", a.second->shift_id, a.second->start_time, a.second->end_time);
- }
- }
- bool worktime::_get_start_worktime(int staff_id, time_t& out_time)
- {
- char strsql[LENGTH_SQL] = {0};
- std::string Error = "";
- snprintf(strsql,sizeof(strsql),"select start_work_time from his_worktime_detail \
- where staff_id=%d and proc_tag=0 order by start_work_time desc limit 1", staff_id);
- YADB::CDBResultSet DBRes;
- if(!sDBConnPool.Query(strsql,DBRes,Error))
- {
- logn_error(2,"worktime执行sql失败:%s, %s", Error.c_str(), strsql);
- return false;
- }
- int nCount = static_cast<int>(DBRes.GetRecordCount(Error));
- if (nCount > 0 && DBRes.GetNextRecod(Error))
- {
- std::string start_work_time;
- DBRes.GetField("start_work_time",start_work_time, Error);
- out_time = to_time(start_work_time);
- return true;
- }
- return false;
- }
- int worktime::_get_shift_id( time_t time)
- {
- uint32_t cur_time = to_time_short(time);
- auto iter = _shitf_map.begin();
- for(; iter != _shitf_map.end(); ++iter)
- {
- if(iter->second->end_time < iter->second->start_time)
- {
- if(cur_time >= iter->second->start_time || cur_time < iter->second->end_time)
- {
- return iter->first;
- }
- }
- else
- {
- if(cur_time >= iter->second->start_time && cur_time < iter->second->end_time)
- {
- return iter->first;
- }
- }
- }
- return 1;
- }
|