DBOperation.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using MySql.Data.MySqlClient;
  7. namespace parkMonitor.DataBase
  8. {
  9. public class DBOperation
  10. {
  11. //查询车库表获得剩余车位数
  12. public int getGarageFreeSpace(string connectionStr, int garageID)
  13. {
  14. MySqlDataReader reader = null;
  15. int garageFreeSpace = 0;
  16. string sql = "select * from garage where garageID = '" + garageID + "'";
  17. Operation oper = new Operation(connectionStr, sql);
  18. int count = 0;
  19. reader = oper.getResultSet(ref count);
  20. if (count > 0 && reader.Read())
  21. {
  22. garageFreeSpace = reader.GetInt32("garageFreeSpace");
  23. return garageFreeSpace;
  24. }
  25. else
  26. {
  27. Console.WriteLine("车位剩余数查无结果");
  28. }
  29. oper.DBClose();
  30. return 0;
  31. }
  32. //查询空闲车位位置
  33. public Dictionary<int, Parking_Space> GetParkingSpace(string connectionStr, int garageID)
  34. {
  35. Dictionary<int, Parking_Space> lps = new Dictionary<int, Parking_Space>();
  36. MySqlDataReader reader = null;
  37. string sql = "select * from parkingspace where parkingSpaceState = 0 and garageID = '" + garageID + "' ";
  38. Operation oper = new Operation(connectionStr, sql);
  39. int count = 0;
  40. reader = oper.getResultSet(ref count);
  41. if (count > 0 && reader.Read())
  42. {
  43. for (int i = 1; i <= count; i++)
  44. {
  45. Parking_Space ps = new Parking_Space();
  46. ps.parkingSpaceID = reader.GetInt32("parkingSpaceID");
  47. ps.parkingSpaceX = reader.GetInt32("parkingSpaceX");
  48. ps.parkingSpaceY = reader.GetInt32("parkingSpaceY");
  49. ps.parkingSpaceZ = reader.GetInt32("parkingSpaceZ");
  50. ps.garageID = garageID;
  51. lps.Add(i, ps);
  52. }
  53. return lps;
  54. }
  55. else
  56. {
  57. Console.WriteLine("空闲车位查无结果");
  58. }
  59. oper.DBClose();
  60. return null;
  61. }
  62. //查询所有车位位置及状态
  63. public Dictionary<int, Parking_Space> GetAllParkingSpace(string connectionStr, int garageID)
  64. {
  65. Dictionary<int, Parking_Space> lps = new Dictionary<int, Parking_Space>();
  66. MySqlDataReader reader = null;
  67. string sql = "select * from parkingspace where garageID = '" + garageID + "' ";
  68. Operation oper = new Operation(connectionStr, sql);
  69. int count = 0;
  70. reader = oper.getResultSet(ref count);
  71. if (count > 0 && reader.Read())
  72. {
  73. for (int i = 1; i <= count; i++)
  74. {
  75. Parking_Space ps = new Parking_Space();
  76. ps.parkingSpaceID = reader.GetInt32("parkingSpaceID");
  77. ps.parkingSpaceX = reader.GetInt32("parkingSpaceX");
  78. ps.parkingSpaceY = reader.GetInt32("parkingSpaceY");
  79. ps.parkingSpaceZ = reader.GetInt32("parkingSpaceZ");
  80. ps.parkingSpaceState = reader.GetInt32("parkingSpaceState");
  81. ps.garageID = garageID;
  82. lps.Add(i, ps);
  83. }
  84. return lps;
  85. }
  86. else
  87. {
  88. Console.WriteLine("所有车位查无结果");
  89. }
  90. oper.DBClose();
  91. return null;
  92. }
  93. //数据插入云记录表,并返回停车记录id
  94. public int InsertToParkingRecords(string connectionStr, int userID, string numberPlate, int parkingSpaceID, int garageID, int parkingRecordsState, string realParkTime)
  95. {
  96. string sql = "insert into parkingrecords(userID,numberPlate,parkingSpaceID,garageID,parkingRecordsState,realParkTime) values('" + userID + "','" + numberPlate + "','" + parkingSpaceID + "','" + garageID + "','" + parkingRecordsState + "','" + realParkTime + "')";
  97. int parkingRecordsID = 0;
  98. Operation oper = new Operation(connectionStr, sql);
  99. oper.getInsert();
  100. parkingRecordsID = oper.getInsertId();
  101. oper.DBClose();
  102. return parkingRecordsID;
  103. }
  104. //根据车牌号更新车辆表
  105. public void UpdateVehicle(string connectionStr, string numberPlate, int vehiclepParkState, string scanEntryTime, int parkingRecordsID, int parkingSpaceID, int vehicleTypeConfirm, int frontWheelbase, int rearWheelbase)
  106. {
  107. string sql = "update vehicle set vehiclepParkState = '" + vehiclepParkState + "',scanEntryTime = '" + scanEntryTime + "',parkingRecordsID = '" + parkingRecordsID + "',parkingSpaceID = '" + parkingSpaceID + "',vehicleTypeConfirm = '" + vehicleTypeConfirm + "',frontwheelbase = '" + frontWheelbase + "',rearwheelbase = '" + rearWheelbase + "' where numberPlate = '" + numberPlate + "'";
  108. Operation oper = new Operation(connectionStr, sql);
  109. oper.getUpdate();
  110. oper.DBClose();
  111. }
  112. //更新车库表剩余车位数
  113. public void UpdateGarageFreeSpace(string connectionStr, int garageFreeSpace, int garageID)
  114. {
  115. string sql = "update garage set garageFreeSpace = '" + garageFreeSpace + "' where garageID = '" + garageID + "'";
  116. Operation oper = new Operation(connectionStr, sql);
  117. oper.getUpdate();
  118. oper.DBClose();
  119. }
  120. //根据车牌号更新车辆状态
  121. public void UpdateVehicleParkState(string connectionStr, string numberPlate, int vehiclepParkState)
  122. {
  123. string sql = "update vehicle set vehiclepParkState = '" + vehiclepParkState + "'where numberPlate = '" + numberPlate + "'";
  124. Operation oper = new Operation(connectionStr, sql);
  125. oper.getUpdate();
  126. oper.DBClose();
  127. }
  128. //更新车位状态
  129. public void UpdateParkingSpaceState(string connectionStr, int parkingSpaceID, int parkingSpaceState)
  130. {
  131. string sql = "update parkingspace set parkingSpaceState = '" + parkingSpaceState + "'where parkingSpaceID = '" + parkingSpaceID + "'";
  132. Operation oper = new Operation(connectionStr, sql);
  133. oper.getUpdate();
  134. oper.DBClose();
  135. }
  136. //更新停车记录表
  137. public void UpdateParkingRecords(string connectionStr, int parkingRecordsState, string realGetTime, int parkingRecordsID)
  138. {
  139. string sql = "update parkingrecords set parkingRecordsState = '" + parkingRecordsState + "',realGetTime = '" + realGetTime + "'where parkingRecordsID = '" + parkingRecordsID + "'";
  140. Operation oper = new Operation(connectionStr, sql);
  141. oper.getUpdate();
  142. oper.DBClose();
  143. }
  144. //插入消息推送表
  145. public void InsertToMessageQueue(string connectionStr, int userID, string context, int messageType)
  146. {
  147. string sql = "insert into messagequeue(userID,context,messageType) values('" + userID + "','" + context + "','" + messageType + "')";
  148. Operation oper = new Operation(connectionStr, sql);
  149. oper.getInsert();
  150. oper.DBClose();
  151. }
  152. //根据车牌查询得到车库id和车位id以及轮距
  153. public Vehicle GetVehicle(string connectionStr, string numberPlate)
  154. {
  155. Vehicle v = new Vehicle();
  156. MySqlDataReader reader = null;
  157. string sql = "select * from vehicle where numberPlate = '" + numberPlate + "'";
  158. Operation oper = new Operation(connectionStr, sql);
  159. int count = 0;
  160. reader = oper.getResultSet(ref count);
  161. if (count > 0 && reader.Read())
  162. {
  163. int parkingSpaceID = reader.GetInt32("parkingSpaceID");
  164. int garageID = reader.GetInt32("garageID");
  165. int frontwheelbase = reader.GetInt32("frontwheelbase");
  166. int rearwheelbase = reader.GetInt32("rearwheelbase");
  167. v.parkingSpaceID = parkingSpaceID;
  168. v.garageID = garageID;
  169. v.frontwheelbase = frontwheelbase;
  170. v.rearwheelbase = rearwheelbase;
  171. return v;
  172. }
  173. else
  174. {
  175. Console.WriteLine("云端轮距查无结果");
  176. }
  177. oper.DBClose();
  178. return null;
  179. }
  180. //根据车位id获得x,y,z
  181. public Parking_Space GetFetchingSpace(string connectionStr, int parkingSpaceID)
  182. {
  183. Parking_Space ps = new Parking_Space();
  184. MySqlDataReader reader = null;
  185. string sql = "select * from parkingspace where parkingSpaceID = '" + parkingSpaceID + " '";
  186. Operation oper = new Operation(connectionStr, sql);
  187. int count = 0;
  188. reader = oper.getResultSet(ref count);
  189. if (count > 0 && reader.Read())
  190. {
  191. ps.parkingSpaceID = parkingSpaceID;
  192. ps.parkingSpaceX = reader.GetInt32("parkingSpaceX");
  193. ps.parkingSpaceY = reader.GetInt32("parkingSpaceY");
  194. ps.parkingSpaceZ = reader.GetInt32("parkingSpaceZ");
  195. return ps;
  196. }
  197. else
  198. {
  199. Console.WriteLine("车位xyz查无结果");
  200. }
  201. oper.DBClose();
  202. return null;
  203. }
  204. //查询手机号是否被注册
  205. public bool IsTelRegister(string connectionStr, string tel)
  206. {
  207. bool isTelRegister = false;
  208. MySqlDataReader reader = null;
  209. string sql = "select * from user where userTelephone = '" + tel + "'";
  210. Operation oper = new Operation(connectionStr, sql);
  211. int count = 0;
  212. reader = oper.getResultSet(ref count);
  213. if (count > 0)
  214. {
  215. isTelRegister = true;
  216. }
  217. else
  218. {
  219. isTelRegister = false;
  220. }
  221. oper.DBClose();
  222. return isTelRegister;
  223. }
  224. //注册信息写入数据库,返回注册成功信息
  225. public int InsertUser(string connectionStr, string tel, string password)
  226. {
  227. string sql = "insert into user(userTelephone,userPassword,userLevel) values('" + tel + "','" + password + "',1)";
  228. int userID = 0;
  229. Operation oper = new Operation(connectionStr, sql);
  230. oper.getInsert();
  231. userID = oper.getInsertId();
  232. oper.DBClose();
  233. return userID;
  234. }
  235. //根据电话号码查询userID
  236. public int GetUserID(string connectionStr, string tel)
  237. {
  238. int userID = 0;
  239. MySqlDataReader reader = null;
  240. string sql = "select userID from user where userTelephone = '" + tel + "'";
  241. int count = 0;
  242. Operation oper = new Operation(connectionStr, sql);
  243. reader = oper.getResultSet(ref count);
  244. if (count > 0 && reader.Read())
  245. {
  246. userID = reader.GetInt32("userID");
  247. return userID;
  248. }
  249. else
  250. {
  251. Console.WriteLine("userID查无结果");
  252. }
  253. oper.DBClose();
  254. return 0;
  255. }
  256. //查询停车记录id
  257. public int GetParkingRecordsID(string connectionStr, string numberPlate)
  258. {
  259. int parkingRecordsID = 0;
  260. MySqlDataReader reader = null;
  261. string sql = "select parkingRecordsID from parkingrecords where numberPlate = '" + numberPlate + "' and parkingRecordsState = 3";
  262. int count = 0;
  263. Operation oper = new Operation(connectionStr, sql);
  264. reader = oper.getResultSet(ref count);
  265. if (count > 0 && reader.Read())
  266. {
  267. parkingRecordsID = reader.GetInt32("parkingRecordsID");
  268. return parkingRecordsID;
  269. }
  270. else
  271. {
  272. Console.WriteLine("停车记录id查无结果");
  273. }
  274. oper.DBClose();
  275. return 0;
  276. }
  277. //车库有无此车
  278. public bool IsNumberPlate(string connectionStr, string numberPlate, int garageID)
  279. {
  280. bool isNumberPlate = true;
  281. MySqlDataReader reader = null;
  282. string sql = "select * from parkingrecords where numberPlate = '" + numberPlate + "' and parkingRecordsState = 3 and garageID = '" + garageID + "'";
  283. Operation oper = new Operation(connectionStr, sql);
  284. int count = 0;
  285. reader = oper.getResultSet(ref count);
  286. if (count > 0 && reader.Read())
  287. {
  288. isNumberPlate = true;
  289. }
  290. else
  291. {
  292. isNumberPlate = false;
  293. }
  294. oper.DBClose();
  295. return isNumberPlate;
  296. }
  297. //数据插入本地记录表,并返回停车记录id
  298. public int InsertToLocalParkingRecords(string connectionStr, int parkingStatus, int userID, string numberPlate, int parkingSpaceID, int garageID, int parkingRecordsState, string realParkTime, int frontWheelbase, int rearWheelbase)
  299. {
  300. string sql = "insert into parkingrecords(parkingStatus,userID,numberPlate,parkingSpaceID,garageID,parkingRecordsState,realParkTime,frontWheelbase,rearWheelbase) values('" + parkingStatus + "','" + userID + "','" + numberPlate + "','" + parkingSpaceID + "','" + garageID + "','" + parkingRecordsState + "','" + realParkTime + "','" + frontWheelbase + "','" + rearWheelbase + "')";
  301. int parkingRecordsID = 0;
  302. Operation oper = new Operation(connectionStr, sql);
  303. oper.getInsert();
  304. parkingRecordsID = oper.getInsertId();
  305. oper.DBClose();
  306. return parkingRecordsID;
  307. }
  308. //更新本地停车记录表
  309. public void UpdateParkingRecords(string connectionStr, int parkingStatus, int parkingRecordsState, string realGetTime, int parkingRecordsID)
  310. {
  311. string sql = "update parkingrecords set parkingStatus = '" + parkingStatus + "', parkingRecordsState = '" + parkingRecordsState + "',realGetTime = '" + realGetTime + "'where parkingRecordsID = '" + parkingRecordsID + "'";
  312. Operation oper = new Operation(connectionStr, sql);
  313. oper.getUpdate();
  314. oper.DBClose();
  315. }
  316. //插入车辆表
  317. public void InsertVehicle(string connectionStr, string numberPlate, int vehiclepParkState, int parkingRecordsID, int parkingSpaceID, int vehicleTypeConfirm)
  318. {
  319. string sql = "insert into vehicle(numberPlate,vehiclepParkState,parkingRecordsID,parkingSpaceID,vehicleTypeConfirm) values('" + numberPlate + "','" + vehiclepParkState + "','" + parkingRecordsID + "','" + parkingSpaceID + "','" + vehicleTypeConfirm + "')";
  320. Operation oper = new Operation(connectionStr, sql);
  321. oper.getInsert();
  322. oper.DBClose();
  323. }
  324. //判断车辆表中是否存在该车辆号牌
  325. public bool IsNumberPlateFromVehicle(string connectionStr, string numberPlate)
  326. {
  327. bool isNumberPlateFromVehicle = true;
  328. string sql = "select * from vehicle where numberPlate = '" + numberPlate + "' and vehiclepParkState = 1";
  329. MySqlDataReader reader = null;
  330. Operation oper = new Operation(connectionStr, sql);
  331. int count = 0;
  332. reader = oper.getResultSet(ref count);
  333. if (count > 0 && reader.Read())
  334. {
  335. isNumberPlateFromVehicle = true;
  336. }
  337. else
  338. {
  339. isNumberPlateFromVehicle = false;
  340. }
  341. oper.DBClose();
  342. return isNumberPlateFromVehicle;
  343. }
  344. //根据车牌号更新车辆表
  345. public void UpdateVehicle(string connectionStr, string numberPlate, int vehiclepParkState, int parkingRecordsID, int parkingSpaceID, int vehicleTypeConfirm)
  346. {
  347. string sql = "update vehicle set vehiclepParkState = '" + vehiclepParkState + "',parkingRecordsID = '" + parkingRecordsID + "',parkingSpaceID = '" + parkingSpaceID + "',vehicleTypeConfirm = '" + vehicleTypeConfirm + "' where numberPlate = '" + numberPlate + "'";
  348. Operation oper = new Operation(connectionStr, sql);
  349. oper.getUpdate();
  350. oper.DBClose();
  351. }
  352. //根据车牌查询得到车库id和车位id以及轮距
  353. public Vehicle GetLocalVehicle(string connectionStr, string numberPlate, int garageID)
  354. {
  355. Vehicle v = new Vehicle();
  356. MySqlDataReader reader = null;
  357. string sql = "select * from parkingrecords where numberPlate = '" + numberPlate + "' and parkingRecordsState = 3 and garageID = '" + garageID + "'";
  358. Operation oper = new Operation(connectionStr, sql);
  359. int count = 0;
  360. reader = oper.getResultSet(ref count);
  361. if (count > 0 && reader.Read())
  362. {
  363. int parkingSpaceID = reader.GetInt32("parkingSpaceID");
  364. int frontwheelbase = reader.GetInt32("frontwheelbase");
  365. int rearwheelbase = reader.GetInt32("rearwheelbase");
  366. v.parkingSpaceID = parkingSpaceID;
  367. v.garageID = garageID;
  368. v.frontwheelbase = frontwheelbase;
  369. v.rearwheelbase = rearwheelbase;
  370. return v;
  371. }
  372. else
  373. {
  374. Console.WriteLine("本地轮距查无结果");
  375. }
  376. oper.DBClose();
  377. return null;
  378. }
  379. }
  380. }