parkspace_operating_function.cpp 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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. std::cout<<"query_parkspace_sql = "<<query_parkspace_sql<<std::endl;
  842. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  843. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  844. if(ec == SUCCESS)
  845. {
  846. if(tp_result == nullptr)
  847. {
  848. return DB_RESULT_SET_EMPTY;
  849. }
  850. if (tp_result->next())
  851. {
  852. char buf[1024];
  853. memset(buf, 0, 1024);
  854. try
  855. {
  856. if ( tp_result->getInt("parkingspace_status") != message::Parkspace_status::eParkspace_empty )
  857. {
  858. return Error_manager(Error_code::DB_QUERY_DATA_FAILED, Error_level::MAJOR_ERROR,
  859. "数据库查询数据错误 Parkspace_operating_function::query_one_empty_parkspace error ");
  860. }
  861. parkspace_info.set_parkingspace_status(message::Parkspace_status::eParkspace_empty);
  862. parkspace_info.set_parkingspace_index_id(tp_result->getInt("parkingSpaceID"));
  863. parkspace_info.set_parkingspace_unit_id(tp_result->getInt("parkingspace_unit_id"));
  864. parkspace_info.set_parkingspace_label_id(tp_result->getInt("parkingspace_label_id"));
  865. parkspace_info.set_parkingspace_room_id(tp_result->getInt("parkingspace_room_id"));
  866. parkspace_info.set_parkingspace_direction(tp_result->getInt("parkingspace_direction") > 0 ? message::Direction::eForward : message::Direction::eBackward);
  867. parkspace_info.set_parkingspace_floor_id(tp_result->getInt("parkingspace_floor_id"));
  868. parkspace_info.set_parkingspace_width(tp_result->getDouble("parkingspace_width"));
  869. parkspace_info.set_parkingspace_height(tp_result->getDouble("parkingspace_height"));
  870. parkspace_info.mutable_car_info()->set_car_numberplate(tp_result->getString("parkSpaceCarNumberPlate"));
  871. parkspace_info.mutable_car_info()->set_license(tp_result->getString("parkSpaceCarLicense"));
  872. parkspace_info.mutable_car_info()->set_car_length(tp_result->getDouble("parkSpaceCarLength"));
  873. parkspace_info.mutable_car_info()->set_car_width(tp_result->getDouble("parkSpaceCarWidth"));
  874. parkspace_info.mutable_car_info()->set_car_height(tp_result->getDouble("parkSpaceCarHeight"));
  875. parkspace_info.set_car_type((message::Car_type)tp_result->getInt("parkSpaceCarType"));
  876. parkspace_info.mutable_car_info()->set_car_wheel_base(tp_result->getDouble("parkSpaceCarWheelBase"));
  877. parkspace_info.mutable_car_info()->set_car_wheel_width(tp_result->getDouble("parkSpaceCarWheelWidth"));
  878. parkspace_info.set_entry_time(tp_result->getString("entryTime"));
  879. parkspace_info.set_leave_time(tp_result->getString("leaveTime"));
  880. }
  881. catch (sql::SQLException &e)
  882. {
  883. /* Use what() (derived from std::runtime_error) to fetch the error message */
  884. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  885. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  886. }
  887. catch (std::runtime_error &e)
  888. {
  889. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  890. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  891. }
  892. }
  893. else
  894. {
  895. return Error_manager(Error_code::DB_NOT_QUERY_EMPTY_PARKSPACE, Error_level::MINOR_ERROR,
  896. "数据库未查询到空车位 Parkspace_operating_function::query_one_empty_parkspace error ");
  897. }
  898. return SUCCESS;
  899. }
  900. else
  901. {
  902. return ec;
  903. }
  904. return SUCCESS;
  905. }
  906. // 查询车辆状态,暂时不使用
  907. Error_manager Parkspace_operating_function::query_vehicle(std::string license, message::Vehicle_status &vehicle_status, int &park_record_id)
  908. {
  909. //执行sql操作
  910. std::string query_parkspace_sql = std::string("select * from vehicle where numberPlate='").append(license).append("'");
  911. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  912. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  913. if(ec == SUCCESS)
  914. {
  915. if(tp_result == nullptr)
  916. {
  917. return Error_manager(Error_code::DB_RESULT_SET_EMPTY, Error_level::MINOR_ERROR,
  918. "Parkspace_operating_function::query_vehicle error ");
  919. }
  920. if (tp_result->next())
  921. {
  922. char buf[1024];
  923. memset(buf, 0, 1024);
  924. try
  925. {
  926. switch (tp_result->getInt("vehicleParkState"))
  927. {
  928. case 1:
  929. vehicle_status = message::Vehicle_status::eVehicle_idle;
  930. break;
  931. case 2:
  932. vehicle_status = message::Vehicle_status::eVehicle_in_garage;
  933. break;
  934. case 3:
  935. vehicle_status = message::Vehicle_status::eVehicle_parking;
  936. break;
  937. case 4:
  938. vehicle_status = message::Vehicle_status::eVehicle_fetching;
  939. break;
  940. case 5:
  941. vehicle_status = message::Vehicle_status::eVehicle_reserved;
  942. break;
  943. default:
  944. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  945. "参数错误 Parkspace_operating_function::query_vehicle error ");
  946. }
  947. park_record_id = tp_result->getInt("parkingRecordsID");
  948. }
  949. catch (sql::SQLException &e)
  950. {
  951. /* Use what() (derived from std::runtime_error) to fetch the error message */
  952. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  953. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  954. }
  955. catch (std::runtime_error &e)
  956. {
  957. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  958. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  959. }
  960. }
  961. return SUCCESS;
  962. }
  963. else
  964. {
  965. return ec;
  966. }
  967. }
  968. // 查询停车记录,根据车牌号、车位id查询,反馈停车记录id
  969. Error_manager Parkspace_operating_function::query_parking_record(message::Parkspace_info &parkspace_info, int &record_id)
  970. {
  971. //执行sql操作
  972. if(!parkspace_info.has_car_info() || parkspace_info.parkingspace_index_id() <= 0)
  973. {
  974. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  975. " Parkspace_operating_function::query_parking_record error ");
  976. }
  977. 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");
  978. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  979. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_park_record_sql, tp_result);
  980. if(ec == SUCCESS)
  981. {
  982. if(tp_result == nullptr)
  983. {
  984. return Error_manager(Error_code::DB_RESULT_SET_EMPTY, Error_level::MINOR_ERROR,
  985. " Parkspace_operating_function::query_parking_record error ");
  986. }
  987. if (tp_result->next())
  988. {
  989. char buf[1024];
  990. memset(buf, 0, 1024);
  991. try
  992. {
  993. record_id = tp_result->getInt("parkingRecordsID");
  994. }
  995. catch (sql::SQLException &e)
  996. {
  997. /* Use what() (derived from std::runtime_error) to fetch the error message */
  998. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  999. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  1000. }
  1001. catch (std::runtime_error &e)
  1002. {
  1003. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  1004. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  1005. }
  1006. }
  1007. return SUCCESS;
  1008. }
  1009. else
  1010. {
  1011. return ec;
  1012. }
  1013. }