parkspace_db_manager.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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_db_manager.h"
  9. // 车位数据库管理初始化
  10. Error_manager Parkspace_db_manager::Parkspace_db_manager_init(parkspace_proto::database_config config)
  11. {
  12. Error_manager ec = SUCCESS;
  13. mp_db_controller = boost::shared_ptr<Database_controller>(Database_controller::get_instance_pointer());
  14. if(mp_db_controller != nullptr)
  15. {
  16. ec = mp_db_controller->database_controller_init(config.db_ip(), config.db_port(), config.db_username(), config.db_passwd(), config.db_name(), config.db_conn_pool_size());
  17. mb_initialized = true;
  18. return ec;
  19. }
  20. else
  21. {
  22. mb_initialized = false;
  23. return POINTER_IS_NULL;
  24. }
  25. }
  26. // 车位数据库管理反初始化
  27. Error_manager Parkspace_db_manager::Parkspace_db_manager_uninit()
  28. {
  29. if(mp_db_controller!=nullptr)
  30. {
  31. return mp_db_controller->database_controller_uninit();
  32. }else{
  33. return POINTER_IS_NULL;
  34. }
  35. }
  36. // 连接状态
  37. bool Parkspace_db_manager::is_connected()
  38. {
  39. if(mp_db_controller!=nullptr)
  40. {
  41. mb_connected = mp_db_controller->is_connected();
  42. return mb_connected;
  43. }
  44. else{
  45. return false;
  46. }
  47. }
  48. // 初始化状态
  49. bool Parkspace_db_manager::is_initialized()
  50. {
  51. return mb_initialized;
  52. }
  53. // 从数据库获得所有车位信息,用于车位模块初始化
  54. Error_manager Parkspace_db_manager::get_all_parkspace_info(message::Parkspace_allocation_status_msg &all_parkspace_info)
  55. {
  56. // 基本条件判断
  57. if(!mb_initialized)
  58. {
  59. return DB_UNINITIALIZED;
  60. }
  61. if(mp_db_controller == nullptr)
  62. {
  63. return POINTER_IS_NULL;
  64. }else{
  65. //执行sql操作
  66. std::string get_all_space_sql = "select * from parkingspace";
  67. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  68. Error_manager ec = mp_db_controller->sql_query(get_all_space_sql, tp_result);
  69. if(ec == SUCCESS)
  70. {
  71. if(tp_result == nullptr)
  72. {
  73. return DB_RESULT_SET_EMPTY;
  74. }
  75. all_parkspace_info.clear_parkspace_info();
  76. while (tp_result->next())
  77. {
  78. message::Parkspace_info *t_parkspace = all_parkspace_info.add_parkspace_info();
  79. char buf[1024];
  80. memset(buf, 0, 1024);
  81. try
  82. {
  83. t_parkspace->set_parkspace_id(tp_result->getInt("parkingSpaceID"));
  84. t_parkspace->set_index(tp_result->getInt("parkingSpaceIndex"));
  85. if (tp_result->getInt("parkingSpaceDirection") > 0)
  86. {
  87. t_parkspace->set_direction(message::Direction::eForward);
  88. }
  89. else
  90. {
  91. t_parkspace->set_direction(message::Direction::eBackward);
  92. }
  93. t_parkspace->set_floor(tp_result->getInt("parkingSpaceFloor"));
  94. switch (tp_result->getInt("parkingSpaceState"))
  95. {
  96. case 0:
  97. t_parkspace->set_parkspace_status(message::Parkspace_status::eParkspace_empty);
  98. break;
  99. case 1:
  100. t_parkspace->set_parkspace_status(message::Parkspace_status::eParkspace_occupied);
  101. break;
  102. case 2:
  103. t_parkspace->set_parkspace_status(message::Parkspace_status::eParkspace_reserved);
  104. break;
  105. case 3:
  106. t_parkspace->set_parkspace_status(message::Parkspace_status::eParkspace_locked);
  107. break;
  108. case 4:
  109. t_parkspace->set_parkspace_status(message::Parkspace_status::eParkspace_error);
  110. break;
  111. default:
  112. t_parkspace->set_parkspace_status(message::Parkspace_status::eParkspace_error);
  113. break;
  114. }
  115. t_parkspace->set_width(tp_result->getDouble("parkSpaceWidth"));
  116. t_parkspace->set_height(tp_result->getDouble("parkSpaceHeight"));
  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_entry_time(tp_result->getString("entryTime"));
  122. t_parkspace->set_leave_time(tp_result->getString("leaveTime"));
  123. }
  124. catch (sql::SQLException &e)
  125. {
  126. /* Use what() (derived from std::runtime_error) to fetch the error message */
  127. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  128. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  129. }
  130. catch (std::runtime_error &e)
  131. {
  132. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  133. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  134. }
  135. }
  136. return SUCCESS;
  137. }
  138. else
  139. {
  140. return ec;
  141. }
  142. }
  143. }
  144. // 清除数据库中所有车位号牌,状态全修改为空闲
  145. Error_manager Parkspace_db_manager::clean_all_parkspace_info()
  146. {
  147. // 基本条件判断
  148. if(!mb_initialized)
  149. {
  150. return DB_UNINITIALIZED;
  151. }
  152. if(mp_db_controller == nullptr)
  153. {
  154. return POINTER_IS_NULL;
  155. }else{
  156. //执行sql操作
  157. char update_space_sql[1024];
  158. memset(update_space_sql, 0, 1024);
  159. sprintf(update_space_sql, "update parkingspace set parkingSpaceState = 0, parkSpaceCarLicense=''");
  160. Error_manager ec = mp_db_controller->sql_update(update_space_sql);
  161. return ec;
  162. }
  163. return ERROR;
  164. }
  165. // 更新数据库中所有车位信息
  166. Error_manager Parkspace_db_manager::update_all_parkspace_info(message::Parkspace_allocation_status_msg all_parkspace_info)
  167. {
  168. // 基本条件判断
  169. if(!mb_initialized)
  170. {
  171. return DB_UNINITIALIZED;
  172. }
  173. if(mp_db_controller == nullptr)
  174. {
  175. return POINTER_IS_NULL;
  176. }else if(all_parkspace_info.parkspace_info_size() >0){
  177. Error_manager ec = SUCCESS;
  178. for (size_t i = 0; i < all_parkspace_info.parkspace_info_size(); i++)
  179. {
  180. Error_manager t_ec = update_parkspace_status(all_parkspace_info.parkspace_info(i));
  181. ec.compare_and_cover_error(&t_ec);
  182. }
  183. return ec;
  184. }
  185. return ERROR;
  186. }
  187. // 更新数据库中车位状态
  188. Error_manager Parkspace_db_manager::update_parkspace_status(message::Parkspace_info parkspace_info)
  189. {
  190. // 基本条件判断
  191. if(!mb_initialized)
  192. {
  193. return DB_UNINITIALIZED;
  194. }
  195. if(mp_db_controller == nullptr)
  196. {
  197. return POINTER_IS_NULL;
  198. }else{
  199. //执行sql操作
  200. char update_space_sql[1024];
  201. memset(update_space_sql, 0, 1024);
  202. int status = -1;
  203. switch (parkspace_info.parkspace_status())
  204. {
  205. case message::Parkspace_status::eParkspace_empty:
  206. status = 0;
  207. break;
  208. case message::Parkspace_status::eParkspace_occupied:
  209. status = 1;
  210. break;
  211. case message::Parkspace_status::eParkspace_reserved:
  212. status = 2;
  213. break;
  214. case message::Parkspace_status::eParkspace_locked:
  215. status = 3;
  216. break;
  217. case message::Parkspace_status::eParkspace_error:
  218. status = 4;
  219. break;
  220. default:
  221. break;
  222. }
  223. if(parkspace_info.has_car_info())
  224. {
  225. sprintf(update_space_sql, "update parkingspace set parkingSpaceState = %d,parkSpaceCarLicense = '%s',parkSpaceCarLength = %.3f,parkSpaceCarWidth = %.3f,parkSpaceCarHeight = %.3f,entryTime = '%s',leaveTime = '%s' where parkingSpaceID = %d",
  226. status,
  227. parkspace_info.car_info().license().c_str(),
  228. parkspace_info.car_info().has_car_length()?parkspace_info.car_info().car_length():0.0f,
  229. parkspace_info.car_info().car_width(),
  230. parkspace_info.car_info().car_height(),
  231. parkspace_info.entry_time().c_str(),
  232. parkspace_info.leave_time().c_str(),
  233. parkspace_info.parkspace_id());
  234. }else{
  235. sprintf(update_space_sql, "update parkingspace set parkingSpaceState = %d where parkingSpaceID = %d", status, parkspace_info.parkspace_id());
  236. }
  237. if(status < 0)
  238. return ERROR;
  239. //boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  240. Error_manager ec = mp_db_controller->sql_update(update_space_sql);
  241. return ec;
  242. }
  243. }
  244. // 更新数据库车辆状态,车位信息中包含车辆信息用于匹配更新
  245. Error_manager Parkspace_db_manager::update_vehicle_status(message::Parkspace_info parkspace_info, message::Vehicle_status &vehicle_status)
  246. {
  247. // 基本条件判断
  248. if(!mb_initialized)
  249. {
  250. return DB_UNINITIALIZED;
  251. }
  252. if(mp_db_controller == nullptr)
  253. {
  254. return POINTER_IS_NULL;
  255. }else{
  256. //执行sql操作
  257. if(!parkspace_info.has_car_info())
  258. {
  259. return PARAMETER_ERROR;
  260. }
  261. std::string find_vehicle_sql = std::string("select * from vehicle where numberPlate = '").append(parkspace_info.car_info().license()).append("'");
  262. boost::shared_ptr<sql::ResultSet> query_vehicle_result = nullptr;
  263. mp_db_controller->sql_query(find_vehicle_sql, query_vehicle_result);
  264. //判断车辆是否存在
  265. if(query_vehicle_result == nullptr || !query_vehicle_result->next())
  266. {
  267. return insert_vehicle(parkspace_info, vehicle_status);
  268. }else{
  269. char update_vehicle_sql[1024];
  270. memset(update_vehicle_sql, 0, 1024);
  271. int vehicle_status_code = -1;
  272. switch (vehicle_status)
  273. {
  274. case message::Vehicle_status::eVehicle_idle:
  275. vehicle_status_code = 0;
  276. break;
  277. case message::Vehicle_status::eVehicle_in_garage:
  278. vehicle_status_code = 1;
  279. break;
  280. case message::Vehicle_status::eVehicle_parking:
  281. vehicle_status_code = 2;
  282. break;
  283. case message::Vehicle_status::eVehicle_fetching:
  284. vehicle_status_code = 3;
  285. break;
  286. case message::Vehicle_status::eVehicle_reserved:
  287. vehicle_status_code = 4;
  288. break;
  289. default:
  290. break;
  291. }
  292. //车位为空,仅更新车辆状态与长宽高
  293. if(parkspace_info.parkspace_id() <= 0)
  294. {
  295. sprintf(update_vehicle_sql, "update vehicle set vehicleParkState = %d,carLength=%.3f,carWidth=%.3f,carHeight=%.3f where numberPlate = '%s'",
  296. vehicle_status_code,
  297. parkspace_info.car_info().car_length(),
  298. parkspace_info.car_info().car_width(),
  299. parkspace_info.car_info().car_height(),
  300. parkspace_info.car_info().license().c_str());
  301. }
  302. //车位不为空,表示车辆所在车位,同时更新
  303. else
  304. {
  305. sprintf(update_vehicle_sql, "update vehicle set vehicleParkState = %d,carLength=%.3f,carWidth=%.3f,carHeight=%.3f,parkingSpaceID=%d where numberPlate = '%s'",
  306. vehicle_status_code,
  307. parkspace_info.car_info().car_length(),
  308. parkspace_info.car_info().car_width(),
  309. parkspace_info.car_info().car_height(),
  310. parkspace_info.parkspace_id(),
  311. parkspace_info.car_info().license().c_str());
  312. }
  313. if (vehicle_status_code < 0)
  314. return ERROR;
  315. Error_manager ec = mp_db_controller->sql_update(update_vehicle_sql);
  316. return ec;
  317. }
  318. }
  319. }
  320. // 插入车辆
  321. Error_manager Parkspace_db_manager::insert_vehicle(message::Parkspace_info parkspace_info, message::Vehicle_status &vehicle_status)
  322. {
  323. // 基本条件判断
  324. if(!mb_initialized)
  325. {
  326. return DB_UNINITIALIZED;
  327. }
  328. if(mp_db_controller == nullptr)
  329. {
  330. return POINTER_IS_NULL;
  331. }else{
  332. //执行sql操作
  333. if(!parkspace_info.has_car_info())
  334. {
  335. return PARAMETER_ERROR;
  336. }
  337. char insert_vehicle_sql[1024];
  338. memset(insert_vehicle_sql, 0, 1024);
  339. int vehicle_status_code = -1;
  340. switch (vehicle_status)
  341. {
  342. case message::Vehicle_status::eVehicle_idle:
  343. vehicle_status_code = 0;
  344. break;
  345. case message::Vehicle_status::eVehicle_in_garage:
  346. vehicle_status_code = 1;
  347. break;
  348. case message::Vehicle_status::eVehicle_parking:
  349. vehicle_status_code = 2;
  350. break;
  351. case message::Vehicle_status::eVehicle_fetching:
  352. vehicle_status_code = 3;
  353. break;
  354. case message::Vehicle_status::eVehicle_reserved:
  355. vehicle_status_code = 4;
  356. break;
  357. default:
  358. break;
  359. }
  360. //车位为空,仅更新车辆状态与长宽高
  361. if(parkspace_info.parkspace_id() <= 0)
  362. {
  363. sprintf(insert_vehicle_sql, "INSERT INTO vehicle (numberPlate,vehicleParkState,carLength,carWidth,carHeight) values ('%s',%d,%.3f,%.3f,%.3f)",
  364. parkspace_info.car_info().license().c_str(),
  365. vehicle_status_code,
  366. parkspace_info.car_info().car_length(),
  367. parkspace_info.car_info().car_width(),
  368. parkspace_info.car_info().car_height());
  369. }
  370. //车位不为空,表示车辆在车位,同时更新
  371. else
  372. {
  373. sprintf(insert_vehicle_sql, "INSERT INTO vehicle (numberPlate,vehicleParkState,carLength,carWidth,carHeight,parkingSpaceID) values ('%s',%d,%.3f,%.3f,%.3f,%d)",
  374. parkspace_info.car_info().license().c_str(),
  375. vehicle_status_code,
  376. parkspace_info.car_info().car_length(),
  377. parkspace_info.car_info().car_width(),
  378. parkspace_info.car_info().car_height(),
  379. parkspace_info.parkspace_id());
  380. }
  381. if (vehicle_status_code < 0)
  382. return ERROR;
  383. Error_manager ec = mp_db_controller->sql_insert(insert_vehicle_sql);
  384. return ec;
  385. }
  386. }
  387. // 插入停车记录
  388. Error_manager Parkspace_db_manager::insert_parking_record(message::Parkspace_info parkspace_info)
  389. {
  390. // 基本条件判断
  391. if(!mb_initialized)
  392. {
  393. return DB_UNINITIALIZED;
  394. }
  395. if(mp_db_controller == nullptr)
  396. {
  397. return POINTER_IS_NULL;
  398. }else{
  399. //参数中必须包含车辆信息与车位编号
  400. if(!parkspace_info.has_car_info() || parkspace_info.parkspace_id() <= 0)
  401. {
  402. return PARAMETER_ERROR;
  403. }
  404. char insert_parking_record_sql[1024];
  405. memset(insert_parking_record_sql, 0, 1024);
  406. //将车辆号牌,车位ID,记录状态,停车与取车时间写入
  407. sprintf(insert_parking_record_sql, "INSERT INTO parkingrecords (numberPlate,parkingSpaceID,realParkTime) values ('%s',%d,'%s')",
  408. parkspace_info.car_info().license().c_str(),
  409. parkspace_info.parkspace_id(),
  410. parkspace_info.entry_time().c_str());
  411. Error_manager ec = mp_db_controller->sql_insert(insert_parking_record_sql);
  412. return ec;
  413. }
  414. }
  415. // 更新停车记录,待计费系统加入后完善!!!
  416. Error_manager Parkspace_db_manager::update_parking_record(message::Parkspace_info parkspace_info, int record_id)
  417. {
  418. // 基本条件判断
  419. if(!mb_initialized)
  420. {
  421. return DB_UNINITIALIZED;
  422. }
  423. if(mp_db_controller == nullptr)
  424. {
  425. return POINTER_IS_NULL;
  426. }else{
  427. //执行sql操作
  428. if(!parkspace_info.has_car_info() || parkspace_info.parkspace_id() <= 0)
  429. {
  430. return PARAMETER_ERROR;
  431. }
  432. char update_parking_record_sql[1024];
  433. memset(update_parking_record_sql, 0, 1024);
  434. sprintf(update_parking_record_sql, "update parkingrecords set realGetTime='%s',parkingPrice=%d where parkingRecordsID = %d",
  435. parkspace_info.leave_time().c_str(),
  436. 0,
  437. record_id);
  438. Error_manager ec = mp_db_controller->sql_update(update_parking_record_sql);
  439. return ec;
  440. }
  441. }
  442. // 根据车位编号查询车位状态
  443. Error_manager Parkspace_db_manager::query_parkspace(int parkspace_id, message::Parkspace_info &parkspace_info)
  444. {
  445. // 基本条件判断
  446. if(!mb_initialized)
  447. {
  448. return DB_UNINITIALIZED;
  449. }
  450. if(mp_db_controller == nullptr)
  451. {
  452. return POINTER_IS_NULL;
  453. }else{
  454. //执行sql操作
  455. std::string query_parkspace_sql = std::string("select * from parkingspace where parkspace_id=").append(std::to_string(parkspace_id));
  456. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  457. Error_manager ec = mp_db_controller->sql_query(query_parkspace_sql, tp_result);
  458. if(ec == SUCCESS)
  459. {
  460. if(tp_result == nullptr)
  461. {
  462. return DB_RESULT_SET_EMPTY;
  463. }
  464. if (tp_result->next())
  465. {
  466. char buf[1024];
  467. memset(buf, 0, 1024);
  468. try
  469. {
  470. switch (tp_result->getInt("parkingSpaceState"))
  471. {
  472. case 0:
  473. parkspace_info.set_parkspace_status(message::Parkspace_status::eParkspace_empty);
  474. break;
  475. case 1:
  476. parkspace_info.set_parkspace_status(message::Parkspace_status::eParkspace_occupied);
  477. break;
  478. case 2:
  479. parkspace_info.set_parkspace_status(message::Parkspace_status::eParkspace_reserved);
  480. break;
  481. case 3:
  482. parkspace_info.set_parkspace_status(message::Parkspace_status::eParkspace_locked);
  483. break;
  484. case 4:
  485. parkspace_info.set_parkspace_status(message::Parkspace_status::eParkspace_error);
  486. break;
  487. default:
  488. parkspace_info.set_parkspace_status(message::Parkspace_status::eParkspace_error);
  489. break;
  490. }
  491. parkspace_info.set_index(tp_result->getInt("parkingSpaceIndex"));
  492. parkspace_info.set_direction(tp_result->getInt("parkingSpaceDirection") > 0 ? message::Direction::eForward : message::Direction::eBackward);
  493. parkspace_info.set_floor(tp_result->getInt("parkingSpaceFloor"));
  494. parkspace_info.mutable_car_info()->set_license(tp_result->getString("parkSpaceCarLicense"));
  495. parkspace_info.mutable_car_info()->set_car_length(tp_result->getDouble("parkSpaceCatLength"));
  496. parkspace_info.mutable_car_info()->set_car_width(tp_result->getDouble("parkSpaceCarWidth"));
  497. parkspace_info.mutable_car_info()->set_car_height(tp_result->getDouble("parkSpaceCarHeight"));
  498. parkspace_info.set_entry_time(tp_result->getString("entryTime"));
  499. parkspace_info.set_leave_time(tp_result->getString("leaveTime"));
  500. }
  501. catch (sql::SQLException &e)
  502. {
  503. /* Use what() (derived from std::runtime_error) to fetch the error message */
  504. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  505. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  506. }
  507. catch (std::runtime_error &e)
  508. {
  509. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  510. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  511. }
  512. }
  513. return SUCCESS;
  514. }
  515. else
  516. {
  517. return ec;
  518. }
  519. }
  520. }
  521. // 查询车辆状态,暂时不使用
  522. Error_manager Parkspace_db_manager::query_vehicle(std::string license, message::Vehicle_status &vehicle_status, int &park_record_id)
  523. {
  524. // 基本条件判断
  525. if(!mb_initialized)
  526. {
  527. return DB_UNINITIALIZED;
  528. }
  529. if(mp_db_controller == nullptr)
  530. {
  531. return POINTER_IS_NULL;
  532. }else{
  533. //执行sql操作
  534. std::string query_parkspace_sql = std::string("select * from vehicle where numberPlate='").append(license).append("'");
  535. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  536. Error_manager ec = mp_db_controller->sql_query(query_parkspace_sql, tp_result);
  537. if(ec == SUCCESS)
  538. {
  539. if(tp_result == nullptr)
  540. {
  541. return DB_RESULT_SET_EMPTY;
  542. }
  543. if (tp_result->next())
  544. {
  545. char buf[1024];
  546. memset(buf, 0, 1024);
  547. try
  548. {
  549. switch (tp_result->getInt("vehicleParkState"))
  550. {
  551. case 0:
  552. vehicle_status = message::Vehicle_status::eVehicle_idle;
  553. break;
  554. case 1:
  555. vehicle_status = message::Vehicle_status::eVehicle_in_garage;
  556. break;
  557. case 2:
  558. vehicle_status = message::Vehicle_status::eVehicle_parking;
  559. break;
  560. case 3:
  561. vehicle_status = message::Vehicle_status::eVehicle_fetching;
  562. break;
  563. case 4:
  564. vehicle_status = message::Vehicle_status::eVehicle_reserved;
  565. break;
  566. default:
  567. vehicle_status = message::Vehicle_status::eVehicle_reserved;
  568. break;
  569. }
  570. park_record_id = tp_result->getInt("parkingRecordsID");
  571. }
  572. catch (sql::SQLException &e)
  573. {
  574. /* Use what() (derived from std::runtime_error) to fetch the error message */
  575. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  576. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  577. }
  578. catch (std::runtime_error &e)
  579. {
  580. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  581. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  582. }
  583. }
  584. return SUCCESS;
  585. }
  586. else
  587. {
  588. return ec;
  589. }
  590. }
  591. }
  592. // 查询停车记录,根据车牌号、车位id查询,反馈停车记录id
  593. Error_manager Parkspace_db_manager::query_parking_record(message::Parkspace_info &parkspace_info, int &record_id)
  594. {
  595. // 基本条件判断
  596. if(!mb_initialized)
  597. {
  598. return DB_UNINITIALIZED;
  599. }
  600. if(mp_db_controller == nullptr)
  601. {
  602. return POINTER_IS_NULL;
  603. }else{
  604. //执行sql操作
  605. if(!parkspace_info.has_car_info() || parkspace_info.parkspace_id() <= 0)
  606. {
  607. return PARAMETER_ERROR;
  608. }
  609. 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.parkspace_id())).append(" ORDER BY parkingRecordsID DESC");
  610. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  611. Error_manager ec = mp_db_controller->sql_query(query_park_record_sql, tp_result);
  612. if(ec == SUCCESS)
  613. {
  614. if(tp_result == nullptr)
  615. {
  616. return DB_RESULT_SET_EMPTY;
  617. }
  618. if (tp_result->next())
  619. {
  620. char buf[1024];
  621. memset(buf, 0, 1024);
  622. try
  623. {
  624. record_id = tp_result->getInt("parkingRecordsID");
  625. }
  626. catch (sql::SQLException &e)
  627. {
  628. /* Use what() (derived from std::runtime_error) to fetch the error message */
  629. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  630. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  631. }
  632. catch (std::runtime_error &e)
  633. {
  634. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  635. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  636. }
  637. }
  638. return SUCCESS;
  639. }
  640. else
  641. {
  642. return ec;
  643. }
  644. }
  645. }