parkspace_operating_function.cpp 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  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,message::Locate_information locateInformation)
  444. {
  445. //参数中必须包含车辆信息与车位编号
  446. if(!parkspace_info.has_car_info() || parkspace_info.parkingspace_index_id() <= 0 || !locateInformation.has_locate_x())
  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,locateX,locateY,locateAngle,locateLength,locateWidth,locateHeigth,locateWheelBase,locateWheelWidth) values ('%s',%d,'%s',%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f)",
  455. parkspace_info.car_info().license().c_str(),
  456. parkspace_info.parkingspace_index_id(),
  457. parkspace_info.entry_time().c_str(),
  458. locateInformation.locate_x(),
  459. locateInformation.locate_y(),
  460. locateInformation.locate_angle(),
  461. locateInformation.locate_length(),
  462. locateInformation.locate_width(),
  463. locateInformation.locate_height(),
  464. locateInformation.locate_wheel_base(),
  465. locateInformation.locate_wheel_width());
  466. Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_parking_record_sql);
  467. return ec;
  468. }
  469. // 更新停车记录,待计费系统加入后完善!!!
  470. Error_manager Parkspace_operating_function::update_parking_record(message::Parkspace_info parkspace_info, int record_id)
  471. {
  472. //执行sql操作
  473. if(!parkspace_info.has_car_info() || parkspace_info.parkingspace_index_id() <= 0)
  474. {
  475. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  476. "参数错误 update_parking_record::insert_parking_record error ");;
  477. }
  478. char update_parking_record_sql[1024];
  479. memset(update_parking_record_sql, 0, 1024);
  480. sprintf(update_parking_record_sql, "update parkingrecords set realGetTime='%s',parkingPrice=%d where parkingRecordsID = %d",
  481. parkspace_info.leave_time().c_str(),
  482. 0,
  483. record_id);
  484. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_parking_record_sql);
  485. return ec;
  486. }
  487. // 根据车位编号查询车位信息
  488. Error_manager Parkspace_operating_function::query_one_parkspace_with_parkspace_id(int parkspace_id, message::Parkspace_info &parkspace_info)
  489. {
  490. //执行sql操作
  491. std::string query_parkspace_sql = std::string("select * from parkingspace where parkingSpaceID=").append(std::to_string(parkspace_id));
  492. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  493. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  494. if(ec == SUCCESS)
  495. {
  496. if(tp_result == nullptr)
  497. {
  498. return Error_manager(Error_code::DB_RESULT_SET_EMPTY, Error_level::MAJOR_ERROR,
  499. " 数据库外层查询返回结果空指针 Parkspace_operating_function::query_one_parkspace_with_parkspace_id error ");;
  500. }
  501. if (tp_result->next())
  502. {
  503. char buf[1024];
  504. memset(buf, 0, 1024);
  505. try
  506. {
  507. parkspace_info.set_parkingspace_status((message::Parkspace_status)tp_result->getInt("parkingspace_status"));
  508. parkspace_info.set_parkingspace_index_id(tp_result->getInt("parkingSpaceID"));
  509. parkspace_info.set_parkingspace_unit_id(tp_result->getInt("parkingspace_unit_id"));
  510. parkspace_info.set_parkingspace_label_id(tp_result->getInt("parkingspace_label_id"));
  511. parkspace_info.set_parkingspace_room_id(tp_result->getInt("parkingspace_room_id"));
  512. parkspace_info.set_parkingspace_direction(tp_result->getInt("parkingspace_direction") > 0 ? message::Direction::eForward : message::Direction::eBackward);
  513. parkspace_info.set_parkingspace_floor_id(tp_result->getInt("parkingspace_floor_id"));
  514. parkspace_info.set_parkingspace_width(tp_result->getDouble("parkingspace_width"));
  515. parkspace_info.set_parkingspace_height(tp_result->getDouble("parkingspace_height"));
  516. parkspace_info.mutable_car_info()->set_car_numberplate(tp_result->getString("parkSpaceCarNumberPlate"));
  517. parkspace_info.mutable_car_info()->set_license(tp_result->getString("parkSpaceCarLicense"));
  518. parkspace_info.mutable_car_info()->set_car_length(tp_result->getDouble("parkSpaceCarLength"));
  519. parkspace_info.mutable_car_info()->set_car_width(tp_result->getDouble("parkSpaceCarWidth"));
  520. parkspace_info.mutable_car_info()->set_car_height(tp_result->getDouble("parkSpaceCarHeight"));
  521. parkspace_info.set_car_type((message::Car_type)tp_result->getInt("parkSpaceCarType"));
  522. parkspace_info.mutable_car_info()->set_car_wheel_base(tp_result->getDouble("parkSpaceCarWheelBase"));
  523. parkspace_info.mutable_car_info()->set_car_wheel_width(tp_result->getDouble("parkSpaceCarWheelWidth"));
  524. parkspace_info.set_entry_time(tp_result->getString("entryTime"));
  525. parkspace_info.set_leave_time(tp_result->getString("leaveTime"));
  526. }
  527. catch (sql::SQLException &e)
  528. {
  529. /* Use what() (derived from std::runtime_error) to fetch the error message */
  530. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  531. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  532. }
  533. catch (std::runtime_error &e)
  534. {
  535. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  536. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  537. }
  538. if (tp_result->next())
  539. {
  540. return Error_manager(Error_code::DB_QUERY_DATA_REPEAT, Error_level::MAJOR_ERROR,
  541. "数据库查询数据重复 Parkspace_operating_function::query_one_parkspace_with_parkspace_id error ");
  542. }
  543. }
  544. else
  545. {
  546. return Error_manager(Error_code::DB_QUERY_NOT_DATA, Error_level::MINOR_ERROR,
  547. "数据库查询未找到数据 Parkspace_operating_function::query_one_parkspace_with_parkspace_id error ");
  548. }
  549. return SUCCESS;
  550. }
  551. else
  552. {
  553. return ec;
  554. }
  555. }
  556. // 根据车牌号查询状态为占用的车位
  557. Error_manager Parkspace_operating_function::query_one_occupied_parkspace_with_license(std::string license, message::Parkspace_info &parkspace_info)
  558. {
  559. //执行sql操作
  560. //std::string query_parkspace_sql = std::string("select * from parkingspace where parkingspace_status == 2 and parkSpaceCarLicense='").append(license).append("'");
  561. char query_parkspace_sql[1024];
  562. memset(query_parkspace_sql, 0, 1024);
  563. sprintf(query_parkspace_sql,"select * from parkingspace where parkingspace_status = %d and parkSpaceCarLicense='%s' ",message::Parkspace_status::eParkspace_occupied,license.c_str());
  564. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  565. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  566. if(ec == SUCCESS)
  567. {
  568. if(tp_result == nullptr)
  569. {
  570. return Error_manager(Error_code::DB_RESULT_SET_EMPTY, Error_level::MINOR_ERROR,
  571. "数据库外层查询返回结果空指针 Parkspace_operating_function::query_one_parkspace_with_license error ");
  572. }
  573. if (tp_result->next())
  574. {
  575. char buf[1024];
  576. memset(buf, 0, 1024);
  577. try
  578. {
  579. parkspace_info.set_parkingspace_status((message::Parkspace_status)tp_result->getInt("parkingspace_status"));
  580. parkspace_info.set_parkingspace_index_id(tp_result->getInt("parkingSpaceID"));
  581. parkspace_info.set_parkingspace_unit_id(tp_result->getInt("parkingspace_unit_id"));
  582. parkspace_info.set_parkingspace_label_id(tp_result->getInt("parkingspace_label_id"));
  583. parkspace_info.set_parkingspace_room_id(tp_result->getInt("parkingspace_room_id"));
  584. parkspace_info.set_parkingspace_direction(tp_result->getInt("parkingspace_direction") > 0 ? message::Direction::eForward : message::Direction::eBackward);
  585. parkspace_info.set_parkingspace_floor_id(tp_result->getInt("parkingspace_floor_id"));
  586. parkspace_info.set_parkingspace_width(tp_result->getDouble("parkingspace_width"));
  587. parkspace_info.set_parkingspace_height(tp_result->getDouble("parkingspace_height"));
  588. parkspace_info.mutable_car_info()->set_car_numberplate(tp_result->getString("parkSpaceCarNumberPlate"));
  589. parkspace_info.mutable_car_info()->set_license(tp_result->getString("parkSpaceCarLicense"));
  590. parkspace_info.mutable_car_info()->set_car_length(tp_result->getDouble("parkSpaceCarLength"));
  591. parkspace_info.mutable_car_info()->set_car_width(tp_result->getDouble("parkSpaceCarWidth"));
  592. parkspace_info.mutable_car_info()->set_car_height(tp_result->getDouble("parkSpaceCarHeight"));
  593. parkspace_info.set_car_type((message::Car_type)tp_result->getInt("parkSpaceCarType"));
  594. parkspace_info.mutable_car_info()->set_car_wheel_base(tp_result->getDouble("parkSpaceCarWheelBase"));
  595. parkspace_info.mutable_car_info()->set_car_wheel_width(tp_result->getDouble("parkSpaceCarWheelWidth"));
  596. parkspace_info.set_entry_time(tp_result->getString("entryTime"));
  597. parkspace_info.set_leave_time(tp_result->getString("leaveTime"));
  598. }
  599. catch (sql::SQLException &e)
  600. {
  601. /* Use what() (derived from std::runtime_error) to fetch the error message */
  602. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  603. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  604. }
  605. catch (std::runtime_error &e)
  606. {
  607. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  608. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  609. }
  610. if (tp_result->next())
  611. {
  612. return Error_manager(Error_code::DB_QUERY_DATA_REPEAT, Error_level::MAJOR_ERROR,
  613. "数据库查询数据重复 Parkspace_operating_function::query_one_parkspace_with_license error ");
  614. }
  615. }
  616. else
  617. {
  618. return Error_manager(Error_code::DB_QUERY_NOT_DATA, Error_level::MINOR_ERROR,
  619. "数据库未查询到数据 Parkspace_operating_function::query_one_parkspace_with_license error ");
  620. }
  621. return SUCCESS;
  622. }
  623. else
  624. {
  625. return ec;
  626. }
  627. }
  628. // 根据车牌号查询车位
  629. Error_manager Parkspace_operating_function::query_one_parkspace_with_license(std::string license, message::Parkspace_info &parkspace_info)
  630. {
  631. //执行sql操作
  632. //std::string query_parkspace_sql = std::string("select * from parkingspace where parkingspace_status == 2 and parkSpaceCarLicense='").append(license).append("'");
  633. char query_parkspace_sql[1024];
  634. memset(query_parkspace_sql, 0, 1024);
  635. sprintf(query_parkspace_sql,"select * from parkingspace where parkingspace_status != %d and parkSpaceCarNumberPlate='%s' ",message::Parkspace_status::eParkspace_empty,license.c_str());
  636. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  637. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  638. if(ec == SUCCESS)
  639. {
  640. if(tp_result == nullptr)
  641. {
  642. return Error_manager(Error_code::DB_RESULT_SET_EMPTY, Error_level::MINOR_ERROR,
  643. "数据库外层查询返回结果空指针 Parkspace_operating_function::query_one_parkspace_with_license error ");
  644. }
  645. if (tp_result->next())
  646. {
  647. char buf[1024];
  648. memset(buf, 0, 1024);
  649. try
  650. {
  651. parkspace_info.set_parkingspace_status((message::Parkspace_status)tp_result->getInt("parkingspace_status"));
  652. parkspace_info.set_parkingspace_index_id(tp_result->getInt("parkingSpaceID"));
  653. parkspace_info.set_parkingspace_unit_id(tp_result->getInt("parkingspace_unit_id"));
  654. parkspace_info.set_parkingspace_label_id(tp_result->getInt("parkingspace_label_id"));
  655. parkspace_info.set_parkingspace_room_id(tp_result->getInt("parkingspace_room_id"));
  656. parkspace_info.set_parkingspace_direction(tp_result->getInt("parkingspace_direction") > 0 ? message::Direction::eForward : message::Direction::eBackward);
  657. parkspace_info.set_parkingspace_floor_id(tp_result->getInt("parkingspace_floor_id"));
  658. parkspace_info.set_parkingspace_width(tp_result->getDouble("parkingspace_width"));
  659. parkspace_info.set_parkingspace_height(tp_result->getDouble("parkingspace_height"));
  660. parkspace_info.mutable_car_info()->set_car_numberplate(tp_result->getString("parkSpaceCarNumberPlate"));
  661. parkspace_info.mutable_car_info()->set_license(tp_result->getString("parkSpaceCarLicense"));
  662. parkspace_info.mutable_car_info()->set_car_length(tp_result->getDouble("parkSpaceCarLength"));
  663. parkspace_info.mutable_car_info()->set_car_width(tp_result->getDouble("parkSpaceCarWidth"));
  664. parkspace_info.mutable_car_info()->set_car_height(tp_result->getDouble("parkSpaceCarHeight"));
  665. parkspace_info.set_car_type((message::Car_type)tp_result->getInt("parkSpaceCarType"));
  666. parkspace_info.mutable_car_info()->set_car_wheel_base(tp_result->getDouble("parkSpaceCarWheelBase"));
  667. parkspace_info.mutable_car_info()->set_car_wheel_width(tp_result->getDouble("parkSpaceCarWheelWidth"));
  668. parkspace_info.set_entry_time(tp_result->getString("entryTime"));
  669. parkspace_info.set_leave_time(tp_result->getString("leaveTime"));
  670. }
  671. catch (sql::SQLException &e)
  672. {
  673. /* Use what() (derived from std::runtime_error) to fetch the error message */
  674. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  675. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  676. }
  677. catch (std::runtime_error &e)
  678. {
  679. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  680. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  681. }
  682. if (tp_result->next())
  683. {
  684. return Error_manager(Error_code::DB_QUERY_DATA_REPEAT, Error_level::MAJOR_ERROR,
  685. "数据库查询数据重复 Parkspace_operating_function::query_one_parkspace_with_license error ");
  686. }
  687. }
  688. else
  689. {
  690. return Error_manager(Error_code::DB_QUERY_NOT_DATA, Error_level::MINOR_ERROR,
  691. "数据库未查询到数据 Parkspace_operating_function::query_one_parkspace_with_license error ");
  692. }
  693. return SUCCESS;
  694. }
  695. else
  696. {
  697. return ec;
  698. }
  699. }
  700. // 根据车牌号查询车位
  701. Error_manager Parkspace_operating_function::query_one_parkspace_with_numberPlate(std::string numberPlate, message::Parkspace_info &parkspace_info)
  702. {
  703. //执行sql操作
  704. //std::string query_parkspace_sql = std::string("select * from parkingspace where parkingspace_status == 2 and parkSpaceCarLicense='").append(license).append("'");
  705. char query_parkspace_sql[1024];
  706. memset(query_parkspace_sql, 0, 1024);
  707. sprintf(query_parkspace_sql,"select * from parkingspace where parkingspace_status != %d and parkSpaceCarNumberPlate='%s' ",message::Parkspace_status::eParkspace_empty,numberPlate.c_str());
  708. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  709. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  710. if(ec == SUCCESS)
  711. {
  712. if(tp_result == nullptr)
  713. {
  714. return Error_manager(Error_code::DB_RESULT_SET_EMPTY, Error_level::MINOR_ERROR,
  715. "数据库外层查询返回结果空指针 Parkspace_operating_function::query_one_parkspace_with_license error ");
  716. }
  717. if (tp_result->next())
  718. {
  719. char buf[1024];
  720. memset(buf, 0, 1024);
  721. try
  722. {
  723. parkspace_info.set_parkingspace_status((message::Parkspace_status)tp_result->getInt("parkingspace_status"));
  724. parkspace_info.set_parkingspace_index_id(tp_result->getInt("parkingSpaceID"));
  725. parkspace_info.set_parkingspace_unit_id(tp_result->getInt("parkingspace_unit_id"));
  726. parkspace_info.set_parkingspace_label_id(tp_result->getInt("parkingspace_label_id"));
  727. parkspace_info.set_parkingspace_room_id(tp_result->getInt("parkingspace_room_id"));
  728. parkspace_info.set_parkingspace_direction(tp_result->getInt("parkingspace_direction") > 0 ? message::Direction::eForward : message::Direction::eBackward);
  729. parkspace_info.set_parkingspace_floor_id(tp_result->getInt("parkingspace_floor_id"));
  730. parkspace_info.set_parkingspace_width(tp_result->getDouble("parkingspace_width"));
  731. parkspace_info.set_parkingspace_height(tp_result->getDouble("parkingspace_height"));
  732. parkspace_info.mutable_car_info()->set_car_numberplate(tp_result->getString("parkSpaceCarNumberPlate"));
  733. parkspace_info.mutable_car_info()->set_license(tp_result->getString("parkSpaceCarLicense"));
  734. parkspace_info.mutable_car_info()->set_car_length(tp_result->getDouble("parkSpaceCarLength"));
  735. parkspace_info.mutable_car_info()->set_car_width(tp_result->getDouble("parkSpaceCarWidth"));
  736. parkspace_info.mutable_car_info()->set_car_height(tp_result->getDouble("parkSpaceCarHeight"));
  737. parkspace_info.set_car_type((message::Car_type)tp_result->getInt("parkSpaceCarType"));
  738. parkspace_info.mutable_car_info()->set_car_wheel_base(tp_result->getDouble("parkSpaceCarWheelBase"));
  739. parkspace_info.mutable_car_info()->set_car_wheel_width(tp_result->getDouble("parkSpaceCarWheelWidth"));
  740. parkspace_info.set_entry_time(tp_result->getString("entryTime"));
  741. parkspace_info.set_leave_time(tp_result->getString("leaveTime"));
  742. }
  743. catch (sql::SQLException &e)
  744. {
  745. /* Use what() (derived from std::runtime_error) to fetch the error message */
  746. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  747. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  748. }
  749. catch (std::runtime_error &e)
  750. {
  751. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  752. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  753. }
  754. if (tp_result->next())
  755. {
  756. return Error_manager(Error_code::DB_QUERY_DATA_REPEAT, Error_level::MAJOR_ERROR,
  757. "数据库查询数据重复 Parkspace_operating_function::query_one_parkspace_with_license error ");
  758. }
  759. }
  760. else
  761. {
  762. return Error_manager(Error_code::DB_QUERY_NOT_DATA, Error_level::MINOR_ERROR,
  763. "数据库未查询到数据 Parkspace_operating_function::query_one_parkspace_with_license error ");
  764. }
  765. return SUCCESS;
  766. }
  767. else
  768. {
  769. return ec;
  770. }
  771. }
  772. // 找到一个空车位
  773. Error_manager Parkspace_operating_function::query_one_empty_parkspace(message::Parkspace_info &parkspace_info)
  774. {
  775. //执行sql操作
  776. std::string query_parkspace_sql = std::string("select * from parkingspace where parkingspace_status=").append(std::to_string(message::Parkspace_status::eParkspace_empty));
  777. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  778. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  779. if(ec == SUCCESS)
  780. {
  781. if(tp_result == nullptr)
  782. {
  783. return DB_RESULT_SET_EMPTY;
  784. }
  785. if (tp_result->next())
  786. {
  787. char buf[1024];
  788. memset(buf, 0, 1024);
  789. try
  790. {
  791. if ( tp_result->getInt("parkingspace_status") != message::Parkspace_status::eParkspace_empty )
  792. {
  793. return Error_manager(Error_code::DB_QUERY_DATA_FAILED, Error_level::MAJOR_ERROR,
  794. "数据库查询数据错误 Parkspace_operating_function::query_one_empty_parkspace error ");
  795. }
  796. parkspace_info.set_parkingspace_status(message::Parkspace_status::eParkspace_empty);
  797. parkspace_info.set_parkingspace_index_id(tp_result->getInt("parkingSpaceID"));
  798. parkspace_info.set_parkingspace_unit_id(tp_result->getInt("parkingspace_unit_id"));
  799. parkspace_info.set_parkingspace_label_id(tp_result->getInt("parkingspace_label_id"));
  800. parkspace_info.set_parkingspace_room_id(tp_result->getInt("parkingspace_room_id"));
  801. parkspace_info.set_parkingspace_direction(tp_result->getInt("parkingspace_direction") > 0 ? message::Direction::eForward : message::Direction::eBackward);
  802. parkspace_info.set_parkingspace_floor_id(tp_result->getInt("parkingspace_floor_id"));
  803. parkspace_info.set_parkingspace_width(tp_result->getDouble("parkingspace_width"));
  804. parkspace_info.set_parkingspace_height(tp_result->getDouble("parkingspace_height"));
  805. parkspace_info.mutable_car_info()->set_car_numberplate(tp_result->getString("parkSpaceCarNumberPlate"));
  806. parkspace_info.mutable_car_info()->set_license(tp_result->getString("parkSpaceCarLicense"));
  807. parkspace_info.mutable_car_info()->set_car_length(tp_result->getDouble("parkSpaceCarLength"));
  808. parkspace_info.mutable_car_info()->set_car_width(tp_result->getDouble("parkSpaceCarWidth"));
  809. parkspace_info.mutable_car_info()->set_car_height(tp_result->getDouble("parkSpaceCarHeight"));
  810. parkspace_info.set_car_type((message::Car_type)tp_result->getInt("parkSpaceCarType"));
  811. parkspace_info.mutable_car_info()->set_car_wheel_base(tp_result->getDouble("parkSpaceCarWheelBase"));
  812. parkspace_info.mutable_car_info()->set_car_wheel_width(tp_result->getDouble("parkSpaceCarWheelWidth"));
  813. parkspace_info.set_entry_time(tp_result->getString("entryTime"));
  814. parkspace_info.set_leave_time(tp_result->getString("leaveTime"));
  815. }
  816. catch (sql::SQLException &e)
  817. {
  818. /* Use what() (derived from std::runtime_error) to fetch the error message */
  819. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  820. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  821. }
  822. catch (std::runtime_error &e)
  823. {
  824. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  825. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  826. }
  827. }
  828. else
  829. {
  830. return Error_manager(Error_code::DB_NOT_QUERY_EMPTY_PARKSPACE, Error_level::MINOR_ERROR,
  831. "数据库未查询到空车位 Parkspace_operating_function::query_one_empty_parkspace error ");
  832. }
  833. return SUCCESS;
  834. }
  835. else
  836. {
  837. return ec;
  838. }
  839. return SUCCESS;
  840. }
  841. // 找到一个指定单元空车位
  842. Error_manager Parkspace_operating_function::query_one_specified_unit_empty_parkspace(int unit_id,message::Parkspace_info &parkspace_info)
  843. {
  844. //执行sql操作
  845. char query_parkspace_sql[1024];
  846. memset(query_parkspace_sql, 0, 1024);
  847. sprintf(query_parkspace_sql,"select * from parkingspace where parkingspace_status=%d and parkingspace_unit_id=%d",message::Parkspace_status::eParkspace_empty,unit_id);
  848. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  849. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  850. if(ec == SUCCESS)
  851. {
  852. if(tp_result == nullptr)
  853. {
  854. return DB_RESULT_SET_EMPTY;
  855. }
  856. if (tp_result->next())
  857. {
  858. char buf[1024];
  859. memset(buf, 0, 1024);
  860. try
  861. {
  862. if ( tp_result->getInt("parkingspace_status") != message::Parkspace_status::eParkspace_empty )
  863. {
  864. return Error_manager(Error_code::DB_QUERY_DATA_FAILED, Error_level::MAJOR_ERROR,
  865. "数据库查询数据错误 Parkspace_operating_function::query_one_empty_parkspace error ");
  866. }
  867. parkspace_info.set_parkingspace_status(message::Parkspace_status::eParkspace_empty);
  868. parkspace_info.set_parkingspace_index_id(tp_result->getInt("parkingSpaceID"));
  869. parkspace_info.set_parkingspace_unit_id(tp_result->getInt("parkingspace_unit_id"));
  870. parkspace_info.set_parkingspace_label_id(tp_result->getInt("parkingspace_label_id"));
  871. parkspace_info.set_parkingspace_room_id(tp_result->getInt("parkingspace_room_id"));
  872. parkspace_info.set_parkingspace_direction(tp_result->getInt("parkingspace_direction") > 0 ? message::Direction::eForward : message::Direction::eBackward);
  873. parkspace_info.set_parkingspace_floor_id(tp_result->getInt("parkingspace_floor_id"));
  874. parkspace_info.set_parkingspace_width(tp_result->getDouble("parkingspace_width"));
  875. parkspace_info.set_parkingspace_height(tp_result->getDouble("parkingspace_height"));
  876. parkspace_info.mutable_car_info()->set_car_numberplate(tp_result->getString("parkSpaceCarNumberPlate"));
  877. parkspace_info.mutable_car_info()->set_license(tp_result->getString("parkSpaceCarLicense"));
  878. parkspace_info.mutable_car_info()->set_car_length(tp_result->getDouble("parkSpaceCarLength"));
  879. parkspace_info.mutable_car_info()->set_car_width(tp_result->getDouble("parkSpaceCarWidth"));
  880. parkspace_info.mutable_car_info()->set_car_height(tp_result->getDouble("parkSpaceCarHeight"));
  881. parkspace_info.set_car_type((message::Car_type)tp_result->getInt("parkSpaceCarType"));
  882. parkspace_info.mutable_car_info()->set_car_wheel_base(tp_result->getDouble("parkSpaceCarWheelBase"));
  883. parkspace_info.mutable_car_info()->set_car_wheel_width(tp_result->getDouble("parkSpaceCarWheelWidth"));
  884. parkspace_info.set_entry_time(tp_result->getString("entryTime"));
  885. parkspace_info.set_leave_time(tp_result->getString("leaveTime"));
  886. }
  887. catch (sql::SQLException &e)
  888. {
  889. /* Use what() (derived from std::runtime_error) to fetch the error message */
  890. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  891. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  892. }
  893. catch (std::runtime_error &e)
  894. {
  895. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  896. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  897. }
  898. }
  899. else
  900. {
  901. return Error_manager(Error_code::DB_NOT_QUERY_EMPTY_PARKSPACE, Error_level::MINOR_ERROR,
  902. "数据库未查询到空车位 Parkspace_operating_function::query_one_empty_parkspace error ");
  903. }
  904. return SUCCESS;
  905. }
  906. else
  907. {
  908. return ec;
  909. }
  910. return SUCCESS;
  911. }
  912. // 找到一个指定单元指定类型的空车位
  913. Error_manager Parkspace_operating_function::query_one_specified_unit_and_type_empty_parkspace(int unit_id,int parkspace_type,message::Parkspace_info &parkspace_info)
  914. {
  915. //执行sql操作
  916. char query_parkspace_sql[1024];
  917. memset(query_parkspace_sql, 0, 1024);
  918. 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);
  919. std::cout<<"query_parkspace_sql = "<<query_parkspace_sql<<std::endl;
  920. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  921. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  922. if(ec == SUCCESS)
  923. {
  924. if(tp_result == nullptr)
  925. {
  926. return DB_RESULT_SET_EMPTY;
  927. }
  928. if (tp_result->next())
  929. {
  930. char buf[1024];
  931. memset(buf, 0, 1024);
  932. try
  933. {
  934. if ( tp_result->getInt("parkingspace_status") != message::Parkspace_status::eParkspace_empty )
  935. {
  936. return Error_manager(Error_code::DB_QUERY_DATA_FAILED, Error_level::MAJOR_ERROR,
  937. "数据库查询数据错误 Parkspace_operating_function::query_one_empty_parkspace error ");
  938. }
  939. parkspace_info.set_parkingspace_status(message::Parkspace_status::eParkspace_empty);
  940. parkspace_info.set_parkingspace_index_id(tp_result->getInt("parkingSpaceID"));
  941. parkspace_info.set_parkingspace_unit_id(tp_result->getInt("parkingspace_unit_id"));
  942. parkspace_info.set_parkingspace_label_id(tp_result->getInt("parkingspace_label_id"));
  943. parkspace_info.set_parkingspace_room_id(tp_result->getInt("parkingspace_room_id"));
  944. parkspace_info.set_parkingspace_direction(tp_result->getInt("parkingspace_direction") > 0 ? message::Direction::eForward : message::Direction::eBackward);
  945. parkspace_info.set_parkingspace_floor_id(tp_result->getInt("parkingspace_floor_id"));
  946. parkspace_info.set_parkingspace_width(tp_result->getDouble("parkingspace_width"));
  947. parkspace_info.set_parkingspace_height(tp_result->getDouble("parkingspace_height"));
  948. parkspace_info.mutable_car_info()->set_car_numberplate(tp_result->getString("parkSpaceCarNumberPlate"));
  949. parkspace_info.mutable_car_info()->set_license(tp_result->getString("parkSpaceCarLicense"));
  950. parkspace_info.mutable_car_info()->set_car_length(tp_result->getDouble("parkSpaceCarLength"));
  951. parkspace_info.mutable_car_info()->set_car_width(tp_result->getDouble("parkSpaceCarWidth"));
  952. parkspace_info.mutable_car_info()->set_car_height(tp_result->getDouble("parkSpaceCarHeight"));
  953. parkspace_info.set_car_type((message::Car_type)tp_result->getInt("parkSpaceCarType"));
  954. parkspace_info.mutable_car_info()->set_car_wheel_base(tp_result->getDouble("parkSpaceCarWheelBase"));
  955. parkspace_info.mutable_car_info()->set_car_wheel_width(tp_result->getDouble("parkSpaceCarWheelWidth"));
  956. parkspace_info.set_entry_time(tp_result->getString("entryTime"));
  957. parkspace_info.set_leave_time(tp_result->getString("leaveTime"));
  958. }
  959. catch (sql::SQLException &e)
  960. {
  961. /* Use what() (derived from std::runtime_error) to fetch the error message */
  962. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  963. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  964. }
  965. catch (std::runtime_error &e)
  966. {
  967. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  968. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  969. }
  970. }
  971. else
  972. {
  973. return Error_manager(Error_code::DB_NOT_QUERY_EMPTY_PARKSPACE, Error_level::MINOR_ERROR,
  974. "数据库未查询到空车位 Parkspace_operating_function::query_one_empty_parkspace error ");
  975. }
  976. return SUCCESS;
  977. }
  978. else
  979. {
  980. return ec;
  981. }
  982. return SUCCESS;
  983. }
  984. // 查询车辆状态,暂时不使用
  985. Error_manager Parkspace_operating_function::query_vehicle(std::string license, message::Vehicle_status &vehicle_status, int &park_record_id)
  986. {
  987. //执行sql操作
  988. std::string query_parkspace_sql = std::string("select * from vehicle where numberPlate='").append(license).append("'");
  989. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  990. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  991. if(ec == SUCCESS)
  992. {
  993. if(tp_result == nullptr)
  994. {
  995. return Error_manager(Error_code::DB_RESULT_SET_EMPTY, Error_level::MINOR_ERROR,
  996. "Parkspace_operating_function::query_vehicle error ");
  997. }
  998. if (tp_result->next())
  999. {
  1000. char buf[1024];
  1001. memset(buf, 0, 1024);
  1002. try
  1003. {
  1004. switch (tp_result->getInt("vehicleParkState"))
  1005. {
  1006. case 1:
  1007. vehicle_status = message::Vehicle_status::eVehicle_idle;
  1008. break;
  1009. case 2:
  1010. vehicle_status = message::Vehicle_status::eVehicle_in_garage;
  1011. break;
  1012. case 3:
  1013. vehicle_status = message::Vehicle_status::eVehicle_parking;
  1014. break;
  1015. case 4:
  1016. vehicle_status = message::Vehicle_status::eVehicle_fetching;
  1017. break;
  1018. case 5:
  1019. vehicle_status = message::Vehicle_status::eVehicle_reserved;
  1020. break;
  1021. default:
  1022. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  1023. "参数错误 Parkspace_operating_function::query_vehicle error ");
  1024. }
  1025. park_record_id = tp_result->getInt("parkingRecordsID");
  1026. }
  1027. catch (sql::SQLException &e)
  1028. {
  1029. /* Use what() (derived from std::runtime_error) to fetch the error message */
  1030. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  1031. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  1032. }
  1033. catch (std::runtime_error &e)
  1034. {
  1035. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  1036. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  1037. }
  1038. }
  1039. return SUCCESS;
  1040. }
  1041. else
  1042. {
  1043. return ec;
  1044. }
  1045. }
  1046. // 查询停车记录,根据车牌号、车位id查询,反馈停车记录id
  1047. Error_manager Parkspace_operating_function::query_parking_record(message::Parkspace_info &parkspace_info, int &record_id)
  1048. {
  1049. //执行sql操作
  1050. if(!parkspace_info.has_car_info() || parkspace_info.parkingspace_index_id() <= 0)
  1051. {
  1052. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  1053. " Parkspace_operating_function::query_parking_record error ");
  1054. }
  1055. 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");
  1056. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  1057. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_park_record_sql, tp_result);
  1058. if(ec == SUCCESS)
  1059. {
  1060. if(tp_result == nullptr)
  1061. {
  1062. return Error_manager(Error_code::DB_RESULT_SET_EMPTY, Error_level::MINOR_ERROR,
  1063. " Parkspace_operating_function::query_parking_record error ");
  1064. }
  1065. if (tp_result->next())
  1066. {
  1067. char buf[1024];
  1068. memset(buf, 0, 1024);
  1069. try
  1070. {
  1071. record_id = tp_result->getInt("parkingRecordsID");
  1072. }
  1073. catch (sql::SQLException &e)
  1074. {
  1075. /* Use what() (derived from std::runtime_error) to fetch the error message */
  1076. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  1077. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  1078. }
  1079. catch (std::runtime_error &e)
  1080. {
  1081. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  1082. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  1083. }
  1084. }
  1085. return SUCCESS;
  1086. }
  1087. else
  1088. {
  1089. return ec;
  1090. }
  1091. }