DBStation.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. using MeterVision.model;
  2. using MeterVision.Stand;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data.SQLite;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace MeterVision.db
  10. {
  11. public class DBStation
  12. {
  13. public static bool InsertTStation(TStation tStation)
  14. {
  15. // 构建插入的 SQL 语句
  16. string sql = "INSERT OR IGNORE INTO t_station (id,station_id, station_name, device_sn,meter_type, bright_val, flow_rate, " +
  17. "dial_region, num_count, ind_count, feature_region, last_unit, " +
  18. "last_value, last_time, create_time, stand_id) " +
  19. "VALUES (@StationId, @StationName, @DeviceSn, @MeterType, @BrightVal, @FlowRate, " +
  20. "@DialRegion, @NumCount, @IndCount, @FeatureRegion, @LastUnit, " +
  21. "@LastValue, @LastTime, @CreateTime, @StandId)";
  22. // 创建参数数组
  23. SQLiteParameter[] parameters = new SQLiteParameter[]
  24. {
  25. new SQLiteParameter("@Id", tStation.Id),
  26. new SQLiteParameter("@StationId", tStation.StationId),
  27. new SQLiteParameter("@StationName", tStation.StationName),
  28. new SQLiteParameter("@DeviceSn", tStation.DeviceSn),
  29. new SQLiteParameter("@MeterType", tStation.MeterType),
  30. new SQLiteParameter("@BrightVal", tStation.BrightVal),
  31. new SQLiteParameter("@FlowRate", tStation.FlowRate),
  32. new SQLiteParameter("@DialRegion", tStation.DialRegion),
  33. new SQLiteParameter("@NumCount", tStation.NumCount),
  34. new SQLiteParameter("@IndCount", tStation.IndCount),
  35. new SQLiteParameter("@FeatureRegion", tStation.FeatureRegion),
  36. new SQLiteParameter("@LastUnit", tStation.LastUnit),
  37. new SQLiteParameter("@LastValue", tStation.LastValue),
  38. new SQLiteParameter("@LastTime", tStation.LastTime),
  39. new SQLiteParameter("@CreateTime", tStation.CreateTime),
  40. new SQLiteParameter("@StandId", tStation.StandId),
  41. };
  42. try
  43. {
  44. // 调用 SQLiteHelper 执行插入操作
  45. int rowsAffected = SQLiteHelper.ExecuteSql(sql, parameters);
  46. // 如果插入成功,返回 true,否则返回 false
  47. return rowsAffected > 0;
  48. }
  49. catch (Exception ex)
  50. {
  51. // 处理异常(如果有的话)
  52. Console.WriteLine("Error inserting data: " + ex.Message);
  53. return false;
  54. }
  55. }
  56. public static bool InsertTStations(List<StationInfo> stationInfos)
  57. {
  58. //INSERT OR IGNORE INTO
  59. string sql = @"
  60. INSERT OR IGNORE INTO t_station (id, stand_id,create_time, station_id, device_sn, num_count, last_unit)
  61. VALUES (@Id, @StandId, @CreateTime, @StationId, @DeviceSn, @NumCount, @LastUnit);";
  62. try
  63. {
  64. // 开始事务
  65. using (SQLiteConnection connection = SQLiteHelper.GetConnection())
  66. {
  67. connection.Open();
  68. using (SQLiteTransaction transaction = connection.BeginTransaction())
  69. {
  70. // 遍历列表插入每一条数据
  71. foreach (var stationInfo in stationInfos)
  72. {
  73. SQLiteParameter[] parameters = new SQLiteParameter[]
  74. {
  75. new SQLiteParameter("@Id", Guid.NewGuid().ToString()),
  76. new SQLiteParameter("@StandId", stationInfo.StandId),
  77. new SQLiteParameter("@CreateTime", ThisApp.GetNowTime_yyyyMMddHHmmss()),
  78. new SQLiteParameter("@StationId", stationInfo.StationId),
  79. new SQLiteParameter("@DeviceSn", stationInfo.DeviceSn),
  80. new SQLiteParameter("@NumCount", stationInfo.NumCount),
  81. new SQLiteParameter("@LastUnit", stationInfo.LastUnit2)
  82. };
  83. // 执行插入操作
  84. SQLiteHelper.ExecuteNonQuery(sql, parameters, transaction);
  85. }
  86. // 提交事务
  87. transaction.Commit();
  88. }
  89. }
  90. return true;
  91. }
  92. catch(Exception ex)
  93. {
  94. Console.WriteLine(ex.Message);
  95. return false;
  96. }
  97. }
  98. public static Tuple<int, int, List<TStation>> GetPagedTStations(int pageNumber, int pageSize, string stationId)
  99. {
  100. // 计算 OFFSET 值
  101. int offset = (pageNumber - 1) * pageSize;
  102. // 定义 SQL 查询语句,带有分页
  103. // 定义 SQL 查询语句的基础部分
  104. string sql = @"
  105. SELECT id,station_id, station_name, device_sn,meter_type, bright_val, flow_rate, dial_region,
  106. num_count, ind_count, feature_region, last_unit,
  107. last_value, last_time, create_time,stand_id
  108. FROM t_station ";
  109. // 如果传入的 stationId 不为空,增加过滤条件
  110. if (!string.IsNullOrEmpty(stationId))
  111. {
  112. sql += "WHERE station_id LIKE @StationId ";
  113. }
  114. // 添加排序和分页
  115. sql += "ORDER BY station_id ASC LIMIT @PageSize OFFSET @Offset;";
  116. // 创建 SQL 参数
  117. List<SQLiteParameter> parameters = new List<SQLiteParameter>
  118. {
  119. new SQLiteParameter("@PageSize", pageSize),
  120. new SQLiteParameter("@Offset", offset),
  121. };
  122. // 如果传入了 stationId,添加到参数中
  123. if (!string.IsNullOrEmpty(stationId))
  124. {
  125. parameters.Add(new SQLiteParameter("@StationId", "%" + stationId + "%"));
  126. }
  127. // 执行查询并获取结果
  128. List<TStation> stations = new List<TStation>();
  129. using (SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql, parameters.ToArray()))
  130. {
  131. while (reader.Read())
  132. {
  133. // 将查询结果映射到 TStation 对象
  134. TStation station = new TStation
  135. {
  136. Id = reader.GetString(reader.GetOrdinal("id")),
  137. StationId = reader.GetString(reader.GetOrdinal("station_id")),
  138. StationName = reader.GetString(reader.GetOrdinal("station_name")),
  139. DeviceSn = reader.GetString(reader.GetOrdinal("device_sn")),
  140. MeterType = reader.GetInt32(reader.GetOrdinal("meter_type")),
  141. BrightVal = reader.GetDouble(reader.GetOrdinal("bright_val")),
  142. FlowRate = reader.GetInt32(reader.GetOrdinal("flow_rate")),
  143. DialRegion = reader.GetString(reader.GetOrdinal("dial_region")),
  144. NumCount = reader.GetInt32(reader.GetOrdinal("num_count")),
  145. IndCount = reader.GetInt32(reader.GetOrdinal("ind_count")),
  146. FeatureRegion = reader.GetString(reader.GetOrdinal("feature_region")),
  147. LastUnit = reader.GetDouble(reader.GetOrdinal("last_unit")),
  148. LastValue = reader.GetDouble(reader.GetOrdinal("last_value")),
  149. LastTime = reader.GetString(reader.GetOrdinal("last_time")),
  150. CreateTime = reader.GetString(reader.GetOrdinal("create_time")),
  151. StandId = reader.GetString(reader.GetOrdinal("stand_id"))
  152. };
  153. stations.Add(station);
  154. }
  155. }
  156. // 获取总记录数,用于计算总页数
  157. string countSql = "SELECT COUNT(*) FROM t_station";
  158. int totalRecords = Convert.ToInt32(SQLiteHelper.GetSingle(countSql));
  159. // 计算总页数
  160. int totalPages = (int)Math.Ceiling((double)totalRecords / pageSize);
  161. // 返回元组,包含总记录数、总页数和结果列表
  162. return Tuple.Create(totalRecords, totalPages, stations);
  163. }
  164. //public static Tuple<int, int, List<TStation>> GetPagedTStations(int pageNumber, int pageSize,string stationId,string standId)
  165. //{
  166. // // 计算 OFFSET 值
  167. // int offset = (pageNumber - 1) * pageSize;
  168. // // 定义 SQL 查询语句,带有分页
  169. // // 定义 SQL 查询语句的基础部分
  170. // string sql = @"
  171. // SELECT id,station_id, station_name, device_sn,meter_type, bright_val, flow_rate, dial_region,
  172. // num_count, ind_count, num_region, ht_region, last_num_unit, last_ind_unit,
  173. // last_value, last_time, create_time,stand_id
  174. // FROM t_station ";
  175. // sql += "WHERE stand_id = @StandId";
  176. // // 如果传入的 stationId 不为空,增加过滤条件
  177. // if (!string.IsNullOrEmpty(stationId))
  178. // {
  179. // //sql += "WHERE station_id LIKE @StationId ";
  180. // sql += " AND station_id LIKE @StationId ";
  181. // }
  182. // // 添加排序和分页
  183. // sql += " ORDER BY station_id ASC LIMIT @PageSize OFFSET @Offset;";
  184. // // 创建 SQL 参数
  185. // List<SQLiteParameter> parameters = new List<SQLiteParameter>
  186. // {
  187. // new SQLiteParameter("@PageSize", pageSize),
  188. // new SQLiteParameter("@Offset", offset),
  189. // new SQLiteParameter("@StandId",standId)
  190. // };
  191. // // 如果传入了 stationId,添加到参数中
  192. // if (!string.IsNullOrEmpty(stationId))
  193. // {
  194. // parameters.Add(new SQLiteParameter("@StationId", "%" + stationId + "%"));
  195. // }
  196. // // 执行查询并获取结果
  197. // List<TStation> stations = new List<TStation>();
  198. // using (SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql, parameters.ToArray()))
  199. // {
  200. // while (reader.Read())
  201. // {
  202. // // 将查询结果映射到 TStation 对象
  203. // TStation station = new TStation
  204. // {
  205. // Id = reader.GetString(reader.GetOrdinal("id")),
  206. // StationId = reader.GetString(reader.GetOrdinal("station_id")),
  207. // StationName = reader.GetString(reader.GetOrdinal("station_name")),
  208. // DeviceSn = reader.GetString(reader.GetOrdinal("device_sn")),
  209. // MeterType = reader.GetInt32(reader.GetOrdinal("meter_type")),
  210. // BrightVal = reader.GetDouble(reader.GetOrdinal("bright_val")),
  211. // FlowRate = reader.GetInt32(reader.GetOrdinal("flow_rate")),
  212. // DialRegion = reader.GetString(reader.GetOrdinal("dial_region")),
  213. // NumCount = reader.GetInt32(reader.GetOrdinal("num_count")),
  214. // IndCount = reader.GetInt32(reader.GetOrdinal("ind_count")),
  215. // NumRegion = reader.GetString(reader.GetOrdinal("num_region")),
  216. // HtRegion = reader.GetString(reader.GetOrdinal("ht_region")),
  217. // LastNumUnit = reader.GetDouble(reader.GetOrdinal("last_num_unit")),
  218. // LastIndUnit = reader.GetDouble(reader.GetOrdinal("last_ind_unit")),
  219. // LastValue = reader.GetDouble(reader.GetOrdinal("last_value")),
  220. // LastTime = reader.GetString(reader.GetOrdinal("last_time")),
  221. // CreateTime = reader.GetString(reader.GetOrdinal("create_time")),
  222. // StandId = reader.GetString(reader.GetOrdinal("stand_id"))
  223. // };
  224. // stations.Add(station);
  225. // }
  226. // }
  227. // // 获取总记录数,用于计算总页数
  228. // string countSql = "SELECT COUNT(*) FROM t_station ";
  229. // int totalRecords = Convert.ToInt32(SQLiteHelper.GetSingle(countSql));
  230. // // 计算总页数
  231. // int totalPages = (int)Math.Ceiling((double)totalRecords / pageSize);
  232. // // 返回元组,包含总记录数、总页数和结果列表
  233. // return Tuple.Create(totalRecords, totalPages, stations);
  234. //}
  235. public static Tuple<int, int, List<TStation>> GetPagedTStations(int pageNumber, int pageSize, string stationId, string standId)
  236. {
  237. // 计算 OFFSET 值
  238. int offset = (pageNumber - 1) * pageSize;
  239. // 定义 SQL 查询语句
  240. string baseSql = @"
  241. FROM t_station
  242. WHERE stand_id = @StandId";
  243. // 如果传入的 stationId 不为空,增加过滤条件
  244. if (!string.IsNullOrEmpty(stationId))
  245. {
  246. baseSql += " AND station_id LIKE @StationId";
  247. }
  248. // 查询分页数据
  249. string sql = $@"
  250. SELECT id, station_id, station_name, device_sn, meter_type, bright_val, flow_rate, dial_region,
  251. num_count, ind_count, feature_region, last_unit,
  252. last_value, last_time, create_time, stand_id
  253. {baseSql}
  254. ORDER BY station_id ASC
  255. LIMIT @PageSize OFFSET @Offset;";
  256. // 查询总记录数
  257. string countSql = $"SELECT COUNT(*) {baseSql};";
  258. // 创建 SQL 参数
  259. List<SQLiteParameter> parameters = new List<SQLiteParameter>
  260. {
  261. new SQLiteParameter("@StandId", standId),
  262. new SQLiteParameter("@PageSize", pageSize),
  263. new SQLiteParameter("@Offset", offset)
  264. };
  265. // 如果传入了 stationId,添加到参数中
  266. if (!string.IsNullOrEmpty(stationId))
  267. {
  268. parameters.Add(new SQLiteParameter("@StationId", "%" + stationId + "%"));
  269. }
  270. // 获取数据列表
  271. List<TStation> stations = new List<TStation>();
  272. using (SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql, parameters.ToArray()))
  273. {
  274. while (reader.Read())
  275. {
  276. TStation station = new TStation
  277. {
  278. Id = reader.GetString(reader.GetOrdinal("id")),
  279. StationId = reader.GetString(reader.GetOrdinal("station_id")),
  280. StationName = reader.GetString(reader.GetOrdinal("station_name")),
  281. DeviceSn = reader.GetString(reader.GetOrdinal("device_sn")),
  282. MeterType = reader.GetInt32(reader.GetOrdinal("meter_type")),
  283. BrightVal = reader.GetDouble(reader.GetOrdinal("bright_val")),
  284. FlowRate = reader.GetInt32(reader.GetOrdinal("flow_rate")),
  285. DialRegion = reader.GetString(reader.GetOrdinal("dial_region")),
  286. NumCount = reader.GetInt32(reader.GetOrdinal("num_count")),
  287. IndCount = reader.GetInt32(reader.GetOrdinal("ind_count")),
  288. FeatureRegion = reader.GetString(reader.GetOrdinal("feature_region")),
  289. LastUnit = reader.GetDouble(reader.GetOrdinal("last_unit")),
  290. LastValue = reader.GetDouble(reader.GetOrdinal("last_value")),
  291. LastTime = reader.GetString(reader.GetOrdinal("last_time")),
  292. CreateTime = reader.GetString(reader.GetOrdinal("create_time")),
  293. StandId = reader.GetString(reader.GetOrdinal("stand_id"))
  294. };
  295. stations.Add(station);
  296. }
  297. }
  298. // 获取符合条件的总记录数
  299. int totalRecords = Convert.ToInt32(SQLiteHelper.GetSingle(countSql, parameters.ToArray()));
  300. // 计算总页数
  301. int totalPages = (int)Math.Ceiling((double)totalRecords / pageSize);
  302. // 返回元组,包含总记录数、总页数和结果列表
  303. return Tuple.Create(totalRecords, totalPages, stations);
  304. }
  305. public static Tuple<int, int, List<TStation>> GetPagedVStations(int pageNumber, int pageSize,
  306. string stationId, MarkFindType markFindType,string standId)
  307. {
  308. // 计算 OFFSET 值
  309. int offset = (pageNumber - 1) * pageSize;
  310. // 定义 SQL 查询语句
  311. string baseSql = @"
  312. FROM v_station
  313. WHERE stand_id = @StandId";
  314. // 如果传入的 stationId 不为空,增加过滤条件
  315. if (!string.IsNullOrEmpty(stationId))
  316. {
  317. baseSql += " AND station_id LIKE @StationId";
  318. }
  319. if(markFindType == MarkFindType.ALL)
  320. {
  321. }
  322. else if(markFindType == MarkFindType.MARK_YES)
  323. {
  324. baseSql += " AND mark_count > 0";
  325. }
  326. else if(markFindType == MarkFindType.MARK_NO)
  327. {
  328. baseSql += " And mark_count = 0";
  329. }
  330. // 查询分页数据
  331. string sql = $@"
  332. SELECT id, stand_id, station_id, create_time, mark_count
  333. {baseSql}
  334. ORDER BY station_id ASC
  335. LIMIT @PageSize OFFSET @Offset;";
  336. // 查询总记录数
  337. string countSql = $"SELECT COUNT(*) {baseSql};";
  338. // 创建 SQL 参数
  339. List<SQLiteParameter> parameters = new List<SQLiteParameter>
  340. {
  341. new SQLiteParameter("@StandId", standId),
  342. new SQLiteParameter("@PageSize", pageSize),
  343. new SQLiteParameter("@Offset", offset)
  344. };
  345. // 如果传入了 stationId,添加到参数中
  346. if (!string.IsNullOrEmpty(stationId))
  347. {
  348. parameters.Add(new SQLiteParameter("@StationId", "%" + stationId + "%"));
  349. }
  350. // 获取数据列表
  351. List<TStation> stations = new List<TStation>();
  352. using (SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql, parameters.ToArray()))
  353. {
  354. while (reader.Read())
  355. {
  356. TStation station = new TStation
  357. {
  358. Id = reader.GetString(reader.GetOrdinal("id")),
  359. StandId = reader.GetString(reader.GetOrdinal("stand_id")),
  360. StationId = reader.GetString(reader.GetOrdinal("station_id")),
  361. CreateTime = reader.GetString(reader.GetOrdinal("create_time")),
  362. MarkCount = reader.GetInt32(reader.GetOrdinal("mark_count")),
  363. };
  364. stations.Add(station);
  365. }
  366. }
  367. // 获取符合条件的总记录数
  368. int totalRecords = Convert.ToInt32(SQLiteHelper.GetSingle(countSql, parameters.ToArray()));
  369. // 计算总页数
  370. int totalPages = (int)Math.Ceiling((double)totalRecords / pageSize);
  371. // 返回元组,包含总记录数、总页数和结果列表
  372. return Tuple.Create(totalRecords, totalPages, stations);
  373. }
  374. public static int GetMarkCount(string id)
  375. {
  376. int iMarkCount = 0;
  377. string sql = $@"
  378. SELECT mark_count
  379. FROM v_station
  380. WHERE id = @Id";
  381. // 定义参数
  382. SQLiteParameter[] parameters = new SQLiteParameter[]
  383. {
  384. new SQLiteParameter("@Id", id)
  385. };
  386. // 执行查询并读取数据
  387. try
  388. {
  389. using (SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql, parameters))
  390. {
  391. if (reader.Read())
  392. {
  393. return reader.GetInt32(reader.GetOrdinal("mark_count"));
  394. }
  395. }
  396. }
  397. catch (Exception ex)
  398. {
  399. throw new Exception($"查询最小 SampleTime 记录时发生错误:{ex.Message}", ex);
  400. }
  401. return iMarkCount;
  402. }
  403. public static List<TStation> GetAllStation(string standId)
  404. {
  405. // 查询分页数据
  406. string sql = $@"
  407. SELECT id, station_id, station_name, device_sn, meter_type, bright_val, flow_rate, dial_region,
  408. num_count, ind_count, feature_region, last_unit,
  409. last_value, last_time, create_time, stand_id
  410. FROM t_station
  411. WHERE stand_id = @StandId AND meter_type > 0
  412. AND dial_region <> ''
  413. AND feature_region <> ''
  414. AND last_time <> ''
  415. ORDER BY station_id ASC";
  416. // 定义参数
  417. SQLiteParameter[] parameters = new SQLiteParameter[]
  418. {
  419. new SQLiteParameter("@StandId", standId)
  420. };
  421. // 执行查询并读取数据
  422. List<TStation> stations = new List<TStation>();
  423. try
  424. {
  425. using (SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql, parameters.ToArray()))
  426. {
  427. while (reader.Read())
  428. {
  429. TStation station = new TStation
  430. {
  431. Id = reader.GetString(reader.GetOrdinal("id")),
  432. StationId = reader.GetString(reader.GetOrdinal("station_id")),
  433. StationName = reader.GetString(reader.GetOrdinal("station_name")),
  434. DeviceSn = reader.GetString(reader.GetOrdinal("device_sn")),
  435. MeterType = reader.GetInt32(reader.GetOrdinal("meter_type")),
  436. BrightVal = reader.GetDouble(reader.GetOrdinal("bright_val")),
  437. FlowRate = reader.GetInt32(reader.GetOrdinal("flow_rate")),
  438. DialRegion = reader.GetString(reader.GetOrdinal("dial_region")),
  439. NumCount = reader.GetInt32(reader.GetOrdinal("num_count")),
  440. IndCount = reader.GetInt32(reader.GetOrdinal("ind_count")),
  441. FeatureRegion = reader.GetString(reader.GetOrdinal("feature_region")),
  442. LastUnit = reader.GetDouble(reader.GetOrdinal("last_unit")),
  443. LastValue = reader.GetDouble(reader.GetOrdinal("last_value")),
  444. LastTime = reader.GetString(reader.GetOrdinal("last_time")),
  445. CreateTime = reader.GetString(reader.GetOrdinal("create_time")),
  446. StandId = reader.GetString(reader.GetOrdinal("stand_id"))
  447. };
  448. stations.Add(station);
  449. }
  450. }
  451. }
  452. catch (Exception ex)
  453. {
  454. // 根据需要记录日志或抛出异常
  455. throw new Exception($"查询 TStation表 时发生错误:{ex.Message}", ex);
  456. }
  457. return stations;
  458. }
  459. public static List<TStation> GetAllVStation(string standId)
  460. {
  461. // 查询分页数据
  462. string sql = $@"
  463. SELECT id, stand_id, station_id, create_time, mark_count
  464. FROM v_station
  465. WHERE stand_id = @StandId AND mark_count > 0
  466. ORDER BY station_id ASC";
  467. // 定义参数
  468. SQLiteParameter[] parameters = new SQLiteParameter[]
  469. {
  470. new SQLiteParameter("@StandId", standId)
  471. };
  472. // 执行查询并读取数据
  473. List<TStation> stations = new List<TStation>();
  474. try
  475. {
  476. using (SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql, parameters.ToArray()))
  477. {
  478. while (reader.Read())
  479. {
  480. TStation station = new TStation
  481. {
  482. Id = reader.GetString(reader.GetOrdinal("id")),
  483. StandId = reader.GetString(reader.GetOrdinal("stand_id")),
  484. StationId = reader.GetString(reader.GetOrdinal("station_id")),
  485. CreateTime = reader.GetString(reader.GetOrdinal("create_time")),
  486. MarkCount = reader.GetInt32(reader.GetOrdinal("mark_count"))
  487. };
  488. stations.Add(station);
  489. }
  490. }
  491. }
  492. catch (Exception ex)
  493. {
  494. // 根据需要记录日志或抛出异常
  495. throw new Exception($"查询 TStation表 时发生错误:{ex.Message}", ex);
  496. }
  497. return stations;
  498. }
  499. public static bool DeleteTStation(string id)
  500. {
  501. // 定义删除的 SQL 语句
  502. string sql = "DELETE FROM t_station WHERE id = @Id";
  503. // 创建 SQL 参数
  504. SQLiteParameter[] parameters = new SQLiteParameter[]
  505. {
  506. new SQLiteParameter("@Id", id)
  507. };
  508. try
  509. {
  510. // 调用 SQLiteHelper 执行删除操作
  511. int rowsAffected = SQLiteHelper.ExecuteSql(sql, parameters);
  512. // 如果删除成功,返回 true,否则返回 false
  513. return rowsAffected > 0;
  514. }
  515. catch (Exception ex)
  516. {
  517. // 处理异常(如果有的话)
  518. Console.WriteLine("Error deleting data: " + ex.Message);
  519. return false;
  520. }
  521. }
  522. public static bool DeleteTStationByStandId(string standId)
  523. {
  524. // 定义删除的 SQL 语句
  525. string sql = "DELETE FROM t_station WHERE stand_id = @StandId";
  526. // 创建 SQL 参数
  527. SQLiteParameter[] parameters = new SQLiteParameter[]
  528. {
  529. new SQLiteParameter("@StandId", standId)
  530. };
  531. try
  532. {
  533. // 调用 SQLiteHelper 执行删除操作
  534. int rowsAffected = SQLiteHelper.ExecuteSql(sql, parameters);
  535. // 如果删除成功,返回 true,否则返回 false
  536. //return rowsAffected > 0;
  537. return true;
  538. }
  539. catch (Exception ex)
  540. {
  541. // 处理异常(如果有的话)
  542. Console.WriteLine("Error deleting data: " + ex.Message);
  543. return false;
  544. }
  545. }
  546. public static bool DeleteStationAndStandDetails(string standId,string stationId)
  547. {
  548. // 定义删除的 SQL 语句,使用事务来保证一致性
  549. string sql = @"
  550. BEGIN TRANSACTION;
  551. -- 删除 t_stand_detail 表中与 stand_id 相关的数据
  552. DELETE FROM t_stand_detail WHERE station_id = @StationId AND stand_id = @StandId;
  553. -- 删除 t_station 表中与 stand_id 相关的数据
  554. DELETE FROM t_station WHERE station_id = @StationId AND stand_id = @StandId;
  555. COMMIT;";
  556. // 创建参数
  557. SQLiteParameter[] parameters = new SQLiteParameter[]
  558. {
  559. new SQLiteParameter("@StandId", standId),
  560. new SQLiteParameter("@StationId",stationId)
  561. };
  562. try
  563. {
  564. // 执行 SQL 删除操作
  565. int rowsAffected = SQLiteHelper.ExecuteSql(sql, parameters);
  566. // 返回是否成功删除
  567. return rowsAffected > 0;
  568. }
  569. catch (Exception ex)
  570. {
  571. // 处理异常(如果有的话)
  572. Console.WriteLine("Error deleting data: " + ex.Message);
  573. return false;
  574. }
  575. }
  576. public static bool UpdateTStation(TStation tStation)
  577. {
  578. // 定义更新的 SQL 语句
  579. string sql = @"
  580. UPDATE t_station
  581. SET
  582. station_name = @StationName,
  583. meter_type = @MeterType,
  584. bright_val = @BrightVal,
  585. flow_rate = @FlowRate,
  586. dial_region = @DialRegion,
  587. num_count = @NumCount,
  588. ind_count = @IndCount,
  589. feature_region = @FeatureRegion,
  590. last_unit = @LastUnit,
  591. last_value = @LastValue,
  592. last_time = @LastTime
  593. WHERE id = @Id";
  594. // 创建 SQL 参数
  595. SQLiteParameter[] parameters = new SQLiteParameter[]
  596. {
  597. //new SQLiteParameter("@StationId", tStation.StationId),
  598. new SQLiteParameter("@Id",tStation.Id),
  599. new SQLiteParameter("@StationName", tStation.StationName),
  600. new SQLiteParameter("@MeterType", tStation.MeterType),
  601. new SQLiteParameter("@BrightVal", tStation.BrightVal),
  602. new SQLiteParameter("@FlowRate", tStation.FlowRate),
  603. new SQLiteParameter("@DialRegion", tStation.DialRegion),
  604. new SQLiteParameter("@NumCount", tStation.NumCount),
  605. new SQLiteParameter("@IndCount", tStation.IndCount),
  606. new SQLiteParameter("@FeatureRegion", tStation.FeatureRegion),
  607. new SQLiteParameter("@LastUnit", tStation.LastUnit),
  608. new SQLiteParameter("@LastValue", tStation.LastValue),
  609. new SQLiteParameter("@LastTime", tStation.LastTime)
  610. };
  611. try
  612. {
  613. // 调用 SQLiteHelper 执行更新操作
  614. int rowsAffected = SQLiteHelper.ExecuteSql(sql, parameters);
  615. // 如果更新成功,返回 true,否则返回 false
  616. return rowsAffected > 0;
  617. }
  618. catch (Exception ex)
  619. {
  620. // 处理异常(如果有的话)
  621. Console.WriteLine("Error updating data: " + ex.Message);
  622. return false;
  623. }
  624. }
  625. public static bool ClearTStationTable()
  626. {
  627. // 构建清空表的 SQL 语句
  628. string sql = "DELETE FROM t_station";
  629. try
  630. {
  631. // 调用 SQLiteHelper 执行清空操作
  632. SQLiteHelper.ExecuteSql(sql, null);
  633. // 返回 true 表示操作成功
  634. return true;
  635. }
  636. catch (Exception ex)
  637. {
  638. // 处理异常(如果有的话)
  639. Console.WriteLine("Error clearing table: " + ex.Message);
  640. return false;
  641. }
  642. }
  643. //-----------------------------------------------------
  644. }
  645. }