DBStand.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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 DBStand
  11. {
  12. public static bool InsertTStand(TStand tStand)
  13. {
  14. // 构建插入的 SQL 语句
  15. string sql = "INSERT INTO t_stand (stand_id, create_time, stand_name, stand_type, stand_file) " +
  16. "VALUES (@StandId, @CreateTime, @StandName,@StandType,@StandFile)";
  17. // 创建参数数组
  18. SQLiteParameter[] parameters = new SQLiteParameter[]
  19. {
  20. new SQLiteParameter("@StandId", tStand.StandId),
  21. new SQLiteParameter("@CreateTime", tStand.CreateTime),
  22. new SQLiteParameter("@StandName", tStand.StandName),
  23. new SQLiteParameter("@StandType", tStand.StandType),
  24. new SQLiteParameter("@StandFile", tStand.StandFile)
  25. };
  26. try
  27. {
  28. // 调用 SQLiteHelper 执行插入操作
  29. int rowsAffected = SQLiteHelper.ExecuteSql(sql, parameters);
  30. // 如果插入成功,返回 true,否则返回 false
  31. return rowsAffected > 0;
  32. }
  33. catch (Exception ex)
  34. {
  35. // 处理异常(如果有的话)
  36. Console.WriteLine("Error inserting data: " + ex.Message);
  37. return false;
  38. }
  39. }
  40. public static bool DeleteStandAndDetails(string standId)
  41. {
  42. // 定义删除的 SQL 语句,使用事务来保证一致性
  43. string sql = @"
  44. BEGIN TRANSACTION;
  45. -- 删除 t_stand_detail 表中与 stand_id 相关的数据
  46. DELETE FROM t_stand_detail WHERE stand_id = @StandId;
  47. -- 删除 t_stand 表中与 stand_id 相关的数据
  48. DELETE FROM t_stand WHERE stand_id = @StandId;
  49. -- 删除 t_station 表中与 stand_id 相关的数据
  50. DELETE FROM t_station WHERE stand_id = @StandId;
  51. COMMIT;";
  52. // 创建参数
  53. SQLiteParameter[] parameters = new SQLiteParameter[]
  54. {
  55. new SQLiteParameter("@StandId", standId)
  56. };
  57. try
  58. {
  59. // 执行 SQL 删除操作
  60. int rowsAffected = SQLiteHelper.ExecuteSql(sql, parameters);
  61. // 返回是否成功删除
  62. return rowsAffected > 0;
  63. }
  64. catch (Exception ex)
  65. {
  66. // 处理异常(如果有的话)
  67. Console.WriteLine("Error deleting data: " + ex.Message);
  68. return false;
  69. }
  70. }
  71. public static Tuple<int,int, List<VStand>> GetPagedVStands(int pageNumber, int pageSize)
  72. {
  73. // 计算 OFFSET 值
  74. int offset = (pageNumber - 1) * pageSize;
  75. // 定义 SQL 查询语句,带有分页
  76. string sql = @"
  77. SELECT stand_id, create_time, stand_name,stand_type,stand_file,stand_count
  78. FROM v_stand ORDER BY create_time DESC
  79. LIMIT @PageSize OFFSET @Offset;";
  80. // 创建 SQL 参数
  81. SQLiteParameter[] parameters = new SQLiteParameter[]
  82. {
  83. new SQLiteParameter("@PageSize", pageSize),
  84. new SQLiteParameter("@Offset", offset)
  85. };
  86. // 执行查询并获取结果
  87. List<VStand> stands = new List<VStand>();
  88. using (SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql, parameters))
  89. {
  90. while (reader.Read())
  91. {
  92. // 将查询结果映射到 TStand 对象
  93. VStand stand = new VStand
  94. {
  95. StandId = reader.GetString(reader.GetOrdinal("stand_id")),
  96. CreateTime = reader.GetString(reader.GetOrdinal("create_time")),
  97. StandName = reader.GetString(reader.GetOrdinal("stand_name")),
  98. StandType = reader.GetInt32(reader.GetOrdinal("stand_type")),
  99. StandFile = reader.GetString(reader.GetOrdinal("stand_file")),
  100. StandCount = reader.GetInt32(reader.GetOrdinal("stand_count"))
  101. };
  102. stands.Add(stand);
  103. }
  104. }
  105. // 获取总记录数,用于计算总页数
  106. string countSql = "SELECT COUNT(*) FROM v_stand";
  107. int totalRecords = Convert.ToInt32(SQLiteHelper.GetSingle(countSql));
  108. // 计算总页数
  109. int totalPages = (int)Math.Ceiling((double)totalRecords / pageSize);
  110. // 返回元组,包含总页数和结果列表
  111. return Tuple.Create(totalRecords,totalPages, stands);
  112. }
  113. public static List<VStand> GetAllVStands()
  114. {
  115. // 定义 SQL 查询语句,带有分页
  116. string sql = @"
  117. SELECT stand_id, create_time, stand_name,stand_type,stand_file,stand_count
  118. FROM v_stand WHERE stand_count>0 ORDER BY create_time DESC";
  119. // 执行查询并获取结果
  120. List<VStand> stands = new List<VStand>();
  121. using (SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql))
  122. {
  123. while (reader.Read())
  124. {
  125. // 将查询结果映射到 TStand 对象
  126. VStand stand = new VStand
  127. {
  128. StandId = reader.GetString(reader.GetOrdinal("stand_id")),
  129. CreateTime = reader.GetString(reader.GetOrdinal("create_time")),
  130. StandName = reader.GetString(reader.GetOrdinal("stand_name")),
  131. StandType = reader.GetInt32(reader.GetOrdinal("stand_type")),
  132. StandFile = reader.GetString(reader.GetOrdinal("stand_file")),
  133. StandCount = reader.GetInt32(reader.GetOrdinal("stand_count")),
  134. };
  135. stands.Add(stand);
  136. }
  137. }
  138. return stands;
  139. }
  140. public static bool InsertStandDetail(TStandDetail standDetail)
  141. {
  142. // 插入SQL语句
  143. string sql = @"
  144. INSERT OR IGNORE INTO t_stand_detail (stand_detail_id, create_time, stand_id, src_image, stand_value,
  145. station_id, device_sn,sample_time, num_count, last_unit)
  146. VALUES (@StandDetailId, @CreateTime, @StandId, @SrcImage, @StandValue,
  147. @StationId, @DeviceSn, @SampleTime, @NumCount, @LastUnit);";
  148. // 定义参数
  149. SQLiteParameter[] parameters = new SQLiteParameter[]
  150. {
  151. new SQLiteParameter("@StandDetailId", standDetail.StandDetailId),
  152. new SQLiteParameter("@CreateTime", standDetail.CreateTime),
  153. new SQLiteParameter("@StandId", standDetail.StandId),
  154. new SQLiteParameter("@SrcImage", standDetail.SrcImage),
  155. new SQLiteParameter("@StandValue", standDetail.StandValue),
  156. new SQLiteParameter("@StationId", standDetail.StationId),
  157. new SQLiteParameter("@DeviceSn",standDetail.DeviceSn),
  158. new SQLiteParameter("@SampleTime",standDetail.SampleTime),
  159. new SQLiteParameter("@NumCount", standDetail.NumCount),
  160. new SQLiteParameter("@LastUnit", standDetail.LastUnit)
  161. };
  162. // 执行插入操作
  163. try
  164. {
  165. int rowsAffected = SQLiteHelper.ExecuteNonQuery(sql, parameters);
  166. return rowsAffected > 0; // 返回是否成功插入
  167. }
  168. catch (Exception ex)
  169. {
  170. // 处理异常
  171. Console.WriteLine(ex.Message);
  172. return false;
  173. }
  174. }
  175. public static bool InsertStandDetails(List<TStandDetail> standDetails)
  176. {
  177. // 插入SQL语句
  178. string sql = @"
  179. INSERT OR IGNORE INTO t_stand_detail (stand_detail_id, create_time, stand_id, src_image, stand_value,
  180. station_id, device_sn, sample_time, num_count, last_unit)
  181. VALUES (@StandDetailId, @CreateTime, @StandId, @SrcImage, @StandValue,
  182. @StationId, ,@DeviceSn, @SampleTime, @NumCount, @LastUnit);";
  183. try
  184. {
  185. // 开始事务
  186. using (SQLiteConnection connection = SQLiteHelper.GetConnection())
  187. {
  188. connection.Open();
  189. using (SQLiteTransaction transaction = connection.BeginTransaction())
  190. {
  191. // 遍历列表插入每一条数据
  192. foreach (var standDetail in standDetails)
  193. {
  194. SQLiteParameter[] parameters = new SQLiteParameter[]
  195. {
  196. new SQLiteParameter("@StandDetailId", standDetail.StandDetailId),
  197. new SQLiteParameter("@CreateTime", standDetail.CreateTime),
  198. new SQLiteParameter("@StandId", standDetail.StandId),
  199. new SQLiteParameter("@SrcImage", standDetail.SrcImage),
  200. new SQLiteParameter("@StandValue", standDetail.StandValue),
  201. new SQLiteParameter("@StationId", standDetail.StationId),
  202. new SQLiteParameter("@DeviceSn",standDetail.DeviceSn),
  203. new SQLiteParameter("@SampleTime",standDetail.SampleTime),
  204. new SQLiteParameter("@NumCount", standDetail.NumCount),
  205. new SQLiteParameter("@LastUnit", standDetail.LastUnit)
  206. };
  207. // 执行插入操作
  208. SQLiteHelper.ExecuteNonQuery(sql, parameters, transaction);
  209. }
  210. // 提交事务
  211. transaction.Commit();
  212. }
  213. }
  214. return true;
  215. }
  216. catch (Exception ex)
  217. {
  218. // 处理异常
  219. Console.WriteLine(ex.Message);
  220. return false;
  221. }
  222. }
  223. //同时插入t_stand与t_stand_detail表(在一个事务中)
  224. public static bool InsertStandAndDetails(TStand stand, List<TStandDetail> standDetails)
  225. {
  226. try
  227. {
  228. using (SQLiteConnection connection = SQLiteHelper.GetConnection())
  229. {
  230. connection.Open();
  231. using (SQLiteTransaction transaction = connection.BeginTransaction())
  232. {
  233. // 构建插入的 SQL 语句
  234. string standSql = "INSERT INTO t_stand (stand_id, create_time, stand_name, stand_type, stand_file) " +
  235. "VALUES (@StandId, @CreateTime, @StandName,@StandType,@StandFile)";
  236. SQLiteParameter[] standParams = new SQLiteParameter[]
  237. {
  238. new SQLiteParameter("@StandId", stand.StandId),
  239. new SQLiteParameter("@CreateTime", stand.CreateTime),
  240. new SQLiteParameter("@StandName", stand.StandName),
  241. new SQLiteParameter("@StandType", stand.StandType),
  242. new SQLiteParameter("@StandFile", stand.StandFile)
  243. };
  244. SQLiteHelper.ExecuteNonQuery(standSql, standParams, transaction);
  245. // 插入 t_patch_detail 表
  246. foreach (var standDetail in standDetails)
  247. {
  248. string detailSql = @"
  249. INSERT OR IGNORE INTO t_stand_detail (stand_detail_id, create_time, stand_id, src_image, stand_value,
  250. station_id, device_sn,sample_time, num_count, last_unit)
  251. VALUES (@StandDetailId, @CreateTime, @StandId, @SrcImage, @StandValue,
  252. @StationId, @DeviceSn ,@SampleTime, @NumCount, @LastUnit);";
  253. SQLiteParameter[] detailParams = new SQLiteParameter[]
  254. {
  255. new SQLiteParameter("@StandDetailId", standDetail.StandDetailId),
  256. new SQLiteParameter("@CreateTime", standDetail.CreateTime),
  257. new SQLiteParameter("@StandId", standDetail.StandId),
  258. new SQLiteParameter("@SrcImage", standDetail.SrcImage),
  259. new SQLiteParameter("@StandValue", standDetail.StandValue),
  260. new SQLiteParameter("@StationId", standDetail.StationId),
  261. new SQLiteParameter("@DeviceSn", standDetail.DeviceSn),
  262. new SQLiteParameter("@SampleTime",standDetail.SampleTime),
  263. new SQLiteParameter("@NumCount", standDetail.NumCount),
  264. new SQLiteParameter("@LastUnit", standDetail.LastUnit),
  265. };
  266. SQLiteHelper.ExecuteNonQuery(detailSql, detailParams, transaction);
  267. }
  268. // 提交事务
  269. transaction.Commit();
  270. return true;
  271. }
  272. }
  273. }
  274. catch (Exception ex)
  275. {
  276. //Console.WriteLine($"插入数据失败:{ex.Message}");
  277. throw new Exception($"插入数据失败:{ex.Message}");
  278. //return false;
  279. }
  280. }
  281. public static Tuple<int,int, List<TStandDetail>> GetPagedStandDetails(int pageNumber, int pageSize,string standId)
  282. {
  283. // 计算偏移量
  284. int offset = (pageNumber - 1) * pageSize;
  285. // 分页查询SQL
  286. string sql = @"
  287. SELECT stand_detail_id, create_time, stand_id, src_image, stand_value, station_id, device_sn, sample_time, num_count, last_unit
  288. FROM t_stand_detail WHERE stand_id=@StandId
  289. ORDER BY create_time DESC -- 根据需要修改排序字段
  290. LIMIT @PageSize OFFSET @Offset;";
  291. // 定义参数
  292. SQLiteParameter[] parameters = new SQLiteParameter[]
  293. {
  294. new SQLiteParameter("@StandId",standId),
  295. new SQLiteParameter("@PageSize", pageSize),
  296. new SQLiteParameter("@Offset", offset)
  297. };
  298. // 执行查询并读取数据
  299. List<TStandDetail> standDetails = new List<TStandDetail>();
  300. using (SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql, parameters))
  301. {
  302. while (reader.Read())
  303. {
  304. // 将查询结果映射到 TStandDetail 对象
  305. TStandDetail standDetail = new TStandDetail
  306. {
  307. StandDetailId = reader.GetString(reader.GetOrdinal("stand_detail_id")),
  308. CreateTime = reader.GetString(reader.GetOrdinal("create_time")),
  309. StandId = reader.GetString(reader.GetOrdinal("stand_id")),
  310. SrcImage = reader.GetString(reader.GetOrdinal("src_image")),
  311. StandValue = reader.GetString(reader.GetOrdinal("stand_value")),
  312. StationId = reader.GetString(reader.GetOrdinal("station_id")),
  313. DeviceSn = reader.GetString(reader.GetOrdinal("device_sn")),
  314. SampleTime = reader.GetString(reader.GetOrdinal("sample_time")),
  315. NumCount = reader.GetInt32(reader.GetOrdinal("num_count")),
  316. LastUnit = reader.GetInt32(reader.GetOrdinal("last_unit"))
  317. };
  318. standDetails.Add(standDetail);
  319. }
  320. }
  321. // 获取总记录数,用于计算总页数
  322. //string countSql = "SELECT COUNT(*) FROM t_stand_detail";
  323. //int totalRecords = Convert.ToInt32(SQLiteHelper.GetSingle(countSql));
  324. string countSql = "SELECT COUNT(*) FROM t_stand_detail WHERE stand_id = @StandId";
  325. int totalRecords = Convert.ToInt32(SQLiteHelper.GetSingle(countSql, new SQLiteParameter("@StandId", standId)));
  326. // 计算总页数
  327. int totalPages = (int)Math.Ceiling((double)totalRecords / pageSize);
  328. // 返回总页数和分页结果
  329. return Tuple.Create(totalRecords,totalPages, standDetails);
  330. }
  331. public static List<TStandDetail> GetAllStandDetails(string standId)
  332. {
  333. // 分页查询 SQL
  334. string sql = @"
  335. SELECT stand_detail_id, create_time, stand_id, src_image, stand_value, station_id, device_sn, sample_time, num_count, last_unit
  336. FROM t_stand_detail WHERE stand_id = @StandId
  337. ORDER BY create_time DESC";
  338. // 定义参数
  339. SQLiteParameter[] parameters = new SQLiteParameter[]
  340. {
  341. new SQLiteParameter("@StandId", standId)
  342. };
  343. // 执行查询并读取数据
  344. List<TStandDetail> standDetails = new List<TStandDetail>();
  345. try
  346. {
  347. using (SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql, parameters))
  348. {
  349. while (reader.Read())
  350. {
  351. // 将查询结果映射到 TStandDetail 对象
  352. var standDetail = new TStandDetail
  353. {
  354. StandDetailId = reader.GetString(reader.GetOrdinal("stand_detail_id")),
  355. CreateTime = reader.GetString(reader.GetOrdinal("create_time")),
  356. StandId = reader.GetString(reader.GetOrdinal("stand_id")),
  357. SrcImage = reader.GetString(reader.GetOrdinal("src_image")),
  358. StandValue = reader.GetString(reader.GetOrdinal("stand_value")),
  359. StationId = reader.GetString(reader.GetOrdinal("station_id")),
  360. DeviceSn = reader.GetString(reader.GetOrdinal("device_sn")),
  361. SampleTime = reader.GetString(reader.GetOrdinal("sample_time")),
  362. NumCount = reader.GetInt32(reader.GetOrdinal("num_count")),
  363. LastUnit = reader.GetInt32(reader.GetOrdinal("last_unit"))
  364. };
  365. standDetails.Add(standDetail);
  366. }
  367. }
  368. }
  369. catch (Exception ex)
  370. {
  371. // 根据需要记录日志或抛出异常
  372. throw new Exception($"查询 StandDetails 时发生错误:{ex.Message}", ex);
  373. }
  374. return standDetails;
  375. }
  376. public static async Task<List<TStandDetail>> GetAllStandDetailsAsync(string standId)
  377. {
  378. // 分页查询 SQL
  379. string sql = @"
  380. SELECT stand_detail_id, create_time, stand_id, src_image, stand_value, station_id, device_sn, sample_id, num_count, last_unit
  381. FROM t_stand_detail WHERE stand_id = @StandId
  382. ORDER BY create_time DESC";
  383. // 定义参数
  384. SQLiteParameter[] parameters = new SQLiteParameter[]
  385. {
  386. new SQLiteParameter("@StandId", standId)
  387. };
  388. // 执行查询并读取数据
  389. List<TStandDetail> standDetails = new List<TStandDetail>();
  390. try
  391. {
  392. using (SQLiteDataReader reader = await SQLiteHelper.ExecuteReaderAsync(sql, parameters))
  393. {
  394. while (await reader.ReadAsync())
  395. {
  396. // 将查询结果映射到 TStandDetail 对象
  397. var standDetail = new TStandDetail
  398. {
  399. StandDetailId = reader.GetString(reader.GetOrdinal("stand_detail_id")),
  400. CreateTime = reader.GetString(reader.GetOrdinal("create_time")),
  401. StandId = reader.GetString(reader.GetOrdinal("stand_id")),
  402. SrcImage = reader.GetString(reader.GetOrdinal("src_image")),
  403. StandValue = reader.GetString(reader.GetOrdinal("stand_value")),
  404. StationId = reader.GetString(reader.GetOrdinal("station_id")),
  405. DeviceSn = reader.GetString(reader.GetOrdinal("device_sn")),
  406. SampleTime = reader.GetString(reader.GetOrdinal("sample_time")),
  407. NumCount = reader.GetInt32(reader.GetOrdinal("num_count")),
  408. LastUnit = reader.GetInt32(reader.GetOrdinal("last_unit"))
  409. };
  410. standDetails.Add(standDetail);
  411. }
  412. }
  413. }
  414. catch (Exception ex)
  415. {
  416. // 根据需要记录日志或抛出异常
  417. throw new Exception($"查询 StandDetails 时发生错误:{ex.Message}", ex);
  418. }
  419. return standDetails;
  420. }
  421. // 使用 SQLiteHelper 从 v_stand 中根据 stand_id 查询记录
  422. public static VStand GetVStandById(string standId)
  423. {
  424. VStand vStand = null;
  425. // 查询 SQL
  426. string query = @"
  427. SELECT
  428. stand_id,
  429. create_time,
  430. stand_name,
  431. stand_type,
  432. stand_file,
  433. stand_count
  434. FROM v_stand
  435. WHERE stand_id = @StandId;";
  436. // 构造参数
  437. SQLiteParameter[] parameters = new SQLiteParameter[]
  438. {
  439. new SQLiteParameter("@StandId", standId)
  440. };
  441. // 执行查询
  442. using (SQLiteDataReader reader = SQLiteHelper.ExecuteReader(query, parameters))
  443. {
  444. if (reader.Read())
  445. {
  446. vStand = new VStand
  447. {
  448. StandId = reader["stand_id"].ToString(),
  449. CreateTime = reader["create_time"].ToString(),
  450. StandName = reader["stand_name"].ToString(),
  451. StandType = reader["stand_type"] == DBNull.Value ? 0 : Convert.ToInt32(reader["stand_type"]),
  452. StandFile = reader["stand_file"].ToString(),
  453. StandCount = reader["stand_count"] == DBNull.Value ? 0 : Convert.ToInt32(reader["stand_count"])
  454. };
  455. }
  456. }
  457. return vStand;
  458. }
  459. //t_stand_detail是否有重复数据
  460. public static bool IsSrcImageExitInStand(string filePath,string standId)
  461. {
  462. string query = "SELECT COUNT(*) FROM t_stand_detail WHERE src_image = @fileName";
  463. SQLiteParameter[] parameters = new SQLiteParameter[]
  464. {
  465. new SQLiteParameter("@fileName", filePath)
  466. };
  467. int result = Convert.ToInt32(SQLiteHelper.ExecuteSql(query, parameters));
  468. return result > 0;
  469. }
  470. public static bool UpdateStandDetailStandValue(string srcImage, string standValue)
  471. {
  472. string sql = @"
  473. UPDATE t_stand_detail
  474. SET stand_value = @StandValue
  475. WHERE src_image = @SrcImage;";
  476. // 创建 SQL 参数
  477. SQLiteParameter[] parameters = new SQLiteParameter[]
  478. {
  479. new SQLiteParameter("@SrcImage", srcImage),
  480. new SQLiteParameter("@StandValue", standValue)
  481. };
  482. // 执行更新操作并返回受影响的行数
  483. return SQLiteHelper.ExecuteNonQuery(sql, parameters) > 0;
  484. }
  485. public static bool UpdateStandName(string standId, string standName)
  486. {
  487. string sql = @"
  488. UPDATE t_stand
  489. SET stand_name = @StandName
  490. WHERE stand_id = @StandId;";
  491. // 创建 SQL 参数
  492. SQLiteParameter[] parameters = new SQLiteParameter[]
  493. {
  494. new SQLiteParameter("@StandName", standName),
  495. new SQLiteParameter("@StandId", standId)
  496. };
  497. // 执行更新操作并返回受影响的行数
  498. return SQLiteHelper.ExecuteNonQuery(sql, parameters) > 0;
  499. }
  500. /// <summary>
  501. /// 根据 stand_detail_id 删除指定的记录
  502. /// </summary>
  503. /// <param name="standDetailId">要删除的记录的 ID</param>
  504. /// <returns>删除成功返回 true,否则返回 false</returns>
  505. public static bool DeleteTStandDetailById(string standDetailId)
  506. {
  507. // 构建删除的 SQL 语句
  508. string sql = "DELETE FROM t_stand_detail WHERE stand_detail_id = @StandDetailId";
  509. // 创建参数数组
  510. SQLiteParameter[] parameters = new SQLiteParameter[]
  511. {
  512. new SQLiteParameter("@StandDetailId", standDetailId)
  513. };
  514. try
  515. {
  516. // 调用 SQLiteHelper 执行删除操作
  517. int rowsAffected = SQLiteHelper.ExecuteSql(sql, parameters);
  518. // 如果删除成功,返回 true,否则返回 false
  519. return rowsAffected > 0;
  520. }
  521. catch (Exception ex)
  522. {
  523. // 处理异常(如果有的话)
  524. Console.WriteLine("Error deleting data: " + ex.Message);
  525. return false;
  526. }
  527. }
  528. public static List<StationInfo> GetUniqueStationInfo(string standId)
  529. {
  530. try
  531. {
  532. string sql = @"
  533. SELECT
  534. stand_id,
  535. station_id,
  536. device_sn,
  537. MAX(num_count) AS num_count,
  538. MAX(last_unit) AS last_unit
  539. FROM
  540. t_stand_detail
  541. WHERE
  542. stand_id = @StandId
  543. GROUP BY
  544. station_id,device_sn
  545. ORDER BY
  546. station_id ASC;";
  547. SQLiteParameter[] parameters = new SQLiteParameter[]
  548. {
  549. new SQLiteParameter("@StandId", standId)
  550. };
  551. List<StationInfo> result = new List<StationInfo>();
  552. using (SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql, parameters))
  553. {
  554. while (reader.Read())
  555. {
  556. StationInfo station = new StationInfo
  557. {
  558. StandId = reader.GetString(reader.GetOrdinal("stand_id")),
  559. StationId = reader.GetString(reader.GetOrdinal("station_id")),
  560. DeviceSn = reader.GetString(reader.GetOrdinal("device_sn")),
  561. NumCount = reader.GetInt32(reader.GetOrdinal("num_count")),
  562. LastUnit = reader.GetInt32(reader.GetOrdinal("last_unit"))
  563. };
  564. result.Add(station);
  565. }
  566. }
  567. return result;
  568. }
  569. catch (Exception ex)
  570. {
  571. Console.WriteLine($"查询失败:{ex.Message}");
  572. return null;
  573. }
  574. }
  575. //----------------------------------------------------------------------
  576. }
  577. }