parkspace_operating_function.cpp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. /*
  2. * @Description: 车位数据库管理
  3. * @Author: yct
  4. * @Date: 2020-07-19 09:57:45
  5. * @LastEditTime: 2020-07-22 19:07:45
  6. * @LastEditors: yct
  7. */
  8. #include "parkspace_operating_function.h"
  9. // 从数据库获得所有车位信息,用于车位模块初始化
  10. Error_manager Parkspace_operating_function::get_all_parkspace_info(message::Parkspace_allocation_data_msg &all_parkspace_info)
  11. {
  12. //执行sql操作
  13. std::string get_all_space_sql = "select * from parkingspace";
  14. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  15. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(get_all_space_sql, tp_result);
  16. if(ec == SUCCESS)
  17. {
  18. if (tp_result == nullptr)
  19. {
  20. return DB_RESULT_SET_EMPTY;
  21. }
  22. all_parkspace_info.clear_parkspace_info_ex();
  23. while (tp_result->next())
  24. {
  25. message::Parkspace_info *t_parkspace = all_parkspace_info.add_parkspace_info_ex();
  26. char buf[1024];
  27. memset(buf, 0, 1024);
  28. try
  29. {
  30. t_parkspace->set_parkingspace_index_id(tp_result->getInt("parkingSpaceID"));
  31. t_parkspace->set_parkingspace_unit_id(tp_result->getInt("parkingspace_unit_id"));
  32. t_parkspace->set_parkingspace_label_id(tp_result->getInt("parkingspace_label_id"));
  33. t_parkspace->set_parkingspace_room_id(tp_result->getInt("parkingspace_room_id"));
  34. if (tp_result->getInt("parkingspace_direction") > 0)
  35. {
  36. t_parkspace->set_parkingspace_direction(message::Direction::eForward);
  37. }
  38. else
  39. {
  40. t_parkspace->set_parkingspace_direction(message::Direction::eBackward);
  41. }
  42. t_parkspace->set_parkingspace_floor_id(tp_result->getInt("parkingspace_floor_id"));
  43. t_parkspace->set_parkingspace_status((message::Parkspace_status)tp_result->getInt("parkingspace_status"));
  44. t_parkspace->set_parkingspace_width(tp_result->getDouble("parkingspace_width"));
  45. t_parkspace->set_parkingspace_height(tp_result->getDouble("parkingspace_height"));
  46. t_parkspace->mutable_car_info()->set_car_numberplate(tp_result->getString("parkSpaceCarNumberPlate"));
  47. t_parkspace->mutable_car_info()->set_license(tp_result->getString("parkSpaceCarLicense"));
  48. t_parkspace->mutable_car_info()->set_car_length(tp_result->getDouble("parkSpaceCarLength"));
  49. t_parkspace->mutable_car_info()->set_car_width(tp_result->getDouble("parkSpaceCarWidth"));
  50. t_parkspace->mutable_car_info()->set_car_height(tp_result->getDouble("parkSpaceCarHeight"));
  51. t_parkspace->mutable_car_info()->set_car_wheel_base(tp_result->getDouble("parkSpaceCarWheelBase"));
  52. t_parkspace->mutable_car_info()->set_car_wheel_width(tp_result->getDouble("parkSpaceCarWheelWidth"));
  53. t_parkspace->set_car_type((message::Car_type)tp_result->getInt("parkSpaceCarType"));
  54. t_parkspace->set_entry_time(tp_result->getString("entryTime"));
  55. t_parkspace->set_leave_time(tp_result->getString("leaveTime"));
  56. }
  57. catch (sql::SQLException &e)
  58. {
  59. /* Use what() (derived from std::runtime_error) to fetch the error message */
  60. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(),
  61. e.getSQLState().c_str());
  62. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  63. }
  64. catch (std::runtime_error &e)
  65. {
  66. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  67. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  68. }
  69. }
  70. return SUCCESS;
  71. }
  72. else
  73. {
  74. return ec;
  75. }
  76. }
  77. // 从数据库获得所有指定类型空车位车位信息,用于车位模块初始化
  78. 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)
  79. {
  80. //执行sql操作
  81. char all_specify_the_type_sql[1024];
  82. memset(all_specify_the_type_sql, 0, 1024);
  83. sprintf(all_specify_the_type_sql,"select * from parkingspace where parkingspace_type= %d and parkingspace_status = %d",parkspace_type,message::Parkspace_status::eParkspace_empty);
  84. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  85. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(all_specify_the_type_sql, tp_result);
  86. if(ec == SUCCESS)
  87. {
  88. if (tp_result == nullptr)
  89. {
  90. return DB_RESULT_SET_EMPTY;
  91. }
  92. all_specify_the_type_parkspace_info.clear_parkspace_info_ex();
  93. while (tp_result->next())
  94. {
  95. message::Parkspace_info *t_parkspace = all_specify_the_type_parkspace_info.add_parkspace_info_ex();
  96. char buf[1024];
  97. memset(buf, 0, 1024);
  98. try
  99. {
  100. t_parkspace->set_parkingspace_index_id(tp_result->getInt("parkingSpaceID"));
  101. t_parkspace->set_parkingspace_unit_id(tp_result->getInt("parkingspace_unit_id"));
  102. t_parkspace->set_parkingspace_label_id(tp_result->getInt("parkingspace_label_id"));
  103. t_parkspace->set_parkingspace_room_id(tp_result->getInt("parkingspace_room_id"));
  104. if (tp_result->getInt("parkingspace_direction") > 0)
  105. {
  106. t_parkspace->set_parkingspace_direction(message::Direction::eForward);
  107. }
  108. else
  109. {
  110. t_parkspace->set_parkingspace_direction(message::Direction::eBackward);
  111. }
  112. t_parkspace->set_parkingspace_floor_id(tp_result->getInt("parkingspace_floor_id"));
  113. t_parkspace->set_parkingspace_status((message::Parkspace_status)tp_result->getInt("parkingspace_status"));
  114. t_parkspace->set_parkingspace_width(tp_result->getDouble("parkingspace_width"));
  115. t_parkspace->set_parkingspace_height(tp_result->getDouble("parkingspace_height"));
  116. t_parkspace->mutable_car_info()->set_car_numberplate(tp_result->getString("parkSpaceCarNumberPlate"));
  117. t_parkspace->mutable_car_info()->set_license(tp_result->getString("parkSpaceCarLicense"));
  118. t_parkspace->mutable_car_info()->set_car_length(tp_result->getDouble("parkSpaceCarLength"));
  119. t_parkspace->mutable_car_info()->set_car_width(tp_result->getDouble("parkSpaceCarWidth"));
  120. t_parkspace->mutable_car_info()->set_car_height(tp_result->getDouble("parkSpaceCarHeight"));
  121. t_parkspace->set_car_type((message::Car_type)tp_result->getInt("parkSpaceCarType"));
  122. t_parkspace->mutable_car_info()->set_car_wheel_base(tp_result->getDouble("parkSpaceCarWheelBase"));
  123. t_parkspace->mutable_car_info()->set_car_wheel_width(tp_result->getDouble("parkSpaceCarWheelWidth"));
  124. t_parkspace->set_entry_time(tp_result->getString("entryTime"));
  125. t_parkspace->set_leave_time(tp_result->getString("leaveTime"));
  126. }
  127. catch (sql::SQLException &e)
  128. {
  129. /* Use what() (derived from std::runtime_error) to fetch the error message */
  130. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(),
  131. e.getSQLState().c_str());
  132. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  133. }
  134. catch (std::runtime_error &e)
  135. {
  136. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  137. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  138. }
  139. }
  140. return SUCCESS;
  141. }
  142. else
  143. {
  144. return ec;
  145. }
  146. }
  147. // 清除数据库中所有车位号牌,状态全修改为空闲
  148. Error_manager Parkspace_operating_function::clear_all_parkspace_info()
  149. {
  150. //执行sql操作
  151. char update_space_sql[1024];
  152. memset(update_space_sql, 0, 1024);
  153. 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);
  154. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_space_sql);
  155. return ec;
  156. }
  157. // 更新数据库中车位状态
  158. //根据车位ID更改车位状态
  159. Error_manager Parkspace_operating_function::update_parkspace_data(message::Parkspace_info parkspace_info)
  160. {
  161. //执行sql操作
  162. char update_space_sql[1024];
  163. memset(update_space_sql, 0, 1024);
  164. int status = parkspace_info.parkingspace_status();
  165. if ( status < 1 || status >5 )
  166. {
  167. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  168. "参数错误 Parkspace_operating_function::update_parkspace_data error ");
  169. }
  170. // switch (parkspace_info.parkingspace_status())
  171. // {
  172. // case message::Parkspace_status::eParkspace_empty:
  173. // status = 0;
  174. // break;
  175. // case message::Parkspace_status::eParkspace_occupied:
  176. // status = 1;
  177. // break;
  178. // case message::Parkspace_status::eParkspace_reserved:
  179. // status = 2;
  180. // break;
  181. // case message::Parkspace_status::eParkspace_locked:
  182. // status = 3;
  183. // break;
  184. // case message::Parkspace_status::eParkspace_error:
  185. // status = 4;
  186. // break;
  187. // default:
  188. // return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  189. // "参数错误 Parkspace_operating_function::update_parkspace_data error ");
  190. // break;
  191. // }
  192. if(parkspace_info.has_car_info() && status!=message::Parkspace_status::eParkspace_empty)
  193. {
  194. 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",
  195. status,
  196. parkspace_info.car_info().car_numberplate().c_str(),
  197. parkspace_info.car_info().license().c_str(),
  198. parkspace_info.car_info().has_car_length()?parkspace_info.car_info().car_length():0.0f,
  199. parkspace_info.car_info().car_width(),
  200. parkspace_info.car_info().car_height(),
  201. parkspace_info.car_type(),
  202. parkspace_info.car_info().car_wheel_base(),
  203. parkspace_info.car_info().car_wheel_width(),
  204. parkspace_info.entry_time().c_str(),
  205. parkspace_info.leave_time().c_str(),
  206. parkspace_info.parkingspace_index_id());
  207. }else
  208. {
  209. 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());
  210. }
  211. //boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  212. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_space_sql);
  213. return ec;
  214. }
  215. // 更新数据库车辆信息,车位信息中包含车辆信息用于匹配更新
  216. Error_manager Parkspace_operating_function::update_vehicle_with_parkspace(message::Parkspace_info parkspace_info, message::Vehicle_status &vehicle_status)
  217. {
  218. //执行sql操作
  219. if(!parkspace_info.has_car_info())
  220. {
  221. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  222. "参数错误 Parkspace_operating_function::update_vehicle_with_parkspace error ");;
  223. }
  224. std::string find_vehicle_sql = std::string("select * from vehicle where numberPlate = '").append(parkspace_info.car_info().license()).append("'");
  225. boost::shared_ptr<sql::ResultSet> query_vehicle_result = nullptr;
  226. Database_controller::get_instance_pointer()->sql_query(find_vehicle_sql, query_vehicle_result);
  227. //判断车辆是否存在
  228. if(query_vehicle_result == nullptr || !query_vehicle_result->next())
  229. {
  230. return insert_vehicle_with_parkspace(parkspace_info, vehicle_status);
  231. }
  232. else
  233. {
  234. char update_vehicle_sql[1024];
  235. memset(update_vehicle_sql, 0, 1024);
  236. int vehicle_status_code = -1;
  237. switch (vehicle_status)
  238. {
  239. case message::Vehicle_status::eVehicle_idle:
  240. vehicle_status_code = 1;
  241. break;
  242. case message::Vehicle_status::eVehicle_in_garage:
  243. vehicle_status_code = 2;
  244. break;
  245. case message::Vehicle_status::eVehicle_parking:
  246. vehicle_status_code = 3;
  247. break;
  248. case message::Vehicle_status::eVehicle_fetching:
  249. vehicle_status_code = 4;
  250. break;
  251. case message::Vehicle_status::eVehicle_reserved:
  252. vehicle_status_code = 5;
  253. break;
  254. default:
  255. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  256. "参数错误 Parkspace_operating_function::update_vehicle_with_parkspace error ");
  257. break;
  258. }
  259. if ( vehicle_status_code == 1 )
  260. {
  261. 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'",
  262. parkspace_info.car_info().license().c_str());
  263. }
  264. //车位为空,仅更新车辆状态与长宽高
  265. else if(parkspace_info.parkingspace_index_id() <= 0)
  266. {
  267. sprintf(update_vehicle_sql, "update vehicle set vehicleParkState = %d,carLength=%.3f,carWidth=%.3f,carHeight=%.3f,carWheelBase = %.3f,carWheelWidth = %.3f where numberPlate = '%s'",
  268. vehicle_status_code,
  269. parkspace_info.car_info().car_length(),
  270. parkspace_info.car_info().car_width(),
  271. parkspace_info.car_info().car_height(),
  272. parkspace_info.car_info().car_wheel_base(),
  273. parkspace_info.car_info().car_wheel_width(),
  274. parkspace_info.car_info().license().c_str());
  275. }
  276. //车位不为空,表示车辆所在车位,同时更新
  277. else
  278. {
  279. sprintf(update_vehicle_sql, "update vehicle set vehicleParkState = %d,carLength=%.3f,carWidth=%.3f,carHeight=%.3f,carWheelBase = %.3f,carWheelWidth = %.3f,parkingSpaceID=%d where numberPlate = '%s'",
  280. vehicle_status_code,
  281. parkspace_info.car_info().car_length(),
  282. parkspace_info.car_info().car_width(),
  283. parkspace_info.car_info().car_height(),
  284. parkspace_info.car_info().car_wheel_base(),
  285. parkspace_info.car_info().car_wheel_width(),
  286. parkspace_info.parkingspace_index_id(),
  287. parkspace_info.car_info().license().c_str());
  288. }
  289. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_vehicle_sql);
  290. return ec;
  291. }
  292. }
  293. //更新停车记录ID
  294. Error_manager Parkspace_operating_function::update_record_id(std::string license, int record_id)
  295. {
  296. message::Vehicle_status vehicle_status;
  297. int t_record_id;
  298. Error_manager ec = query_vehicle(license,vehicle_status,t_record_id);
  299. if ( vehicle_status != message::Vehicle_status::eVehicle_in_garage )
  300. {
  301. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  302. " Parkspace_operating_function::insert_vehicle_with_parkspace error ");
  303. }
  304. char update_vehicle_sql[1024];
  305. memset(update_vehicle_sql, 0, 1024);
  306. sprintf(update_vehicle_sql, "update vehicle set parkingRecordsID = %d where numberPlate = '%s'",record_id,license.c_str());
  307. ec = Database_controller::get_instance_pointer()->sql_update(update_vehicle_sql);
  308. return ec;
  309. }
  310. // 插入车辆
  311. Error_manager Parkspace_operating_function::insert_vehicle_with_parkspace(message::Parkspace_info parkspace_info, message::Vehicle_status &vehicle_status)
  312. {
  313. //执行sql操作
  314. if(!parkspace_info.has_car_info())
  315. {
  316. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  317. " Parkspace_operating_function::insert_vehicle_with_parkspace error ");
  318. }
  319. char insert_vehicle_sql[1024];
  320. memset(insert_vehicle_sql, 0, 1024);
  321. int vehicle_status_code = -1;
  322. switch (vehicle_status)
  323. {
  324. case message::Vehicle_status::eVehicle_idle:
  325. vehicle_status_code = 1;
  326. break;
  327. case message::Vehicle_status::eVehicle_in_garage:
  328. vehicle_status_code = 2;
  329. break;
  330. case message::Vehicle_status::eVehicle_parking:
  331. vehicle_status_code = 3;
  332. break;
  333. case message::Vehicle_status::eVehicle_fetching:
  334. vehicle_status_code = 4;
  335. break;
  336. case message::Vehicle_status::eVehicle_reserved:
  337. vehicle_status_code = 5;
  338. break;
  339. default:
  340. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  341. "参数错误 Parkspace_operating_function::insert_vehicle_with_parkspace error ");
  342. break;
  343. }
  344. //车位为空,仅更新车辆状态与长宽高
  345. if(parkspace_info.parkingspace_index_id() <= 0)
  346. {
  347. sprintf(insert_vehicle_sql, "INSERT INTO vehicle (numberPlate,vehicleParkState,carLength,carWidth,carHeight,carWheelBase,carWheelWidth) values ('%s',%d,%.3f,%.3f,%.3f,%.3f,%.3f)",
  348. parkspace_info.car_info().license().c_str(),
  349. vehicle_status_code,
  350. parkspace_info.car_info().car_length(),
  351. parkspace_info.car_info().car_width(),
  352. parkspace_info.car_info().car_height(),
  353. parkspace_info.car_info().car_wheel_base(),
  354. parkspace_info.car_info().car_wheel_width());
  355. }
  356. //车位不为空,表示车辆在车位,同时更新
  357. else
  358. {
  359. sprintf(insert_vehicle_sql, "INSERT INTO vehicle (numberPlate,vehicleParkState,carLength,carWidth,carHeight,carWheelBase,carWheelWidth,parkingSpaceID) values ('%s',%d,%.3f,%.3f,%.3f,%.3f,%.3f,%d)",
  360. parkspace_info.car_info().license().c_str(),
  361. vehicle_status_code,
  362. parkspace_info.car_info().car_length(),
  363. parkspace_info.car_info().car_width(),
  364. parkspace_info.car_info().car_height(),
  365. parkspace_info.car_info().car_wheel_base(),
  366. parkspace_info.car_info().car_wheel_width(),
  367. parkspace_info.parkingspace_index_id());
  368. }
  369. Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_vehicle_sql);
  370. return ec;
  371. }
  372. // 插入停车记录
  373. Error_manager Parkspace_operating_function::insert_parking_record(message::Parkspace_info parkspace_info)
  374. {
  375. //参数中必须包含车辆信息与车位编号
  376. if(!parkspace_info.has_car_info() || parkspace_info.parkingspace_index_id() <= 0)
  377. {
  378. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  379. "参数错误 Parkspace_operating_function::insert_parking_record error ");
  380. }
  381. char insert_parking_record_sql[1024];
  382. memset(insert_parking_record_sql, 0, 1024);
  383. //将车辆号牌,车位ID,记录状态,停车与取车时间写入
  384. sprintf(insert_parking_record_sql, "INSERT INTO parkingrecords (numberPlate,parkingSpaceID,realParkTime) values ('%s',%d,'%s')",
  385. parkspace_info.car_info().license().c_str(),
  386. parkspace_info.parkingspace_index_id(),
  387. parkspace_info.entry_time().c_str());
  388. Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_parking_record_sql);
  389. return ec;
  390. }
  391. // 更新停车记录,待计费系统加入后完善!!!
  392. Error_manager Parkspace_operating_function::update_parking_record(message::Parkspace_info parkspace_info, int record_id)
  393. {
  394. //执行sql操作
  395. if(!parkspace_info.has_car_info() || parkspace_info.parkingspace_index_id() <= 0)
  396. {
  397. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  398. "参数错误 update_parking_record::insert_parking_record error ");;
  399. }
  400. char update_parking_record_sql[1024];
  401. memset(update_parking_record_sql, 0, 1024);
  402. sprintf(update_parking_record_sql, "update parkingrecords set realGetTime='%s',parkingPrice=%d where parkingRecordsID = %d",
  403. parkspace_info.leave_time().c_str(),
  404. 0,
  405. record_id);
  406. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_parking_record_sql);
  407. return ec;
  408. }
  409. // 根据车位编号查询车位信息
  410. Error_manager Parkspace_operating_function::query_one_parkspace_with_parkspace_id(int parkspace_id, message::Parkspace_info &parkspace_info)
  411. {
  412. //执行sql操作
  413. std::string query_parkspace_sql = std::string("select * from parkingspace where parkingSpaceID=").append(std::to_string(parkspace_id));
  414. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  415. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  416. if(ec == SUCCESS)
  417. {
  418. if(tp_result == nullptr)
  419. {
  420. return Error_manager(Error_code::DB_RESULT_SET_EMPTY, Error_level::MAJOR_ERROR,
  421. " 数据库外层查询返回结果空指针 Parkspace_operating_function::query_one_parkspace_with_parkspace_id error ");;
  422. }
  423. if (tp_result->next())
  424. {
  425. char buf[1024];
  426. memset(buf, 0, 1024);
  427. try
  428. {
  429. parkspace_info.set_parkingspace_status((message::Parkspace_status)tp_result->getInt("parkingspace_status"));
  430. parkspace_info.set_parkingspace_index_id(tp_result->getInt("parkingSpaceID"));
  431. parkspace_info.set_parkingspace_unit_id(tp_result->getInt("parkingspace_unit_id"));
  432. parkspace_info.set_parkingspace_label_id(tp_result->getInt("parkingspace_label_id"));
  433. parkspace_info.set_parkingspace_room_id(tp_result->getInt("parkingspace_room_id"));
  434. parkspace_info.set_parkingspace_direction(tp_result->getInt("parkingspace_direction") > 0 ? message::Direction::eForward : message::Direction::eBackward);
  435. parkspace_info.set_parkingspace_floor_id(tp_result->getInt("parkingspace_floor_id"));
  436. parkspace_info.set_parkingspace_width(tp_result->getDouble("parkingspace_width"));
  437. parkspace_info.set_parkingspace_height(tp_result->getDouble("parkingspace_height"));
  438. parkspace_info.mutable_car_info()->set_car_numberplate(tp_result->getString("parkSpaceCarNumberPlate"));
  439. parkspace_info.mutable_car_info()->set_license(tp_result->getString("parkSpaceCarLicense"));
  440. parkspace_info.mutable_car_info()->set_car_length(tp_result->getDouble("parkSpaceCarLength"));
  441. parkspace_info.mutable_car_info()->set_car_width(tp_result->getDouble("parkSpaceCarWidth"));
  442. parkspace_info.mutable_car_info()->set_car_height(tp_result->getDouble("parkSpaceCarHeight"));
  443. parkspace_info.set_car_type((message::Car_type)tp_result->getInt("parkSpaceCarType"));
  444. parkspace_info.mutable_car_info()->set_car_wheel_base(tp_result->getDouble("parkSpaceCarWheelBase"));
  445. parkspace_info.mutable_car_info()->set_car_wheel_width(tp_result->getDouble("parkSpaceCarWheelWidth"));
  446. parkspace_info.set_entry_time(tp_result->getString("entryTime"));
  447. parkspace_info.set_leave_time(tp_result->getString("leaveTime"));
  448. }
  449. catch (sql::SQLException &e)
  450. {
  451. /* Use what() (derived from std::runtime_error) to fetch the error message */
  452. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  453. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  454. }
  455. catch (std::runtime_error &e)
  456. {
  457. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  458. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  459. }
  460. if (tp_result->next())
  461. {
  462. return Error_manager(Error_code::DB_QUERY_DATA_REPEAT, Error_level::MAJOR_ERROR,
  463. "数据库查询数据重复 Parkspace_operating_function::query_one_parkspace_with_parkspace_id error ");
  464. }
  465. }
  466. else
  467. {
  468. return Error_manager(Error_code::DB_QUERY_NOT_DATA, Error_level::MINOR_ERROR,
  469. "数据库查询未找到数据 Parkspace_operating_function::query_one_parkspace_with_parkspace_id error ");
  470. }
  471. return SUCCESS;
  472. }
  473. else
  474. {
  475. return ec;
  476. }
  477. }
  478. // 根据车牌号查询状态为占用的车位
  479. Error_manager Parkspace_operating_function::query_one_occupied_parkspace_with_license(std::string license, message::Parkspace_info &parkspace_info)
  480. {
  481. //执行sql操作
  482. //std::string query_parkspace_sql = std::string("select * from parkingspace where parkingspace_status == 2 and parkSpaceCarLicense='").append(license).append("'");
  483. char query_parkspace_sql[1024];
  484. memset(query_parkspace_sql, 0, 1024);
  485. sprintf(query_parkspace_sql,"select * from parkingspace where parkingspace_status = %d and parkSpaceCarLicense='%s' ",message::Parkspace_status::eParkspace_occupied,license.c_str());
  486. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  487. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  488. if(ec == SUCCESS)
  489. {
  490. if(tp_result == nullptr)
  491. {
  492. return Error_manager(Error_code::DB_RESULT_SET_EMPTY, Error_level::MINOR_ERROR,
  493. "数据库外层查询返回结果空指针 Parkspace_operating_function::query_one_parkspace_with_license error ");
  494. }
  495. if (tp_result->next())
  496. {
  497. char buf[1024];
  498. memset(buf, 0, 1024);
  499. try
  500. {
  501. parkspace_info.set_parkingspace_status((message::Parkspace_status)tp_result->getInt("parkingspace_status"));
  502. parkspace_info.set_parkingspace_index_id(tp_result->getInt("parkingSpaceID"));
  503. parkspace_info.set_parkingspace_unit_id(tp_result->getInt("parkingspace_unit_id"));
  504. parkspace_info.set_parkingspace_label_id(tp_result->getInt("parkingspace_label_id"));
  505. parkspace_info.set_parkingspace_room_id(tp_result->getInt("parkingspace_room_id"));
  506. parkspace_info.set_parkingspace_direction(tp_result->getInt("parkingspace_direction") > 0 ? message::Direction::eForward : message::Direction::eBackward);
  507. parkspace_info.set_parkingspace_floor_id(tp_result->getInt("parkingspace_floor_id"));
  508. parkspace_info.set_parkingspace_width(tp_result->getDouble("parkingspace_width"));
  509. parkspace_info.set_parkingspace_height(tp_result->getDouble("parkingspace_height"));
  510. parkspace_info.mutable_car_info()->set_car_numberplate(tp_result->getString("parkSpaceCarNumberPlate"));
  511. parkspace_info.mutable_car_info()->set_license(tp_result->getString("parkSpaceCarLicense"));
  512. parkspace_info.mutable_car_info()->set_car_length(tp_result->getDouble("parkSpaceCarLength"));
  513. parkspace_info.mutable_car_info()->set_car_width(tp_result->getDouble("parkSpaceCarWidth"));
  514. parkspace_info.mutable_car_info()->set_car_height(tp_result->getDouble("parkSpaceCarHeight"));
  515. parkspace_info.set_car_type((message::Car_type)tp_result->getInt("parkSpaceCarType"));
  516. parkspace_info.mutable_car_info()->set_car_wheel_base(tp_result->getDouble("parkSpaceCarWheelBase"));
  517. parkspace_info.mutable_car_info()->set_car_wheel_width(tp_result->getDouble("parkSpaceCarWheelWidth"));
  518. parkspace_info.set_entry_time(tp_result->getString("entryTime"));
  519. parkspace_info.set_leave_time(tp_result->getString("leaveTime"));
  520. }
  521. catch (sql::SQLException &e)
  522. {
  523. /* Use what() (derived from std::runtime_error) to fetch the error message */
  524. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  525. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  526. }
  527. catch (std::runtime_error &e)
  528. {
  529. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  530. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  531. }
  532. if (tp_result->next())
  533. {
  534. return Error_manager(Error_code::DB_QUERY_DATA_REPEAT, Error_level::MAJOR_ERROR,
  535. "数据库查询数据重复 Parkspace_operating_function::query_one_parkspace_with_license error ");
  536. }
  537. }
  538. else
  539. {
  540. return Error_manager(Error_code::DB_QUERY_NOT_DATA, Error_level::MINOR_ERROR,
  541. "数据库未查询到数据 Parkspace_operating_function::query_one_parkspace_with_license error ");
  542. }
  543. return SUCCESS;
  544. }
  545. else
  546. {
  547. return ec;
  548. }
  549. }
  550. // 根据车牌号查询车位
  551. Error_manager Parkspace_operating_function::query_one_parkspace_with_license(std::string license, message::Parkspace_info &parkspace_info)
  552. {
  553. //执行sql操作
  554. //std::string query_parkspace_sql = std::string("select * from parkingspace where parkingspace_status == 2 and parkSpaceCarLicense='").append(license).append("'");
  555. char query_parkspace_sql[1024];
  556. memset(query_parkspace_sql, 0, 1024);
  557. sprintf(query_parkspace_sql,"select * from parkingspace where parkingspace_status != %d and parkSpaceCarLicense='%s' ",message::Parkspace_status::eParkspace_empty,license.c_str());
  558. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  559. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  560. if(ec == SUCCESS)
  561. {
  562. if(tp_result == nullptr)
  563. {
  564. return Error_manager(Error_code::DB_RESULT_SET_EMPTY, Error_level::MINOR_ERROR,
  565. "数据库外层查询返回结果空指针 Parkspace_operating_function::query_one_parkspace_with_license error ");
  566. }
  567. if (tp_result->next())
  568. {
  569. char buf[1024];
  570. memset(buf, 0, 1024);
  571. try
  572. {
  573. parkspace_info.set_parkingspace_status((message::Parkspace_status)tp_result->getInt("parkingspace_status"));
  574. parkspace_info.set_parkingspace_index_id(tp_result->getInt("parkingSpaceID"));
  575. parkspace_info.set_parkingspace_unit_id(tp_result->getInt("parkingspace_unit_id"));
  576. parkspace_info.set_parkingspace_label_id(tp_result->getInt("parkingspace_label_id"));
  577. parkspace_info.set_parkingspace_room_id(tp_result->getInt("parkingspace_room_id"));
  578. parkspace_info.set_parkingspace_direction(tp_result->getInt("parkingspace_direction") > 0 ? message::Direction::eForward : message::Direction::eBackward);
  579. parkspace_info.set_parkingspace_floor_id(tp_result->getInt("parkingspace_floor_id"));
  580. parkspace_info.set_parkingspace_width(tp_result->getDouble("parkingspace_width"));
  581. parkspace_info.set_parkingspace_height(tp_result->getDouble("parkingspace_height"));
  582. parkspace_info.mutable_car_info()->set_car_numberplate(tp_result->getString("parkSpaceCarNumberPlate"));
  583. parkspace_info.mutable_car_info()->set_license(tp_result->getString("parkSpaceCarLicense"));
  584. parkspace_info.mutable_car_info()->set_car_length(tp_result->getDouble("parkSpaceCarLength"));
  585. parkspace_info.mutable_car_info()->set_car_width(tp_result->getDouble("parkSpaceCarWidth"));
  586. parkspace_info.mutable_car_info()->set_car_height(tp_result->getDouble("parkSpaceCarHeight"));
  587. parkspace_info.set_car_type((message::Car_type)tp_result->getInt("parkSpaceCarType"));
  588. parkspace_info.mutable_car_info()->set_car_wheel_base(tp_result->getDouble("parkSpaceCarWheelBase"));
  589. parkspace_info.mutable_car_info()->set_car_wheel_width(tp_result->getDouble("parkSpaceCarWheelWidth"));
  590. parkspace_info.set_entry_time(tp_result->getString("entryTime"));
  591. parkspace_info.set_leave_time(tp_result->getString("leaveTime"));
  592. }
  593. catch (sql::SQLException &e)
  594. {
  595. /* Use what() (derived from std::runtime_error) to fetch the error message */
  596. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  597. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  598. }
  599. catch (std::runtime_error &e)
  600. {
  601. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  602. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  603. }
  604. if (tp_result->next())
  605. {
  606. return Error_manager(Error_code::DB_QUERY_DATA_REPEAT, Error_level::MAJOR_ERROR,
  607. "数据库查询数据重复 Parkspace_operating_function::query_one_parkspace_with_license error ");
  608. }
  609. }
  610. else
  611. {
  612. return Error_manager(Error_code::DB_QUERY_NOT_DATA, Error_level::MINOR_ERROR,
  613. "数据库未查询到数据 Parkspace_operating_function::query_one_parkspace_with_license error ");
  614. }
  615. return SUCCESS;
  616. }
  617. else
  618. {
  619. return ec;
  620. }
  621. }
  622. // 根据车牌号查询车位
  623. Error_manager Parkspace_operating_function::query_one_parkspace_with_numberPlate(std::string numberPlate, message::Parkspace_info &parkspace_info)
  624. {
  625. //执行sql操作
  626. //std::string query_parkspace_sql = std::string("select * from parkingspace where parkingspace_status == 2 and parkSpaceCarLicense='").append(license).append("'");
  627. char query_parkspace_sql[1024];
  628. memset(query_parkspace_sql, 0, 1024);
  629. sprintf(query_parkspace_sql,"select * from parkingspace where parkingspace_status != %d and parkSpaceCarNumberPlate='%s' ",message::Parkspace_status::eParkspace_empty,numberPlate.c_str());
  630. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  631. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  632. if(ec == SUCCESS)
  633. {
  634. if(tp_result == nullptr)
  635. {
  636. return Error_manager(Error_code::DB_RESULT_SET_EMPTY, Error_level::MINOR_ERROR,
  637. "数据库外层查询返回结果空指针 Parkspace_operating_function::query_one_parkspace_with_license error ");
  638. }
  639. if (tp_result->next())
  640. {
  641. char buf[1024];
  642. memset(buf, 0, 1024);
  643. try
  644. {
  645. parkspace_info.set_parkingspace_status((message::Parkspace_status)tp_result->getInt("parkingspace_status"));
  646. parkspace_info.set_parkingspace_index_id(tp_result->getInt("parkingSpaceID"));
  647. parkspace_info.set_parkingspace_unit_id(tp_result->getInt("parkingspace_unit_id"));
  648. parkspace_info.set_parkingspace_label_id(tp_result->getInt("parkingspace_label_id"));
  649. parkspace_info.set_parkingspace_room_id(tp_result->getInt("parkingspace_room_id"));
  650. parkspace_info.set_parkingspace_direction(tp_result->getInt("parkingspace_direction") > 0 ? message::Direction::eForward : message::Direction::eBackward);
  651. parkspace_info.set_parkingspace_floor_id(tp_result->getInt("parkingspace_floor_id"));
  652. parkspace_info.set_parkingspace_width(tp_result->getDouble("parkingspace_width"));
  653. parkspace_info.set_parkingspace_height(tp_result->getDouble("parkingspace_height"));
  654. parkspace_info.mutable_car_info()->set_car_numberplate(tp_result->getString("parkSpaceCarNumberPlate"));
  655. parkspace_info.mutable_car_info()->set_license(tp_result->getString("parkSpaceCarLicense"));
  656. parkspace_info.mutable_car_info()->set_car_length(tp_result->getDouble("parkSpaceCarLength"));
  657. parkspace_info.mutable_car_info()->set_car_width(tp_result->getDouble("parkSpaceCarWidth"));
  658. parkspace_info.mutable_car_info()->set_car_height(tp_result->getDouble("parkSpaceCarHeight"));
  659. parkspace_info.set_car_type((message::Car_type)tp_result->getInt("parkSpaceCarType"));
  660. parkspace_info.mutable_car_info()->set_car_wheel_base(tp_result->getDouble("parkSpaceCarWheelBase"));
  661. parkspace_info.mutable_car_info()->set_car_wheel_width(tp_result->getDouble("parkSpaceCarWheelWidth"));
  662. parkspace_info.set_entry_time(tp_result->getString("entryTime"));
  663. parkspace_info.set_leave_time(tp_result->getString("leaveTime"));
  664. }
  665. catch (sql::SQLException &e)
  666. {
  667. /* Use what() (derived from std::runtime_error) to fetch the error message */
  668. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  669. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  670. }
  671. catch (std::runtime_error &e)
  672. {
  673. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  674. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  675. }
  676. if (tp_result->next())
  677. {
  678. return Error_manager(Error_code::DB_QUERY_DATA_REPEAT, Error_level::MAJOR_ERROR,
  679. "数据库查询数据重复 Parkspace_operating_function::query_one_parkspace_with_license error ");
  680. }
  681. }
  682. else
  683. {
  684. return Error_manager(Error_code::DB_QUERY_NOT_DATA, Error_level::MINOR_ERROR,
  685. "数据库未查询到数据 Parkspace_operating_function::query_one_parkspace_with_license error ");
  686. }
  687. return SUCCESS;
  688. }
  689. else
  690. {
  691. return ec;
  692. }
  693. }
  694. // 找到一个空车位
  695. Error_manager Parkspace_operating_function::query_one_empty_parkspace(message::Parkspace_info &parkspace_info)
  696. {
  697. //执行sql操作
  698. std::string query_parkspace_sql = std::string("select * from parkingspace where parkingspace_status=").append(std::to_string(message::Parkspace_status::eParkspace_empty));
  699. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  700. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  701. if(ec == SUCCESS)
  702. {
  703. if(tp_result == nullptr)
  704. {
  705. return DB_RESULT_SET_EMPTY;
  706. }
  707. if (tp_result->next())
  708. {
  709. char buf[1024];
  710. memset(buf, 0, 1024);
  711. try
  712. {
  713. if ( tp_result->getInt("parkingspace_status") != message::Parkspace_status::eParkspace_empty )
  714. {
  715. return Error_manager(Error_code::DB_QUERY_DATA_FAILED, Error_level::MAJOR_ERROR,
  716. "数据库查询数据错误 Parkspace_operating_function::query_one_empty_parkspace error ");
  717. }
  718. parkspace_info.set_parkingspace_status(message::Parkspace_status::eParkspace_empty);
  719. parkspace_info.set_parkingspace_index_id(tp_result->getInt("parkingSpaceID"));
  720. parkspace_info.set_parkingspace_unit_id(tp_result->getInt("parkingspace_unit_id"));
  721. parkspace_info.set_parkingspace_label_id(tp_result->getInt("parkingspace_label_id"));
  722. parkspace_info.set_parkingspace_room_id(tp_result->getInt("parkingspace_room_id"));
  723. parkspace_info.set_parkingspace_direction(tp_result->getInt("parkingspace_direction") > 0 ? message::Direction::eForward : message::Direction::eBackward);
  724. parkspace_info.set_parkingspace_floor_id(tp_result->getInt("parkingspace_floor_id"));
  725. parkspace_info.set_parkingspace_width(tp_result->getDouble("parkingspace_width"));
  726. parkspace_info.set_parkingspace_height(tp_result->getDouble("parkingspace_height"));
  727. parkspace_info.mutable_car_info()->set_car_numberplate(tp_result->getString("parkSpaceCarNumberPlate"));
  728. parkspace_info.mutable_car_info()->set_license(tp_result->getString("parkSpaceCarLicense"));
  729. parkspace_info.mutable_car_info()->set_car_length(tp_result->getDouble("parkSpaceCarLength"));
  730. parkspace_info.mutable_car_info()->set_car_width(tp_result->getDouble("parkSpaceCarWidth"));
  731. parkspace_info.mutable_car_info()->set_car_height(tp_result->getDouble("parkSpaceCarHeight"));
  732. parkspace_info.set_car_type((message::Car_type)tp_result->getInt("parkSpaceCarType"));
  733. parkspace_info.mutable_car_info()->set_car_wheel_base(tp_result->getDouble("parkSpaceCarWheelBase"));
  734. parkspace_info.mutable_car_info()->set_car_wheel_width(tp_result->getDouble("parkSpaceCarWheelWidth"));
  735. parkspace_info.set_entry_time(tp_result->getString("entryTime"));
  736. parkspace_info.set_leave_time(tp_result->getString("leaveTime"));
  737. }
  738. catch (sql::SQLException &e)
  739. {
  740. /* Use what() (derived from std::runtime_error) to fetch the error message */
  741. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  742. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  743. }
  744. catch (std::runtime_error &e)
  745. {
  746. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  747. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  748. }
  749. }
  750. else
  751. {
  752. return Error_manager(Error_code::DB_NOT_QUERY_EMPTY_PARKSPACE, Error_level::MINOR_ERROR,
  753. "数据库未查询到空车位 Parkspace_operating_function::query_one_empty_parkspace error ");
  754. }
  755. return SUCCESS;
  756. }
  757. else
  758. {
  759. return ec;
  760. }
  761. return SUCCESS;
  762. }
  763. // 找到一个指定单元空车位
  764. Error_manager Parkspace_operating_function::query_one_specified_unit_empty_parkspace(int unit_id,message::Parkspace_info &parkspace_info)
  765. {
  766. //执行sql操作
  767. char query_parkspace_sql[1024];
  768. memset(query_parkspace_sql, 0, 1024);
  769. sprintf(query_parkspace_sql,"select * from parkingspace where parkingspace_status=%d and parkingspace_unit_id=%d",message::Parkspace_status::eParkspace_empty,unit_id);
  770. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  771. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  772. if(ec == SUCCESS)
  773. {
  774. if(tp_result == nullptr)
  775. {
  776. return DB_RESULT_SET_EMPTY;
  777. }
  778. if (tp_result->next())
  779. {
  780. char buf[1024];
  781. memset(buf, 0, 1024);
  782. try
  783. {
  784. if ( tp_result->getInt("parkingspace_status") != message::Parkspace_status::eParkspace_empty )
  785. {
  786. return Error_manager(Error_code::DB_QUERY_DATA_FAILED, Error_level::MAJOR_ERROR,
  787. "数据库查询数据错误 Parkspace_operating_function::query_one_empty_parkspace error ");
  788. }
  789. parkspace_info.set_parkingspace_status(message::Parkspace_status::eParkspace_empty);
  790. parkspace_info.set_parkingspace_index_id(tp_result->getInt("parkingSpaceID"));
  791. parkspace_info.set_parkingspace_unit_id(tp_result->getInt("parkingspace_unit_id"));
  792. parkspace_info.set_parkingspace_label_id(tp_result->getInt("parkingspace_label_id"));
  793. parkspace_info.set_parkingspace_room_id(tp_result->getInt("parkingspace_room_id"));
  794. parkspace_info.set_parkingspace_direction(tp_result->getInt("parkingspace_direction") > 0 ? message::Direction::eForward : message::Direction::eBackward);
  795. parkspace_info.set_parkingspace_floor_id(tp_result->getInt("parkingspace_floor_id"));
  796. parkspace_info.set_parkingspace_width(tp_result->getDouble("parkingspace_width"));
  797. parkspace_info.set_parkingspace_height(tp_result->getDouble("parkingspace_height"));
  798. parkspace_info.mutable_car_info()->set_car_numberplate(tp_result->getString("parkSpaceCarNumberPlate"));
  799. parkspace_info.mutable_car_info()->set_license(tp_result->getString("parkSpaceCarLicense"));
  800. parkspace_info.mutable_car_info()->set_car_length(tp_result->getDouble("parkSpaceCarLength"));
  801. parkspace_info.mutable_car_info()->set_car_width(tp_result->getDouble("parkSpaceCarWidth"));
  802. parkspace_info.mutable_car_info()->set_car_height(tp_result->getDouble("parkSpaceCarHeight"));
  803. parkspace_info.set_car_type((message::Car_type)tp_result->getInt("parkSpaceCarType"));
  804. parkspace_info.mutable_car_info()->set_car_wheel_base(tp_result->getDouble("parkSpaceCarWheelBase"));
  805. parkspace_info.mutable_car_info()->set_car_wheel_width(tp_result->getDouble("parkSpaceCarWheelWidth"));
  806. parkspace_info.set_entry_time(tp_result->getString("entryTime"));
  807. parkspace_info.set_leave_time(tp_result->getString("leaveTime"));
  808. }
  809. catch (sql::SQLException &e)
  810. {
  811. /* Use what() (derived from std::runtime_error) to fetch the error message */
  812. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  813. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  814. }
  815. catch (std::runtime_error &e)
  816. {
  817. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  818. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  819. }
  820. }
  821. else
  822. {
  823. return Error_manager(Error_code::DB_NOT_QUERY_EMPTY_PARKSPACE, Error_level::MINOR_ERROR,
  824. "数据库未查询到空车位 Parkspace_operating_function::query_one_empty_parkspace error ");
  825. }
  826. return SUCCESS;
  827. }
  828. else
  829. {
  830. return ec;
  831. }
  832. return SUCCESS;
  833. }
  834. // 找到一个指定单元指定类型的空车位
  835. Error_manager Parkspace_operating_function::query_one_specified_unit_and_type_empty_parkspace(int unit_id,int parkspace_type,message::Parkspace_info &parkspace_info)
  836. {
  837. //执行sql操作
  838. char query_parkspace_sql[1024];
  839. memset(query_parkspace_sql, 0, 1024);
  840. 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);
  841. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  842. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  843. if(ec == SUCCESS)
  844. {
  845. if(tp_result == nullptr)
  846. {
  847. return DB_RESULT_SET_EMPTY;
  848. }
  849. if (tp_result->next())
  850. {
  851. char buf[1024];
  852. memset(buf, 0, 1024);
  853. try
  854. {
  855. if ( tp_result->getInt("parkingspace_status") != message::Parkspace_status::eParkspace_empty )
  856. {
  857. return Error_manager(Error_code::DB_QUERY_DATA_FAILED, Error_level::MAJOR_ERROR,
  858. "数据库查询数据错误 Parkspace_operating_function::query_one_empty_parkspace error ");
  859. }
  860. parkspace_info.set_parkingspace_status(message::Parkspace_status::eParkspace_empty);
  861. parkspace_info.set_parkingspace_index_id(tp_result->getInt("parkingSpaceID"));
  862. parkspace_info.set_parkingspace_unit_id(tp_result->getInt("parkingspace_unit_id"));
  863. parkspace_info.set_parkingspace_label_id(tp_result->getInt("parkingspace_label_id"));
  864. parkspace_info.set_parkingspace_room_id(tp_result->getInt("parkingspace_room_id"));
  865. parkspace_info.set_parkingspace_direction(tp_result->getInt("parkingspace_direction") > 0 ? message::Direction::eForward : message::Direction::eBackward);
  866. parkspace_info.set_parkingspace_floor_id(tp_result->getInt("parkingspace_floor_id"));
  867. parkspace_info.set_parkingspace_width(tp_result->getDouble("parkingspace_width"));
  868. parkspace_info.set_parkingspace_height(tp_result->getDouble("parkingspace_height"));
  869. parkspace_info.mutable_car_info()->set_car_numberplate(tp_result->getString("parkSpaceCarNumberPlate"));
  870. parkspace_info.mutable_car_info()->set_license(tp_result->getString("parkSpaceCarLicense"));
  871. parkspace_info.mutable_car_info()->set_car_length(tp_result->getDouble("parkSpaceCarLength"));
  872. parkspace_info.mutable_car_info()->set_car_width(tp_result->getDouble("parkSpaceCarWidth"));
  873. parkspace_info.mutable_car_info()->set_car_height(tp_result->getDouble("parkSpaceCarHeight"));
  874. parkspace_info.set_car_type((message::Car_type)tp_result->getInt("parkSpaceCarType"));
  875. parkspace_info.mutable_car_info()->set_car_wheel_base(tp_result->getDouble("parkSpaceCarWheelBase"));
  876. parkspace_info.mutable_car_info()->set_car_wheel_width(tp_result->getDouble("parkSpaceCarWheelWidth"));
  877. parkspace_info.set_entry_time(tp_result->getString("entryTime"));
  878. parkspace_info.set_leave_time(tp_result->getString("leaveTime"));
  879. }
  880. catch (sql::SQLException &e)
  881. {
  882. /* Use what() (derived from std::runtime_error) to fetch the error message */
  883. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  884. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  885. }
  886. catch (std::runtime_error &e)
  887. {
  888. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  889. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  890. }
  891. }
  892. else
  893. {
  894. return Error_manager(Error_code::DB_NOT_QUERY_EMPTY_PARKSPACE, Error_level::MINOR_ERROR,
  895. "数据库未查询到空车位 Parkspace_operating_function::query_one_empty_parkspace error ");
  896. }
  897. return SUCCESS;
  898. }
  899. else
  900. {
  901. return ec;
  902. }
  903. return SUCCESS;
  904. }
  905. // 查询车辆状态,暂时不使用
  906. Error_manager Parkspace_operating_function::query_vehicle(std::string license, message::Vehicle_status &vehicle_status, int &park_record_id)
  907. {
  908. //执行sql操作
  909. std::string query_parkspace_sql = std::string("select * from vehicle where numberPlate='").append(license).append("'");
  910. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  911. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  912. if(ec == SUCCESS)
  913. {
  914. if(tp_result == nullptr)
  915. {
  916. return Error_manager(Error_code::DB_RESULT_SET_EMPTY, Error_level::MINOR_ERROR,
  917. "Parkspace_operating_function::query_vehicle error ");
  918. }
  919. if (tp_result->next())
  920. {
  921. char buf[1024];
  922. memset(buf, 0, 1024);
  923. try
  924. {
  925. switch (tp_result->getInt("vehicleParkState"))
  926. {
  927. case 1:
  928. vehicle_status = message::Vehicle_status::eVehicle_idle;
  929. break;
  930. case 2:
  931. vehicle_status = message::Vehicle_status::eVehicle_in_garage;
  932. break;
  933. case 3:
  934. vehicle_status = message::Vehicle_status::eVehicle_parking;
  935. break;
  936. case 4:
  937. vehicle_status = message::Vehicle_status::eVehicle_fetching;
  938. break;
  939. case 5:
  940. vehicle_status = message::Vehicle_status::eVehicle_reserved;
  941. break;
  942. default:
  943. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  944. "参数错误 Parkspace_operating_function::query_vehicle error ");
  945. }
  946. park_record_id = tp_result->getInt("parkingRecordsID");
  947. }
  948. catch (sql::SQLException &e)
  949. {
  950. /* Use what() (derived from std::runtime_error) to fetch the error message */
  951. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  952. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  953. }
  954. catch (std::runtime_error &e)
  955. {
  956. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  957. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  958. }
  959. }
  960. return SUCCESS;
  961. }
  962. else
  963. {
  964. return ec;
  965. }
  966. }
  967. // 查询停车记录,根据车牌号、车位id查询,反馈停车记录id
  968. Error_manager Parkspace_operating_function::query_parking_record(message::Parkspace_info &parkspace_info, int &record_id)
  969. {
  970. //执行sql操作
  971. if(!parkspace_info.has_car_info() || parkspace_info.parkingspace_index_id() <= 0)
  972. {
  973. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  974. " Parkspace_operating_function::query_parking_record error ");
  975. }
  976. 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");
  977. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  978. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_park_record_sql, tp_result);
  979. if(ec == SUCCESS)
  980. {
  981. if(tp_result == nullptr)
  982. {
  983. return Error_manager(Error_code::DB_RESULT_SET_EMPTY, Error_level::MINOR_ERROR,
  984. " Parkspace_operating_function::query_parking_record error ");
  985. }
  986. if (tp_result->next())
  987. {
  988. char buf[1024];
  989. memset(buf, 0, 1024);
  990. try
  991. {
  992. record_id = tp_result->getInt("parkingRecordsID");
  993. }
  994. catch (sql::SQLException &e)
  995. {
  996. /* Use what() (derived from std::runtime_error) to fetch the error message */
  997. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  998. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  999. }
  1000. catch (std::runtime_error &e)
  1001. {
  1002. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  1003. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  1004. }
  1005. }
  1006. return SUCCESS;
  1007. }
  1008. else
  1009. {
  1010. return ec;
  1011. }
  1012. }