#include "special_area.h"

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

special_area_list * special_area_list::instance()
{
	static special_area_list spl;
	return &spl;
}
int32_t special_area_list::get_special_id(uint32_t cardid,const point p,int32_t categoryid)
{
	int32_t f = -1;
	for(const auto s:m_v)	
	{
		if(p.x<=std::max(s.start_point.x,s.end_point.x)
			&&p.x>=std::min(s.start_point.x,s.end_point.x)
			&&p.y<=std::max(s.start_point.y,s.end_point.y)
			&&p.y>=std::min(s.start_point.y,s.end_point.y))	
			//&&p.z<=std::max(s.start_point.z,s.end_point.z)
			//&&p.z>=std::min(s.start_point.z,s.end_point.z))	
		{
			if(!s.car_type.empty())
				if(s.car_type.find(std::to_string(categoryid)) == std::string::npos)	
					continue;
			if(m_m.find(s.special_area_id) != m_m.end())
			{
				auto v=m_m[s.special_area_id];
				if(std::find(v.begin(),v.end(),cardid)==v.end())
					continue;
			}
			f=s.special_area_id;
		}
	}

	return f;
}
void special_area_list::init_from_db()
{
	init_special_area_entry();
	init_special_card();
}
void special_area_list::init_special_area_entry()
{
	const char * sql = "SELECT serial_num, start_point_x, start_point_y, start_point_z, end_point_x, end_point_y, \
					  end_point_z, special_area_id,car_type FROM dat_special_area_entry;";
	std::string Error;
	YADB::CDBResultSet DBRes;
	sDBConnPool.Query(sql,DBRes,Error);
	int nCount = DBRes.GetRecordCount( Error );
	if (nCount > 0)
	{
		log_info( "init_special_area. The record count=%d\n", nCount );

		while ( DBRes.GetNextRecod(Error) )
		{
			double start_x=0;
			DBRes.GetField( "start_point_x",start_x, Error );
			double start_y=0;
			DBRes.GetField( "start_point_y",start_y, Error );
			double start_z=0;
			DBRes.GetField( "start_point_z",start_z, Error );
			double end_x=0;
			DBRes.GetField( "end_point_x",end_x, Error );

			double end_y=0;
			DBRes.GetField( "end_point_y",end_y, Error );

			double end_z=0;
			DBRes.GetField( "end_point_z",end_z, Error );

			int areaid = 0;
			DBRes.GetField( "special_area_id",areaid, Error );
			std::string categoryid;
			DBRes.GetField( "car_type",categoryid, Error );
			
			m_v.emplace_back(start_x,start_y,start_z,end_x,end_y,end_z,areaid,categoryid);

			log_info("special area .....areaid:%d,categoryid:%s",areaid,categoryid.c_str());
		}
	}

}
void special_area_list::init_special_card()
{
	const char * sql = "SELECT b.vehicle_id,a.special_area FROM dat_special_card a , dat_vehicle b WHERE a.card_name = b.name;";
	std::string Error;
	YADB::CDBResultSet DBRes;
	sDBConnPool.Query(sql,DBRes,Error);
	int nCount = DBRes.GetRecordCount( Error );
	if (nCount > 0)
	{
		log_info( "init_special_card. The record count=%d\n", nCount );

		while ( DBRes.GetNextRecod(Error) )
		{
			int vehicle_id = 0;
			DBRes.GetField( "vehicle_id",vehicle_id, Error );
			
			int areaid=0;
			DBRes.GetField( "special_area",areaid, Error );

			auto iter = m_m.find(areaid);
			if(iter != m_m.end())
				iter->second.push_back(vehicle_id);
			else
				m_m[areaid].push_back(vehicle_id);

			log_info("special card .....areaid:%d,categoryid:%d %d ..... %d",areaid,vehicle_id,m_m.size(),m_m[areaid].size());
		}
	}


}

