//
// Created by huli on 2021/3/24.
//

#include "dispatch_coordinates.h"
#include "../tool/proto_tool.h"


Dispatch_coordinates::Dispatch_coordinates()
{
	mpp_space_lock = nullptr;
	m_space_lock_rows = 0;
	m_space_lock_columns = 0;
	m_space_lock_total = 0;

}
Dispatch_coordinates::~Dispatch_coordinates()
{
	dispatch_coordinates_uninit();
}

//初始化 调度的坐标系 模块。如下三选一
Error_manager Dispatch_coordinates::dispatch_coordinates_init()
{
	return  dispatch_coordinates_init_from_protobuf(SETTING_PATH DISPATCH_COORDINATES_PARAMETER_PATH);
}

//初始化 调度的坐标系 模块。从文件读取
Error_manager Dispatch_coordinates::dispatch_coordinates_init_from_protobuf(std::string prototxt_path)
{
	Dispatch_proto::Dispatch_coordinates_parameter_all t_dispatch_coordinates_parameter_all;
	if(!  proto_tool::read_proto_param(prototxt_path,t_dispatch_coordinates_parameter_all) )
	{
		return Error_manager(DISPATCH_COORDINATES_READ_PROTOBUF_ERROR,MINOR_ERROR,
							 "Dispatch_coordinates read_proto_param  failed");
	}

	return dispatch_coordinates_init_from_protobuf(t_dispatch_coordinates_parameter_all);
}

//初始化 调度的坐标系 模块。从protobuf读取
Error_manager Dispatch_coordinates::dispatch_coordinates_init_from_protobuf(Dispatch_proto::Dispatch_coordinates_parameter_all& dispatch_coordinates_parameter_all)
{
	LOG(INFO) << " ---Dispatch_coordinates::dispatch_coordinates_init_from_protobuf() run--- "<< this;
	Error_manager t_error;

	m_space_lock_rows = dispatch_coordinates_parameter_all.parkspace_number().rows();
	m_space_lock_columns = dispatch_coordinates_parameter_all.parkspace_number().columns();
	m_space_lock_total = dispatch_coordinates_parameter_all.parkspace_number().total();
	m_liftway_width = dispatch_coordinates_parameter_all.parkspace_number().liftway_width();

	mpp_space_lock = new space_device*[m_space_lock_rows];
	for (int i = 0; i < m_space_lock_rows; ++i)
	{
		mpp_space_lock[i] = new space_device[m_space_lock_columns];
		for (int j = 0; j < m_space_lock_columns; ++j)
		{
			//注意了, 这里一定要初始化为-1, 因为设备id=0 是有效设备
			mpp_space_lock[i][j].m_catcher_id = -1;
			mpp_space_lock[i][j].m_carrier_id = -1;
		}
	}



	int size;
	size = dispatch_coordinates_parameter_all.packspace_coordinates_parameters_size();
	for (int i = 0; i < size; ++i)
	{
		Point3D_tool::Point3D t_point3d;
		int id = dispatch_coordinates_parameter_all.packspace_coordinates_parameters(i).id();
		t_point3d.x = dispatch_coordinates_parameter_all.packspace_coordinates_parameters(i).x();
		t_point3d.y = dispatch_coordinates_parameter_all.packspace_coordinates_parameters(i).y();
		t_point3d.z = dispatch_coordinates_parameter_all.packspace_coordinates_parameters(i).z();
		m_packspace_coordinates_map[id] = t_point3d;
	}

	size = dispatch_coordinates_parameter_all.passageway_coordinates_parameters_size();
	for (int i = 0; i < size; ++i)
	{
		Point3D_tool::Point3D t_point3d;
		int id = dispatch_coordinates_parameter_all.passageway_coordinates_parameters(i).id();
		t_point3d.x = dispatch_coordinates_parameter_all.passageway_coordinates_parameters(i).x();
		t_point3d.y = dispatch_coordinates_parameter_all.passageway_coordinates_parameters(i).y();
		t_point3d.z = dispatch_coordinates_parameter_all.passageway_coordinates_parameters(i).z();
		m_passageway_coordinates_map[id] = t_point3d;
	}

	size = dispatch_coordinates_parameter_all.carrier_coordinates_parameters_size();
	for (int i = 0; i < size; ++i)
	{
		Point3D_tool::Point3D t_point3d;
		int id = dispatch_coordinates_parameter_all.carrier_coordinates_parameters(i).id();
		t_point3d.x = dispatch_coordinates_parameter_all.carrier_coordinates_parameters(i).x();
		t_point3d.y = dispatch_coordinates_parameter_all.carrier_coordinates_parameters(i).y();
		t_point3d.z = dispatch_coordinates_parameter_all.carrier_coordinates_parameters(i).z();
		m_carrier_coordinates_map[id] = t_point3d;
	}

	size = dispatch_coordinates_parameter_all.catcher_coordinates_parameters_size();
	for (int i = 0; i < size; ++i)
	{
		Point3D_tool::Point3D t_point3d;
		int id = dispatch_coordinates_parameter_all.catcher_coordinates_parameters(i).id();
		t_point3d.x = dispatch_coordinates_parameter_all.catcher_coordinates_parameters(i).x();
		t_point3d.y = dispatch_coordinates_parameter_all.catcher_coordinates_parameters(i).y();
		t_point3d.z = dispatch_coordinates_parameter_all.catcher_coordinates_parameters(i).z();
		m_catcher_coordinates_map[id] = t_point3d;
	}


	m_carrier_box.x_min = dispatch_coordinates_parameter_all.carrier_box_parameters().x_min();
	m_carrier_box.x_max = dispatch_coordinates_parameter_all.carrier_box_parameters().x_max();
	m_carrier_box.y_min = dispatch_coordinates_parameter_all.carrier_box_parameters().y_min();
	m_carrier_box.y_max = dispatch_coordinates_parameter_all.carrier_box_parameters().y_max();
	m_carrier_box.z_min = dispatch_coordinates_parameter_all.carrier_box_parameters().z_min();
	m_carrier_box.z_max = dispatch_coordinates_parameter_all.carrier_box_parameters().z_max();

	m_catcher_box.x_min = dispatch_coordinates_parameter_all.catcher_box_parameters().x_min();
	m_catcher_box.x_max = dispatch_coordinates_parameter_all.catcher_box_parameters().x_max();
	m_catcher_box.y_min = dispatch_coordinates_parameter_all.catcher_box_parameters().y_min();
	m_catcher_box.y_max = dispatch_coordinates_parameter_all.catcher_box_parameters().y_max();
	m_catcher_box.z_min = dispatch_coordinates_parameter_all.catcher_box_parameters().z_min();
	m_catcher_box.z_max = dispatch_coordinates_parameter_all.catcher_box_parameters().z_max();

	m_catcher_b_min = dispatch_coordinates_parameter_all.catcher_b_min();
	m_catcher_b_max = dispatch_coordinates_parameter_all.catcher_b_max();
	m_catcher_d1_min = dispatch_coordinates_parameter_all.catcher_d1_min();
	m_catcher_d1_max = dispatch_coordinates_parameter_all.catcher_d1_max();
	m_catcher_d2_min = dispatch_coordinates_parameter_all.catcher_d2_min();
	m_catcher_d2_max = dispatch_coordinates_parameter_all.catcher_d2_max();

	std::cout << " huli test :::: " << " m_packspace_coordinates_map.size() = " << m_packspace_coordinates_map.size() << std::endl;
	std::cout << " huli test :::: " << " m_passageway_coordinates_map.size() = " << m_passageway_coordinates_map.size() << std::endl;
	std::cout << " huli test :::: " << " m_carrier_coordinates_map.size() = " << m_carrier_coordinates_map.size() << std::endl;
	std::cout << " huli test :::: " << " m_catcher_coordinates_map.size() = " << m_catcher_coordinates_map.size() << std::endl;

	m_default_wheelbase = dispatch_coordinates_parameter_all.default_wheelbase();

	m_catcher_1th_floor_z = dispatch_coordinates_parameter_all.catcher_1th_floor_z();
	m_catcher_2th_floor_z = dispatch_coordinates_parameter_all.catcher_2th_floor_z();
	m_catcher_3th_floor_z = dispatch_coordinates_parameter_all.catcher_3th_floor_z();
	m_catcher_4th_floor_z = dispatch_coordinates_parameter_all.catcher_4th_floor_z();

	m_carrier_1th_floor_z = dispatch_coordinates_parameter_all.carrier_1th_floor_z();
	m_carrier_2th_floor_z = dispatch_coordinates_parameter_all.carrier_2th_floor_z();
	m_carrier_3th_floor_z = dispatch_coordinates_parameter_all.carrier_3th_floor_z();
	m_carrier_4th_floor_z = dispatch_coordinates_parameter_all.carrier_4th_floor_z();

	m_catcher_d1_d2_distance = dispatch_coordinates_parameter_all.catcher_d1_d2_distance();
	m_catcher_wheel_base_limit = dispatch_coordinates_parameter_all.catcher_wheel_base_limit();

	m_carrier_y_y1_distance = dispatch_coordinates_parameter_all.carrier_y_y1_distance();
	m_carrier_default_y1_back = dispatch_coordinates_parameter_all.carrier_default_y1_back();
	m_carrier_default_y1_leave = dispatch_coordinates_parameter_all.carrier_default_y1_leave();
	m_carrier_default_y_back = dispatch_coordinates_parameter_all.carrier_default_y_back();
	m_carrier_default_y_leave = dispatch_coordinates_parameter_all.carrier_default_y_leave();

	m_carrier_default_x_left = dispatch_coordinates_parameter_all.carrier_default_x_left();
	m_carrier_default_x_right = dispatch_coordinates_parameter_all.carrier_default_x_right();

	m_passageway_terminal_id_min = dispatch_coordinates_parameter_all.passageway_terminal_id_min();
	m_passageway_terminal_id_max = dispatch_coordinates_parameter_all.passageway_terminal_id_max();
	m_passageway_terminal_number = dispatch_coordinates_parameter_all.passageway_terminal_number();
	size = dispatch_coordinates_parameter_all.passageway_functioning_patterns_size();
	for (int i = m_passageway_terminal_id_min; i < size; ++i)
	{
		m_passageway_functioning_pattern_map[i] = (Passageway_functioning_pattern)dispatch_coordinates_parameter_all.passageway_functioning_patterns(i);
	}
	if ( m_passageway_terminal_number != m_passageway_terminal_id_max - m_passageway_terminal_id_min +1 ||
		 m_passageway_terminal_number != size)
	{
		return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
							 " Dispatch_coordinates::dispatch_coordinates_init_from_protobuf PARAMRTER ERROR ");
	}

	m_parkspace_id_min = dispatch_coordinates_parameter_all.parkspace_id_min();
	m_parkspace_id_max = dispatch_coordinates_parameter_all.parkspace_id_max();
	m_parkspace_id_total = dispatch_coordinates_parameter_all.parkspace_id_total();

	m_third_floor_parkspace_id_min = dispatch_coordinates_parameter_all.third_floor_parkspace_id_min();
	m_third_floor_parkspace_id_max = dispatch_coordinates_parameter_all.third_floor_parkspace_id_max();
	m_third_floor_parkspace_id_total = dispatch_coordinates_parameter_all.third_floor_parkspace_id_total();

	m_car_wheel_base_min = dispatch_coordinates_parameter_all.car_wheel_base_min();
	m_car_wheel_base_max = dispatch_coordinates_parameter_all.car_wheel_base_max();

	m_separated_distance_z = dispatch_coordinates_parameter_all.m_separated_distance_z();
	m_separated_distance_x = dispatch_coordinates_parameter_all.m_separated_distance_x();

	return Error_code::SUCCESS;
}
//反初始化
Error_manager Dispatch_coordinates::dispatch_coordinates_uninit()
{
	for (int i = 0; i < m_space_lock_rows; ++i)
	{
		delete [](mpp_space_lock[i]);
	}
	delete [](mpp_space_lock);
	return Error_code::SUCCESS;
}

//获取2点之间的路径长度, 只算x轴和z轴, 只是计算一个大概值
Error_manager Dispatch_coordinates::get_distance(int coordinates_id_1, int coordinates_id_2, Parkspace_path parkspace_path, float & distance)
{
	//是否在1楼的标记位,
	float t_distance_x = 0;
	float t_distance_z = 0;

	//检查合法性
	if ( m_carrier_coordinates_map.find(coordinates_id_1) != m_carrier_coordinates_map.end() &&
		 m_carrier_coordinates_map.find(coordinates_id_2) != m_carrier_coordinates_map.end() )
	{
		//计算距离
		if(coordinates_id_1 == coordinates_id_2)
		{
			distance = 0;
		}
		else if ( m_carrier_coordinates_map[coordinates_id_1].z == m_carrier_coordinates_map[coordinates_id_2].z )
		{
			distance = m_carrier_coordinates_map[coordinates_id_1].x - m_carrier_coordinates_map[coordinates_id_2].x;
			if ( distance<0 )
			{
				distance = -distance;
			}
		}
		else if ( parkspace_path == Dispatch_coordinates::Parkspace_path::LEFT_PATH )
		{
			t_distance_x = (m_carrier_coordinates_map[coordinates_id_1].x - m_carrier_default_x_left) +
						   (m_carrier_coordinates_map[coordinates_id_2].x - m_carrier_default_x_left);
			t_distance_z = m_carrier_coordinates_map[coordinates_id_1].z - m_carrier_coordinates_map[coordinates_id_2].z;
			if ( t_distance_z<0 )
			{
				t_distance_z = -t_distance_z;
			}
			distance = t_distance_x + t_distance_z;
		}
		else if ( parkspace_path == Dispatch_coordinates::Parkspace_path::RIGHT_PATH )
		{
			t_distance_x = (m_carrier_default_x_right - m_carrier_coordinates_map[coordinates_id_1].x ) +
						   (m_carrier_default_x_right - m_carrier_coordinates_map[coordinates_id_2].x );
			t_distance_z = m_carrier_coordinates_map[coordinates_id_1].z - m_carrier_coordinates_map[coordinates_id_2].z;
			if ( t_distance_z<0 )
			{
				t_distance_z = -t_distance_z;
			}
			distance = t_distance_x + t_distance_z;
		}
		else if ( parkspace_path == Dispatch_coordinates::Parkspace_path::TEMPORARY_CACHE_PATH )
		{
			t_distance_x = m_carrier_coordinates_map[coordinates_id_1].x - m_carrier_coordinates_map[coordinates_id_2].x;
			if ( t_distance_x<0 )
			{
				t_distance_x = -t_distance_x;
			}
			t_distance_z = m_carrier_coordinates_map[coordinates_id_1].z - m_carrier_coordinates_map[coordinates_id_2].z;
			if ( t_distance_z<0 )
			{
				t_distance_z = -t_distance_z;
			}
			distance = t_distance_x + t_distance_z;
		}
		else
		{
			return Error_manager(Error_code::DISPATCH_COORDINATES_PATH_ERROR, Error_level::MINOR_ERROR,
								 " Dispatch_coordinates::get_distance parkspace_path error ");
		}
	}
	else
	{
		return Error_manager(Error_code::DISPATCH_COORDINATES_ID_ERROR, Error_level::MINOR_ERROR,
							 " Dispatch_coordinates::get_distance coordinates_id error ");
	}
	return Error_code::SUCCESS;
}

//把1楼出入口的坐标修正到2楼.
bool Dispatch_coordinates::amend_coordinates_from_1st_floor_to_2nd_floor(int & coordinates_id)
{
	if ( coordinates_id >= PASSAGEWAY_ID_BASE+1 && coordinates_id<= PASSAGEWAY_ID_BASE + m_passageway_terminal_id_max-1)
	{
		coordinates_id = coordinates_id+100;	//例如:把 1103 改为 1203
		return true;
	}
	else
	{
		return false;
	}
	return false;
}

//选择最近的搬运器, return true:选左边, return false:选右边
bool Dispatch_coordinates::choice_nearest_carrier(int dispatch_source, int coordinates_id_1, int coordinates_id_2)
{
	Error_manager t_error;
	//选最近的搬运器
	float t_distance_left = 0;
	t_error = get_distance(dispatch_source, coordinates_id_1, Dispatch_coordinates::Parkspace_path::LEFT_PATH, t_distance_left );
	if ( t_error != Error_code::SUCCESS )
	{
		LOG(INFO) << " Dispatch_manager::resource_allocation() m_process_pickup_list error = "<< t_error.to_string() << "   " << this;
	}
	float t_distance_right = 0;
	t_error = get_distance(dispatch_source, coordinates_id_2, Dispatch_coordinates::Parkspace_path::RIGHT_PATH, t_distance_right );
	if ( t_error != Error_code::SUCCESS )
	{
		LOG(INFO) << " Dispatch_manager::resource_allocation() m_process_pickup_list error = "<< t_error.to_string() << "   " << this;
	}
	if ( t_distance_left <= t_distance_right )
	{
		return true;
	}
	else
	{
		return false;
	}
}

//判断取车的路径
Dispatch_coordinates::Parkspace_path Dispatch_coordinates::get_pickup_path(int dispatch_source, int dispatch_destination)
{
	if ( dispatch_source >= 1 && dispatch_source <= 165 &&
		 dispatch_destination >= 1100 && dispatch_destination <= 1107)
	{
		if ( dispatch_source >= 19 && dispatch_source <= 27 )
		{
			if ( dispatch_destination >= 1101 && dispatch_destination <= 1106 )
			{
				return Dispatch_coordinates::Parkspace_path::THIRD_FLOOR_PATH;
			}
			else
			{
				return Dispatch_coordinates::Parkspace_path::UNKNOW_PATH;
			}
		}
		else
		{
			int t_columns = dispatch_source % 15;
			if ( (t_columns ==1 || t_columns == 2 || t_columns ==3) ||
				 (dispatch_source >= 34 && dispatch_source <= 37) )
			{
				if ( dispatch_destination >= 1100 && dispatch_destination <= 1106 )
				{
					return Dispatch_coordinates::Parkspace_path::LEFT_PATH;
				}
				else
				{
					return Dispatch_coordinates::Parkspace_path::UNKNOW_PATH;
				}
			}
			else if ( (t_columns ==13 || t_columns == 14 || t_columns ==0) ||
					  (dispatch_source >= 38 && dispatch_source <= 42) )
			{
				if ( dispatch_destination >= 1101 && dispatch_destination <= 1107 )
				{
					return Dispatch_coordinates::Parkspace_path::RIGHT_PATH;
				}
				else
				{
					return Dispatch_coordinates::Parkspace_path::UNKNOW_PATH;
				}
			}
			else
			{
				if ( dispatch_destination >= 1101 && dispatch_destination <= 1106 )
				{
					return Dispatch_coordinates::Parkspace_path::DOUBLE_PATH;
				}
				else if ( dispatch_destination == 1100 )
				{
					return Dispatch_coordinates::Parkspace_path::LEFT_PATH;
				}
				else if ( dispatch_destination == 1107 )
				{
					return Dispatch_coordinates::Parkspace_path::RIGHT_PATH;
				}
				else
				{
					return Dispatch_coordinates::Parkspace_path::UNKNOW_PATH;
				}
			}
		}
	}
	else
	{
		return Dispatch_coordinates::Parkspace_path::UNKNOW_PATH;
	}

	return Dispatch_coordinates::Parkspace_path::UNKNOW_PATH;
}


//检查停车位和取车位, 判断是否能改道
bool Dispatch_coordinates::is_able_redirect(int pickup_parkspace_id, int store_parkspace_id)
{
	return true;
}

//搬运器尝试空间加锁
Error_manager Dispatch_coordinates::carrier_try_space_lock(int row_min, int column_min, int row_max, int column_max, int carrier_id, int& avoid_catcher_id )
{
	std::unique_lock<std::mutex> t_lock(m_lock);
	bool try_flag = true;
	avoid_catcher_id = -1;
	for (int i = row_min; i <= row_max; ++i)
	{
		for (int j = column_min; j <= column_max ; ++j)
		{
			if ( mpp_space_lock[i][j].m_carrier_id != -1 &&
				 mpp_space_lock[i][j].m_carrier_id != carrier_id )
			{
				try_flag = false;
				break;
			}
			if ( mpp_space_lock[i][j].m_catcher_id != -1 )
			{
				try_flag = false;
				avoid_catcher_id = mpp_space_lock[i][j].m_catcher_id;
				break;
			}
		}
	}

	if ( try_flag )
	{
		for (int i = row_min; i <= row_max; ++i)
		{
			for (int j = column_min; j <= column_max ; ++j)
			{
				mpp_space_lock[i][j].m_carrier_id = carrier_id;
			}
		}
		return Error_code::SUCCESS;
	}
	else
	{
		return Error_manager(Error_code::DISPATCH_COORDINATES_CAN_NOT_LOCK, Error_level::MINOR_ERROR,
							 " Dispatch_coordinates::carrier_try_space_lock error ");
	}
}
//抓取器尝试空间加锁
Error_manager Dispatch_coordinates::catcher_try_space_lock(int row_min, int column_min, int row_max, int column_max, int catcher_id )
{
	std::unique_lock<std::mutex> t_lock(m_lock);
	bool try_flag = true;
	for (int i = row_min; i <= row_max; ++i)
	{
		for (int j = column_min; j <= column_max ; ++j)
		{
			if ( (mpp_space_lock[i][j].m_catcher_id != -1 && mpp_space_lock[i][j].m_catcher_id != catcher_id)
				 || mpp_space_lock[i][j].m_carrier_id != -1)
			{
				try_flag = false;
				break;
			}
		}
	}

	if ( try_flag )
	{
		for (int i = row_min; i <= row_max; ++i)
		{
			for (int j = column_min; j <= column_max ; ++j)
			{
				mpp_space_lock[i][j].m_catcher_id = catcher_id;
			}
		}
		return Error_code::SUCCESS;
	}
	else
	{
		return Error_manager(Error_code::DISPATCH_COORDINATES_CAN_NOT_LOCK, Error_level::MINOR_ERROR,
							 " Dispatch_coordinates::catcher_try_space_lock error ");
	}
}
//搬运器强制空间加锁(不做判断了, 直接加锁)
Error_manager Dispatch_coordinates::carrier_force_space_lock(int row_min, int column_min, int row_max, int column_max, int carrier_id )
{
	//不做判断了, 直接加锁
	std::unique_lock<std::mutex> t_lock(m_lock);
	for (int i = row_min; i <= row_max; ++i)
	{
		for (int j = column_min; j <= column_max ; ++j)
		{
			mpp_space_lock[i][j].m_carrier_id = carrier_id;
		}
	}
	return Error_code::SUCCESS;
}
//抓取器强制空间加锁(不做判断了, 直接加锁)
Error_manager Dispatch_coordinates::catcher_force_space_lock(int row_min, int column_min, int row_max, int column_max, int catcher_id )
{
	//不做判断了, 直接加锁
	std::unique_lock<std::mutex> t_lock(m_lock);
	for (int i = row_min; i <= row_max; ++i)
	{
		for (int j = column_min; j <= column_max ; ++j)
		{
			mpp_space_lock[i][j].m_catcher_id = catcher_id;
		}
	}
	return Error_code::SUCCESS;
}
//搬运器解锁, 退回电梯井, 全部解锁
Error_manager Dispatch_coordinates::carrier_space_unlock(int carrier_id)
{
	std::unique_lock<std::mutex> t_lock(m_lock);
	for (int i = 0; i < m_space_lock_rows; ++i)
	{
		for (int j = 0; j < m_space_lock_columns ; ++j)
		{
			if ( mpp_space_lock[i][j].m_carrier_id == carrier_id )
			{
				mpp_space_lock[i][j].m_carrier_id = -1;
			}
		}
	}
	return Error_code::SUCCESS;
}
//抓取器解锁, 上升到最上层, 全部解锁, 保留4楼的空间锁
Error_manager Dispatch_coordinates::catcher_space_unlock(int catcher_coordinates_id, int catcher_id)
{
	std::unique_lock<std::mutex> t_lock(m_lock);
	for (int i = 0; i < m_space_lock_rows; ++i)
	{
		for (int j = 0; j < m_space_lock_columns ; ++j)
		{
			if ( mpp_space_lock[i][j].m_catcher_id == catcher_id )
			{
				mpp_space_lock[i][j].m_catcher_id = -1;
			}
		}
	}
	//保留4楼的空间锁
	if ( catcher_coordinates_id >= 34 && catcher_coordinates_id <= 42 )
	{
		int t_coordinates_columns = catcher_coordinates_id -30-1;
		catcher_3rd_4st_floor_space_lock(t_coordinates_columns, catcher_id);
	}
	else if ( catcher_coordinates_id >= 1401 && catcher_coordinates_id <= 1406 )
	{
		int t_coordinates_columns = catcher_coordinates_id - 1400;
		int temp = (t_coordinates_columns-1)%2;
		t_coordinates_columns = 4 + ((t_coordinates_columns-1)/2)*3 + temp*2 -1;
		catcher_3rd_4st_floor_space_lock(t_coordinates_columns, catcher_id);
	}
	else
	{
		return Error_manager(Error_code::DISPATCH_COORDINATES_ID_ERROR, Error_level::MINOR_ERROR,
							 " Dispatch_coordinates::catcher_space_unlock error ");
	}
	return Error_code::SUCCESS;
}
//抓取器 加锁3楼和4楼的2*3个车位
Error_manager Dispatch_coordinates::catcher_3rd_4st_floor_space_lock(int catcher_4st_floor_coordinates_columns, int catcher_id)
{
	//周围2*3个车位 加锁
	mpp_space_lock[1][catcher_4st_floor_coordinates_columns-1].m_catcher_id = catcher_id;
	mpp_space_lock[1][catcher_4st_floor_coordinates_columns].m_catcher_id = catcher_id;
	mpp_space_lock[1][catcher_4st_floor_coordinates_columns+1].m_catcher_id = catcher_id;
	mpp_space_lock[2][catcher_4st_floor_coordinates_columns-1].m_catcher_id = catcher_id;
	mpp_space_lock[2][catcher_4st_floor_coordinates_columns].m_catcher_id = catcher_id;
	mpp_space_lock[2][catcher_4st_floor_coordinates_columns+1].m_catcher_id = catcher_id;
	return Error_code::SUCCESS;
}

void Dispatch_coordinates::set_space_lock(int space_rows, int space_columns, int catcher_id, int carrier_id)
{
	std::unique_lock<std::mutex> t_lock(m_lock);
	mpp_space_lock[space_rows][space_columns].m_catcher_id = catcher_id;
	mpp_space_lock[space_rows][space_columns].m_carrier_id = carrier_id;
}
void Dispatch_coordinates::set_space_lock_for_catcher(int space_rows, int space_columns, int catcher_id)
{
	std::unique_lock<std::mutex> t_lock(m_lock);
	mpp_space_lock[space_rows][space_columns].m_catcher_id = catcher_id;
}
void Dispatch_coordinates::set_space_lock_for_carrier(int space_rows, int space_columns, int carrier_id)
{
	std::unique_lock<std::mutex> t_lock(m_lock);
	mpp_space_lock[space_rows][space_columns].m_carrier_id = carrier_id;
}
void Dispatch_coordinates::set_space_lock(int space_id, int catcher_id, int carrier_id)
{
	int space_rows = (space_id-1) / m_space_lock_columns;
	int space_columns = (space_id-1) % m_space_lock_columns;
	set_space_lock(space_rows, space_columns, catcher_id, carrier_id);
}
void Dispatch_coordinates::set_space_lock_for_catcher(int space_id, int catcher_id)
{
	int space_rows = (space_id-1) / m_space_lock_columns;
	int space_columns = (space_id-1) % m_space_lock_columns;
	set_space_lock_for_catcher(space_rows, space_columns, catcher_id);
}
void Dispatch_coordinates::set_space_lock_for_carrier(int space_id, int carrier_id)
{
	int space_rows = (space_id-1) / m_space_lock_columns;
	int space_columns = (space_id-1) % m_space_lock_columns;
	set_space_lock_for_carrier(space_rows, space_columns, carrier_id);
}
void Dispatch_coordinates::cout_space_lock()
{
	std::unique_lock<std::mutex> t_lock(m_lock);
	std::cout << " huli test :::: " << " Dispatch_coordinates::cout_space_lock() = " << "mpp_space_lock" << std::endl;
	for (int i = m_space_lock_rows-1; i >=0; --i)
	{
		for (int j = m_space_lock_columns-1; j >=0  ; --j)
		{
			if ( mpp_space_lock[i][j].m_carrier_id == -1 )
			{
				printf("( _");
			}
			else
			{
				printf("(%d_", mpp_space_lock[i][j].m_carrier_id);
			}
			if ( mpp_space_lock[i][j].m_catcher_id == -1 )
			{
				printf(" )    ");
			}
			else
			{
				printf("%d)    ", mpp_space_lock[i][j].m_catcher_id);
			}
		}
		std::cout << std::endl;
	}
}
