#include "ya_setting.h"
#include "db/db_api/CDBSingletonDefine.h"
#include "log.h"

SSys_setting CYaSetting::m_sys_setting;
/*
* 从数据库的dat_setting表初始化系统阈值，
* 包括：井下人员阈值，井下车辆阈值，人员超时阈值，车辆超时阈值，车辆超速阈值
*/
bool CYaSetting::Init_sys_setting()
{
	std::string Error;
	YADB::CDBResultSet DBRes;
	std::string sql = "select setting_id, name, type, value from dat_setting;";
	sDBConnPool.Query(sql.c_str(),DBRes,Error);
	int nCount = DBRes.GetRecordCount( Error );
	if (nCount > 0)
	{
		while ( DBRes.GetNextRecod(Error) )
		{
			std::string strVal = "";
			std::string val="";
			DBRes.GetField("name",strVal, Error );
#define D_GetValue(K,N,X,PS) \
			if(strVal==N)\
			{DBRes.GetField("value",val, Error);\
			log_info("init_setting:%s,%s",N,val.c_str());\
			K = atoi(val.c_str())*X;}\


			D_GetValue(m_sys_setting.over_count_person,"over_count_person",1,"人员井下超员")
			D_GetValue(m_sys_setting.over_count_vehicle,"over_count_vehicle",1,"车辆井下超员")
			D_GetValue(m_sys_setting.over_time_person,"over_time_person",60,"人员井下超时")
			D_GetValue(m_sys_setting.over_time_vehicle,"over_time_vehicle",60,"车辆井下超时")
			D_GetValue(m_sys_setting.over_speed,"over_speed",1,"车辆超速限值")
			D_GetValue(m_sys_setting.att_starttime_offset_staff,"att_starttime_offset_staff",60,"考勤开始时间向前偏移分钟数")
			D_GetValue(m_sys_setting.att_endtime_offset_staff,"att_endtime_offset_staff",60,"车辆考勤开始时间向前偏移分钟数")
			D_GetValue(m_sys_setting.att_starttime_offset_vehicle,"att_starttime_offset_vehicle",60,"车辆考勤结束时间向后偏移分钟数")
			D_GetValue(m_sys_setting.att_endtime_offset_vehicle,"att_endtime_offset_vehicle",60,"车辆考勤结束时间向后偏移分钟数")
			D_GetValue(m_sys_setting.att_person_thre_hour,"att_person_thre_hour",60*60,"")
			D_GetValue(m_sys_setting.rear_end_d,"rear_end_distance",1,"")
			D_GetValue(m_sys_setting.rear_end_t,"rear_end_time",1,"")
			D_GetValue(m_sys_setting.geofault_warn_dis,"geofault_warn",1,"")
#undef D_GetValue
		}
		log_info("init_setting:rear_end_d:%f,t:%ld,person:%u,v:%u,tp:%u,tv:%u,s:%f",m_sys_setting.rear_end_d,m_sys_setting.rear_end_t,
					m_sys_setting.over_count_person,m_sys_setting.over_count_vehicle,m_sys_setting.over_time_person,m_sys_setting.over_time_vehicle,m_sys_setting.over_speed);
	}

    return true;
}

