DBStation.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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, num_region, ht_region, last_num_unit, last_ind_unit, " +
  17. "last_value, last_time, create_time, stand_id) " +
  18. "VALUES (@StationId, @StationName, @DeviceSn, @MeterType, @BrightVal, @FlowRate, " +
  19. "@DialRegion, @NumCount, @IndCount, @NumRegion, @HtRegion, @LastNumUnit, @LastIndUnit, " +
  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("@NumRegion", tStation.NumRegion),
  35. new SQLiteParameter("@HtRegion", tStation.HtRegion),
  36. new SQLiteParameter("@LastNumUnit", tStation.LastNumUnit),
  37. new SQLiteParameter("@LastIndUnit", tStation.LastIndUnit),
  38. new SQLiteParameter("@LastValue", tStation.LastValue),
  39. new SQLiteParameter("@LastTime", tStation.LastTime),
  40. new SQLiteParameter("@CreateTime", tStation.CreateTime),
  41. new SQLiteParameter("@StandId", tStation.StandId),
  42. };
  43. try
  44. {
  45. // 调用 SQLiteHelper 执行插入操作
  46. int rowsAffected = SQLiteHelper.ExecuteSql(sql, parameters);
  47. // 如果插入成功,返回 true,否则返回 false
  48. return rowsAffected > 0;
  49. }
  50. catch (Exception ex)
  51. {
  52. // 处理异常(如果有的话)
  53. Console.WriteLine("Error inserting data: " + ex.Message);
  54. return false;
  55. }
  56. }
  57. public static bool InsertTStations(List<StationInfo> stationInfos)
  58. {
  59. string sql = @"
  60. INSERT INTO t_station (id, stand_id,create_time, station_id, device_sn, num_count, last_num_unit)
  61. VALUES (@Id, @StandId, @CreateTime, @StationId, @DeviceSn, @NumCount, @LastNumUnit);";
  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("@LastNumUnit", 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, num_region, ht_region, last_num_unit, last_ind_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_name ASC LIMIT @PageSize OFFSET @Offset;";
  116. // 创建 SQL 参数
  117. //SQLiteParameter[] parameters = new SQLiteParameter[]
  118. //{
  119. // new SQLiteParameter("@PageSize", pageSize),
  120. // new SQLiteParameter("@Offset", offset)
  121. //};
  122. // 创建 SQL 参数
  123. List<SQLiteParameter> parameters = new List<SQLiteParameter>
  124. {
  125. new SQLiteParameter("@PageSize", pageSize),
  126. new SQLiteParameter("@Offset", offset)
  127. };
  128. // 如果传入了 stationId,添加到参数中
  129. if (!string.IsNullOrEmpty(stationId))
  130. {
  131. parameters.Add(new SQLiteParameter("@StationId", "%" + stationId + "%"));
  132. }
  133. // 执行查询并获取结果
  134. List<TStation> stations = new List<TStation>();
  135. using (SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql, parameters.ToArray()))
  136. {
  137. while (reader.Read())
  138. {
  139. // 将查询结果映射到 TStation 对象
  140. TStation station = new TStation
  141. {
  142. Id = reader.GetString(reader.GetOrdinal("id")),
  143. StationId = reader.GetString(reader.GetOrdinal("station_id")),
  144. StationName = reader.GetString(reader.GetOrdinal("station_name")),
  145. DeviceSn = reader.GetString(reader.GetOrdinal("device_sn")),
  146. MeterType = reader.GetInt32(reader.GetOrdinal("meter_type")),
  147. BrightVal = reader.GetDouble(reader.GetOrdinal("bright_val")),
  148. FlowRate = reader.GetInt32(reader.GetOrdinal("flow_rate")),
  149. DialRegion = reader.GetString(reader.GetOrdinal("dial_region")),
  150. NumCount = reader.GetInt32(reader.GetOrdinal("num_count")),
  151. IndCount = reader.GetInt32(reader.GetOrdinal("ind_count")),
  152. NumRegion = reader.GetString(reader.GetOrdinal("num_region")),
  153. HtRegion = reader.GetString(reader.GetOrdinal("ht_region")),
  154. LastNumUnit = reader.GetDouble(reader.GetOrdinal("last_num_unit")),
  155. LastIndUnit = reader.GetDouble(reader.GetOrdinal("last_ind_unit")),
  156. LastValue = reader.GetDouble(reader.GetOrdinal("last_value")),
  157. LastTime = reader.GetString(reader.GetOrdinal("last_time")),
  158. CreateTime = reader.GetString(reader.GetOrdinal("create_time")),
  159. StandId = reader.GetString(reader.GetOrdinal("stand_id"))
  160. //Id = reader.IsDBNull(reader.GetOrdinal("id")) ? null : reader.GetString(reader.GetOrdinal("id")),
  161. //StationId = reader.IsDBNull(reader.GetOrdinal("station_id")) ? null : reader.GetString(reader.GetOrdinal("station_id")),
  162. //StationName = reader.IsDBNull(reader.GetOrdinal("station_name")) ? null : reader.GetString(reader.GetOrdinal("station_name")),
  163. //DeviceSn = reader.IsDBNull(reader.GetOrdinal("device_sn")) ? null : reader.GetString(reader.GetOrdinal("device_sn")),
  164. //MeterType = reader.IsDBNull(reader.GetOrdinal("meter_type")) ? 0 : reader.GetInt32(reader.GetOrdinal("meter_type")),
  165. //BrightVal = reader.IsDBNull(reader.GetOrdinal("bright_val")) ? 0.0 : reader.GetDouble(reader.GetOrdinal("bright_val")),
  166. //FlowRate = reader.IsDBNull(reader.GetOrdinal("flow_rate")) ? 0 : reader.GetInt32(reader.GetOrdinal("flow_rate")),
  167. //DialRegion = reader.IsDBNull(reader.GetOrdinal("dial_region")) ? null : reader.GetString(reader.GetOrdinal("dial_region")),
  168. //NumCount = reader.IsDBNull(reader.GetOrdinal("num_count")) ? 0 : reader.GetInt32(reader.GetOrdinal("num_count")),
  169. //IndCount = reader.IsDBNull(reader.GetOrdinal("ind_count")) ? 0 : reader.GetInt32(reader.GetOrdinal("ind_count")),
  170. //NumRegion = reader.IsDBNull(reader.GetOrdinal("num_region")) ? null : reader.GetString(reader.GetOrdinal("num_region")),
  171. //HtRegion = reader.IsDBNull(reader.GetOrdinal("ht_region")) ? null : reader.GetString(reader.GetOrdinal("ht_region")),
  172. //LastNumUnit = reader.IsDBNull(reader.GetOrdinal("last_num_unit")) ? 0.0 : reader.GetDouble(reader.GetOrdinal("last_num_unit")),
  173. //LastIndUnit = reader.IsDBNull(reader.GetOrdinal("last_ind_unit")) ? 0.0 : reader.GetDouble(reader.GetOrdinal("last_ind_unit")),
  174. //LastValue = reader.IsDBNull(reader.GetOrdinal("last_value")) ? 0.0 : reader.GetDouble(reader.GetOrdinal("last_value")),
  175. //LastTime = reader.IsDBNull(reader.GetOrdinal("last_time")) ? null : reader.GetString(reader.GetOrdinal("last_time")),
  176. //CreateTime = reader.IsDBNull(reader.GetOrdinal("create_time")) ? null : reader.GetString(reader.GetOrdinal("create_time")),
  177. //StandId = reader.IsDBNull(reader.GetOrdinal("stand_id")) ? null : reader.GetString(reader.GetOrdinal("stand_id"))
  178. };
  179. stations.Add(station);
  180. }
  181. }
  182. // 获取总记录数,用于计算总页数
  183. string countSql = "SELECT COUNT(*) FROM t_station";
  184. int totalRecords = Convert.ToInt32(SQLiteHelper.GetSingle(countSql));
  185. // 计算总页数
  186. int totalPages = (int)Math.Ceiling((double)totalRecords / pageSize);
  187. // 返回元组,包含总记录数、总页数和结果列表
  188. return Tuple.Create(totalRecords, totalPages, stations);
  189. }
  190. public static bool DeleteTStation(string id)
  191. {
  192. // 定义删除的 SQL 语句
  193. string sql = "DELETE FROM t_station WHERE id = @Id";
  194. // 创建 SQL 参数
  195. SQLiteParameter[] parameters = new SQLiteParameter[]
  196. {
  197. new SQLiteParameter("@Id", id)
  198. };
  199. try
  200. {
  201. // 调用 SQLiteHelper 执行删除操作
  202. int rowsAffected = SQLiteHelper.ExecuteSql(sql, parameters);
  203. // 如果删除成功,返回 true,否则返回 false
  204. return rowsAffected > 0;
  205. }
  206. catch (Exception ex)
  207. {
  208. // 处理异常(如果有的话)
  209. Console.WriteLine("Error deleting data: " + ex.Message);
  210. return false;
  211. }
  212. }
  213. public static bool UpdateTStation(TStation tStation)
  214. {
  215. // 定义更新的 SQL 语句
  216. string sql = @"
  217. UPDATE t_station
  218. SET
  219. station_name = @StationName,
  220. meter_type = @MeterType,
  221. bright_val = @BrightVal,
  222. flow_rate = @FlowRate,
  223. dial_region = @DialRegion,
  224. num_count = @NumCount,
  225. ind_count = @IndCount,
  226. num_region = @NumRegion,
  227. ht_region = @HtRegion,
  228. last_num_unit = @LastNumUnit,
  229. last_ind_unit = @LastIndUnit,
  230. last_value = @LastValue,
  231. last_time = @LastTime
  232. WHERE id = @Id";
  233. // 创建 SQL 参数
  234. SQLiteParameter[] parameters = new SQLiteParameter[]
  235. {
  236. //new SQLiteParameter("@StationId", tStation.StationId),
  237. new SQLiteParameter("@Id",tStation.Id),
  238. new SQLiteParameter("@StationName", tStation.StationName),
  239. new SQLiteParameter("@MeterType", tStation.MeterType),
  240. new SQLiteParameter("@BrightVal", tStation.BrightVal),
  241. new SQLiteParameter("@FlowRate", tStation.FlowRate),
  242. new SQLiteParameter("@DialRegion", tStation.DialRegion),
  243. new SQLiteParameter("@NumCount", tStation.NumCount),
  244. new SQLiteParameter("@IndCount", tStation.IndCount),
  245. new SQLiteParameter("@NumRegion", tStation.NumRegion),
  246. new SQLiteParameter("@HtRegion", tStation.HtRegion),
  247. new SQLiteParameter("@LastNumUnit", tStation.LastNumUnit),
  248. new SQLiteParameter("@LastIndUnit", tStation.LastIndUnit),
  249. new SQLiteParameter("@LastValue", tStation.LastValue),
  250. new SQLiteParameter("@LastTime", tStation.LastTime)
  251. };
  252. try
  253. {
  254. // 调用 SQLiteHelper 执行更新操作
  255. int rowsAffected = SQLiteHelper.ExecuteSql(sql, parameters);
  256. // 如果更新成功,返回 true,否则返回 false
  257. return rowsAffected > 0;
  258. }
  259. catch (Exception ex)
  260. {
  261. // 处理异常(如果有的话)
  262. Console.WriteLine("Error updating data: " + ex.Message);
  263. return false;
  264. }
  265. }
  266. public static bool ClearTStationTable()
  267. {
  268. // 构建清空表的 SQL 语句
  269. string sql = "DELETE FROM t_station";
  270. try
  271. {
  272. // 调用 SQLiteHelper 执行清空操作
  273. SQLiteHelper.ExecuteSql(sql, null);
  274. // 返回 true 表示操作成功
  275. return true;
  276. }
  277. catch (Exception ex)
  278. {
  279. // 处理异常(如果有的话)
  280. Console.WriteLine("Error clearing table: " + ex.Message);
  281. return false;
  282. }
  283. }
  284. //-----------------------------------------------------
  285. }
  286. }