
#include <memory>
#include <unordered_map>
#include "log.h"
#include "config_file.h"
#include "db_api/CDBResultSet.h"
#include "db_api/CDBSingletonDefine.h"

#include "../card_base.h"
#include "../card_person.h"
#include "../common.h"
#include "../common_tool.h"
extern config_file config;
namespace db_card
{
	std::unordered_map<uint64_t,std::shared_ptr<card_location_base>> load_car(int64_t id64)
	{
		std::string sql = "SELECT ve.vehicle_id, ve.card_id, c.card_type_id, \
						   ve.dept_id, ve.group_id, v.vehicle_type_id, vt.vehicle_level_id, \
						   vt.is_railroad AS vt_is_railroad,ve.need_display ,ve.power_alarm,\
						   vt.vehicle_category_id,v.bigger_car_flag,vc.over_speed \
						   FROM dat_vehicle_extend ve \
						   LEFT JOIN dat_vehicle v ON ve.vehicle_id = v.vehicle_id \
						   LEFT JOIN dat_card c ON ve.card_id = c.card_id \
						   LEFT JOIN dat_dept d ON ve.dept_id = d.dept_id \
						   LEFT JOIN dat_group g ON ve.group_id = g.group_id \
						   LEFT JOIN dat_vehicle_type vt ON v.vehicle_type_id = vt.vehicle_type_id \
						   LEFT JOIN dat_vehicle_category vc ON vc.vehicle_category_id = vt.vehicle_category_id \
						   WHERE c.state_id = 0";

		std::unordered_map<uint64_t,std::shared_ptr<card_location_base>> map;

		std::string card_id_str = tool_other::to13str(id64);

		if(-1 == id64)
		{
			sql.append(";");
		}
		else
		{
			sql.append(" AND ve.card_id ='");
			sql.append(card_id_str);
			sql.append("';");
		}

		std::string Error;
		YADB::CDBResultSet DBRes;
		sDBConnPool.Query(sql.c_str(),DBRes,Error);
		int nCount = DBRes.GetRecordCount( Error );
		if (nCount < 1)
		{
			log_error("增加或修改失败，数据库中找不到: sql=%s", sql.c_str());
			return map;
		}

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

			std::string card_id;
			DBRes.GetField( "card_id",card_id, Error );

			uint32_t vsid = atoi(card_id.substr(3).c_str());

			unsigned int card_type_id  = 0;
			DBRes.GetField( "card_type_id",card_type_id, Error );

			int dept_id = 0;
			DBRes.GetField( "dept_id",dept_id, Error );

			int group_id = 0;
			DBRes.GetField( "group_id",group_id, Error );

			int vehicle_type_id = 0;
			DBRes.GetField( "vehicle_type_id",vehicle_type_id, Error );

			int vehicle_level_id = 0;
			DBRes.GetField( "vehicle_level_id",vehicle_level_id, Error );

			int need_display = 0;
			DBRes.GetField( "need_display",need_display, Error );

			int power_alarm = 0;
			DBRes.GetField( "power_alarm",power_alarm, Error );

			int vehicle_category_id = 0;
			DBRes.GetField( "vehicle_category_id",vehicle_category_id, Error );

			int bigger_car_flag= 0;
			DBRes.GetField( "bigger_car_flag",bigger_car_flag, Error );

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

			std::string strategy;
			if(card_type_id ==CT_VEHICLE)
				strategy = config.get("car.strategy","CS_1");
			else
				strategy = config.get("coal_winning.strategy","WS_1");

			auto clb = card_location_base::make_car(strategy,vsid,need_display,card_type_id,
					dept_id,vehicle_category_id, vehicle_type_id,vehicle_level_id,vehicle_id);
			uint64_t cardid = tool_other::type_id_to_u64(card_type_id,vsid);
			log_info("cardId:%llu,id:%d dept_id:%d,need_display:%d-cardid:%s,categoryid:%d,vchile_id:%d,type:%d",
					cardid,vsid,dept_id,need_display,card_id.c_str(),vehicle_category_id,vehicle_id,card_type_id);
			map.insert({cardid,clb});
		}

		return map;
	}

	std::unordered_map<uint64_t,std::shared_ptr<card_location_base>> load_person(int64_t id64,const std::string&strategy)
	{
		std::string sql = "SELECT s.staff_id, s.card_id, c.card_type_id, s.dept_id, s.group_id, s.occupation_id, \
						   ol.occupation_level_id,s.worktype_id,s.need_display,s.work_line\
							,ds.name as staffer_name,dd.name as dept_name\
						   FROM dat_staff_extend s \
						   LEFT JOIN dat_staff ds ON ds.staff_id = s.staff_id \
			               LEFT JOIN dat_dept dd ON dd.dept_id = s.dept_id \
						   LEFT JOIN dat_card c ON s.card_id = c.card_id \
						   LEFT JOIN dat_occupation o ON s.occupation_id = o.occupation_id \
						   LEFT JOIN dat_occupation_level ol ON ol.occupation_level_id = o.occupation_level_id \
						   WHERE s.duty_id = 0 AND c.state_id = 0";

		std::unordered_map<uint64_t,std::shared_ptr<card_location_base>> map;
		std::string card_id_str = tool_other::to13str(id64);
		if(-1 == id64)
		{
			sql.append(";");
		}
		else
		{
			sql.append(" AND s.card_id = '");
			sql.append(card_id_str);
			sql.append("';");
		}

		std::string Error;
		YADB::CDBResultSet DBRes;
		sDBConnPool.Query(sql.c_str(),DBRes,Error);
		int nCount = DBRes.GetRecordCount( Error );
		if (nCount < 1)
		{
			log_error("增加或修改失败，数据库中找不到: sql", sql.c_str());
			return map;
		}

		while ( DBRes.GetNextRecod(Error) )
		{
			unsigned int staff_id  = 0;
			DBRes.GetField( "staff_id",staff_id, Error );

			std::string card_id;
			DBRes.GetField( "card_id",card_id, Error );

			uint32_t vsid = atoi(card_id.substr(3).c_str());

			unsigned int card_type_id  = 0;
			DBRes.GetField( "card_type_id",card_type_id, Error );

			int dept_id = 0;
			DBRes.GetField( "dept_id",dept_id, Error );

			int group_id = 0;
			DBRes.GetField( "group_id",group_id, Error );

			int occupation_id = 0;
			DBRes.GetField( "occupation_id",occupation_id, Error );

			int occupation_level_id = 0;
			DBRes.GetField( "occupation_level_id",occupation_level_id, Error );

			int need_display = 0;
			DBRes.GetField( "need_display",need_display, Error );

			int work_line = 0;
			DBRes.GetField( "work_line",work_line, Error );

			std::string staffer_name;
			DBRes.GetField( "staffer_name",staffer_name, Error );

			std::string dept_name;
			DBRes.GetField( "dept_name",dept_name, Error );

			int worktype_id;
			DBRes.GetField( "worktype_id",worktype_id, Error );

            std::shared_ptr<card_location_base> clb =
                    std::make_shared<person>(strategy,vsid,need_display,card_type_id,dept_id,occupation_level_id,staff_id,work_line,staffer_name,dept_name,worktype_id);
            uint64_t cardid = tool_other::type_id_to_u64(card_type_id,vsid);
            log_info("cardId:%llu,id:%d dept_id:%d,need_display:%d,card:%s:work_line:%d,staff_id:%d,type:%d,staffer_name:%s,dept_name:%s",
                     cardid,vsid,dept_id,need_display,card_id.c_str(),work_line,staff_id,card_type_id,staffer_name.c_str(),dept_name.c_str());
			map.insert({cardid,clb});
		}
		return map;
	}
}
