DBStation.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. using MeterVision.model;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data.SQLite;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace MeterVision.db
  9. {
  10. public class DBStation
  11. {
  12. public static bool InsertTStation(TStation tStation)
  13. {
  14. // 构建插入的 SQL 语句
  15. string sql = "INSERT INTO t_station (id,station_id, station_name, device_sn,meter_type, bright_val, flow_rate, " +
  16. "dial_region, num_count, ind_count, feature_region, last_unit, " +
  17. "last_value, last_time, create_time, stand_id) " +
  18. "VALUES (@StationId, @StationName, @DeviceSn, @MeterType, @BrightVal, @FlowRate, " +
  19. "@DialRegion, @NumCount, @IndCount, @FeatureRegion, @LastUnit, " +
  20. "@LastValue, @LastTime, @CreateTime, @StandId)";
  21. // 创建参数数组
  22. SQLiteParameter[] parameters = new SQLiteParameter[]
  23. {
  24. new SQLiteParameter("@Id", tStation.Id),
  25. new SQLiteParameter("@StationId", tStation.StationId),
  26. new SQLiteParameter("@StationName", tStation.StationName),
  27. new SQLiteParameter("@DeviceSn", tStation.DeviceSn),
  28. new SQLiteParameter("@MeterType", tStation.MeterType),
  29. new SQLiteParameter("@BrightVal", tStation.BrightVal),
  30. new SQLiteParameter("@FlowRate", tStation.FlowRate),
  31. new SQLiteParameter("@DialRegion", tStation.DialRegion),
  32. new SQLiteParameter("@NumCount", tStation.NumCount),
  33. new SQLiteParameter("@IndCount", tStation.IndCount),
  34. new SQLiteParameter("@FeatureRegion", tStation.FeatureRegion),
  35. new SQLiteParameter("@LastUnit", tStation.LastUnit),
  36. new SQLiteParameter("@LastValue", tStation.LastValue),
  37. new SQLiteParameter("@LastTime", tStation.LastTime),
  38. new SQLiteParameter("@CreateTime", tStation.CreateTime),
  39. new SQLiteParameter("@StandId", tStation.StandId),
  40. };
  41. try
  42. {
  43. // 调用 SQLiteHelper 执行插入操作
  44. int rowsAffected = SQLiteHelper.ExecuteSql(sql, parameters);
  45. // 如果插入成功,返回 true,否则返回 false
  46. return rowsAffected > 0;
  47. }
  48. catch (Exception ex)
  49. {
  50. // 处理异常(如果有的话)
  51. Console.WriteLine("Error inserting data: " + ex.Message);
  52. return false;
  53. }
  54. }
  55. public static bool InsertTStations(List<StationInfo> stationInfos)
  56. {
  57. string sql = @"
  58. INSERT INTO t_station (id, stand_id,create_time, station_id, device_sn, num_count, last_unit)
  59. VALUES (@Id, @StandId, @CreateTime, @StationId, @DeviceSn, @NumCount, @LastUnit);";
  60. try
  61. {
  62. // 开始事务
  63. using (SQLiteConnection connection = SQLiteHelper.GetConnection())
  64. {
  65. connection.Open();
  66. using (SQLiteTransaction transaction = connection.BeginTransaction())
  67. {
  68. // 遍历列表插入每一条数据
  69. foreach (var stationInfo in stationInfos)
  70. {
  71. SQLiteParameter[] parameters = new SQLiteParameter[]
  72. {
  73. new SQLiteParameter("@Id", Guid.NewGuid().ToString()),
  74. new SQLiteParameter("@StandId", stationInfo.StandId),
  75. new SQLiteParameter("@CreateTime", ThisApp.GetNowTime_yyyyMMddHHmmss()),
  76. new SQLiteParameter("@StationId", stationInfo.StationId),
  77. new SQLiteParameter("@DeviceSn", stationInfo.DeviceSn),
  78. new SQLiteParameter("@NumCount", stationInfo.NumCount),
  79. new SQLiteParameter("@LastUnit", stationInfo.LastUnit2)
  80. };
  81. // 执行插入操作
  82. SQLiteHelper.ExecuteNonQuery(sql, parameters, transaction);
  83. }
  84. // 提交事务
  85. transaction.Commit();
  86. }
  87. }
  88. return true;
  89. }
  90. catch(Exception ex)
  91. {
  92. Console.WriteLine(ex.Message);
  93. return false;
  94. }
  95. }
  96. public static Tuple<int, int, List<TStation>> GetPagedTStations(int pageNumber, int pageSize, string stationId)
  97. {
  98. // 计算 OFFSET 值
  99. int offset = (pageNumber - 1) * pageSize;
  100. // 定义 SQL 查询语句,带有分页
  101. // 定义 SQL 查询语句的基础部分
  102. string sql = @"
  103. SELECT id,station_id, station_name, device_sn,meter_type, bright_val, flow_rate, dial_region,
  104. num_count, ind_count, feature_region, last_unit,
  105. last_value, last_time, create_time,stand_id
  106. FROM t_station ";
  107. // 如果传入的 stationId 不为空,增加过滤条件
  108. if (!string.IsNullOrEmpty(stationId))
  109. {
  110. sql += "WHERE station_id LIKE @StationId ";
  111. }
  112. // 添加排序和分页
  113. sql += "ORDER BY station_id ASC LIMIT @PageSize OFFSET @Offset;";
  114. // 创建 SQL 参数
  115. List<SQLiteParameter> parameters = new List<SQLiteParameter>
  116. {
  117. new SQLiteParameter("@PageSize", pageSize),
  118. new SQLiteParameter("@Offset", offset),
  119. };
  120. // 如果传入了 stationId,添加到参数中
  121. if (!string.IsNullOrEmpty(stationId))
  122. {
  123. parameters.Add(new SQLiteParameter("@StationId", "%" + stationId + "%"));
  124. }
  125. // 执行查询并获取结果
  126. List<TStation> stations = new List<TStation>();
  127. using (SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql, parameters.ToArray()))
  128. {
  129. while (reader.Read())
  130. {
  131. // 将查询结果映射到 TStation 对象
  132. TStation station = new TStation
  133. {
  134. Id = reader.GetString(reader.GetOrdinal("id")),
  135. StationId = reader.GetString(reader.GetOrdinal("station_id")),
  136. StationName = reader.GetString(reader.GetOrdinal("station_name")),
  137. DeviceSn = reader.GetString(reader.GetOrdinal("device_sn")),
  138. MeterType = reader.GetInt32(reader.GetOrdinal("meter_type")),
  139. BrightVal = reader.GetDouble(reader.GetOrdinal("bright_val")),
  140. FlowRate = reader.GetInt32(reader.GetOrdinal("flow_rate")),
  141. DialRegion = reader.GetString(reader.GetOrdinal("dial_region")),
  142. NumCount = reader.GetInt32(reader.GetOrdinal("num_count")),
  143. IndCount = reader.GetInt32(reader.GetOrdinal("ind_count")),
  144. FeatureRegion = reader.GetString(reader.GetOrdinal("feature_region")),
  145. LastUnit = reader.GetDouble(reader.GetOrdinal("last_unit")),
  146. LastValue = reader.GetDouble(reader.GetOrdinal("last_value")),
  147. LastTime = reader.GetString(reader.GetOrdinal("last_time")),
  148. CreateTime = reader.GetString(reader.GetOrdinal("create_time")),
  149. StandId = reader.GetString(reader.GetOrdinal("stand_id"))
  150. };
  151. stations.Add(station);
  152. }
  153. }
  154. // 获取总记录数,用于计算总页数
  155. string countSql = "SELECT COUNT(*) FROM t_station";
  156. int totalRecords = Convert.ToInt32(SQLiteHelper.GetSingle(countSql));
  157. // 计算总页数
  158. int totalPages = (int)Math.Ceiling((double)totalRecords / pageSize);
  159. // 返回元组,包含总记录数、总页数和结果列表
  160. return Tuple.Create(totalRecords, totalPages, stations);
  161. }
  162. //public static Tuple<int, int, List<TStation>> GetPagedTStations(int pageNumber, int pageSize,string stationId,string standId)
  163. //{
  164. // // 计算 OFFSET 值
  165. // int offset = (pageNumber - 1) * pageSize;
  166. // // 定义 SQL 查询语句,带有分页
  167. // // 定义 SQL 查询语句的基础部分
  168. // string sql = @"
  169. // SELECT id,station_id, station_name, device_sn,meter_type, bright_val, flow_rate, dial_region,
  170. // num_count, ind_count, num_region, ht_region, last_num_unit, last_ind_unit,
  171. // last_value, last_time, create_time,stand_id
  172. // FROM t_station ";
  173. // sql += "WHERE stand_id = @StandId";
  174. // // 如果传入的 stationId 不为空,增加过滤条件
  175. // if (!string.IsNullOrEmpty(stationId))
  176. // {
  177. // //sql += "WHERE station_id LIKE @StationId ";
  178. // sql += " AND station_id LIKE @StationId ";
  179. // }
  180. // // 添加排序和分页
  181. // sql += " ORDER BY station_id ASC LIMIT @PageSize OFFSET @Offset;";
  182. // // 创建 SQL 参数
  183. // List<SQLiteParameter> parameters = new List<SQLiteParameter>
  184. // {
  185. // new SQLiteParameter("@PageSize", pageSize),
  186. // new SQLiteParameter("@Offset", offset),
  187. // new SQLiteParameter("@StandId",standId)
  188. // };
  189. // // 如果传入了 stationId,添加到参数中
  190. // if (!string.IsNullOrEmpty(stationId))
  191. // {
  192. // parameters.Add(new SQLiteParameter("@StationId", "%" + stationId + "%"));
  193. // }
  194. // // 执行查询并获取结果
  195. // List<TStation> stations = new List<TStation>();
  196. // using (SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql, parameters.ToArray()))
  197. // {
  198. // while (reader.Read())
  199. // {
  200. // // 将查询结果映射到 TStation 对象
  201. // TStation station = new TStation
  202. // {
  203. // Id = reader.GetString(reader.GetOrdinal("id")),
  204. // StationId = reader.GetString(reader.GetOrdinal("station_id")),
  205. // StationName = reader.GetString(reader.GetOrdinal("station_name")),
  206. // DeviceSn = reader.GetString(reader.GetOrdinal("device_sn")),
  207. // MeterType = reader.GetInt32(reader.GetOrdinal("meter_type")),
  208. // BrightVal = reader.GetDouble(reader.GetOrdinal("bright_val")),
  209. // FlowRate = reader.GetInt32(reader.GetOrdinal("flow_rate")),
  210. // DialRegion = reader.GetString(reader.GetOrdinal("dial_region")),
  211. // NumCount = reader.GetInt32(reader.GetOrdinal("num_count")),
  212. // IndCount = reader.GetInt32(reader.GetOrdinal("ind_count")),
  213. // NumRegion = reader.GetString(reader.GetOrdinal("num_region")),
  214. // HtRegion = reader.GetString(reader.GetOrdinal("ht_region")),
  215. // LastNumUnit = reader.GetDouble(reader.GetOrdinal("last_num_unit")),
  216. // LastIndUnit = reader.GetDouble(reader.GetOrdinal("last_ind_unit")),
  217. // LastValue = reader.GetDouble(reader.GetOrdinal("last_value")),
  218. // LastTime = reader.GetString(reader.GetOrdinal("last_time")),
  219. // CreateTime = reader.GetString(reader.GetOrdinal("create_time")),
  220. // StandId = reader.GetString(reader.GetOrdinal("stand_id"))
  221. // };
  222. // stations.Add(station);
  223. // }
  224. // }
  225. // // 获取总记录数,用于计算总页数
  226. // string countSql = "SELECT COUNT(*) FROM t_station ";
  227. // int totalRecords = Convert.ToInt32(SQLiteHelper.GetSingle(countSql));
  228. // // 计算总页数
  229. // int totalPages = (int)Math.Ceiling((double)totalRecords / pageSize);
  230. // // 返回元组,包含总记录数、总页数和结果列表
  231. // return Tuple.Create(totalRecords, totalPages, stations);
  232. //}
  233. public static Tuple<int, int, List<TStation>> GetPagedTStations(int pageNumber, int pageSize, string stationId, string standId)
  234. {
  235. // 计算 OFFSET 值
  236. int offset = (pageNumber - 1) * pageSize;
  237. // 定义 SQL 查询语句
  238. string baseSql = @"
  239. FROM t_station
  240. WHERE stand_id = @StandId";
  241. // 如果传入的 stationId 不为空,增加过滤条件
  242. if (!string.IsNullOrEmpty(stationId))
  243. {
  244. baseSql += " AND station_id LIKE @StationId";
  245. }
  246. // 查询分页数据
  247. string sql = $@"
  248. SELECT id, station_id, station_name, device_sn, meter_type, bright_val, flow_rate, dial_region,
  249. num_count, ind_count, feature_region, last_unit,
  250. last_value, last_time, create_time, stand_id
  251. {baseSql}
  252. ORDER BY station_id ASC
  253. LIMIT @PageSize OFFSET @Offset;";
  254. // 查询总记录数
  255. string countSql = $"SELECT COUNT(*) {baseSql};";
  256. // 创建 SQL 参数
  257. List<SQLiteParameter> parameters = new List<SQLiteParameter>
  258. {
  259. new SQLiteParameter("@StandId", standId),
  260. new SQLiteParameter("@PageSize", pageSize),
  261. new SQLiteParameter("@Offset", offset)
  262. };
  263. // 如果传入了 stationId,添加到参数中
  264. if (!string.IsNullOrEmpty(stationId))
  265. {
  266. parameters.Add(new SQLiteParameter("@StationId", "%" + stationId + "%"));
  267. }
  268. // 获取数据列表
  269. List<TStation> stations = new List<TStation>();
  270. using (SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql, parameters.ToArray()))
  271. {
  272. while (reader.Read())
  273. {
  274. TStation station = new TStation
  275. {
  276. Id = reader.GetString(reader.GetOrdinal("id")),
  277. StationId = reader.GetString(reader.GetOrdinal("station_id")),
  278. StationName = reader.GetString(reader.GetOrdinal("station_name")),
  279. DeviceSn = reader.GetString(reader.GetOrdinal("device_sn")),
  280. MeterType = reader.GetInt32(reader.GetOrdinal("meter_type")),
  281. BrightVal = reader.GetDouble(reader.GetOrdinal("bright_val")),
  282. FlowRate = reader.GetInt32(reader.GetOrdinal("flow_rate")),
  283. DialRegion = reader.GetString(reader.GetOrdinal("dial_region")),
  284. NumCount = reader.GetInt32(reader.GetOrdinal("num_count")),
  285. IndCount = reader.GetInt32(reader.GetOrdinal("ind_count")),
  286. FeatureRegion = reader.GetString(reader.GetOrdinal("feature_region")),
  287. LastUnit = reader.GetDouble(reader.GetOrdinal("last_unit")),
  288. LastValue = reader.GetDouble(reader.GetOrdinal("last_value")),
  289. LastTime = reader.GetString(reader.GetOrdinal("last_time")),
  290. CreateTime = reader.GetString(reader.GetOrdinal("create_time")),
  291. StandId = reader.GetString(reader.GetOrdinal("stand_id"))
  292. };
  293. stations.Add(station);
  294. }
  295. }
  296. // 获取符合条件的总记录数
  297. int totalRecords = Convert.ToInt32(SQLiteHelper.GetSingle(countSql, parameters.ToArray()));
  298. // 计算总页数
  299. int totalPages = (int)Math.Ceiling((double)totalRecords / pageSize);
  300. // 返回元组,包含总记录数、总页数和结果列表
  301. return Tuple.Create(totalRecords, totalPages, stations);
  302. }
  303. public static bool DeleteTStation(string id)
  304. {
  305. // 定义删除的 SQL 语句
  306. string sql = "DELETE FROM t_station WHERE id = @Id";
  307. // 创建 SQL 参数
  308. SQLiteParameter[] parameters = new SQLiteParameter[]
  309. {
  310. new SQLiteParameter("@Id", id)
  311. };
  312. try
  313. {
  314. // 调用 SQLiteHelper 执行删除操作
  315. int rowsAffected = SQLiteHelper.ExecuteSql(sql, parameters);
  316. // 如果删除成功,返回 true,否则返回 false
  317. return rowsAffected > 0;
  318. }
  319. catch (Exception ex)
  320. {
  321. // 处理异常(如果有的话)
  322. Console.WriteLine("Error deleting data: " + ex.Message);
  323. return false;
  324. }
  325. }
  326. public static bool UpdateTStation(TStation tStation)
  327. {
  328. // 定义更新的 SQL 语句
  329. string sql = @"
  330. UPDATE t_station
  331. SET
  332. station_name = @StationName,
  333. meter_type = @MeterType,
  334. bright_val = @BrightVal,
  335. flow_rate = @FlowRate,
  336. dial_region = @DialRegion,
  337. num_count = @NumCount,
  338. ind_count = @IndCount,
  339. feature_region = @FeatureRegion,
  340. last_unit = @LastUnit,
  341. last_value = @LastValue,
  342. last_time = @LastTime
  343. WHERE id = @Id";
  344. // 创建 SQL 参数
  345. SQLiteParameter[] parameters = new SQLiteParameter[]
  346. {
  347. //new SQLiteParameter("@StationId", tStation.StationId),
  348. new SQLiteParameter("@Id",tStation.Id),
  349. new SQLiteParameter("@StationName", tStation.StationName),
  350. new SQLiteParameter("@MeterType", tStation.MeterType),
  351. new SQLiteParameter("@BrightVal", tStation.BrightVal),
  352. new SQLiteParameter("@FlowRate", tStation.FlowRate),
  353. new SQLiteParameter("@DialRegion", tStation.DialRegion),
  354. new SQLiteParameter("@NumCount", tStation.NumCount),
  355. new SQLiteParameter("@IndCount", tStation.IndCount),
  356. new SQLiteParameter("@FeatureRegion", tStation.FeatureRegion),
  357. new SQLiteParameter("@LastUnit", tStation.LastUnit),
  358. new SQLiteParameter("@LastValue", tStation.LastValue),
  359. new SQLiteParameter("@LastTime", tStation.LastTime)
  360. };
  361. try
  362. {
  363. // 调用 SQLiteHelper 执行更新操作
  364. int rowsAffected = SQLiteHelper.ExecuteSql(sql, parameters);
  365. // 如果更新成功,返回 true,否则返回 false
  366. return rowsAffected > 0;
  367. }
  368. catch (Exception ex)
  369. {
  370. // 处理异常(如果有的话)
  371. Console.WriteLine("Error updating data: " + ex.Message);
  372. return false;
  373. }
  374. }
  375. public static bool ClearTStationTable()
  376. {
  377. // 构建清空表的 SQL 语句
  378. string sql = "DELETE FROM t_station";
  379. try
  380. {
  381. // 调用 SQLiteHelper 执行清空操作
  382. SQLiteHelper.ExecuteSql(sql, null);
  383. // 返回 true 表示操作成功
  384. return true;
  385. }
  386. catch (Exception ex)
  387. {
  388. // 处理异常(如果有的话)
  389. Console.WriteLine("Error clearing table: " + ex.Message);
  390. return false;
  391. }
  392. }
  393. //-----------------------------------------------------
  394. }
  395. }