12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139 |
- /*
- * @Description: 车位数据库管理
- * @Author: yct
- * @Date: 2020-07-19 09:57:45
- * @LastEditTime: 2020-07-22 19:07:45
- * @LastEditors: yct
- */
- #include "parkspace_operating_function.h"
- // 从数据库获得所有车位信息,用于车位模块初始化
- Error_manager Parkspace_operating_function::get_all_parkspace_info(message::Parkspace_allocation_data_msg &all_parkspace_info)
- {
- //执行sql操作
- std::string get_all_space_sql = "select * from parkingspace";
- boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
- Error_manager ec = Database_controller::get_instance_pointer()->sql_query(get_all_space_sql, tp_result);
- if(ec == SUCCESS)
- {
- if (tp_result == nullptr)
- {
- return DB_RESULT_SET_EMPTY;
- }
- all_parkspace_info.clear_parkspace_info_ex();
- while (tp_result->next())
- {
- message::Parkspace_info *t_parkspace = all_parkspace_info.add_parkspace_info_ex();
- char buf[1024];
- memset(buf, 0, 1024);
- try
- {
- t_parkspace->set_parkingspace_index_id(tp_result->getInt("parkingSpaceID"));
- t_parkspace->set_parkingspace_unit_id(tp_result->getInt("parkingspace_unit_id"));
- t_parkspace->set_parkingspace_label_id(tp_result->getInt("parkingspace_label_id"));
- t_parkspace->set_parkingspace_room_id(tp_result->getInt("parkingspace_room_id"));
- if (tp_result->getInt("parkingspace_direction") > 0)
- {
- t_parkspace->set_parkingspace_direction(message::Direction::eForward);
- }
- else
- {
- t_parkspace->set_parkingspace_direction(message::Direction::eBackward);
- }
- t_parkspace->set_parkingspace_floor_id(tp_result->getInt("parkingspace_floor_id"));
- t_parkspace->set_parkingspace_status((message::Parkspace_status)tp_result->getInt("parkingspace_status"));
- t_parkspace->set_parkingspace_width(tp_result->getDouble("parkingspace_width"));
- t_parkspace->set_parkingspace_height(tp_result->getDouble("parkingspace_height"));
- t_parkspace->mutable_car_info()->set_car_numberplate(tp_result->getString("parkSpaceCarNumberPlate"));
- t_parkspace->mutable_car_info()->set_license(tp_result->getString("parkSpaceCarLicense"));
- t_parkspace->mutable_car_info()->set_car_length(tp_result->getDouble("parkSpaceCarLength"));
- t_parkspace->mutable_car_info()->set_car_width(tp_result->getDouble("parkSpaceCarWidth"));
- t_parkspace->mutable_car_info()->set_car_height(tp_result->getDouble("parkSpaceCarHeight"));
- t_parkspace->mutable_car_info()->set_car_wheel_base(tp_result->getDouble("parkSpaceCarWheelBase"));
- t_parkspace->mutable_car_info()->set_car_wheel_width(tp_result->getDouble("parkSpaceCarWheelWidth"));
- t_parkspace->set_car_type((message::Car_type)tp_result->getInt("parkSpaceCarType"));
- t_parkspace->set_entry_time(tp_result->getString("entryTime"));
- t_parkspace->set_leave_time(tp_result->getString("leaveTime"));
- }
- 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 SUCCESS;
- }
- else
- {
- return ec;
- }
- }
- // 从数据库获得所有指定类型空车位车位信息,用于车位模块初始化
- Error_manager Parkspace_operating_function::get_specify_the_type_parkspace_info(message::Parkspace_allocation_data_msg &all_specify_the_type_parkspace_info,int parkspace_type)
- {
- //执行sql操作
- char all_specify_the_type_sql[1024];
- memset(all_specify_the_type_sql, 0, 1024);
- sprintf(all_specify_the_type_sql,"select * from parkingspace where parkingspace_type= %d and parkingspace_status = %d",parkspace_type,message::Parkspace_status::eParkspace_empty);
- boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
- Error_manager ec = Database_controller::get_instance_pointer()->sql_query(all_specify_the_type_sql, tp_result);
- if(ec == SUCCESS)
- {
- if (tp_result == nullptr)
- {
- return DB_RESULT_SET_EMPTY;
- }
- all_specify_the_type_parkspace_info.clear_parkspace_info_ex();
- while (tp_result->next())
- {
- message::Parkspace_info *t_parkspace = all_specify_the_type_parkspace_info.add_parkspace_info_ex();
- char buf[1024];
- memset(buf, 0, 1024);
- try
- {
- t_parkspace->set_parkingspace_index_id(tp_result->getInt("parkingSpaceID"));
- t_parkspace->set_parkingspace_unit_id(tp_result->getInt("parkingspace_unit_id"));
- t_parkspace->set_parkingspace_label_id(tp_result->getInt("parkingspace_label_id"));
- t_parkspace->set_parkingspace_room_id(tp_result->getInt("parkingspace_room_id"));
- if (tp_result->getInt("parkingspace_direction") > 0)
- {
- t_parkspace->set_parkingspace_direction(message::Direction::eForward);
- }
- else
- {
- t_parkspace->set_parkingspace_direction(message::Direction::eBackward);
- }
- t_parkspace->set_parkingspace_floor_id(tp_result->getInt("parkingspace_floor_id"));
- t_parkspace->set_parkingspace_status((message::Parkspace_status)tp_result->getInt("parkingspace_status"));
- t_parkspace->set_parkingspace_width(tp_result->getDouble("parkingspace_width"));
- t_parkspace->set_parkingspace_height(tp_result->getDouble("parkingspace_height"));
- t_parkspace->mutable_car_info()->set_car_numberplate(tp_result->getString("parkSpaceCarNumberPlate"));
- t_parkspace->mutable_car_info()->set_license(tp_result->getString("parkSpaceCarLicense"));
- t_parkspace->mutable_car_info()->set_car_length(tp_result->getDouble("parkSpaceCarLength"));
- t_parkspace->mutable_car_info()->set_car_width(tp_result->getDouble("parkSpaceCarWidth"));
- t_parkspace->mutable_car_info()->set_car_height(tp_result->getDouble("parkSpaceCarHeight"));
- t_parkspace->set_car_type((message::Car_type)tp_result->getInt("parkSpaceCarType"));
- t_parkspace->mutable_car_info()->set_car_wheel_base(tp_result->getDouble("parkSpaceCarWheelBase"));
- t_parkspace->mutable_car_info()->set_car_wheel_width(tp_result->getDouble("parkSpaceCarWheelWidth"));
- t_parkspace->set_entry_time(tp_result->getString("entryTime"));
- t_parkspace->set_leave_time(tp_result->getString("leaveTime"));
- }
- 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 SUCCESS;
- }
- else
- {
- return ec;
- }
- }
- // 从数据库获得所有指定类型指定单元空车位车位信息,用于车位模块初始化
- Error_manager Parkspace_operating_function::get_specify_the_type_status_unit_parkspace_info(message::Parkspace_allocation_data_msg &all_specify_the_type_parkspace_info,int parkspace_type,message::Parkspace_status status,int unit)
- {
- //执行sql操作
- char all_specify_the_type_sql[1024];
- memset(all_specify_the_type_sql, 0, 1024);
- sprintf(all_specify_the_type_sql,"select * from parkingspace where parkingspace_type= %d and parkingspace_status = %d and parkingspace_unit_id = %d",parkspace_type,status,unit);
- boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
- Error_manager ec = Database_controller::get_instance_pointer()->sql_query(all_specify_the_type_sql, tp_result);
- if(ec == SUCCESS)
- {
- if (tp_result == nullptr)
- {
- return DB_RESULT_SET_EMPTY;
- }
- all_specify_the_type_parkspace_info.clear_parkspace_info_ex();
- while (tp_result->next())
- {
- message::Parkspace_info *t_parkspace = all_specify_the_type_parkspace_info.add_parkspace_info_ex();
- char buf[1024];
- memset(buf, 0, 1024);
- try
- {
- t_parkspace->set_parkingspace_index_id(tp_result->getInt("parkingSpaceID"));
- t_parkspace->set_parkingspace_unit_id(tp_result->getInt("parkingspace_unit_id"));
- t_parkspace->set_parkingspace_label_id(tp_result->getInt("parkingspace_label_id"));
- t_parkspace->set_parkingspace_room_id(tp_result->getInt("parkingspace_room_id"));
- if (tp_result->getInt("parkingspace_direction") > 0)
- {
- t_parkspace->set_parkingspace_direction(message::Direction::eForward);
- }
- else
- {
- t_parkspace->set_parkingspace_direction(message::Direction::eBackward);
- }
- t_parkspace->set_parkingspace_floor_id(tp_result->getInt("parkingspace_floor_id"));
- t_parkspace->set_parkingspace_status((message::Parkspace_status)tp_result->getInt("parkingspace_status"));
- t_parkspace->set_parkingspace_width(tp_result->getDouble("parkingspace_width"));
- t_parkspace->set_parkingspace_height(tp_result->getDouble("parkingspace_height"));
- t_parkspace->mutable_car_info()->set_car_numberplate(tp_result->getString("parkSpaceCarNumberPlate"));
- t_parkspace->mutable_car_info()->set_license(tp_result->getString("parkSpaceCarLicense"));
- t_parkspace->mutable_car_info()->set_car_length(tp_result->getDouble("parkSpaceCarLength"));
- t_parkspace->mutable_car_info()->set_car_width(tp_result->getDouble("parkSpaceCarWidth"));
- t_parkspace->mutable_car_info()->set_car_height(tp_result->getDouble("parkSpaceCarHeight"));
- t_parkspace->set_car_type((message::Car_type)tp_result->getInt("parkSpaceCarType"));
- t_parkspace->mutable_car_info()->set_car_wheel_base(tp_result->getDouble("parkSpaceCarWheelBase"));
- t_parkspace->mutable_car_info()->set_car_wheel_width(tp_result->getDouble("parkSpaceCarWheelWidth"));
- t_parkspace->set_entry_time(tp_result->getString("entryTime"));
- t_parkspace->set_leave_time(tp_result->getString("leaveTime"));
- }
- 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 SUCCESS;
- }
- else
- {
- return ec;
- }
- }
- // 清除数据库中所有车位号牌,状态全修改为空闲
- Error_manager Parkspace_operating_function::clear_all_parkspace_info()
- {
- //执行sql操作
- char update_space_sql[1024];
- memset(update_space_sql, 0, 1024);
- sprintf(update_space_sql, "update parkingspace set parkingspace_status = %d,parkSpaceCarLicense = NULL,parkSpaceCarLength = 0,parkSpaceCarWidth = 0,parkSpaceCarHeight = 0,parkSpaceCarType = 0,parkSpaceCarWheelBase = 0,parkSpaceCarWheelWidth = 0,entryTime = NULL,leaveTime = NULL",message::Parkspace_status::eParkspace_empty);
- Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_space_sql);
- return ec;
- }
- // 更新数据库中车位状态
- //根据车位ID更改车位状态
- Error_manager Parkspace_operating_function::update_parkspace_data(message::Parkspace_info parkspace_info)
- {
- //执行sql操作
- char update_space_sql[1024];
- memset(update_space_sql, 0, 1024);
- int status = parkspace_info.parkingspace_status();
- if ( status < 1 || status >5 )
- {
- return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
- "参数错误 Parkspace_operating_function::update_parkspace_data error ");
- }
- // switch (parkspace_info.parkingspace_status())
- // {
- // case message::Parkspace_status::eParkspace_empty:
- // status = 0;
- // break;
- // case message::Parkspace_status::eParkspace_occupied:
- // status = 1;
- // break;
- // case message::Parkspace_status::eParkspace_reserved:
- // status = 2;
- // break;
- // case message::Parkspace_status::eParkspace_locked:
- // status = 3;
- // break;
- // case message::Parkspace_status::eParkspace_error:
- // status = 4;
- // break;
- // default:
- // return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
- // "参数错误 Parkspace_operating_function::update_parkspace_data error ");
- // break;
- // }
- if(parkspace_info.has_car_info() && status!=message::Parkspace_status::eParkspace_empty)
- {
- sprintf(update_space_sql, "update parkingspace set parkingspace_status = %d,parkSpaceCarNumberPlate = '%s',parkSpaceCarLicense = '%s',parkSpaceCarLength = %.3f,parkSpaceCarWidth = %.3f,parkSpaceCarHeight = %.3f,parkSpaceCarType = %d,parkSpaceCarWheelBase = %.3f,parkSpaceCarWheelWidth = %.3f,entryTime = '%s',leaveTime = '%s' where parkingSpaceID = %d",
- status,
- parkspace_info.car_info().car_numberplate().c_str(),
- parkspace_info.car_info().license().c_str(),
- parkspace_info.car_info().has_car_length()?parkspace_info.car_info().car_length():0.0f,
- parkspace_info.car_info().car_width(),
- parkspace_info.car_info().car_height(),
- parkspace_info.car_type(),
- parkspace_info.car_info().car_wheel_base(),
- parkspace_info.car_info().car_wheel_width(),
- parkspace_info.entry_time().c_str(),
- parkspace_info.leave_time().c_str(),
- parkspace_info.parkingspace_index_id());
- }else
- {
- sprintf(update_space_sql, "update parkingspace set parkingspace_status = %d,parkSpaceCarNumberPlate = NULL,parkSpaceCarLicense = NULL,parkSpaceCarLength = 0,parkSpaceCarWidth = 0,parkSpaceCarHeight = 0,parkSpaceCarType = 0,parkSpaceCarWheelBase = 0,parkSpaceCarWheelWidth = 0,entryTime = NULL,leaveTime = NULL where parkingSpaceID = %d", status, parkspace_info.parkingspace_index_id());
- }
- //boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
- Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_space_sql);
- return ec;
- }
- // 更新数据库车辆信息,车位信息中包含车辆信息用于匹配更新
- Error_manager Parkspace_operating_function::update_vehicle_with_parkspace(message::Parkspace_info parkspace_info, message::Vehicle_status &vehicle_status)
- {
- //执行sql操作
- if(!parkspace_info.has_car_info())
- {
- return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
- "参数错误 Parkspace_operating_function::update_vehicle_with_parkspace error ");;
- }
- std::string find_vehicle_sql = std::string("select * from vehicle where numberPlate = '").append(parkspace_info.car_info().license()).append("'");
- boost::shared_ptr<sql::ResultSet> query_vehicle_result = nullptr;
- Database_controller::get_instance_pointer()->sql_query(find_vehicle_sql, query_vehicle_result);
- //判断车辆是否存在
- if(query_vehicle_result == nullptr || !query_vehicle_result->next())
- {
- return insert_vehicle_with_parkspace(parkspace_info, vehicle_status);
- }
- else
- {
- char update_vehicle_sql[1024];
- memset(update_vehicle_sql, 0, 1024);
- int vehicle_status_code = -1;
- switch (vehicle_status)
- {
- case message::Vehicle_status::eVehicle_idle:
- vehicle_status_code = 1;
- break;
- case message::Vehicle_status::eVehicle_in_garage:
- vehicle_status_code = 2;
- break;
- case message::Vehicle_status::eVehicle_parking:
- vehicle_status_code = 3;
- break;
- case message::Vehicle_status::eVehicle_fetching:
- vehicle_status_code = 4;
- break;
- case message::Vehicle_status::eVehicle_reserved:
- vehicle_status_code = 5;
- break;
- default:
- return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
- "参数错误 Parkspace_operating_function::update_vehicle_with_parkspace error ");
- break;
- }
- if ( vehicle_status_code == 1 )
- {
- sprintf(update_vehicle_sql, "update vehicle set vehicleParkState = 1,carLength=NULL,carWidth=NULL,carHeight=NULL,carWheelBase = NULL,carWheelWidth = NULL,parkingSpaceID=NULL,parkingRecordsID=NULL where numberPlate = '%s'",
- parkspace_info.car_info().license().c_str());
- }
- //车位为空,仅更新车辆状态与长宽高
- else if(parkspace_info.parkingspace_index_id() <= 0)
- {
- sprintf(update_vehicle_sql, "update vehicle set vehicleParkState = %d,carLength=%.3f,carWidth=%.3f,carHeight=%.3f,carWheelBase = %.3f,carWheelWidth = %.3f where numberPlate = '%s'",
- vehicle_status_code,
- parkspace_info.car_info().car_length(),
- parkspace_info.car_info().car_width(),
- parkspace_info.car_info().car_height(),
- parkspace_info.car_info().car_wheel_base(),
- parkspace_info.car_info().car_wheel_width(),
- parkspace_info.car_info().license().c_str());
- }
- //车位不为空,表示车辆所在车位,同时更新
- else
- {
- sprintf(update_vehicle_sql, "update vehicle set vehicleParkState = %d,carLength=%.3f,carWidth=%.3f,carHeight=%.3f,carWheelBase = %.3f,carWheelWidth = %.3f,parkingSpaceID=%d where numberPlate = '%s'",
- vehicle_status_code,
- parkspace_info.car_info().car_length(),
- parkspace_info.car_info().car_width(),
- parkspace_info.car_info().car_height(),
- parkspace_info.car_info().car_wheel_base(),
- parkspace_info.car_info().car_wheel_width(),
- parkspace_info.parkingspace_index_id(),
- parkspace_info.car_info().license().c_str());
- }
- Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_vehicle_sql);
- return ec;
- }
- }
- //更新停车记录ID
- Error_manager Parkspace_operating_function::update_record_id(std::string license, int record_id)
- {
- message::Vehicle_status vehicle_status;
- int t_record_id;
- Error_manager ec = query_vehicle(license,vehicle_status,t_record_id);
- if ( vehicle_status != message::Vehicle_status::eVehicle_in_garage )
- {
- return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
- " Parkspace_operating_function::insert_vehicle_with_parkspace error ");
- }
- char update_vehicle_sql[1024];
- memset(update_vehicle_sql, 0, 1024);
- sprintf(update_vehicle_sql, "update vehicle set parkingRecordsID = %d where numberPlate = '%s'",record_id,license.c_str());
- ec = Database_controller::get_instance_pointer()->sql_update(update_vehicle_sql);
- return ec;
- }
- // 插入车辆
- Error_manager Parkspace_operating_function::insert_vehicle_with_parkspace(message::Parkspace_info parkspace_info, message::Vehicle_status &vehicle_status)
- {
- //执行sql操作
- if(!parkspace_info.has_car_info())
- {
- return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
- " Parkspace_operating_function::insert_vehicle_with_parkspace error ");
- }
- char insert_vehicle_sql[1024];
- memset(insert_vehicle_sql, 0, 1024);
- int vehicle_status_code = -1;
- switch (vehicle_status)
- {
- case message::Vehicle_status::eVehicle_idle:
- vehicle_status_code = 1;
- break;
- case message::Vehicle_status::eVehicle_in_garage:
- vehicle_status_code = 2;
- break;
- case message::Vehicle_status::eVehicle_parking:
- vehicle_status_code = 3;
- break;
- case message::Vehicle_status::eVehicle_fetching:
- vehicle_status_code = 4;
- break;
- case message::Vehicle_status::eVehicle_reserved:
- vehicle_status_code = 5;
- break;
- default:
- return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
- "参数错误 Parkspace_operating_function::insert_vehicle_with_parkspace error ");
- break;
- }
- //车位为空,仅更新车辆状态与长宽高
- if(parkspace_info.parkingspace_index_id() <= 0)
- {
- sprintf(insert_vehicle_sql, "INSERT INTO vehicle (numberPlate,vehicleParkState,carLength,carWidth,carHeight,carWheelBase,carWheelWidth) values ('%s',%d,%.3f,%.3f,%.3f,%.3f,%.3f)",
- parkspace_info.car_info().license().c_str(),
- vehicle_status_code,
- parkspace_info.car_info().car_length(),
- parkspace_info.car_info().car_width(),
- parkspace_info.car_info().car_height(),
- parkspace_info.car_info().car_wheel_base(),
- parkspace_info.car_info().car_wheel_width());
- }
- //车位不为空,表示车辆在车位,同时更新
- else
- {
- sprintf(insert_vehicle_sql, "INSERT INTO vehicle (numberPlate,vehicleParkState,carLength,carWidth,carHeight,carWheelBase,carWheelWidth,parkingSpaceID) values ('%s',%d,%.3f,%.3f,%.3f,%.3f,%.3f,%d)",
- parkspace_info.car_info().license().c_str(),
- vehicle_status_code,
- parkspace_info.car_info().car_length(),
- parkspace_info.car_info().car_width(),
- parkspace_info.car_info().car_height(),
- parkspace_info.car_info().car_wheel_base(),
- parkspace_info.car_info().car_wheel_width(),
- parkspace_info.parkingspace_index_id());
- }
- Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_vehicle_sql);
- return ec;
- }
- // 插入停车记录
- Error_manager Parkspace_operating_function::insert_parking_record(message::Parkspace_info parkspace_info)
- {
- //参数中必须包含车辆信息与车位编号
- if(!parkspace_info.has_car_info() || parkspace_info.parkingspace_index_id() <= 0)
- {
- return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
- "参数错误 Parkspace_operating_function::insert_parking_record error ");
- }
- char insert_parking_record_sql[1024];
- memset(insert_parking_record_sql, 0, 1024);
- //将车辆号牌,车位ID,记录状态,停车与取车时间写入
- sprintf(insert_parking_record_sql, "INSERT INTO parkingrecords (numberPlate,parkingSpaceID,realParkTime) values ('%s',%d,'%s')",
- parkspace_info.car_info().license().c_str(),
- parkspace_info.parkingspace_index_id(),
- parkspace_info.entry_time().c_str());
- Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_parking_record_sql);
- return ec;
- }
- // 更新停车记录,待计费系统加入后完善!!!
- Error_manager Parkspace_operating_function::update_parking_record(message::Parkspace_info parkspace_info, int record_id)
- {
- //执行sql操作
- if(!parkspace_info.has_car_info() || parkspace_info.parkingspace_index_id() <= 0)
- {
- return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
- "参数错误 update_parking_record::insert_parking_record error ");;
- }
- char update_parking_record_sql[1024];
- memset(update_parking_record_sql, 0, 1024);
- sprintf(update_parking_record_sql, "update parkingrecords set realGetTime='%s',parkingPrice=%d where parkingRecordsID = %d",
- parkspace_info.leave_time().c_str(),
- 0,
- record_id);
- Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_parking_record_sql);
- return ec;
- }
- // 根据车位编号查询车位信息
- Error_manager Parkspace_operating_function::query_one_parkspace_with_parkspace_id(int parkspace_id, message::Parkspace_info &parkspace_info)
- {
- //执行sql操作
- std::string query_parkspace_sql = std::string("select * from parkingspace where parkingSpaceID=").append(std::to_string(parkspace_id));
- 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 Error_manager(Error_code::DB_RESULT_SET_EMPTY, Error_level::MAJOR_ERROR,
- " 数据库外层查询返回结果空指针 Parkspace_operating_function::query_one_parkspace_with_parkspace_id error ");;
- }
- if (tp_result->next())
- {
- char buf[1024];
- memset(buf, 0, 1024);
- try
- {
- parkspace_info.set_parkingspace_status((message::Parkspace_status)tp_result->getInt("parkingspace_status"));
- parkspace_info.set_parkingspace_index_id(tp_result->getInt("parkingSpaceID"));
- parkspace_info.set_parkingspace_unit_id(tp_result->getInt("parkingspace_unit_id"));
- parkspace_info.set_parkingspace_label_id(tp_result->getInt("parkingspace_label_id"));
- parkspace_info.set_parkingspace_room_id(tp_result->getInt("parkingspace_room_id"));
- parkspace_info.set_parkingspace_direction(tp_result->getInt("parkingspace_direction") > 0 ? message::Direction::eForward : message::Direction::eBackward);
- parkspace_info.set_parkingspace_floor_id(tp_result->getInt("parkingspace_floor_id"));
- parkspace_info.set_parkingspace_width(tp_result->getDouble("parkingspace_width"));
- parkspace_info.set_parkingspace_height(tp_result->getDouble("parkingspace_height"));
- parkspace_info.mutable_car_info()->set_car_numberplate(tp_result->getString("parkSpaceCarNumberPlate"));
- parkspace_info.mutable_car_info()->set_license(tp_result->getString("parkSpaceCarLicense"));
- parkspace_info.mutable_car_info()->set_car_length(tp_result->getDouble("parkSpaceCarLength"));
- parkspace_info.mutable_car_info()->set_car_width(tp_result->getDouble("parkSpaceCarWidth"));
- parkspace_info.mutable_car_info()->set_car_height(tp_result->getDouble("parkSpaceCarHeight"));
- parkspace_info.set_car_type((message::Car_type)tp_result->getInt("parkSpaceCarType"));
- parkspace_info.mutable_car_info()->set_car_wheel_base(tp_result->getDouble("parkSpaceCarWheelBase"));
- parkspace_info.mutable_car_info()->set_car_wheel_width(tp_result->getDouble("parkSpaceCarWheelWidth"));
- parkspace_info.set_entry_time(tp_result->getString("entryTime"));
- parkspace_info.set_leave_time(tp_result->getString("leaveTime"));
- }
- 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);
- }
- if (tp_result->next())
- {
- return Error_manager(Error_code::DB_QUERY_DATA_REPEAT, Error_level::MAJOR_ERROR,
- "数据库查询数据重复 Parkspace_operating_function::query_one_parkspace_with_parkspace_id error ");
- }
- }
- else
- {
- return Error_manager(Error_code::DB_QUERY_NOT_DATA, Error_level::MINOR_ERROR,
- "数据库查询未找到数据 Parkspace_operating_function::query_one_parkspace_with_parkspace_id error ");
- }
- return SUCCESS;
- }
- else
- {
- return ec;
- }
- }
- // 根据车牌号查询状态为占用的车位
- Error_manager Parkspace_operating_function::query_one_occupied_parkspace_with_license(std::string license, message::Parkspace_info &parkspace_info)
- {
- //执行sql操作
- //std::string query_parkspace_sql = std::string("select * from parkingspace where parkingspace_status == 2 and parkSpaceCarLicense='").append(license).append("'");
- char query_parkspace_sql[1024];
- memset(query_parkspace_sql, 0, 1024);
- sprintf(query_parkspace_sql,"select * from parkingspace where parkingspace_status = %d and parkSpaceCarLicense='%s' ",message::Parkspace_status::eParkspace_occupied,license.c_str());
- 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 Error_manager(Error_code::DB_RESULT_SET_EMPTY, Error_level::MINOR_ERROR,
- "数据库外层查询返回结果空指针 Parkspace_operating_function::query_one_parkspace_with_license error ");
- }
- if (tp_result->next())
- {
- char buf[1024];
- memset(buf, 0, 1024);
- try
- {
- parkspace_info.set_parkingspace_status((message::Parkspace_status)tp_result->getInt("parkingspace_status"));
- parkspace_info.set_parkingspace_index_id(tp_result->getInt("parkingSpaceID"));
- parkspace_info.set_parkingspace_unit_id(tp_result->getInt("parkingspace_unit_id"));
- parkspace_info.set_parkingspace_label_id(tp_result->getInt("parkingspace_label_id"));
- parkspace_info.set_parkingspace_room_id(tp_result->getInt("parkingspace_room_id"));
- parkspace_info.set_parkingspace_direction(tp_result->getInt("parkingspace_direction") > 0 ? message::Direction::eForward : message::Direction::eBackward);
- parkspace_info.set_parkingspace_floor_id(tp_result->getInt("parkingspace_floor_id"));
- parkspace_info.set_parkingspace_width(tp_result->getDouble("parkingspace_width"));
- parkspace_info.set_parkingspace_height(tp_result->getDouble("parkingspace_height"));
- parkspace_info.mutable_car_info()->set_car_numberplate(tp_result->getString("parkSpaceCarNumberPlate"));
- parkspace_info.mutable_car_info()->set_license(tp_result->getString("parkSpaceCarLicense"));
- parkspace_info.mutable_car_info()->set_car_length(tp_result->getDouble("parkSpaceCarLength"));
- parkspace_info.mutable_car_info()->set_car_width(tp_result->getDouble("parkSpaceCarWidth"));
- parkspace_info.mutable_car_info()->set_car_height(tp_result->getDouble("parkSpaceCarHeight"));
- parkspace_info.set_car_type((message::Car_type)tp_result->getInt("parkSpaceCarType"));
- parkspace_info.mutable_car_info()->set_car_wheel_base(tp_result->getDouble("parkSpaceCarWheelBase"));
- parkspace_info.mutable_car_info()->set_car_wheel_width(tp_result->getDouble("parkSpaceCarWheelWidth"));
- parkspace_info.set_entry_time(tp_result->getString("entryTime"));
- parkspace_info.set_leave_time(tp_result->getString("leaveTime"));
- }
- 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);
- }
- if (tp_result->next())
- {
- return Error_manager(Error_code::DB_QUERY_DATA_REPEAT, Error_level::MAJOR_ERROR,
- "数据库查询数据重复 Parkspace_operating_function::query_one_parkspace_with_license error ");
- }
- }
- 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;
- }
- }
- // 根据车牌号查询车位
- Error_manager Parkspace_operating_function::query_one_parkspace_with_license(std::string license, message::Parkspace_info &parkspace_info)
- {
- //执行sql操作
- //std::string query_parkspace_sql = std::string("select * from parkingspace where parkingspace_status == 2 and parkSpaceCarLicense='").append(license).append("'");
- char query_parkspace_sql[1024];
- memset(query_parkspace_sql, 0, 1024);
- sprintf(query_parkspace_sql,"select * from parkingspace where parkingspace_status != %d and parkSpaceCarNumberPlate='%s' ",message::Parkspace_status::eParkspace_empty,license.c_str());
- 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 Error_manager(Error_code::DB_RESULT_SET_EMPTY, Error_level::MINOR_ERROR,
- "数据库外层查询返回结果空指针 Parkspace_operating_function::query_one_parkspace_with_license error ");
- }
- if (tp_result->next())
- {
- char buf[1024];
- memset(buf, 0, 1024);
- try
- {
- parkspace_info.set_parkingspace_status((message::Parkspace_status)tp_result->getInt("parkingspace_status"));
- parkspace_info.set_parkingspace_index_id(tp_result->getInt("parkingSpaceID"));
- parkspace_info.set_parkingspace_unit_id(tp_result->getInt("parkingspace_unit_id"));
- parkspace_info.set_parkingspace_label_id(tp_result->getInt("parkingspace_label_id"));
- parkspace_info.set_parkingspace_room_id(tp_result->getInt("parkingspace_room_id"));
- parkspace_info.set_parkingspace_direction(tp_result->getInt("parkingspace_direction") > 0 ? message::Direction::eForward : message::Direction::eBackward);
- parkspace_info.set_parkingspace_floor_id(tp_result->getInt("parkingspace_floor_id"));
- parkspace_info.set_parkingspace_width(tp_result->getDouble("parkingspace_width"));
- parkspace_info.set_parkingspace_height(tp_result->getDouble("parkingspace_height"));
- parkspace_info.mutable_car_info()->set_car_numberplate(tp_result->getString("parkSpaceCarNumberPlate"));
- parkspace_info.mutable_car_info()->set_license(tp_result->getString("parkSpaceCarLicense"));
- parkspace_info.mutable_car_info()->set_car_length(tp_result->getDouble("parkSpaceCarLength"));
- parkspace_info.mutable_car_info()->set_car_width(tp_result->getDouble("parkSpaceCarWidth"));
- parkspace_info.mutable_car_info()->set_car_height(tp_result->getDouble("parkSpaceCarHeight"));
- parkspace_info.set_car_type((message::Car_type)tp_result->getInt("parkSpaceCarType"));
- parkspace_info.mutable_car_info()->set_car_wheel_base(tp_result->getDouble("parkSpaceCarWheelBase"));
- parkspace_info.mutable_car_info()->set_car_wheel_width(tp_result->getDouble("parkSpaceCarWheelWidth"));
- parkspace_info.set_entry_time(tp_result->getString("entryTime"));
- parkspace_info.set_leave_time(tp_result->getString("leaveTime"));
- }
- 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);
- }
- if (tp_result->next())
- {
- return Error_manager(Error_code::DB_QUERY_DATA_REPEAT, Error_level::MAJOR_ERROR,
- "数据库查询数据重复 Parkspace_operating_function::query_one_parkspace_with_license error ");
- }
- }
- 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;
- }
- }
- // 根据车牌号查询车位
- Error_manager Parkspace_operating_function::query_one_parkspace_with_numberPlate(std::string numberPlate, message::Parkspace_info &parkspace_info)
- {
- //执行sql操作
- //std::string query_parkspace_sql = std::string("select * from parkingspace where parkingspace_status == 2 and parkSpaceCarLicense='").append(license).append("'");
- char query_parkspace_sql[1024];
- memset(query_parkspace_sql, 0, 1024);
- sprintf(query_parkspace_sql,"select * from parkingspace where parkingspace_status != %d and parkSpaceCarNumberPlate='%s' ",message::Parkspace_status::eParkspace_empty,numberPlate.c_str());
- 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 Error_manager(Error_code::DB_RESULT_SET_EMPTY, Error_level::MINOR_ERROR,
- "数据库外层查询返回结果空指针 Parkspace_operating_function::query_one_parkspace_with_license error ");
- }
- if (tp_result->next())
- {
- char buf[1024];
- memset(buf, 0, 1024);
- try
- {
- parkspace_info.set_parkingspace_status((message::Parkspace_status)tp_result->getInt("parkingspace_status"));
- parkspace_info.set_parkingspace_index_id(tp_result->getInt("parkingSpaceID"));
- parkspace_info.set_parkingspace_unit_id(tp_result->getInt("parkingspace_unit_id"));
- parkspace_info.set_parkingspace_label_id(tp_result->getInt("parkingspace_label_id"));
- parkspace_info.set_parkingspace_room_id(tp_result->getInt("parkingspace_room_id"));
- parkspace_info.set_parkingspace_direction(tp_result->getInt("parkingspace_direction") > 0 ? message::Direction::eForward : message::Direction::eBackward);
- parkspace_info.set_parkingspace_floor_id(tp_result->getInt("parkingspace_floor_id"));
- parkspace_info.set_parkingspace_width(tp_result->getDouble("parkingspace_width"));
- parkspace_info.set_parkingspace_height(tp_result->getDouble("parkingspace_height"));
- parkspace_info.mutable_car_info()->set_car_numberplate(tp_result->getString("parkSpaceCarNumberPlate"));
- parkspace_info.mutable_car_info()->set_license(tp_result->getString("parkSpaceCarLicense"));
- parkspace_info.mutable_car_info()->set_car_length(tp_result->getDouble("parkSpaceCarLength"));
- parkspace_info.mutable_car_info()->set_car_width(tp_result->getDouble("parkSpaceCarWidth"));
- parkspace_info.mutable_car_info()->set_car_height(tp_result->getDouble("parkSpaceCarHeight"));
- parkspace_info.set_car_type((message::Car_type)tp_result->getInt("parkSpaceCarType"));
- parkspace_info.mutable_car_info()->set_car_wheel_base(tp_result->getDouble("parkSpaceCarWheelBase"));
- parkspace_info.mutable_car_info()->set_car_wheel_width(tp_result->getDouble("parkSpaceCarWheelWidth"));
- parkspace_info.set_entry_time(tp_result->getString("entryTime"));
- parkspace_info.set_leave_time(tp_result->getString("leaveTime"));
- }
- 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);
- }
- if (tp_result->next())
- {
- return Error_manager(Error_code::DB_QUERY_DATA_REPEAT, Error_level::MAJOR_ERROR,
- "数据库查询数据重复 Parkspace_operating_function::query_one_parkspace_with_license error ");
- }
- }
- 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;
- }
- }
- // 找到一个空车位
- Error_manager Parkspace_operating_function::query_one_empty_parkspace(message::Parkspace_info &parkspace_info)
- {
- //执行sql操作
- std::string query_parkspace_sql = std::string("select * from parkingspace where parkingspace_status=").append(std::to_string(message::Parkspace_status::eParkspace_empty));
- 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;
- }
- if (tp_result->next())
- {
- char buf[1024];
- memset(buf, 0, 1024);
- try
- {
- if ( tp_result->getInt("parkingspace_status") != message::Parkspace_status::eParkspace_empty )
- {
- return Error_manager(Error_code::DB_QUERY_DATA_FAILED, Error_level::MAJOR_ERROR,
- "数据库查询数据错误 Parkspace_operating_function::query_one_empty_parkspace error ");
- }
- parkspace_info.set_parkingspace_status(message::Parkspace_status::eParkspace_empty);
- parkspace_info.set_parkingspace_index_id(tp_result->getInt("parkingSpaceID"));
- parkspace_info.set_parkingspace_unit_id(tp_result->getInt("parkingspace_unit_id"));
- parkspace_info.set_parkingspace_label_id(tp_result->getInt("parkingspace_label_id"));
- parkspace_info.set_parkingspace_room_id(tp_result->getInt("parkingspace_room_id"));
- parkspace_info.set_parkingspace_direction(tp_result->getInt("parkingspace_direction") > 0 ? message::Direction::eForward : message::Direction::eBackward);
- parkspace_info.set_parkingspace_floor_id(tp_result->getInt("parkingspace_floor_id"));
- parkspace_info.set_parkingspace_width(tp_result->getDouble("parkingspace_width"));
- parkspace_info.set_parkingspace_height(tp_result->getDouble("parkingspace_height"));
- parkspace_info.mutable_car_info()->set_car_numberplate(tp_result->getString("parkSpaceCarNumberPlate"));
- parkspace_info.mutable_car_info()->set_license(tp_result->getString("parkSpaceCarLicense"));
- parkspace_info.mutable_car_info()->set_car_length(tp_result->getDouble("parkSpaceCarLength"));
- parkspace_info.mutable_car_info()->set_car_width(tp_result->getDouble("parkSpaceCarWidth"));
- parkspace_info.mutable_car_info()->set_car_height(tp_result->getDouble("parkSpaceCarHeight"));
- parkspace_info.set_car_type((message::Car_type)tp_result->getInt("parkSpaceCarType"));
- parkspace_info.mutable_car_info()->set_car_wheel_base(tp_result->getDouble("parkSpaceCarWheelBase"));
- parkspace_info.mutable_car_info()->set_car_wheel_width(tp_result->getDouble("parkSpaceCarWheelWidth"));
- parkspace_info.set_entry_time(tp_result->getString("entryTime"));
- parkspace_info.set_leave_time(tp_result->getString("leaveTime"));
- }
- 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 Parkspace_operating_function::query_one_specified_unit_empty_parkspace(int unit_id,message::Parkspace_info &parkspace_info)
- {
- //执行sql操作
- char query_parkspace_sql[1024];
- memset(query_parkspace_sql, 0, 1024);
- sprintf(query_parkspace_sql,"select * from parkingspace where parkingspace_status=%d and parkingspace_unit_id=%d",message::Parkspace_status::eParkspace_empty,unit_id);
- 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;
- }
- if (tp_result->next())
- {
- char buf[1024];
- memset(buf, 0, 1024);
- try
- {
- if ( tp_result->getInt("parkingspace_status") != message::Parkspace_status::eParkspace_empty )
- {
- return Error_manager(Error_code::DB_QUERY_DATA_FAILED, Error_level::MAJOR_ERROR,
- "数据库查询数据错误 Parkspace_operating_function::query_one_empty_parkspace error ");
- }
- parkspace_info.set_parkingspace_status(message::Parkspace_status::eParkspace_empty);
- parkspace_info.set_parkingspace_index_id(tp_result->getInt("parkingSpaceID"));
- parkspace_info.set_parkingspace_unit_id(tp_result->getInt("parkingspace_unit_id"));
- parkspace_info.set_parkingspace_label_id(tp_result->getInt("parkingspace_label_id"));
- parkspace_info.set_parkingspace_room_id(tp_result->getInt("parkingspace_room_id"));
- parkspace_info.set_parkingspace_direction(tp_result->getInt("parkingspace_direction") > 0 ? message::Direction::eForward : message::Direction::eBackward);
- parkspace_info.set_parkingspace_floor_id(tp_result->getInt("parkingspace_floor_id"));
- parkspace_info.set_parkingspace_width(tp_result->getDouble("parkingspace_width"));
- parkspace_info.set_parkingspace_height(tp_result->getDouble("parkingspace_height"));
- parkspace_info.mutable_car_info()->set_car_numberplate(tp_result->getString("parkSpaceCarNumberPlate"));
- parkspace_info.mutable_car_info()->set_license(tp_result->getString("parkSpaceCarLicense"));
- parkspace_info.mutable_car_info()->set_car_length(tp_result->getDouble("parkSpaceCarLength"));
- parkspace_info.mutable_car_info()->set_car_width(tp_result->getDouble("parkSpaceCarWidth"));
- parkspace_info.mutable_car_info()->set_car_height(tp_result->getDouble("parkSpaceCarHeight"));
- parkspace_info.set_car_type((message::Car_type)tp_result->getInt("parkSpaceCarType"));
- parkspace_info.mutable_car_info()->set_car_wheel_base(tp_result->getDouble("parkSpaceCarWheelBase"));
- parkspace_info.mutable_car_info()->set_car_wheel_width(tp_result->getDouble("parkSpaceCarWheelWidth"));
- parkspace_info.set_entry_time(tp_result->getString("entryTime"));
- parkspace_info.set_leave_time(tp_result->getString("leaveTime"));
- }
- 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 Parkspace_operating_function::query_one_specified_unit_and_type_empty_parkspace(int unit_id,int parkspace_type,message::Parkspace_info &parkspace_info)
- {
- //执行sql操作
- char query_parkspace_sql[1024];
- memset(query_parkspace_sql, 0, 1024);
- sprintf(query_parkspace_sql,"select * from parkingspace where parkingspace_status=%d and parkingspace_type=%d and parkingspace_unit_id=%d",message::Parkspace_status::eParkspace_empty,parkspace_type,unit_id);
- 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;
- }
- if (tp_result->next())
- {
- char buf[1024];
- memset(buf, 0, 1024);
- try
- {
- if ( tp_result->getInt("parkingspace_status") != message::Parkspace_status::eParkspace_empty )
- {
- return Error_manager(Error_code::DB_QUERY_DATA_FAILED, Error_level::MAJOR_ERROR,
- "数据库查询数据错误 Parkspace_operating_function::query_one_empty_parkspace error ");
- }
- parkspace_info.set_parkingspace_status(message::Parkspace_status::eParkspace_empty);
- parkspace_info.set_parkingspace_index_id(tp_result->getInt("parkingSpaceID"));
- parkspace_info.set_parkingspace_unit_id(tp_result->getInt("parkingspace_unit_id"));
- parkspace_info.set_parkingspace_label_id(tp_result->getInt("parkingspace_label_id"));
- parkspace_info.set_parkingspace_room_id(tp_result->getInt("parkingspace_room_id"));
- parkspace_info.set_parkingspace_direction(tp_result->getInt("parkingspace_direction") > 0 ? message::Direction::eForward : message::Direction::eBackward);
- parkspace_info.set_parkingspace_floor_id(tp_result->getInt("parkingspace_floor_id"));
- parkspace_info.set_parkingspace_width(tp_result->getDouble("parkingspace_width"));
- parkspace_info.set_parkingspace_height(tp_result->getDouble("parkingspace_height"));
- parkspace_info.mutable_car_info()->set_car_numberplate(tp_result->getString("parkSpaceCarNumberPlate"));
- parkspace_info.mutable_car_info()->set_license(tp_result->getString("parkSpaceCarLicense"));
- parkspace_info.mutable_car_info()->set_car_length(tp_result->getDouble("parkSpaceCarLength"));
- parkspace_info.mutable_car_info()->set_car_width(tp_result->getDouble("parkSpaceCarWidth"));
- parkspace_info.mutable_car_info()->set_car_height(tp_result->getDouble("parkSpaceCarHeight"));
- parkspace_info.set_car_type((message::Car_type)tp_result->getInt("parkSpaceCarType"));
- parkspace_info.mutable_car_info()->set_car_wheel_base(tp_result->getDouble("parkSpaceCarWheelBase"));
- parkspace_info.mutable_car_info()->set_car_wheel_width(tp_result->getDouble("parkSpaceCarWheelWidth"));
- parkspace_info.set_entry_time(tp_result->getString("entryTime"));
- parkspace_info.set_leave_time(tp_result->getString("leaveTime"));
- }
- 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 Parkspace_operating_function::query_vehicle(std::string license, message::Vehicle_status &vehicle_status, int &park_record_id)
- {
- //执行sql操作
- std::string query_parkspace_sql = std::string("select * from vehicle where numberPlate='").append(license).append("'");
- 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 Error_manager(Error_code::DB_RESULT_SET_EMPTY, Error_level::MINOR_ERROR,
- "Parkspace_operating_function::query_vehicle error ");
- }
- if (tp_result->next())
- {
- char buf[1024];
- memset(buf, 0, 1024);
- try
- {
- switch (tp_result->getInt("vehicleParkState"))
- {
- case 1:
- vehicle_status = message::Vehicle_status::eVehicle_idle;
- break;
- case 2:
- vehicle_status = message::Vehicle_status::eVehicle_in_garage;
- break;
- case 3:
- vehicle_status = message::Vehicle_status::eVehicle_parking;
- break;
- case 4:
- vehicle_status = message::Vehicle_status::eVehicle_fetching;
- break;
- case 5:
- vehicle_status = message::Vehicle_status::eVehicle_reserved;
- break;
- default:
- return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
- "参数错误 Parkspace_operating_function::query_vehicle error ");
- }
- park_record_id = tp_result->getInt("parkingRecordsID");
- }
- 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 SUCCESS;
- }
- else
- {
- return ec;
- }
- }
- // 查询停车记录,根据车牌号、车位id查询,反馈停车记录id
- Error_manager Parkspace_operating_function::query_parking_record(message::Parkspace_info &parkspace_info, int &record_id)
- {
- //执行sql操作
- if(!parkspace_info.has_car_info() || parkspace_info.parkingspace_index_id() <= 0)
- {
- return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
- " Parkspace_operating_function::query_parking_record error ");
- }
- std::string query_park_record_sql = std::string("select * from parkingrecords where numberPlate='").append(parkspace_info.car_info().license()).append("' and parkingSpaceID=").append(std::to_string(parkspace_info.parkingspace_index_id())).append(" ORDER BY parkingRecordsID DESC");
- boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
- Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_park_record_sql, tp_result);
- if(ec == SUCCESS)
- {
- if(tp_result == nullptr)
- {
- return Error_manager(Error_code::DB_RESULT_SET_EMPTY, Error_level::MINOR_ERROR,
- " Parkspace_operating_function::query_parking_record error ");
- }
- if (tp_result->next())
- {
- char buf[1024];
- memset(buf, 0, 1024);
- try
- {
- record_id = tp_result->getInt("parkingRecordsID");
- }
- 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 SUCCESS;
- }
- else
- {
- return ec;
- }
- }
|