1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147 |
- /*
- * @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,message::Locate_information locateInformation)
- {
- //参数中必须包含车辆信息与车位编号
- if(!parkspace_info.has_car_info() || parkspace_info.parkingspace_index_id() <= 0 || !locateInformation.has_locate_x())
- {
- 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,locateX,locateY,locateAngle,locateLength,locateWidth,locateHeigth,locateWheelBase,locateWheelWidth) values ('%s',%d,'%s',%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f)",
- parkspace_info.car_info().license().c_str(),
- parkspace_info.parkingspace_index_id(),
- parkspace_info.entry_time().c_str(),
- locateInformation.locate_x(),
- locateInformation.locate_y(),
- locateInformation.locate_angle(),
- locateInformation.locate_length(),
- locateInformation.locate_width(),
- locateInformation.locate_height(),
- locateInformation.locate_wheel_base(),
- locateInformation.locate_wheel_width());
- 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;
- }
- }
|