parkspace_operating_function.cpp 53 KB

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