
#include "dispatch_command.h"
#include <google/protobuf/text_format.h>
#include "../tool/time_tool.h"
#include "dispatch_manager.h"


Dispatch_command::Dispatch_command()
{
	m_dispatch_command_status = E_DISPATCH_COMMAND_UNKNOW;
	m_unit = 0;
	m_device_floor = 0;
	m_dispatch_process_type = Common_data::DISPATCH_PROCESS_TYPE_UNKNOW;
	m_car_type = Common_data::Car_type::UNKNOW_CAR_TYPE;

	m_dispatch_space_info_count = 0;

//	m_wait_park_count = 0;
//	m_wait_pick_count = 0;
}

Dispatch_command::~Dispatch_command()
{

}

#ifdef SHANGGUJIE_PROJECT_PROJECT
//存车指令入队, 检查节点直接通过rabbitmq发送给调度, 调度接受后数据库指令入队,
Error_manager Dispatch_command::insert_park_command(park_table park_command)
{
	Dispatch_command_info t_dispatch_command_info;
	t_dispatch_command_info.m_car_number = park_command.car_number();
	t_dispatch_command_info.m_primary_key = park_command.primary_key();
	t_dispatch_command_info.m_unit = park_command.unit_id();
	t_dispatch_command_info.m_terminal_id = park_command.terminal_id();
	//queue_id 当前之间的总秒, (后续排序的权重参数是 搬运器和起点的距离 用户等待时间  ) (当前之间的总秒, 系统时间单位是纳秒, 除以10^9转化为秒)
	t_dispatch_command_info.m_queue_id = std::chrono::system_clock::now().time_since_epoch().count()/1000000000;
	t_dispatch_command_info.m_type = 1;//1表示存车, 2表示取车
	t_dispatch_command_info.m_statu = 0;//0表示排队中, 1表示运行中, 2表示完成, 3表示故障
	t_dispatch_command_info.m_import_id = get_passway_id(park_command.terminal_id(), 1);//
	t_dispatch_command_info.m_export_id = 0;//出口id, 存车时不写
	t_dispatch_command_info.m_height = park_command.entrance_measure_info().height();
	t_dispatch_command_info.m_space_info_msg;//入队时不写, 后续运行时补全
	t_dispatch_command_info.m_measure_info_msg = park_command.entrance_measure_info();
	t_dispatch_command_info.m_car_number_info_msg = park_command.car_number_info();


	char insert_command_queue_sql[DB_SQL_SIZE];
	memset(insert_command_queue_sql, 0, DB_SQL_SIZE);
	sprintf(insert_command_queue_sql, "INSERT INTO command_queue (car_number,primary_key, unit, terminal_id , queue_id, type, statu, import_id, export_id, height, space_info, measure_info, plate_color, plate_type, plate_confidence, recognition_time, plate_full_image, plate_clip_image) values ('%s','%s', %d, %d, %d, %d, %d, %d, %d, %f, '%s', '%s', '%s', '%s', %d, '%s', '%s', '%s')",
			t_dispatch_command_info.m_car_number.c_str(),
			t_dispatch_command_info.m_primary_key.c_str(),
			t_dispatch_command_info.m_unit,
			t_dispatch_command_info.m_terminal_id,
			t_dispatch_command_info.m_queue_id,
			t_dispatch_command_info.m_type,
			t_dispatch_command_info.m_statu,
			t_dispatch_command_info.m_import_id,
			t_dispatch_command_info.m_export_id,
			t_dispatch_command_info.m_height,
			t_dispatch_command_info.m_space_info_msg.DebugString().c_str(),
			t_dispatch_command_info.m_measure_info_msg.DebugString().c_str(),
			t_dispatch_command_info.m_car_number_info_msg.plate_color().c_str(),
			t_dispatch_command_info.m_car_number_info_msg.plate_type().c_str(),
			t_dispatch_command_info.m_car_number_info_msg.plate_confidence(),
			t_dispatch_command_info.m_car_number_info_msg.recognition_time().c_str(),
			t_dispatch_command_info.m_car_number_info_msg.plate_full_image().c_str(),
			t_dispatch_command_info.m_car_number_info_msg.plate_clip_image().c_str()
	);
	LOG(INFO) << " insert_command_queue_sql =  "<< insert_command_queue_sql << "  "  << this;
	Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_command_queue_sql);
	return ec;


	return Error_code::SUCCESS;
}
//存车指令入队, 检查节点直接通过rabbitmq发送给调度, 调度接受后数据库指令入队,
Error_manager Dispatch_command::insert_park_command(park_table park_command, Dispatch_command::Dispatch_space_info dispatch_space_info)
{
	Dispatch_command_info t_dispatch_command_info;
	t_dispatch_command_info.m_car_number = park_command.car_number();
	t_dispatch_command_info.m_primary_key = park_command.primary_key();
	t_dispatch_command_info.m_unit = park_command.unit_id();
	t_dispatch_command_info.m_terminal_id = park_command.terminal_id();
	//queue_id 当前之间的总秒, (后续排序的权重参数是 搬运器和起点的距离 用户等待时间  ) (当前之间的总秒, 系统时间单位是纳秒, 除以10^9转化为秒)
	t_dispatch_command_info.m_queue_id = std::chrono::system_clock::now().time_since_epoch().count()/1000000000;
	t_dispatch_command_info.m_type = 1;//1表示存车, 2表示取车
	t_dispatch_command_info.m_statu = 0;//0表示排队中, 1表示运行中, 2表示完成, 3表示故障
	t_dispatch_command_info.m_import_id = get_passway_id(park_command.terminal_id(), 1);//
	t_dispatch_command_info.m_export_id = 0;//出口id, 存车时不写
	t_dispatch_command_info.m_height = park_command.entrance_measure_info().height();
	t_dispatch_command_info.m_space_info_msg.set_id(dispatch_space_info.m_id);
	t_dispatch_command_info.m_space_info_msg.set_serial_id(dispatch_space_info.m_serial_id);
	t_dispatch_command_info.m_space_info_msg.set_table_id(dispatch_space_info.m_table_id);
	t_dispatch_command_info.m_space_info_msg.set_unit_id(dispatch_space_info.m_unit);
	t_dispatch_command_info.m_space_info_msg.set_floor(dispatch_space_info.m_floor);
	t_dispatch_command_info.m_space_info_msg.set_room_id(dispatch_space_info.m_room_id);
	t_dispatch_command_info.m_space_info_msg.set_height(dispatch_space_info.m_height);
	t_dispatch_command_info.m_measure_info_msg = park_command.entrance_measure_info();
	t_dispatch_command_info.m_car_number_info_msg = park_command.car_number_info();


	char insert_command_queue_sql[DB_SQL_SIZE];
	memset(insert_command_queue_sql, 0, DB_SQL_SIZE);
	sprintf(insert_command_queue_sql, "INSERT INTO command_queue (car_number,primary_key, unit, terminal_id , queue_id, type, statu, import_id, export_id, height, space_info, measure_info, plate_color, plate_type, plate_confidence, recognition_time, plate_full_image, plate_clip_image) values ('%s','%s', %d, %d, %d, %d, %d, %d, %d, %f, '%s', '%s', '%s', '%s', %d, '%s', '%s', '%s')",
			t_dispatch_command_info.m_car_number.c_str(),
			t_dispatch_command_info.m_primary_key.c_str(),
			t_dispatch_command_info.m_unit,
			t_dispatch_command_info.m_terminal_id,
			t_dispatch_command_info.m_queue_id,
			t_dispatch_command_info.m_type,
			t_dispatch_command_info.m_statu,
			t_dispatch_command_info.m_import_id,
			t_dispatch_command_info.m_export_id,
			t_dispatch_command_info.m_height,
			t_dispatch_command_info.m_space_info_msg.DebugString().c_str(),
			t_dispatch_command_info.m_measure_info_msg.DebugString().c_str(),
			t_dispatch_command_info.m_car_number_info_msg.plate_color().c_str(),
			t_dispatch_command_info.m_car_number_info_msg.plate_type().c_str(),
			t_dispatch_command_info.m_car_number_info_msg.plate_confidence(),
			t_dispatch_command_info.m_car_number_info_msg.recognition_time().c_str(),
			t_dispatch_command_info.m_car_number_info_msg.plate_full_image().c_str(),
			t_dispatch_command_info.m_car_number_info_msg.plate_clip_image().c_str()
	);
	LOG(INFO) << " insert_command_queue_sql =  "<< insert_command_queue_sql << "  "  << this;
	Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_command_queue_sql);
	return ec;


	return Error_code::SUCCESS;
}
//取车指令入队, 检查节点直接通过rabbitmq发送给调度, 调度接受后数据库指令入队,
Error_manager Dispatch_command::insert_pick_command(pick_table pick_command)
{
	Error_manager t_error;
	Dispatch_command_info t_dispatch_command_info;
	//只使用取车表单的唯一码, 其他信息去数据库查.
	t_dispatch_command_info.m_primary_key = pick_command.primary_key();
	t_error =  query_dispatch_space_for_primary_key(t_dispatch_command_info.m_primary_key, t_dispatch_command_info.m_car_number,  t_dispatch_command_info.m_space_info_msg, t_dispatch_command_info.m_measure_info_msg);

	t_dispatch_command_info.m_unit = t_dispatch_command_info.m_space_info_msg.unit_id();
	t_dispatch_command_info.m_terminal_id = pick_command.terminal_id();
	//queue_id 当前之间的总秒, (后续排序的权重参数是 搬运器和起点的距离 用户等待时间  ) (当前之间的总秒, 系统时间单位是纳秒, 除以10^9转化为秒)
	t_dispatch_command_info.m_queue_id = std::chrono::system_clock::now().time_since_epoch().count()/1000000000;
	t_dispatch_command_info.m_type = 2;//1表示存车, 2表示取车
	t_dispatch_command_info.m_statu = 0;//0表示排队中, 1表示运行中, 2表示完成, 3表示故障
	t_dispatch_command_info.m_import_id = 0;//入口id, 取车时不写
	t_dispatch_command_info.m_export_id = 0;//出口id, 入队时不写, 后续运行时补全
	t_dispatch_command_info.m_height = t_dispatch_command_info.m_measure_info_msg.height();


	char insert_command_queue_sql[DB_SQL_SIZE];
	memset(insert_command_queue_sql, 0, DB_SQL_SIZE);
	sprintf(insert_command_queue_sql, "INSERT INTO command_queue (car_number,primary_key, unit, terminal_id , queue_id, type, statu, import_id, export_id, height, space_info, measure_info) values ('%s','%s', %d, %d, %d, %d, %d, %d, %d, %f, '%s', '%s')",
			t_dispatch_command_info.m_car_number.c_str(),
			t_dispatch_command_info.m_primary_key.c_str(),
			t_dispatch_command_info.m_unit,
			t_dispatch_command_info.m_terminal_id,
			t_dispatch_command_info.m_queue_id,
			t_dispatch_command_info.m_type,
			t_dispatch_command_info.m_statu,
			t_dispatch_command_info.m_import_id,
			t_dispatch_command_info.m_export_id,
			t_dispatch_command_info.m_height,
			t_dispatch_command_info.m_space_info_msg.DebugString().c_str(),
			t_dispatch_command_info.m_measure_info_msg.DebugString().c_str()
	);
	Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_command_queue_sql);
	return ec;

	return Error_code::SUCCESS;
}
//取车指令入队, 检查节点直接通过rabbitmq发送给调度, 调度接受后数据库指令入队,
Error_manager Dispatch_command::insert_pick_command(pick_table pick_command, Dispatch_command::Dispatch_space_info dispatch_space_info)
{
	Error_manager t_error;
	Dispatch_command_info t_dispatch_command_info;
	//只使用取车表单的唯一码, 其他信息去数据库查.
	t_dispatch_command_info.m_primary_key = pick_command.primary_key();
	t_error =  query_dispatch_space_for_primary_key(t_dispatch_command_info.m_primary_key, t_dispatch_command_info.m_car_number,  t_dispatch_command_info.m_space_info_msg, t_dispatch_command_info.m_measure_info_msg);

	t_dispatch_command_info.m_unit = t_dispatch_command_info.m_space_info_msg.unit_id();
	t_dispatch_command_info.m_terminal_id = pick_command.terminal_id();
	//queue_id 当前之间的总秒, (后续排序的权重参数是 搬运器和起点的距离 用户等待时间  ) (当前之间的总秒, 系统时间单位是纳秒, 除以10^9转化为秒)
	t_dispatch_command_info.m_queue_id = std::chrono::system_clock::now().time_since_epoch().count()/1000000000;
	t_dispatch_command_info.m_type = 2;//1表示存车, 2表示取车
	t_dispatch_command_info.m_statu = 0;//0表示排队中, 1表示运行中, 2表示完成, 3表示故障
	t_dispatch_command_info.m_import_id = 0;//入口id, 取车时不写
	t_dispatch_command_info.m_export_id = 0;//出口id, 入队时不写, 后续运行时补全
	t_dispatch_command_info.m_height = t_dispatch_command_info.m_measure_info_msg.height();
	t_dispatch_command_info.m_space_info_msg.set_id(dispatch_space_info.m_id);
	t_dispatch_command_info.m_space_info_msg.set_serial_id(dispatch_space_info.m_serial_id);
	t_dispatch_command_info.m_space_info_msg.set_table_id(dispatch_space_info.m_table_id);
	t_dispatch_command_info.m_space_info_msg.set_unit_id(dispatch_space_info.m_unit);
	t_dispatch_command_info.m_space_info_msg.set_floor(dispatch_space_info.m_floor);
	t_dispatch_command_info.m_space_info_msg.set_room_id(dispatch_space_info.m_room_id);
	t_dispatch_command_info.m_space_info_msg.set_height(dispatch_space_info.m_height);


	char insert_command_queue_sql[DB_SQL_SIZE];
	memset(insert_command_queue_sql, 0, DB_SQL_SIZE);
	sprintf(insert_command_queue_sql, "INSERT INTO command_queue (car_number,primary_key, unit, terminal_id , queue_id, type, statu, import_id, export_id, height, space_info, measure_info) values ('%s','%s', %d, %d, %d, %d, %d, %d, %d, %f, '%s', '%s')",
			t_dispatch_command_info.m_car_number.c_str(),
			t_dispatch_command_info.m_primary_key.c_str(),
			t_dispatch_command_info.m_unit,
			t_dispatch_command_info.m_terminal_id,
			t_dispatch_command_info.m_queue_id,
			t_dispatch_command_info.m_type,
			t_dispatch_command_info.m_statu,
			t_dispatch_command_info.m_import_id,
			t_dispatch_command_info.m_export_id,
			t_dispatch_command_info.m_height,
			t_dispatch_command_info.m_space_info_msg.DebugString().c_str(),
			t_dispatch_command_info.m_measure_info_msg.DebugString().c_str()
	);
	Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_command_queue_sql);
	return ec;

	return Error_code::SUCCESS;
}

#endif//SHANGGUJIE_PROJECT_PROJECT


//调度开始前, 向数据库发送请求的相关操作, 输入 穿梭机所在的楼层, 调度id 0~2, 空闲出口id, 如果不是0,就表示有空闲的出口
Error_manager Dispatch_command::dispatch_request_to_sql(int device_floor, int dispatch_id, int outlet_ready)
{
	std::unique_lock<std::mutex> t_lock(m_lock);
	Error_manager t_error;

	m_device_floor = device_floor;
	m_dispatch_id = dispatch_id;
	m_unit = dispatch_id;
	m_outlet_ready = outlet_ready;

	//获取调度指令, 与数据库同步 command_queue
	t_error = query_all_dispatch_command(0);
	if ( t_error != Error_code::SUCCESS )
	{
		return t_error;
	}

	//排序
	if ( outlet_ready == 0 )
	{
		//对调度指令进行排序, 选出最优解, 只比较存车
		t_error = sort_dispatch_command_for_park();
	}
	else
	{
		//对调度指令进行排序, 选出最优解, 比较存车和取车
		t_error = sort_dispatch_command_for_total();
	}

	if ( t_error != Error_code::SUCCESS )
	{
		m_dispatch_process_type = Common_data::DISPATCH_PROCESS_TYPE_UNKNOW;

		//没有找到合适的指令就返回NODATA, 然后设么也不做
		return t_error;
	}
	else
	{
		if ( m_dispatch_command_map[m_car_number_optimal].m_type == 1 )//存车
		{

			//注注注注注意了
			//20230621, 排队存车进入指令表， 提前预约车位，提前写入指令表，车位表，记录表

			//判断车位是否提前预约，
			if ( m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.id() == 0 )
			{
				LOG(INFO) << " congxinfeipei================================================== congxinfeipei   "<< this;
				LOG(INFO) << " m_dispatch_command_map[m_car_number_optimal].m_space_info_msg " << m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.DebugString() << "  " << this;
				//如果没有预约， 那么再次分配一次。
				//查询空闲车位最优解,    存车指令 根据调度指令最优解 获取 空闲车位最优解 m_dispatch_space_info
				t_error = query_dispatch_space_optimal(m_dispatch_command_map[m_car_number_optimal].m_unit,
													   m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.height(),
													   m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.wheelbase(),
													   m_car_type,
													   m_dispatch_space_info[0]);
				if ( t_error != Error_code::SUCCESS )
				{
					update_command_queue_for_statu(3);
					return t_error;
				}
				else
				{
					//将最优解车位信息 补充到指令表
					m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_id(m_dispatch_space_info[0].m_id);
					m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_serial_id(m_dispatch_space_info[0].m_serial_id);
					m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_table_id(m_dispatch_space_info[0].m_table_id);
					m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_unit_id(m_dispatch_space_info[0].m_unit);
					m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_floor(m_dispatch_space_info[0].m_floor);
					m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_room_id(m_dispatch_space_info[0].m_room_id);
					m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_height(m_dispatch_space_info[0].m_height);
					m_dispatch_command_map[m_car_number_optimal].m_space_info_string = m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.DebugString();
				}
			}

			//完善数据库
			m_dispatch_process_type = Common_data::DISPATCH_PROCESS_STORE;
			//更新 指令队列, 根据车牌号 修改状态即可,  //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
			t_error = update_command_queue_for_statu(1);
			//更新 车位状态, 根据车位ID 写入车牌号即可
			t_error = update_parkspace_write_car_number();

			//制作存车表单
			t_error = create_park_table();

			t_error = check_record_for_no_primary_key(m_dispatch_command_map[m_car_number_optimal].m_primary_key);
			if ( t_error == SUCCESS )
			{
				//增加 记录表, 存车指令 完成后添加存车记录
				t_error =  insert_record_for_park_start();
			}
			else
			{
				//更新 记录表, 存车指令 完成后只更新存车开始时间
				t_error =  updata_record_for_park_start();
			}


			LOG(INFO) << " create_park_table 创建存车任务单 =  "<< m_park_table_msg.DebugString() << " --- " << this;
			return Error_code::SUCCESS;

		}
		else if ( m_dispatch_command_map[m_car_number_optimal].m_type == 2 )//取车
		{
			//注注注注注意了
			//取车自带车位信息,没有感测信息, 需要查询车辆表,补全车辆信息

			//查询 取车的车辆感测信息    取车指令 根据key 查询感测信息
//			t_error = query_dispatch_vehicle_for_primary_key();
			if ( t_error != Error_code::SUCCESS )
			{
				update_command_queue_for_statu(3);
				return t_error;
			}
			else
			{
				//补充出口id
				m_dispatch_command_map[m_car_number_optimal].m_export_id = outlet_ready;

				m_dispatch_process_type = Common_data::DISPATCH_PROCESS_PICKUP;

//				m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg = m_dispatch_vehicle_info.m_actually_measure_info_msg;

				//更新 指令队列, 根据车牌号 修改状态即可,  //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
				t_error = update_command_queue_for_statu(1);
				//制作取车表单
				t_error = create_pick_table(outlet_ready);
				//更新 记录表, 取车指令 完成后更新取车记录
				t_error =  updata_record_for_pick_start();

				LOG(INFO) << " create_pick_table 创建取车车任务单 =  "<< m_pick_table_msg.DebugString() << " --- " << this;
			}
		}
		else
		{
			return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
								 " fun error ");
		}
	}
	return Error_code::SUCCESS;
}

//调度完成后, 向数据库发送答复的相关操作
Error_manager Dispatch_command::dispatch_response_to_sql(Error_manager dispatch_result)
{
	std::unique_lock<std::mutex> t_lock(m_lock);
	Error_manager t_error;

	if ( dispatch_result == Error_code::SUCCESS )
	{
		//调度成功, 完善数据库
		if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_STORE )
		{
			LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_STORE 存车答复成功 =  "<< t_error << " --- " << this;
			//增加 车辆表, 存车指令 完成后添加车辆信息
//			t_error = insert_vehicle_for_car_number();
			//更新车牌图片信息,
			update_parkspace_write_car_number_info();
			//更新 指令队列, 根据车牌号 删除指令
			t_error = delete_command_queue_for_car_number();
			//增加 记录表, 存车指令 完成后添加存车记录
			t_error =  updata_record_for_park_end();
		}
		else if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_PICKUP )
		{
			LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_PICKUP 取车答复成功 =  "<< t_error << " --- " << this;

			//删除 车辆表, 取车指令 完成后删除车辆信息
//			t_error = delete_vehicle_for_car_number();
			//删除车牌图片信息,
			t_error = update_parkspace_clear_car_number_info();
			//更新 车位状态, 找到车牌号, 写NULL
			t_error = update_parkspace_clear_car_number();
			//更新 指令队列, 根据车牌号 修改状态即可,  //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
			t_error = update_command_queue_for_statu(2);
			//更新 记录表, 取车指令 完成后更新取车记录
			t_error =  updata_record_for_pick_end();
		}
		else if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REALLOCATE )
		{
			LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_REALLOCATE 改道答复成功 =  "<< t_error << " --- " << this;
			//增加 车辆表, 存车指令 完成后添加车辆信息
//			t_error = insert_vehicle_for_car_number();
			//如果多次重新分配, 需要把之前申请的车位清除,
			for (int i = 0; i < m_dispatch_space_info_count; ++i)
			{
				//如果多次重新分配, 需要把之前申请的车位清除  //更新 车位状态, 找到车牌号, 写NULL
				t_error = update_parkspace_clear_car_number_by_id(m_dispatch_space_info[i].m_id, 10);
			}
			m_dispatch_space_info_count = 0;
			//更新车牌图片信息,
			update_parkspace_write_car_number_info();
			//更新 指令队列, 根据车牌号 删除指令
			t_error = delete_command_queue_for_car_number();
			//增加 记录表, 存车指令 完成后添加存车记录
			t_error =  updata_record_for_park_end();
		}
		else if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REVOCATION )
		{
			LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_REVOCATION 撤销答复成功 =  "<< t_error << " --- " << this;

			//删除 车辆表, 取车指令 完成后删除车辆信息
//			t_error = delete_vehicle_for_car_number();
			//如果多次重新分配, 需要把之前申请的车位清除,
			for (int i = 0; i < m_dispatch_space_info_count; ++i)
			{
				//如果多次重新分配, 需要把之前申请的车位清除  //更新 车位状态, 找到车牌号, 写NULL
				t_error = update_parkspace_clear_car_number_by_id(m_dispatch_space_info[i].m_id, 10);
			}
			m_dispatch_space_info_count = 0;
			//删除车牌图片信息,
			t_error = update_parkspace_clear_car_number_info();
			//更新 车位状态, 找到车牌号, 写NULL
			t_error = update_parkspace_clear_car_number();
			//更新 指令队列, 根据车牌号 修改状态即可,  //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
			t_error = update_command_queue_for_statu(2);
//			LOG(INFO) << "llllllllllllllllllllllllllllllllllllllllll respons =  "<< t_error << " --- " << this;
			//更新 记录表, 取车指令 完成后更新取车记录, 撤销指令, 没有取车时间差
			t_error =  updata_record_for_pick_end_ex();
		}
		else
		{
			return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
								 " fun error ");
		}

		return t_error;
	}
	else
	{
		//调度失败, 回退 数据库
		if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_STORE )
		{
			LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_STORE 存车答复失败 =  "<< t_error << " --- " << this;
			//删除车牌图片信息,
			update_parkspace_clear_car_number_info();
			//更新 车位状态, 找到车牌号, 写NULL
			update_parkspace_clear_car_number();
			//更新 指令队列, 根据车牌号 修改状态即可,  //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
			update_command_queue_for_statu(3);
		}
		else if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_PICKUP )
		{
			LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_PICKUP 取车答复失败 =  "<< t_error << " --- " << this;
			//更新 指令队列, 根据车牌号 修改状态即可,  //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
			update_command_queue_for_statu(3);
		}
		else if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REALLOCATE )
		{
			LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_STORE 改道答复失败 =  "<< t_error << " --- " << this;
			//如果多次重新分配, 需要把之前申请的车位清除,
			for (int i = 0; i < m_dispatch_space_info_count; ++i)
			{
				//如果多次重新分配, 需要把之前申请的车位清除  //更新 车位状态, 找到车牌号, 写NULL
				t_error = update_parkspace_clear_car_number_by_id(m_dispatch_space_info[i].m_id, 10);
			}
			m_dispatch_space_info_count = 0;
			//删除车牌图片信息,
			update_parkspace_clear_car_number_info();
			//更新 车位状态, 找到车牌号, 写NULL
			update_parkspace_clear_car_number();
			//更新 指令队列, 根据车牌号 修改状态即可,  //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
			update_command_queue_for_statu(3);
		}
		else if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REVOCATION )
		{
			LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_PICKUP 撤销答复失败 =  "<< t_error << " --- " << this;
			//如果多次重新分配, 需要把之前申请的车位清除,
			for (int i = 0; i < m_dispatch_space_info_count; ++i)
			{
				//如果多次重新分配, 需要把之前申请的车位清除  //更新 车位状态, 找到车牌号, 写NULL
				t_error = update_parkspace_clear_car_number_by_id(m_dispatch_space_info[i].m_id, 10);
			}
			m_dispatch_space_info_count = 0;
			//删除车牌图片信息,
			update_parkspace_clear_car_number_info();
			//更新 车位状态, 找到车牌号, 写NULL
			update_parkspace_clear_car_number();
			//更新 指令队列, 根据车牌号 修改状态即可,  //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
			update_command_queue_for_statu(3);
		}
		else
		{
			return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
								 " fun error ");
		}
	}

	return Error_code::SUCCESS;
}

//调度 , 向数据库 重新分配车位,
Error_manager Dispatch_command::dispatch_reallocate_to_sql(Common_data::Car_type reallocate_car_type, int outlet_ready)
{
	std::unique_lock<std::mutex> t_lock(m_lock);
	Error_manager t_error;
	Error_manager t_error2;

	m_outlet_ready = outlet_ready;


	//重新申请车位
	//查询空闲车位最优解,    存车指令 根据调度指令最优解 获取 空闲车位最优解
	m_dispatch_space_info_count  = m_dispatch_space_info_count +1;
	t_error = query_dispatch_space_optimal_ex(reallocate_car_type, m_dispatch_space_info[m_dispatch_space_info_count]);




	if ( t_error != Error_code::SUCCESS )
	{
		//没找到车位, 就把车放到出口
		if ( outlet_ready != 0 )
		{
			m_dispatch_process_type = Common_data::DISPATCH_PROCESS_REVOCATION;

			//补充出口id
			m_dispatch_command_map[m_car_number_optimal].m_export_id = outlet_ready;

			m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_id(0);
			m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_serial_id(0);
			m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_table_id(0);
			m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_unit_id(0);
			m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_floor(0);
			m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_room_id(0);
			m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_height(0);
			m_dispatch_command_map[m_car_number_optimal].m_space_info_string = m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.DebugString();

			//更新 指令队列, 根据车牌号 修改状态即可,  //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
//			t_error = update_command_queue_for_statu(1);
			//制作取车表单
			t_error = create_pick_table(outlet_ready);
			LOG(INFO) << " create_pick_table 撤销存车, 取车到出口任务单 =  "<< m_pick_table_msg.DebugString() << " --- " << this;

			return t_error;
		}
		else
		{
			//无限等待, 等待出口空闲
			return Error_code::NODATA;
		}
	}
	else
	{
		m_dispatch_process_type = Common_data::DISPATCH_PROCESS_REALLOCATE;

		//找到新的车位, 重新执行任务
		m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_id(m_dispatch_space_info[m_dispatch_space_info_count].m_id);
		m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_serial_id(m_dispatch_space_info[m_dispatch_space_info_count].m_serial_id);
		m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_table_id(m_dispatch_space_info[m_dispatch_space_info_count].m_table_id);
		m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_unit_id(m_dispatch_space_info[m_dispatch_space_info_count].m_unit);
		m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_floor(m_dispatch_space_info[m_dispatch_space_info_count].m_floor);
		m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_room_id(m_dispatch_space_info[m_dispatch_space_info_count].m_room_id);
		m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_height(m_dispatch_space_info[m_dispatch_space_info_count].m_height);
		m_dispatch_command_map[m_car_number_optimal].m_space_info_string = m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.DebugString();
		//更新 车位状态, 根据车位ID 写入车牌号即可
		t_error = update_parkspace_write_car_number();
		//更新 指令队列, 根据车牌号 修改状态即可,  //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
//		t_error = update_command_queue_for_statu(1);
		//制作存车表单

//		LOG(INFO) << " zxcxzczxczxczxczxczxczxczxczxczxcxzcx =  "<< m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.id() << " --- " << this;
		t_error = create_park_table();

		LOG(INFO) << " create_park_table 改道存车, 存车新车位任务单 =  "<< m_park_table_msg.DebugString() << " --- " << this;
	}

	return Error_code::SUCCESS;
}


//检查出口是否空闲, 检查指令队列的取车完成的出口id是否存在, 不存在就是空闲,返回成功
Error_manager Dispatch_command::check_export_id_is_ready(int export_id)
{
	std::unique_lock<std::mutex> t_lock(m_lock);
	//检查指令队列, 查询指定单元的取车完成状态指令的 出口id是否存在
	char check_export_id_sql[DB_SQL_SIZE];
	memset(check_export_id_sql, 0, DB_SQL_SIZE);
	sprintf(check_export_id_sql,"select * from command_queue where statu = 2 and type = 2 and export_id = %d",export_id);
	boost::shared_ptr<sql::ResultSet>  tp_result = nullptr;
	Error_manager t_error = Database_controller::get_instance_pointer()->sql_query(check_export_id_sql, tp_result);

	if(t_error == Error_code::SUCCESS)
	{
		if (tp_result == nullptr)
		{
			return DB_RESULT_SET_EMPTY;
		}

		//如果存在, 就报错,   不存在就是空闲,返回成功
		if ( tp_result->next() )
		{
			return Error_manager(Error_code::DB_QUERY_OUTLET_OCCUPY, Error_level::MINOR_ERROR,
								 " check_export_id_is_ready fun error, 数据库 查询出口被占用 ");
		}
		else
		{
			return Error_code::SUCCESS;
		}
	}
	else
	{
		return t_error;
	}
	return Error_code::SUCCESS;
}

//检查出口取车是否完成, 指定的出口有取车完成指令就返回成功, 否则报错.
Error_manager Dispatch_command::check_pickup_is_finish(int unit_id, int export_id)
{
	std::unique_lock<std::mutex> t_lock(m_lock);
	//检查指令队列, 查询指定单元的取车完成状态指令的 出口id是否存在
	char check_export_id_sql[DB_SQL_SIZE];
	memset(check_export_id_sql, 0, DB_SQL_SIZE);
	sprintf(check_export_id_sql,"select * from command_queue where statu = 2 and type = 2 and unit = %d and export_id = %d",unit_id, export_id);
	boost::shared_ptr<sql::ResultSet>  tp_result = nullptr;
	Error_manager t_error = Database_controller::get_instance_pointer()->sql_query(check_export_id_sql, tp_result);

	if(t_error == Error_code::SUCCESS)
	{
		if (tp_result == nullptr)
		{
			return DB_RESULT_SET_EMPTY;
		}

		//如果存在, 就报错,   不存在就是空闲,返回成功
		if ( tp_result->next() )
		{
			return Error_code::SUCCESS;
		}
		else
		{
			return Error_manager(Error_code::DB_QUERY_OUTLET_OCCUPY, Error_level::MINOR_ERROR,
								 " check_export_id_is_ready fun error, 数据库 查询出口被占用 ");
		}
	}
	else
	{
		return t_error;
	}
	return Error_code::SUCCESS;
}

//检查入口存车是否开始, 指定的入口有存车开始指令就返回成功, 否则报错.
Error_manager Dispatch_command::check_park_is_start(int import_id)
{
	std::unique_lock<std::mutex> t_lock(m_lock);
	//检查指令队列, 查询指定单元的取车完成状态指令的 出口id是否存在
	char check_export_id_sql[DB_SQL_SIZE];
	memset(check_export_id_sql, 0, DB_SQL_SIZE);
	sprintf(check_export_id_sql,"select * from command_queue where type = 1 and import_id = %d and (statu=0 or statu =1)",import_id);
	boost::shared_ptr<sql::ResultSet>  tp_result = nullptr;
	Error_manager t_error = Database_controller::get_instance_pointer()->sql_query(check_export_id_sql, tp_result);

	if(t_error == Error_code::SUCCESS)
	{
		if (tp_result == nullptr)
		{
			return DB_RESULT_SET_EMPTY;
		}

		//如果存在, 就报错,   不存在就是空闲,返回成功
		if ( tp_result->next() )
		{
			return Error_code::SUCCESS;
		}
		else
		{
			return Error_manager(Error_code::DB_QUERY_OUTLET_OCCUPY, Error_level::MINOR_ERROR,
								 " check_export_id_is_ready fun error, 数据库 查询出口被占用 ");
		}
	}
	else
	{
		return t_error;
	}
	return Error_code::SUCCESS;
}

//获取指定入口的 存车流程的 唯一码,
Error_manager Dispatch_command::get_primary_key_for_store(int import_id, std::string & primary_key )
{
	std::unique_lock<std::mutex> t_lock(m_lock);
	//检查指令队列, 查询指定单元的取车完成状态指令的 出口id是否存在
	char check_export_id_sql[DB_SQL_SIZE];
	memset(check_export_id_sql, 0, DB_SQL_SIZE);
	sprintf(check_export_id_sql,"select * from command_queue where type = 1 and import_id = %d and (statu=0 or statu =1)",import_id);
	boost::shared_ptr<sql::ResultSet>  tp_result = nullptr;
	Error_manager t_error = Database_controller::get_instance_pointer()->sql_query(check_export_id_sql, tp_result);

	if(t_error == Error_code::SUCCESS)
	{
		if (tp_result == nullptr)
		{
			return DB_RESULT_SET_EMPTY;
		}

		//如果存在, 就报错,   不存在就是空闲,返回成功
		if ( tp_result->next() )
		{
			char buf[DB_SQL_SIZE];
			memset(buf, 0, DB_SQL_SIZE);
			try
			{
				//解析数据
				primary_key = tp_result->getString("primary_key");
			}
			catch (sql::SQLException &e)
			{
				/* Use what() (derived from std::runtime_error) to fetch the error message */
				sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
				return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
			}
			catch (std::runtime_error &e)
			{
				sprintf(buf, "# ERR: %s\n ERR: runtime_error in  %s ", e.what(), __FILE__);
				return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
			}
			return Error_code::SUCCESS;
		}
		else
		{
			return Error_manager(Error_code::DB_QUERY_OUTLET_OCCUPY, Error_level::MINOR_ERROR,
								 " check_export_id_is_ready fun error, 数据库 查询出口被占用 ");
		}
	}
	else
	{
		return t_error;
	}
	return Error_code::SUCCESS;
}

//删除 指令队列, 根据出口编号
Error_manager Dispatch_command::delete_command_queue_for_export_id(int unit_id, int export_id)
{
	std::unique_lock<std::mutex> t_lock(m_lock);

	char delete_command_sql[DB_SQL_SIZE];
	memset(delete_command_sql, 0, DB_SQL_SIZE);
	sprintf(delete_command_sql, "delete from command_queue where statu = 2 and type = 2 and unit = %d and export_id = %d ",
			unit_id, export_id );
	Error_manager ec = Database_controller::get_instance_pointer()->sql_delete(delete_command_sql);
	return ec;
}

//添加plc数据储存
Error_manager Dispatch_command::insert_plc_data(std::string plc_data, float hearbeat)
{
	std::unique_lock<std::mutex> t_lock(m_lock);
	char insert_vehicle_sql[4096];
	memset(insert_vehicle_sql, 0, 4096);
	sprintf(insert_vehicle_sql, "INSERT INTO plc_data (data, hearbeat) values ('%s',%f)",
			plc_data.c_str(), hearbeat  );
	Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_vehicle_sql);
	std::cout << " huli test :::: " << " ec = " << ec << std::endl;
	return ec;
}


//查询 取车的车位    取车指令 根据车牌号 查询对应的车位
Error_manager Dispatch_command::query_dispatch_space_for_car_number(std::string car_number, parkspace_info & parkspace_info)
{
//查询车位表
	char query_parkspace_sql[DB_SQL_SIZE];
	memset(query_parkspace_sql, 0, DB_SQL_SIZE);
	int t_statu = 0; //车位状态, 0可用, 1故障
	sprintf(query_parkspace_sql,"select * from space where car_number = '%s' ",car_number.c_str());
//	std::cout<<"query_parkspace_sql = "<<query_parkspace_sql<<std::endl;
	boost::shared_ptr<sql::ResultSet>  tp_result = nullptr;
	Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
	if(ec == SUCCESS)
	{
		if(tp_result == nullptr)
		{
			return DB_RESULT_SET_EMPTY;
		}
		//只取第一条结果, 默认是id最小的,
		if (tp_result->next())
		{
			char buf[DB_SQL_SIZE];
			memset(buf, 0, DB_SQL_SIZE);
			try
			{
				//解析数据
				parkspace_info.set_id(tp_result->getInt("id"));
				parkspace_info.set_serial_id(tp_result->getInt("serial_id"));
				parkspace_info.set_table_id( tp_result->getInt("table_id"));
				parkspace_info.set_unit_id( tp_result->getInt("unit"));
				parkspace_info.set_floor( tp_result->getInt("floor"));
				parkspace_info.set_room_id( tp_result->getInt("room_id"));
				parkspace_info.set_height(tp_result->getDouble("height"));
			}
			catch (sql::SQLException &e)
			{
				/* Use what() (derived from std::runtime_error) to fetch the error message */
				sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
				return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
			}
			catch (std::runtime_error &e)
			{
				sprintf(buf, "# ERR: %s\n ERR: runtime_error in  %s ", e.what(), __FILE__);
				return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
			}
		}
		else
		{
			return Error_manager(Error_code::DB_QUERY_NOT_DATA, Error_level::MINOR_ERROR,
								 "数据库未查询到数据 Parkspace_operating_function::query_one_parkspace_with_license error ");

		}
		return SUCCESS;
	}
	else
	{
		return ec;
	}
	return SUCCESS;
}


//查询 取车的车位    取车指令 根据车牌号 查询对应的车位
Error_manager Dispatch_command::query_dispatch_space_for_primary_key(std::string primary_key,std::string &car_number, parkspace_info & parkspace_info, measure_info & measure_info)
{
//查询车位表
	char query_parkspace_sql[DB_SQL_SIZE];
	memset(query_parkspace_sql, 0, DB_SQL_SIZE);
	int t_statu = 0; //车位状态, 0可用, 1故障
	sprintf(query_parkspace_sql,"select * from space where primary_key = '%s' ",primary_key.c_str());
//	std::cout<<"query_parkspace_sql = "<<query_parkspace_sql<<std::endl;
//	LOG(INFO) << " ++++++++++++++++++++++++++++++++++++ query_parkspace_sql =  "<< query_parkspace_sql << " --- " << this;
	boost::shared_ptr<sql::ResultSet>  tp_result = nullptr;
	Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
	if(ec == SUCCESS)
	{
		if(tp_result == nullptr)
		{
			return DB_RESULT_SET_EMPTY;
		}
		//只取第一条结果, 默认是id最小的,
		if (tp_result->next())
		{
			char buf[DB_SQL_SIZE];
			memset(buf, 0, DB_SQL_SIZE);
			try
			{
				//解析数据
				car_number = tp_result->getString("car_number");

				std::string parkspace_info_string = tp_result->getString("space_info");
				if(! google::protobuf::TextFormat::ParseFromString(parkspace_info_string, &parkspace_info))
				{
					return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
										 "ParseFromString fun error, m_space_info ");
				}

				std::string measure_info_string = tp_result->getString("measure_info");
				if(! google::protobuf::TextFormat::ParseFromString(measure_info_string, &measure_info))
				{
					return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
										 "ParseFromString fun error, m_space_info ");
				}
			}
			catch (sql::SQLException &e)
			{
				/* Use what() (derived from std::runtime_error) to fetch the error message */
				sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
				return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
			}
			catch (std::runtime_error &e)
			{
				sprintf(buf, "# ERR: %s\n ERR: runtime_error in  %s ", e.what(), __FILE__);
				return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
			}
		}
		else
		{
			return Error_manager(Error_code::DB_QUERY_NOT_DATA, Error_level::MINOR_ERROR,
								 "数据库未查询到数据 Parkspace_operating_function::query_one_parkspace_with_license error ");

		}
		return SUCCESS;
	}
	else
	{
		return ec;
	}
	return SUCCESS;
}

//查询 取车的车辆感测信息    取车指令 根据key 查询感测信息
Error_manager Dispatch_command::query_dispatch_vehicle_for_primary_key(std::string primary_key, measure_info & measure_info)
{
	//查询 车辆表
	char query_parkspace_sql[DB_SQL_SIZE];
	memset(query_parkspace_sql, 0, DB_SQL_SIZE);
	int t_statu = 0; //车位状态, 0可用, 1故障
	sprintf(query_parkspace_sql,"select * from vehicle where primary_key = '%s' ",primary_key.c_str());
//	std::cout<<"query_parkspace_sql = "<<query_parkspace_sql<<std::endl;
	boost::shared_ptr<sql::ResultSet>  tp_result = nullptr;
	Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
	if(ec == SUCCESS)
	{
		if(tp_result == nullptr)
		{
			return DB_RESULT_SET_EMPTY;
		}
		//只取第一条结果, 默认是id最小的,
		if (tp_result->next())
		{
			char buf[DB_SQL_SIZE];
			memset(buf, 0, DB_SQL_SIZE);
			try
			{
				//解析数据
				std::string measure_info_string = tp_result->getString("actually_measure_info");

				//取车, 数据库特有车位信息
				if(! google::protobuf::TextFormat::ParseFromString(measure_info_string, &measure_info))
				{
					return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
										 "ParseFromString fun error, m_space_info ");
				}
			}
			catch (sql::SQLException &e)
			{
				/* Use what() (derived from std::runtime_error) to fetch the error message */
				sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
				return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
			}
			catch (std::runtime_error &e)
			{
				sprintf(buf, "# ERR: %s\n ERR: runtime_error in  %s ", e.what(), __FILE__);
				return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
			}
		}
		else
		{
			return Error_manager(Error_code::DB_QUERY_NOT_DATA, Error_level::MINOR_ERROR,
								 "数据库未查询到数据 Parkspace_operating_function::query_one_parkspace_with_license error ");

		}
		return SUCCESS;
	}
	else
	{
		return ec;
	}
	return SUCCESS;
}

//获取调度指令, 与数据库同步 command_queue, statu指令状态, 0:排队等待, 1正在运行, 2:已经完成
Error_manager Dispatch_command::query_all_dispatch_command(int statu)
{
	//从command_queue获取所有排队的指令,
	char query_all_dispatch_command_sql[DB_SQL_SIZE];
	memset(query_all_dispatch_command_sql, 0, DB_SQL_SIZE);
	sprintf(query_all_dispatch_command_sql,"select * from command_queue where statu = %d and unit = %d",statu, m_unit);
	boost::shared_ptr<sql::ResultSet>  tp_result = nullptr;
	Error_manager t_error = Database_controller::get_instance_pointer()->sql_query(query_all_dispatch_command_sql, tp_result);

	m_dispatch_command_map.clear();
	unsigned short			t_wait_small_park_count = 0;			//排队小车存车的数量
	unsigned short			t_wait_middle_park_count = 0;			//排队中车存车的数量
	unsigned short			t_wait_big_park_count = 0;				//排队大车存车的数量
	unsigned short			t_wait_total_park_count = 0;			//排队总车存车的数量
	unsigned short			t_wait_small_pick_count = 0;			//排队小车存车的数量
	unsigned short			t_wait_middle_pick_count = 0;			//排队中车存车的数量
	unsigned short			t_wait_big_pick_count = 0;				//排队大车存车的数量
	unsigned short			t_wait_total_pick_count = 0;			//排队总车存车的数量

	if(t_error == Error_code::SUCCESS)
	{
		if (tp_result == nullptr)
		{
			return DB_RESULT_SET_EMPTY;
		}



		//循环检查所有行
		while (tp_result->next())
		{
			Dispatch_command_info t_dispatch_command_info;
			char t_error_buf[DB_SQL_SIZE];
			memset(t_error_buf, 0, DB_SQL_SIZE);
			try
			{
				//解析数据
				t_dispatch_command_info.m_car_number = tp_result->getString("car_number");
				t_dispatch_command_info.m_primary_key = tp_result->getString("primary_key");
				t_dispatch_command_info.m_unit = tp_result->getInt("unit");
				t_dispatch_command_info.m_terminal_id = tp_result->getInt("terminal_id");
				t_dispatch_command_info.m_queue_id = tp_result->getInt("queue_id");
				t_dispatch_command_info.m_type = tp_result->getInt("type");
				t_dispatch_command_info.m_statu = tp_result->getInt("statu");
				t_dispatch_command_info.m_export_id = tp_result->getInt("export_id");
				t_dispatch_command_info.m_import_id = tp_result->getInt("import_id");
				t_dispatch_command_info.m_height = tp_result->getDouble("height");
				t_dispatch_command_info.m_space_info_string = tp_result->getString("space_info");
				t_dispatch_command_info.m_measure_info_string = tp_result->getString("measure_info");

				t_dispatch_command_info.m_car_number_info_msg.set_plate_color(tp_result->getString("plate_color") );
				t_dispatch_command_info.m_car_number_info_msg.set_plate_type(tp_result->getString("plate_type") );
				t_dispatch_command_info.m_car_number_info_msg.set_plate_confidence(tp_result->getInt("plate_confidence") );
				t_dispatch_command_info.m_car_number_info_msg.set_recognition_time(tp_result->getString("recognition_time") );
				t_dispatch_command_info.m_car_number_info_msg.set_plate_full_image(tp_result->getString("plate_full_image") );
				t_dispatch_command_info.m_car_number_info_msg.set_plate_clip_image(tp_result->getString("plate_clip_image") );
				t_dispatch_command_info.m_car_number_info_string = t_dispatch_command_info.m_car_number_info_msg.DebugString();

				//存车, 数据库特有 雷达信息,  没有车位信息, 需要根据车高,再去车位表申请车位
				if(! google::protobuf::TextFormat::ParseFromString(t_dispatch_command_info.m_measure_info_string, &t_dispatch_command_info.m_measure_info_msg))
				{
					return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
										 "ParseFromString fun error, m_measure_info ");
				}
				//取车, 数据库特有车位信息
				if(! google::protobuf::TextFormat::ParseFromString(t_dispatch_command_info.m_space_info_string, &t_dispatch_command_info.m_space_info_msg))
				{
					return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
										 "ParseFromString fun error, m_space_info ");
				}
				//取车, 数据库特有 车牌图片信息
//				if(! google::protobuf::TextFormat::ParseFromString(t_dispatch_command_info.m_car_number_info_string, &t_dispatch_command_info.m_car_number_info_msg))
//				{
//					return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
//										 "ParseFromString fun error, m_space_info ");
//				}
//				if(! t_dispatch_command_info.m_car_number_info_msg.ParseFromString(t_dispatch_command_info.m_car_number_info_string) )
//				{
//					return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
//										 "ParseFromString fun error, m_space_info ");
//				}



				//m_type指令类型, 0无效, 1存车, 2取车,
				if ( t_dispatch_command_info.m_type == 1)
				{
					t_dispatch_command_info.m_useless_distance = m_device_floor-1;
				}
				else if ( t_dispatch_command_info.m_type == 2 )
				{
					t_dispatch_command_info.m_useless_distance = t_dispatch_command_info.m_space_info_msg.floor()-m_device_floor;
				}
				else
				{
					return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
										 "command_queue m_type error ");
				}

				if ( t_dispatch_command_info.m_useless_distance <0 )
				{
					t_dispatch_command_info.m_useless_distance = 0-t_dispatch_command_info.m_useless_distance;
				}
				m_dispatch_command_map[t_dispatch_command_info.m_car_number] = t_dispatch_command_info;

				//huli 20230612, 统计排队信息
				if ( t_dispatch_command_info.m_type == 1 )
				{
					if ( t_dispatch_command_info.m_height >= 1.48-0.001 &&  t_dispatch_command_info.m_height <= 1.48+0.001)
					{
						t_wait_small_park_count++;
					}
					if ( t_dispatch_command_info.m_height >= 1.50-0.001 &&  t_dispatch_command_info.m_height <= 1.50+0.001)
					{
						t_wait_middle_park_count++;
					}
					if ( t_dispatch_command_info.m_height >= 2.05-0.001 &&  t_dispatch_command_info.m_height <= 2.05+0.001)
					{
						t_wait_big_park_count++;
					}
				}
				if ( t_dispatch_command_info.m_type == 2 )
				{
					if ( t_dispatch_command_info.m_height >= 1.48-0.001 &&  t_dispatch_command_info.m_height <= 1.48+0.001)
					{
						t_wait_small_pick_count++;
					}
					if ( t_dispatch_command_info.m_height >= 1.50-0.001 &&  t_dispatch_command_info.m_height <= 1.50+0.001)
					{
						t_wait_middle_pick_count++;
					}
					if ( t_dispatch_command_info.m_height >= 2.05-0.001 &&  t_dispatch_command_info.m_height <= 2.05+0.001)
					{
						t_wait_big_pick_count++;
					}
				}
			}
			catch (sql::SQLException &e)
			{
				/* Use what() (derived from std::runtime_error) to fetch the error message */
				sprintf(t_error_buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(),
						e.getSQLState().c_str());
				return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, t_error_buf);
			}
			catch (std::runtime_error &e)
			{
				sprintf(t_error_buf, "# ERR: %s\n ERR: runtime_error in  %s ", e.what(), __FILE__);
				return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, t_error_buf);
			}
		}

		Dispatch_manager::get_instance_references().m_dispatch_plc.m_dispatch_plc_status_from_manager_to_plc.m_wait_small_park_count = t_wait_small_park_count;
		Dispatch_manager::get_instance_references().m_dispatch_plc.m_dispatch_plc_status_from_manager_to_plc.m_wait_middle_park_count = t_wait_middle_park_count;
		Dispatch_manager::get_instance_references().m_dispatch_plc.m_dispatch_plc_status_from_manager_to_plc.m_wait_big_park_count = t_wait_big_park_count;
		Dispatch_manager::get_instance_references().m_dispatch_plc.m_dispatch_plc_status_from_manager_to_plc.m_wait_total_park_count = t_wait_small_park_count + t_wait_middle_park_count + t_wait_big_park_count;
		Dispatch_manager::get_instance_references().m_dispatch_plc.m_dispatch_plc_status_from_manager_to_plc.m_wait_small_pick_count = t_wait_small_pick_count;
		Dispatch_manager::get_instance_references().m_dispatch_plc.m_dispatch_plc_status_from_manager_to_plc.m_wait_middle_pick_count = t_wait_middle_pick_count;
		Dispatch_manager::get_instance_references().m_dispatch_plc.m_dispatch_plc_status_from_manager_to_plc.m_wait_big_pick_count = t_wait_big_pick_count;
		Dispatch_manager::get_instance_references().m_dispatch_plc.m_dispatch_plc_status_from_manager_to_plc.m_wait_total_pick_count = t_wait_small_pick_count + t_wait_middle_pick_count + t_wait_big_pick_count;
		return SUCCESS;
	}
	else
	{
		return t_error;
	}

	return Error_code::SUCCESS;
}

//对调度指令进行排序, 选出最优解, 比较存车和取车
Error_manager Dispatch_command::sort_dispatch_command_for_total()
{
	int t_optimal_loss=0x7fffffff;		//最优loss值
	m_car_number_optimal.clear();

	//筛选最优任务
	for (auto iter = m_dispatch_command_map.begin(); iter != m_dispatch_command_map.end(); ++iter)
	{
		//loss = 空跑路程 + 排队编号, 求最小值
		int t_current_loss = iter->second.m_useless_distance * SORT_LOSS_RATIO + iter->second.m_queue_id;
		if ( Dispatch_manager::get_instance_references().m_dispatch_manager_status_last == Dispatch_manager::E_DISPATCH_MANAGER_STORE)
		{
			if ( t_optimal_loss > t_current_loss && iter->second.m_type == 2)
			{
				t_optimal_loss = t_current_loss;
				m_car_number_optimal = iter->first;
//				std::cout << " huli test :::: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << " find park = " << 1 << std::endl;
			}
		}
		else
		{
			if ( t_optimal_loss > t_current_loss && iter->second.m_type == 1)
			{
				t_optimal_loss = t_current_loss;
				m_car_number_optimal = iter->first;
//				std::cout << " huli test :::: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << " find pick = " << 2 << std::endl;
			}
		}
	}
	std::cout << " huli test :::: " << " sort_dispatch_command_for_total = " << Dispatch_manager::get_instance_references().m_dispatch_manager_status_last << std::endl;

	if ( m_car_number_optimal.empty() )
	{
		//没找到也要切换上一次的存取车状态.
		if ( Dispatch_manager::get_instance_references().m_dispatch_manager_status_last == Dispatch_manager::E_DISPATCH_MANAGER_STORE )
		{
			Dispatch_manager::get_instance_references().m_dispatch_manager_status_last = Dispatch_manager::E_DISPATCH_MANAGER_PICKUP;

		}
		else
		{
			Dispatch_manager::get_instance_references().m_dispatch_manager_status_last = Dispatch_manager::E_DISPATCH_MANAGER_STORE;
		}
		//没找到合适的, 返回无效数据
		return Error_code::NODATA;
	}
	else
	{
		if ( m_dispatch_command_map[m_car_number_optimal].m_type == 1 )
		{
			Dispatch_manager::get_instance_references().m_dispatch_manager_status_last = Dispatch_manager::E_DISPATCH_MANAGER_STORE;
		}
		else
		{
			Dispatch_manager::get_instance_references().m_dispatch_manager_status_last = Dispatch_manager::E_DISPATCH_MANAGER_PICKUP;
		}

		return Error_code::SUCCESS;
	}
	return Error_code::SUCCESS;
}


//对调度指令进行排序, 选出最优解, 只比较存车
Error_manager Dispatch_command::sort_dispatch_command_for_park()
{
	int t_optimal_loss=0x7fffffff;		//最优loss值
	m_car_number_optimal.clear();
	for (auto iter = m_dispatch_command_map.begin(); iter != m_dispatch_command_map.end(); ++iter)
	{
		//loss = 空跑路程 + 排队编号, 求最小值
		int t_current_loss = iter->second.m_useless_distance * SORT_LOSS_RATIO + iter->second.m_queue_id;
		if ( t_optimal_loss > t_current_loss && iter->second.m_type == 1)
		{
			t_optimal_loss = t_current_loss;
			m_car_number_optimal = iter->first;
		}
	}
	std::cout << " huli test :::: " << " sort_dispatch_command_for_park = " << Dispatch_manager::get_instance_references().m_dispatch_manager_status_last << std::endl;

	if ( m_car_number_optimal.empty() )
	{
		return Error_code::NODATA;
	}
	return Error_code::SUCCESS;
}

//存车指令 获取 指定车高 指定单元 的车位信息，用于车位分配
Error_manager Dispatch_command::query_specify_height_unit_parkspace_info(int unit, float height, float wheel_base, Dispatch_space_info & dispatch_space_info)
{
	//查询车位表
	char query_parkspace_sql[DB_SQL_SIZE];
	memset(query_parkspace_sql, 0, DB_SQL_SIZE);
	int t_statu = 0; //车位状态, 0空闲, 1预约, 2占有, 3锁定, 6入口, 7出口, 10故障
	sprintf(query_parkspace_sql,"select * from space where car_number is NULL and ABS(height- %f) < 1e-5 and wheel_base < %f + 0.0001 and statu = %d and unit = %d",height,wheel_base,t_statu,unit);
	boost::shared_ptr<sql::ResultSet>  tp_result = nullptr;
	Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
//	std::cout << " huli test :::: " << " query_parkspace_sql = " << query_parkspace_sql << std::endl;
	LOG(INFO) << " sssssssssssssssssssssssssssssssssss " << query_parkspace_sql << " "<< this;

	if(ec == SUCCESS)
	{
		if(tp_result == nullptr)
		{
			return DB_RESULT_SET_EMPTY;
		}
		//只取第一条结果, 默认是id最小的,
		if (tp_result->next())
		{
			char buf[DB_SQL_SIZE];
			memset(buf, 0, DB_SQL_SIZE);
			try
			{
				//解析数据
				dispatch_space_info.m_id = tp_result->getInt("id");
				dispatch_space_info.m_serial_id = tp_result->getInt("serial_id");
				dispatch_space_info.m_table_id = tp_result->getInt("table_id");
				dispatch_space_info.m_unit = tp_result->getInt("unit");
				dispatch_space_info.m_floor = tp_result->getInt("floor");
				dispatch_space_info.m_room_id = tp_result->getInt("room_id");
				dispatch_space_info.m_height = tp_result->getDouble("height");
//				dispatch_space_info.m_primary_key = tp_result->getString("primary_key");
//				dispatch_space_info.m_car_number = tp_result->getString("car_number");
				dispatch_space_info.m_statu = tp_result->getInt("statu");

//				dispatch_space_info.m_space_info_string = tp_result->getString("space_info");
//				dispatch_space_info.m_measure_info_string = tp_result->getString("measure_info");
//				//存车, 数据库特有 雷达信息,  没有车位信息, 需要根据车高,再去车位表申请车位
//				if(! google::protobuf::TextFormat::ParseFromString(dispatch_space_info.m_measure_info_string, &dispatch_space_info.m_measure_info_msg))
//				{
//					return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
//										 "ParseFromString fun error, m_measure_info ");
//				}
//				//取车, 数据库特有车位信息
//				if(! google::protobuf::TextFormat::ParseFromString(dispatch_space_info.m_space_info_string, &dispatch_space_info.m_space_info_msg))
//				{
//					return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
//										 "ParseFromString fun error, m_space_info ");
//				}
			}
			catch (sql::SQLException &e)
			{
				/* Use what() (derived from std::runtime_error) to fetch the error message */
				sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
				return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
			}
			catch (std::runtime_error &e)
			{
				sprintf(buf, "# ERR: %s\n ERR: runtime_error in  %s ", e.what(), __FILE__);
				return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
			}
		}
		else
		{
			return Error_manager(Error_code::DB_NOT_QUERY_EMPTY_PARKSPACE, Error_level::MINOR_ERROR,
								 "数据库未查询到空车位 Parkspace_operating_function::query_one_empty_parkspace error ");
		}
		return SUCCESS;
	}
	else
	{
		return ec;
	}
	return SUCCESS;
}

//获取 指定车高 指定单元 的空闲车位数量，
Error_manager Dispatch_command::query_specify_height_unit_parkspace_info(int unit, float height, float wheel_base, int & dispatch_space_count)
{
	//查询车位表
	char query_parkspace_sql[DB_SQL_SIZE];
	memset(query_parkspace_sql, 0, DB_SQL_SIZE);
	int t_statu = 0; //车位状态, 0空闲, 1预约, 2占有, 3锁定, 6入口, 7出口, 10故障
	sprintf(query_parkspace_sql,"select count(*) from (select * from space where car_number is NULL and ABS(height- %f) < 1e-5 and statu = %d and unit = %d) as tablename2",height,t_statu,unit);
	boost::shared_ptr<sql::ResultSet>  tp_result = nullptr;
	Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
//	std::cout << " huli test :::: " << " query_parkspace_sql = " << query_parkspace_sql << std::endl;
	if(ec == SUCCESS)
	{
		if(tp_result == nullptr)
		{
			return DB_RESULT_SET_EMPTY;
		}
		//只取第一条结果, 默认是id最小的,
		if (tp_result->next())
		{
			char buf[DB_SQL_SIZE];
			memset(buf, 0, DB_SQL_SIZE);
			try
			{
				//解析数据
				dispatch_space_count = tp_result->getInt("count(*)");
			}
			catch (sql::SQLException &e)
			{
				/* Use what() (derived from std::runtime_error) to fetch the error message */
				sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
				return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
			}
			catch (std::runtime_error &e)
			{
				sprintf(buf, "# ERR: %s\n ERR: runtime_error in  %s ", e.what(), __FILE__);
				return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
			}
		}
		else
		{
			return Error_manager(Error_code::DB_NOT_QUERY_EMPTY_PARKSPACE, Error_level::MINOR_ERROR,
								 "数据库未查询到空车位 Parkspace_operating_function::query_one_empty_parkspace error ");
		}
		return SUCCESS;
	}
	else
	{
		return ec;
	}
	return SUCCESS;
}

//查询空闲车位最优解,    存车指令 根据调度指令最优解 获取 空闲车位最优解
Error_manager Dispatch_command::query_dispatch_space_optimal(int unit, float height, float wheel_base, Common_data::Car_type & car_type, Dispatch_space_info & dispatch_space_info)
{
	Error_manager t_error;

	int t_unit = unit;
	float t_height = height;

	//小车对应小车位  中车对应中车位 大车对应大车位
	if ( 0 < t_height && t_height <= CAR_HEIGHT_LIMIT_SMALL +0.000001)
	{
		t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_SMALL, CAR_WHEEL_BASE_LIMIT_SHORT, dispatch_space_info);
		if ( t_error == Error_code::SUCCESS )
		{
			car_type = Common_data::Car_type::MIN_SHORT_CAR;
			return t_error;
		}
		t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_SMALL, CAR_WHEEL_BASE_LIMIT_LONG, dispatch_space_info);
		if ( t_error == Error_code::SUCCESS )
		{
			car_type = Common_data::Car_type::MIN_LONG_CAR;
			return t_error;
		}
	}
	if (  t_height <= CAR_HEIGHT_LIMIT_MIDDLE +0.000001)
	{
		t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_MIDDLE, CAR_WHEEL_BASE_LIMIT_SHORT, dispatch_space_info);
		if ( t_error == Error_code::SUCCESS )
		{
			car_type = Common_data::Car_type::MID_SHORT_CAR;
			return t_error;
		}
		t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_MIDDLE, CAR_WHEEL_BASE_LIMIT_LONG, dispatch_space_info);
		if ( t_error == Error_code::SUCCESS )
		{
			car_type = Common_data::Car_type::MID_LONG_CAR;
			return t_error;
		}
	}
	if ( t_height <= CAR_HEIGHT_LIMIT_BIG +0.000001)
	{
		t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_BIG, CAR_WHEEL_BASE_LIMIT_SHORT, dispatch_space_info);
		if ( t_error == Error_code::SUCCESS )
		{
			car_type = Common_data::Car_type::BIG_SHORT_CAR;
			return t_error;
		}
		t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_BIG, CAR_WHEEL_BASE_LIMIT_LONG, dispatch_space_info);
		if ( t_error == Error_code::SUCCESS )
		{
			car_type = Common_data::Car_type::BIG_LONG_CAR;
			return t_error;
		}
	}
	return Error_manager(Error_code::DB_NOT_QUERY_EMPTY_PARKSPACE, Error_level::MINOR_ERROR,
						 " query_dispatch_space_optimal error ");
}

//查询空闲车位最优解,    存车指令 根据调度指令最优解 获取 空闲车位最优解
Error_manager Dispatch_command::query_dispatch_space_optimal_ex(Common_data::Car_type reallocate_car_type, Dispatch_space_info & dispatch_space_info)
{
	Error_manager t_error;

	int t_unit = m_dispatch_command_map[m_car_number_optimal].m_unit;
//	float t_height = m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.height();

	//小车对应小车位  中车对应中车位 大车对应大车位
	if ( reallocate_car_type == Common_data::Car_type::MIN_SHORT_CAR )
	{
		t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_SMALL, CAR_WHEEL_BASE_LIMIT_SHORT,
														   dispatch_space_info);
		if (t_error == Error_code::SUCCESS)
		{
			m_car_type = Common_data::Car_type::MIN_SHORT_CAR;
			return t_error;
		}
	}
	if ( reallocate_car_type == Common_data::Car_type::MIN_SHORT_CAR ||
		 reallocate_car_type == Common_data::Car_type::MIN_LONG_CAR )
	{
		t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_SMALL, CAR_WHEEL_BASE_LIMIT_LONG, dispatch_space_info);
		if ( t_error == Error_code::SUCCESS )
		{
			m_car_type = Common_data::Car_type::MIN_LONG_CAR;
			return t_error;
		}
	}
	if ( reallocate_car_type == Common_data::Car_type::MIN_SHORT_CAR ||
		 reallocate_car_type == Common_data::Car_type::MID_SHORT_CAR )
	{
		t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_MIDDLE, CAR_WHEEL_BASE_LIMIT_SHORT,
														   dispatch_space_info);
		if (t_error == Error_code::SUCCESS)
		{
			m_car_type = Common_data::Car_type::MID_SHORT_CAR;
			return t_error;
		}
	}
	if ( reallocate_car_type == Common_data::Car_type::MIN_SHORT_CAR ||
		 reallocate_car_type == Common_data::Car_type::MID_SHORT_CAR ||
		 reallocate_car_type == Common_data::Car_type::MIN_LONG_CAR ||
		 reallocate_car_type == Common_data::Car_type::MID_LONG_CAR)
	{
		t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_MIDDLE, CAR_WHEEL_BASE_LIMIT_LONG, dispatch_space_info);
		if ( t_error == Error_code::SUCCESS )
		{
			m_car_type = Common_data::Car_type::MID_LONG_CAR;
			return t_error;
		}
	}
	if ( reallocate_car_type == Common_data::Car_type::MIN_SHORT_CAR ||
		 reallocate_car_type == Common_data::Car_type::MID_SHORT_CAR ||
		 reallocate_car_type == Common_data::Car_type::BIG_SHORT_CAR )
	{
		t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_BIG, CAR_WHEEL_BASE_LIMIT_SHORT,
														   dispatch_space_info);
		if (t_error == Error_code::SUCCESS)
		{
			m_car_type = Common_data::Car_type::BIG_SHORT_CAR;
			return t_error;
		}
	}
	if ( reallocate_car_type == Common_data::Car_type::MIN_SHORT_CAR ||
		 reallocate_car_type == Common_data::Car_type::MID_SHORT_CAR ||
		 reallocate_car_type == Common_data::Car_type::BIG_SHORT_CAR ||
		 reallocate_car_type == Common_data::Car_type::MIN_LONG_CAR ||
		 reallocate_car_type == Common_data::Car_type::MID_LONG_CAR ||
		 reallocate_car_type == Common_data::Car_type::BIG_LONG_CAR)
	{
		t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_BIG, CAR_WHEEL_BASE_LIMIT_LONG, dispatch_space_info);
		if ( t_error == Error_code::SUCCESS )
		{
			m_car_type = Common_data::Car_type::BIG_LONG_CAR;
			return t_error;
		}
	}
	return Error_manager(Error_code::DB_NOT_QUERY_EMPTY_PARKSPACE, Error_level::MINOR_ERROR,
						 " query_dispatch_space_optimal error ");
}

//查询 取车的车位    取车指令 根据车牌号 查询对应的车位
Error_manager Dispatch_command::query_dispatch_space_for_car_number()
{
//查询车位表
	char query_parkspace_sql[DB_SQL_SIZE];
	memset(query_parkspace_sql, 0, DB_SQL_SIZE);
	int t_statu = 2; //车位状态, 0空闲, 1预约, 2占有, 3锁定, 6入口, 7出口, 10故障
	sprintf(query_parkspace_sql,"select * from space where car_number = '%s' ",m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str());
//	std::cout<<"query_parkspace_sql = "<<query_parkspace_sql<<std::endl;
	boost::shared_ptr<sql::ResultSet>  tp_result = nullptr;
	Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
	if(ec == SUCCESS)
	{
		if(tp_result == nullptr)
		{
			return DB_RESULT_SET_EMPTY;
		}
		//只取第一条结果, 默认是id最小的,
		if (tp_result->next())
		{
			char buf[DB_SQL_SIZE];
			memset(buf, 0, DB_SQL_SIZE);
			try
			{
				//解析数据
				m_dispatch_space_info[0].m_id = tp_result->getInt("id");
				m_dispatch_space_info[0].m_serial_id = tp_result->getInt("serial_id");
				m_dispatch_space_info[0].m_table_id = tp_result->getInt("table_id");
				m_dispatch_space_info[0].m_unit = tp_result->getInt("unit");
				m_dispatch_space_info[0].m_floor = tp_result->getInt("floor");
				m_dispatch_space_info[0].m_room_id = tp_result->getInt("room_id");
				m_dispatch_space_info[0].m_height = tp_result->getDouble("height");
				m_dispatch_space_info[0].m_primary_key = tp_result->getString("primary_key");
				m_dispatch_space_info[0].m_car_number = tp_result->getString("car_number");
				m_dispatch_space_info[0].m_statu = tp_result->getInt("statu");


			}
			catch (sql::SQLException &e)
			{
				/* Use what() (derived from std::runtime_error) to fetch the error message */
				sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
				return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
			}
			catch (std::runtime_error &e)
			{
				sprintf(buf, "# ERR: %s\n ERR: runtime_error in  %s ", e.what(), __FILE__);
				return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
			}
		}
		else
		{
			return Error_manager(Error_code::DB_QUERY_NOT_DATA, Error_level::MINOR_ERROR,
								 "数据库未查询到数据 Parkspace_operating_function::query_one_parkspace_with_license error ");

		}
		return SUCCESS;
	}
	else
	{
		return ec;
	}
	return SUCCESS;
}

//查询 取车的车辆感测信息    取车指令 根据key 查询感测信息
//Error_manager Dispatch_command::query_dispatch_vehicle_for_primary_key()
//{
//	//查询 车辆表
//	char query_parkspace_sql[DB_SQL_SIZE];
//	memset(query_parkspace_sql, 0, DB_SQL_SIZE);
//	int t_statu = 0; //车位状态, 0可用, 1故障
//	sprintf(query_parkspace_sql,"select * from vehicle where primary_key = '%s' ",m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str());
////	std::cout<<"query_parkspace_sql = "<<query_parkspace_sql<<std::endl;
//	boost::shared_ptr<sql::ResultSet>  tp_result = nullptr;
//	Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
//	if(ec == SUCCESS)
//	{
//		if(tp_result == nullptr)
//		{
//			return DB_RESULT_SET_EMPTY;
//		}
//		//只取第一条结果, 默认是id最小的,
//		if (tp_result->next())
//		{
//			char buf[DB_SQL_SIZE];
//			memset(buf, 0, DB_SQL_SIZE);
//			try
//			{
//				//解析数据
//				m_dispatch_vehicle_info.m_car_number = tp_result->getString("car_number");
//				m_dispatch_vehicle_info.m_primary_key = tp_result->getString("primary_key");
//				m_dispatch_vehicle_info.m_actually_measure_info = tp_result->getString("actually_measure_info");
//
//				//取车, 数据库特有车位信息
//				if(! google::protobuf::TextFormat::ParseFromString(m_dispatch_vehicle_info.m_actually_measure_info, &m_dispatch_vehicle_info.m_actually_measure_info_msg))
//				{
//					return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
//										 "ParseFromString fun error, m_space_info ");
//				}
//			}
//			catch (sql::SQLException &e)
//			{
//				/* Use what() (derived from std::runtime_error) to fetch the error message */
//				sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
//				return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
//			}
//			catch (std::runtime_error &e)
//			{
//				sprintf(buf, "# ERR: %s\n ERR: runtime_error in  %s ", e.what(), __FILE__);
//				return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
//			}
//		}
//		else
//		{
//			return Error_manager(Error_code::DB_QUERY_NOT_DATA, Error_level::MINOR_ERROR,
//								 "数据库未查询到数据 Parkspace_operating_function::query_one_parkspace_with_license error ");
//
//		}
//		return SUCCESS;
//	}
//	else
//	{
//		return ec;
//	}
//	return SUCCESS;
//}


//更新 车位状态, 根据车位ID 写入车牌号即可
Error_manager Dispatch_command::update_parkspace_write_car_number()
{
	//执行sql操作
	char update_space_sql[DB_SQL_SIZE];
	memset(update_space_sql, 0, DB_SQL_SIZE);
	//int t_statu = 2; //车位状态, 0空闲, 1预约, 2占有, 3锁定, 6入口, 7出口, 10故障
	sprintf(update_space_sql, "update space set car_number = '%s', primary_key = '%s', statu = 2, space_info = '%s', measure_info = '%s' where id = %d",
			m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str(),
			m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str(),
			m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.DebugString().c_str(),
			m_dispatch_command_map[m_car_number_optimal].m_measure_info_string.c_str(),
			m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.id()
	);
	LOG(INFO) << " update_space_sql =  "<< update_space_sql << " --- " << this;
	Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_space_sql);
	return ec;
}

//更新 车位状态,  存车排队预约车位
Error_manager Dispatch_command::update_parkspace_write_car_number(park_table park_command, Dispatch_command::Dispatch_space_info dispatch_space_info)
{
	parkspace_info	t_space_info_msg;
	t_space_info_msg.set_id(dispatch_space_info.m_id);
	t_space_info_msg.set_serial_id(dispatch_space_info.m_serial_id);
	t_space_info_msg.set_table_id(dispatch_space_info.m_table_id);
	t_space_info_msg.set_unit_id(dispatch_space_info.m_unit);
	t_space_info_msg.set_floor(dispatch_space_info.m_floor);
	t_space_info_msg.set_room_id(dispatch_space_info.m_room_id);
	t_space_info_msg.set_height(dispatch_space_info.m_height);

	//执行sql操作
	char update_space_sql[DB_SQL_SIZE];
	memset(update_space_sql, 0, DB_SQL_SIZE);
	//int t_statu = 2; //车位状态, 0空闲, 1预约, 2占有, 3锁定, 6入口, 7出口, 10故障
	sprintf(update_space_sql, "update space set car_number = '%s', primary_key = '%s', statu = 1, space_info = '%s', measure_info = '%s',plate_color = '%s', plate_type = '%s', plate_confidence = %d, recognition_time = '%s', plate_full_image = '%s', plate_clip_image = '%s' where id = %d",
			park_command.car_number().c_str(),
			park_command.primary_key().c_str(),
			t_space_info_msg.DebugString().c_str(),
			park_command.entrance_measure_info().DebugString().c_str(),
			park_command.car_number_info().plate_color().c_str(),
			park_command.car_number_info().plate_type().c_str(),
			park_command.car_number_info().plate_confidence(),
			park_command.car_number_info().recognition_time().c_str(),
			park_command.car_number_info().plate_full_image().c_str(),
			park_command.car_number_info().plate_clip_image().c_str(),
			t_space_info_msg.id()
	);
	LOG(INFO) << " update_space_sql =  "<< update_space_sql << " --- " << this;
	Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_space_sql);
	return ec;
}

//更新 车位状态, 找到车牌号, 写NULL
Error_manager Dispatch_command::update_parkspace_clear_car_number()
{
	//执行sql操作
	char update_space_sql[DB_SQL_SIZE];
	memset(update_space_sql, 0, DB_SQL_SIZE);
	//int t_statu = 2; //车位状态, 0空闲, 1预约, 2占有, 3锁定, 6入口, 7出口, 10故障
	sprintf(update_space_sql, "update space set car_number = null, primary_key = null, statu = 0, space_info = null, measure_info = null where car_number = '%s' ",
			m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str());
	Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_space_sql);
	return ec;
}


//更新 车位状态, 找到车牌号, 写NULL by id
Error_manager Dispatch_command::update_parkspace_clear_car_number_by_id(int space_id, int statu)
{
	//执行sql操作
	char update_space_sql[DB_SQL_SIZE];
	memset(update_space_sql, 0, DB_SQL_SIZE);
	//int t_statu = 2; //车位状态, 0空闲, 1预约, 2占有, 3锁定, 6入口, 7出口, 10故障
	sprintf(update_space_sql, "update space set car_number = null, primary_key = null, statu = %d, space_info = null, measure_info = null where id = %d ",
			statu, space_id);
	Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_space_sql);
	return ec;
}

//更新车牌图片信息,
Error_manager Dispatch_command::update_parkspace_write_car_number_info()
{
	//执行sql操作
	char update_space_sql[DB_SQL_SIZE];
	memset(update_space_sql, 0, DB_SQL_SIZE);
	//int t_statu = 2; //车位状态, 0空闲, 1预约, 2占有, 3锁定, 6入口, 7出口, 10故障
	sprintf(update_space_sql, "update space set plate_color = '%s', plate_type = '%s', plate_confidence = %d, recognition_time = '%s', plate_full_image = '%s', plate_clip_image = '%s' where car_number = '%s' ",
			m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_color().c_str(),
			m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_type().c_str(),
			m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_confidence(),
			m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.recognition_time().c_str(),
			m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_full_image().c_str(),
			m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_clip_image().c_str(),
			m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str()
	);
	LOG(INFO) << "  huli 123123 update_parkspace_write_car_number_info =  " << update_space_sql << "    "<< this;
	Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_space_sql);
	return ec;
}
//删除车牌图片信息,
Error_manager Dispatch_command::update_parkspace_clear_car_number_info()
{
	//执行sql操作
	char update_space_sql[DB_SQL_SIZE];
	memset(update_space_sql, 0, DB_SQL_SIZE);
	//int t_statu = 2; //车位状态, 0空闲, 1预约, 2占有, 3锁定, 6入口, 7出口, 10故障
	sprintf(update_space_sql, "update space set plate_color = null, plate_type = null, plate_confidence = null, recognition_time = null, plate_full_image = null, plate_clip_image = null where car_number = '%s' ",
			m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str()
	);
	Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_space_sql);
	return ec;
}

//更新 指令队列, 根据车牌号 修改状态即可,  //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
Error_manager Dispatch_command::update_command_queue_for_statu(int statu)
{
	char update_command_sql[DB_SQL_SIZE];
	memset(update_command_sql, 0, DB_SQL_SIZE);
	sprintf(update_command_sql, "update command_queue set statu = %d, export_id = %d, space_info = '%s' where car_number = '%s'",
			statu,
			m_dispatch_command_map[m_car_number_optimal].m_export_id,
			m_dispatch_command_map[m_car_number_optimal].m_space_info_string.c_str(),
			m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str()
	);
//	LOG(INFO) << "jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj update_command_sql =  "<< update_command_sql << " --- " << this;
	Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_command_sql);
	return ec;
}

//把整个单元的排队指令改为故障
Error_manager Dispatch_command::update_command_queue_from_wait_to_fault_for_unit(int unit_id)
{
	char update_command_sql[DB_SQL_SIZE];
	memset(update_command_sql, 0, DB_SQL_SIZE);
	sprintf(update_command_sql, "update command_queue set statu = 3 where statu = 0 and unit = %d",
			unit_id
	);
//	LOG(INFO) << "jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj update_command_sql =  "<< update_command_sql << " --- " << this;
	Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_command_sql);
	return ec;
}

//删除 指令队列, 根据车牌号 删除指令
Error_manager Dispatch_command::delete_command_queue_for_car_number()
{
	char delete_command_sql[DB_SQL_SIZE];
	memset(delete_command_sql, 0, DB_SQL_SIZE);
	sprintf(delete_command_sql, "delete from command_queue where car_number = '%s'",
			m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str());
	LOG(INFO) << "  huli 456456 delete_command_queue_for_car_number =  " << delete_command_sql << "    "<< this;

	Error_manager ec = Database_controller::get_instance_pointer()->sql_delete(delete_command_sql);
	return ec;
}

//制作存车表单
Error_manager Dispatch_command::create_park_table()
{
	park_table t_park_table;
	t_park_table.mutable_statu()->set_execute_statu(eNormal);
	t_park_table.set_queue_id(m_dispatch_command_map[m_car_number_optimal].m_queue_id);
	t_park_table.set_car_number(m_dispatch_command_map[m_car_number_optimal].m_car_number);
	t_park_table.set_unit_id(m_dispatch_command_map[m_car_number_optimal].m_unit);
	t_park_table.set_primary_key(m_dispatch_command_map[m_car_number_optimal].m_primary_key);
	t_park_table.mutable_entrance_measure_info()->CopyFrom(m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg);
	t_park_table.mutable_allocated_space_info()->CopyFrom(m_dispatch_command_map[m_car_number_optimal].m_space_info_msg);

	//根据雷达x坐标, 计算入口id
//	int t_terminal = 0;
//	if (m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx()<0)
//	{
//		t_terminal = m_dispatch_id*2+1;
//	}
//	else if(m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx()>0)
//	{
//		t_terminal = m_dispatch_id*2+2;
//	}
	t_park_table.set_terminal_id(m_dispatch_command_map[m_car_number_optimal].m_import_id);
//	std::cout << " huli test :::: " << " iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii m_park_table_msg.entrance_measure_info().cx() = " << m_park_table_msg.entrance_measure_info().cx() << std::endl;
//	std::cout << " huli test :::: " << " iiiiiiiiiiiiiiiiiiiiiiiiiiiiii t_terminal = " << t_terminal << std::endl;
//	LOG(INFO) << " m_park_table_msg.entrance_measure_info().cx() =  "<< m_park_table_msg.entrance_measure_info().cx() << " --- " << this;
//	LOG(INFO) << " t_terminal =  "<< t_terminal << " --- " << this;
//	LOG(INFO) << " m_device_floor =  "<< m_device_floor << " --- " << this;
//	LOG(INFO) << " m_dispatch_id =  "<< m_dispatch_id << " --- " << this;
//	LOG(INFO) << " m_unit =  "<< m_unit << " --- " << this;
//	LOG(INFO) << " m_outlet_ready =  "<< m_outlet_ready << " --- " << this;

	m_park_table_msg = t_park_table;
	return Error_code::SUCCESS;
}

//制作取车表单
Error_manager Dispatch_command::create_pick_table(int outlet_ready)
{
	pick_table t_pick_table;
	t_pick_table.mutable_statu()->set_execute_statu(eNormal);
	t_pick_table.set_queue_id(m_dispatch_command_map[m_car_number_optimal].m_queue_id);
	t_pick_table.set_car_number(m_dispatch_command_map[m_car_number_optimal].m_car_number);
	t_pick_table.set_unit_id(m_dispatch_command_map[m_car_number_optimal].m_unit);
	t_pick_table.set_primary_key(m_dispatch_command_map[m_car_number_optimal].m_primary_key);
	t_pick_table.mutable_actually_measure_info()->CopyFrom(m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg);
	t_pick_table.mutable_actually_space_info()->CopyFrom(m_dispatch_command_map[m_car_number_optimal].m_space_info_msg);

	t_pick_table.set_terminal_id(outlet_ready);

//	LOG(INFO) << " outlet_ready =  "<< outlet_ready << " --- " << this;
//
//	LOG(INFO) << " m_device_floor =  "<< m_device_floor << " --- " << this;
//	LOG(INFO) << " m_dispatch_id =  "<< m_dispatch_id << " --- " << this;
//	LOG(INFO) << " m_unit =  "<< m_unit << " --- " << this;
//	LOG(INFO) << " m_outlet_ready =  "<< m_outlet_ready << " --- " << this;


	m_pick_table_msg = t_pick_table;
	return Error_code::SUCCESS;
}

//增加 车辆表, 存车指令 完成后添加车辆信息
Error_manager Dispatch_command::insert_vehicle_for_car_number()
{
	char insert_vehicle_sql[DB_SQL_SIZE];
	memset(insert_vehicle_sql, 0, DB_SQL_SIZE);
	sprintf(insert_vehicle_sql, "INSERT INTO vehicle (car_number,primary_key,actually_measure_info) values ('%s','%s','%s')",
			m_car_number_optimal.c_str(), m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str(),
			m_dispatch_command_map[m_car_number_optimal].m_measure_info_string.c_str()   );
	Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_vehicle_sql);
	return ec;
}
//删除 车辆表, 取车指令 完成后删除车辆信息
Error_manager Dispatch_command::delete_vehicle_for_car_number()
{
	char delete_vehicle_sql[DB_SQL_SIZE];
	memset(delete_vehicle_sql, 0, DB_SQL_SIZE);
	sprintf(delete_vehicle_sql, "delete from vehicle where car_number = '%s'",
			m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str());
	Error_manager ec = Database_controller::get_instance_pointer()->sql_delete(delete_vehicle_sql);
	return ec;
}

//检查记录表是否提前创建。没有创建就返回成功
Error_manager Dispatch_command::check_record_for_no_primary_key(std::string primary_key)
{
	//查询车位表
	char query_record_sql[DB_SQL_SIZE];
	memset(query_record_sql, 0, DB_SQL_SIZE);
	sprintf(query_record_sql,"select * from record where primary_key = '%s' ",primary_key.c_str());
	boost::shared_ptr<sql::ResultSet>  tp_result = nullptr;
	Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_record_sql, tp_result);
	if(ec == SUCCESS)
	{
		if(tp_result == nullptr)
		{
			return DB_RESULT_SET_EMPTY;
		}

		if (tp_result->next() == false)
		{
			return Error_code::SUCCESS;
		}
		else
		{
			return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
								 "error, check_record_for_no_primary_key has  primary_key ");
		}
		return SUCCESS;
	}
	else
	{
		return ec;
	}
	return SUCCESS;
}

//增加 记录表, 存车指令 完成后添加存车记录
Error_manager Dispatch_command::insert_record_for_park_start()
{
	Time_tool::get_instance_references().time_start(PARK_TIME_FLAG);

	int t_terminal = 0;
#ifdef CHUTIAN_PROJECT_PROJECT
	if (m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx()<0)
	{
		t_terminal = m_dispatch_id*2+1;
	}
	else if(m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx()>0)
	{
		t_terminal = m_dispatch_id*2+2;
	}
#endif//CHUTIAN_PROJECT_PROJECT

#ifdef SHANGGUJIE_PROJECT_PROJECT
	t_terminal = m_dispatch_command_map[m_car_number_optimal].m_import_id;
#endif //SHANGGUJIE_PROJECT_PROJECT

	char insert_record_sql[DB_SQL_SIZE];
	memset(insert_record_sql, 0, DB_SQL_SIZE);
//	sprintf(insert_record_sql, "INSERT INTO record (primary_key,car_number,in_time_start,measure_info,import_id) values ('%s','%s','%s','%s',%d)",
	sprintf(insert_record_sql, "INSERT INTO record (primary_key,car_number,import_id,enqueue_time, in_time_start, measure_info,center_x,center_y,theta,width,height,wheelbase,front_theta, space_info,space_id,table_id,unit_id,floor_id,room_id,plate_color,plate_type,plate_confidence,recognition_time,plate_full_image,plate_clip_image) values ('%s','%s',%d,'%s','%s','%s',%f,%f,%f,%f,%f,%f,%f,'%s',%d,%d,%d,%d,%d,'%s','%s',%d,'%s','%s','%s')",
			m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str(),
			m_car_number_optimal.c_str(),
			m_dispatch_command_map[m_car_number_optimal].m_import_id,
			Time_tool::get_instance_references().get_current_time_seconds().c_str(),
			Time_tool::get_instance_references().get_current_time_seconds().c_str(),
			m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.DebugString().c_str(),
			m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx(),
			m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cy(),
			m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.theta(),
			m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.width(),
			m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.height(),
			m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.wheelbase(),
			m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.front_theta(),
			m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.DebugString().c_str(),
			m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.id(),
			m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.table_id(),
			m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.unit_id(),
			m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.floor(),
			m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.room_id(),
			m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_color().c_str(),
			m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_type().c_str(),
			m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_confidence(),
			m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.recognition_time().c_str(),
			m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_full_image().c_str(),
			m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_clip_image().c_str()
	);
	LOG(INFO) << " insert_record_sql =  "<< insert_record_sql << " --- " << this;
	Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_record_sql);
	return ec;
}

//更新 记录表, 存车指令 完成后只更新存车开始时间
Error_manager Dispatch_command::updata_record_for_park_start()
{
	Time_tool::get_instance_references().time_start(PARK_TIME_FLAG);

	//执行sql操作
	char update_record_sql[DB_SQL_SIZE];
	memset(update_record_sql, 0, DB_SQL_SIZE);
	sprintf(update_record_sql, "update record set in_time_start = '%s', import_id = %d where primary_key = '%s' ",
			Time_tool::get_instance_references().get_current_time_seconds().c_str(),
			m_dispatch_command_map[m_car_number_optimal].m_import_id,
			m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str()	);
	LOG(INFO) << " update_record_sql =  "<< update_record_sql << " --- " << this;
	Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_record_sql);
	return ec;
}

//增加 记录表, 存车指令 完成后添加存车记录
Error_manager Dispatch_command::insert_record_for_park_start(park_table park_command, Dispatch_command::Dispatch_space_info dispatch_space_info)
{
//	Time_tool::get_instance_references().time_start(PARK_TIME_FLAG);

	int t_terminal = 0;
#ifdef CHUTIAN_PROJECT_PROJECT
	if (m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx()<0)
	{
		t_terminal = m_dispatch_id*2+1;
	}
	else if(m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx()>0)
	{
		t_terminal = m_dispatch_id*2+2;
	}
#endif//CHUTIAN_PROJECT_PROJECT

#ifdef SHANGGUJIE_PROJECT_PROJECT
	t_terminal = m_dispatch_command_map[m_car_number_optimal].m_import_id;
#endif //SHANGGUJIE_PROJECT_PROJECT


	parkspace_info	t_space_info_msg;
	t_space_info_msg.set_id(dispatch_space_info.m_id);
	t_space_info_msg.set_serial_id(dispatch_space_info.m_serial_id);
	t_space_info_msg.set_table_id(dispatch_space_info.m_table_id);
	t_space_info_msg.set_unit_id(dispatch_space_info.m_unit);
	t_space_info_msg.set_floor(dispatch_space_info.m_floor);
	t_space_info_msg.set_room_id(dispatch_space_info.m_room_id);
	t_space_info_msg.set_height(dispatch_space_info.m_height);


	char insert_record_sql[DB_SQL_SIZE];
	memset(insert_record_sql, 0, DB_SQL_SIZE);
//	sprintf(insert_record_sql, "INSERT INTO record (primary_key,car_number,in_time_start,measure_info,import_id) values ('%s','%s','%s','%s',%d)",
	sprintf(insert_record_sql, "INSERT INTO record (primary_key,car_number,import_id,enqueue_time, measure_info,center_x,center_y,theta,width,height,wheelbase,front_theta, space_info,space_id,table_id,unit_id,floor_id,room_id,plate_color,plate_type,plate_confidence,recognition_time,plate_full_image,plate_clip_image) values ('%s','%s',%d,'%s','%s',%f,%f,%f,%f,%f,%f,%f,'%s',%d,%d,%d,%d,%d,'%s','%s',%d,'%s','%s','%s')",
			park_command.primary_key().c_str(),
			park_command.car_number().c_str(),
			get_passway_id(park_command.terminal_id(), 1),
			Time_tool::get_instance_references().get_current_time_seconds().c_str(),
			park_command.entrance_measure_info().DebugString().c_str(),
			park_command.entrance_measure_info().cx(),
			park_command.entrance_measure_info().cy(),
			park_command.entrance_measure_info().theta(),
			park_command.entrance_measure_info().width(),
			park_command.entrance_measure_info().height(),
			park_command.entrance_measure_info().wheelbase(),
			park_command.entrance_measure_info().front_theta(),
			t_space_info_msg.DebugString().c_str(),
			t_space_info_msg.id(),
			t_space_info_msg.table_id(),
			t_space_info_msg.unit_id(),
			t_space_info_msg.floor(),
			t_space_info_msg.room_id(),
			park_command.car_number_info().plate_color().c_str(),
			park_command.car_number_info().plate_type().c_str(),
			park_command.car_number_info().plate_confidence(),
			park_command.car_number_info().recognition_time().c_str(),
			park_command.car_number_info().plate_full_image().c_str(),
			park_command.car_number_info().plate_clip_image().c_str()
	);
	LOG(INFO) << " insert_record_sql =  "<< insert_record_sql << " --- " << this;
	Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_record_sql);
	return ec;
}

//增加 记录表, 存车指令 完成后添加存车记录
Error_manager Dispatch_command::updata_record_for_park_end()
{
	Time_tool::get_instance_references().time_end(PARK_TIME_FLAG);
	int in_time_difference = Time_tool::get_instance_references().get_time_seconds(PARK_TIME_FLAG);

	//t_index_id是指定单元的车位id, 1~78
//	int t_table_id = (m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.id() - 1 ) % 78;
//	t_table_id = t_table_id + 1;
//	int t_table_id = m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.table_id();

	//执行sql操作
	char update_record_sql[DB_SQL_SIZE];
	memset(update_record_sql, 0, DB_SQL_SIZE);
//	sprintf(update_record_sql, "update record set in_time_end = '%s', in_time_difference = %d, space_id = %d, space_info = '%s' where primary_key = '%s' ",
	sprintf(update_record_sql, "update record set in_time_end = '%s', in_time_difference = %d, space_id = %d, table_id = %d, unit_id = %d, floor_id = %d, room_id = %d, plate_color = '%s', plate_type = '%s', plate_confidence = %d, recognition_time = '%s', plate_full_image = '%s', plate_clip_image = '%s'  where primary_key = '%s' ",
			Time_tool::get_instance_references().get_current_time_seconds().c_str(),
			in_time_difference,
			m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.id(),
			m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.table_id(),
			m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.unit_id(),
			m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.floor(),
			m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.room_id(),
//			m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.DebugString().c_str(),
			m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_color().c_str(),
			m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_type().c_str(),
			m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_confidence(),
			m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.recognition_time().c_str(),
			m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_full_image().c_str(),
			m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_clip_image().c_str(),
			m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str()
	);
	LOG(INFO) << "  huli 78978978 updata_record_for_park_end =  " << update_record_sql << "    "<< this;
	Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_record_sql);
	return ec;
}

//更新 记录表, 取车指令 完成后更新取车记录
Error_manager Dispatch_command::updata_record_for_pick_start()
{
	Time_tool::get_instance_references().time_start(PICK_TIME_FLAG);

	//执行sql操作
	char update_record_sql[DB_SQL_SIZE];
	memset(update_record_sql, 0, DB_SQL_SIZE);
	sprintf(update_record_sql, "update record set out_time_start = '%s', export_id = %d where primary_key = '%s' ",
			Time_tool::get_instance_references().get_current_time_seconds().c_str(),
			m_dispatch_command_map[m_car_number_optimal].m_export_id,
			m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str()	);
	LOG(INFO) << " update_record_sql =  "<< update_record_sql << " --- " << this;
	Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_record_sql);
	return ec;
}

//更新 记录表, 取车指令 完成后更新取车记录
Error_manager Dispatch_command::updata_record_for_pick_end()
{
	Time_tool::get_instance_references().time_end(PICK_TIME_FLAG);
	int out_time_difference = Time_tool::get_instance_references().get_time_seconds(PICK_TIME_FLAG);

	int parking_time;
	get_parking_time(m_dispatch_command_map[m_car_number_optimal].m_primary_key, parking_time);
	char parking_time_string[256] = "";
	int hour = parking_time / 3600;
	int min = (parking_time / 60)%60;
	int sec = parking_time % 60;
	sprintf(parking_time_string, " %d:%02d:%02d", hour, min, sec);

	//执行sql操作
	char update_record_sql[DB_SQL_SIZE];
	memset(update_record_sql, 0, DB_SQL_SIZE);
	sprintf(update_record_sql, "update record set out_time_end = '%s', out_time_difference = %d, parking_time = '%s' where primary_key = '%s' ",
			Time_tool::get_instance_references().get_current_time_seconds().c_str(),
			out_time_difference,
			parking_time_string,
			m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str()	);
	LOG(INFO) << " update_record_sql =  "<< update_record_sql << " --- " << this;
	Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_record_sql);
	return ec;
}

//更新 记录表, 取车指令 完成后更新取车记录, 撤销指令, 没有取车时间差
Error_manager Dispatch_command::updata_record_for_pick_end_ex()
{
	int parking_time;
	get_parking_time(m_dispatch_command_map[m_car_number_optimal].m_primary_key, parking_time);
	char parking_time_string[256] = "";
	int hour = parking_time / 3600;
	int min = parking_time / 60;
	int sec = parking_time % 60;
	sprintf(parking_time_string, " %d:%02d:%02d", hour, min, sec);

	//执行sql操作
	char update_record_sql[DB_SQL_SIZE];
	memset(update_record_sql, 0, DB_SQL_SIZE);
	sprintf(update_record_sql, "update record set out_time_end = '%s', parking_time = '%s' where primary_key = '%s' ",
			Time_tool::get_instance_references().get_current_time_seconds().c_str(),
			parking_time_string,
			m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str()	);
	LOG(INFO) << " update_record_sql =  "<< update_record_sql << " --- " << this;
	Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_record_sql);
	return ec;
}

//计算汽车在车库的存车时间, 从存车开始到取车结束, 返回 parking_time, 单位秒.
Error_manager Dispatch_command::get_parking_time(std::string primary_key, int & parking_time )
{
	//查询存车时间 in_time_start
	char query_record_sql[DB_SQL_SIZE];
	memset(query_record_sql, 0, DB_SQL_SIZE);
	sprintf(query_record_sql,"select * from record where primary_key = '%s' ", primary_key.c_str() );
	boost::shared_ptr<sql::ResultSet>  tp_result = nullptr;
	Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_record_sql, tp_result);
	if(ec == SUCCESS)
	{
		if(tp_result == nullptr)
		{
			return DB_RESULT_SET_EMPTY;
		}
		//只取第一条结果, 默认是id最小的,
		if (tp_result->next())
		{
			char buf[DB_SQL_SIZE];
			memset(buf, 0, DB_SQL_SIZE);
			try
			{
				//解析数据
				std::string in_time_start_string = tp_result->getString("in_time_start");
				std::chrono::system_clock::time_point in_time_start = Time_tool::get_instance_references().transform_time_string_seconds(in_time_start_string);
				std::chrono::system_clock::time_point out_time_end = std::chrono::system_clock::now();
				double t_parking_time = (out_time_end - in_time_start).count() / 1000000000;
				parking_time = t_parking_time;
				return Error_code::SUCCESS;
			}
			catch (sql::SQLException &e)
			{
				/* Use what() (derived from std::runtime_error) to fetch the error message */
				sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
				return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
			}
			catch (std::runtime_error &e)
			{
				sprintf(buf, "# ERR: %s\n ERR: runtime_error in  %s ", e.what(), __FILE__);
				return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
			}
		}
		else
		{
			return Error_manager(Error_code::DB_NOT_QUERY_EMPTY_PARKSPACE, Error_level::MINOR_ERROR,
								 "数据库未查询到空车位 Parkspace_operating_function::query_one_empty_parkspace error ");
		}
		return SUCCESS;
	}
	else
	{
		return ec;
	}

	return Error_code::SUCCESS;
}


//获取出入口id 1~2为入口, 3~4为出口,
//输入terminal_id 1~19,   输入 action_mode 1:存车, 2:取车
int Dispatch_command::get_passway_id(int terminal_id, int action_mode)
{
	terminal_id = terminal_id%100;
	int passway_id = 0;
	switch ( terminal_id )
	{
		case 1:
		case 2:
		case 4:
		case 6:
		case 8:
		case 10:
		case 12:
		case 14:
		case 16:
		case 17:
		case 19:
		{
			passway_id = 1 + (action_mode-1)*2;
			break;
		}
		case 3:
		case 5:
		case 7:
		case 9:
		case 11:
		case 13:
		case 15:
		case 18:
		{
			passway_id = 2 + (action_mode-1)*2;
			break;
		}
		default:
		{

			break;
		}
	}
	return passway_id;
}
