DBComp.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. //识别结果比较表数据库表类
  11. public class DBComp
  12. {
  13. public static bool InsertTComp(TComp tComp)
  14. {
  15. // 构建插入的 SQL 语句
  16. string sql = "INSERT INTO t_comp (comp_id, create_time, n_patch_id,o_patch_id,stand_id) " +
  17. "VALUES (@CompId, @CreateTime, @NPatchId, @OPatchId, @StandId)";
  18. // 创建参数数组
  19. SQLiteParameter[] parameters = new SQLiteParameter[]
  20. {
  21. new SQLiteParameter("@CompId", tComp.CompId),
  22. new SQLiteParameter("@CreateTime", tComp.CreateTime),
  23. new SQLiteParameter("@NPatchId", tComp.NPatchId),
  24. new SQLiteParameter("@OPatchId", tComp.OPatchId),
  25. new SQLiteParameter("@StandId", tComp.StandId),
  26. };
  27. try
  28. {
  29. // 调用 SQLiteHelper 执行插入操作
  30. int rowsAffected = SQLiteHelper.ExecuteSql(sql, parameters);
  31. // 如果插入成功,返回 true,否则返回 false
  32. return rowsAffected > 0;
  33. }
  34. catch (Exception ex)
  35. {
  36. // 处理异常(如果有的话)
  37. Console.WriteLine("Error inserting data: " + ex.Message);
  38. return false;
  39. }
  40. }
  41. //删除比较表记录
  42. public static bool DelteTComp(string compId)
  43. {
  44. string sql = "DELETE FROM t_comp WHERE comp_id =@CompId";
  45. SQLiteParameter[] parameters = new SQLiteParameter[]
  46. {
  47. new SQLiteParameter("@CompId",compId)
  48. };
  49. try
  50. {
  51. //执行 SQL 删除操作
  52. int rowsAffected = SQLiteHelper.ExecuteSql(sql, parameters);
  53. //返回是否成功删除
  54. return rowsAffected > 0;
  55. }
  56. catch (Exception ex)
  57. {
  58. //处理异常
  59. Console.WriteLine("Error deleting data: " + ex.Message);
  60. return false;
  61. }
  62. }
  63. public static VComp GetVCompById(string compId)
  64. {
  65. VComp vComp = null;
  66. // 查询 SQL,从 v_comp 视图中获取数据
  67. string query = @"
  68. SELECT
  69. comp_id,
  70. create_time,
  71. n_patch_id,
  72. n_patch_time,
  73. n_patch_name,
  74. n_patch_detail_count,
  75. n_patch_equal_count,
  76. o_patch_id,
  77. o_patch_time,
  78. o_patch_name,
  79. o_patch_detail_count,
  80. o_patch_equal_count,
  81. stand_id,
  82. stand_name,
  83. stand_count
  84. FROM v_comp
  85. WHERE comp_id = @CompId;";
  86. // 构造参数
  87. SQLiteParameter[] parameters = new SQLiteParameter[]
  88. {
  89. new SQLiteParameter("@CompId", compId)
  90. };
  91. // 执行查询并映射到实体
  92. using (SQLiteDataReader reader = SQLiteHelper.ExecuteReader(query, parameters))
  93. {
  94. if (reader.Read())
  95. {
  96. vComp = new VComp
  97. {
  98. CompId = reader["comp_id"].ToString(),
  99. CreateTime = reader["create_time"].ToString(),
  100. NPatchId = reader["n_patch_id"].ToString(),
  101. NPatchTime = reader["n_patch_time"].ToString(),
  102. NPatchName = reader["n_patch_name"].ToString(),
  103. NPatchDetailCount = reader["n_patch_detail_count"] == DBNull.Value ? 0 : Convert.ToInt32(reader["n_patch_detail_count"]),
  104. NPatchEqualCount = reader["n_patch_equal_count"] == DBNull.Value ? 0 : Convert.ToInt32(reader["n_patch_equal_count"]),
  105. OPatchId = reader["o_patch_id"].ToString(),
  106. OPatchTime = reader["o_patch_time"].ToString(),
  107. OPatchName = reader["o_patch_name"].ToString(),
  108. OPatchDetailCount = reader["o_patch_detail_count"] == DBNull.Value ? 0 : Convert.ToInt32(reader["o_patch_detail_count"]),
  109. OPatchEqualCount = reader["o_patch_equal_count"] == DBNull.Value ? 0 : Convert.ToInt32(reader["o_patch_equal_count"]),
  110. StandId = reader["stand_id"].ToString(),
  111. StandName = reader["stand_name"].ToString(),
  112. StandCount = reader["stand_count"] == DBNull.Value ? 0 : Convert.ToInt32(reader["stand_count"])
  113. };
  114. }
  115. }
  116. return vComp;
  117. }
  118. public static Tuple<int,int,List<VComp>> GetPagedVComps(int pageNumber,int pageSize)
  119. {
  120. // 计算 OFFSET 值
  121. int offset = (pageNumber - 1) * pageSize;
  122. // 定义 SQL 查询语句,带有分页
  123. string sql = @"
  124. SELECT
  125. comp_id,
  126. create_time,
  127. n_patch_id,
  128. n_patch_time,
  129. n_patch_name,
  130. n_patch_detail_count,
  131. n_patch_equal_count,
  132. o_patch_id,
  133. o_patch_time,
  134. o_patch_name,
  135. o_patch_detail_count,
  136. o_patch_equal_count,
  137. stand_id,
  138. stand_name,
  139. stand_count
  140. FROM v_comp ORDER BY create_time DESC
  141. LIMIT @PageSize OFFSET @Offset;";
  142. // 创建 SQL 参数
  143. SQLiteParameter[] parameters = new SQLiteParameter[]
  144. {
  145. new SQLiteParameter("@PageSize", pageSize),
  146. new SQLiteParameter("@Offset", offset)
  147. };
  148. // 执行查询并获取结果
  149. List<VComp> comps = new List<VComp>();
  150. using (SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql, parameters))
  151. {
  152. while (reader.Read())
  153. {
  154. // 将查询结果映射到 TStand 对象
  155. VComp comp = new VComp
  156. {
  157. CompId = reader["comp_id"].ToString(),
  158. CreateTime = reader["create_time"].ToString(),
  159. NPatchId = reader["n_patch_id"].ToString(),
  160. NPatchTime = reader["n_patch_time"].ToString(),
  161. NPatchName = reader["n_patch_name"].ToString(),
  162. NPatchDetailCount = reader["n_patch_detail_count"] == DBNull.Value ? 0 : Convert.ToInt32(reader["n_patch_detail_count"]),
  163. NPatchEqualCount = reader["n_patch_equal_count"] == DBNull.Value ? 0 : Convert.ToInt32(reader["n_patch_equal_count"]),
  164. OPatchId = reader["o_patch_id"].ToString(),
  165. OPatchTime = reader["o_patch_time"].ToString(),
  166. OPatchName = reader["o_patch_name"].ToString(),
  167. OPatchDetailCount = reader["o_patch_detail_count"] == DBNull.Value ? 0 : Convert.ToInt32(reader["o_patch_detail_count"]),
  168. OPatchEqualCount = reader["o_patch_equal_count"] == DBNull.Value ? 0 : Convert.ToInt32(reader["o_patch_equal_count"]),
  169. StandId = reader["stand_id"].ToString(),
  170. StandName = reader["stand_name"].ToString(),
  171. StandCount = reader["stand_count"] == DBNull.Value ? 0 : Convert.ToInt32(reader["stand_count"])
  172. };
  173. comps.Add(comp);
  174. }
  175. }
  176. //获取总记录数,用于计算总页数
  177. string countSql = "SELECT COUNT(*) FROM v_comp";
  178. int totalRecords = Convert.ToInt32(SQLiteHelper.GetSingle(countSql));
  179. // 计算总页数
  180. int totalPages = (int)Math.Ceiling((double)totalRecords / pageSize);
  181. // 返回元组,包含总页数和结果列表
  182. return Tuple.Create(totalRecords, totalPages, comps);
  183. }
  184. //获取数据
  185. public static Tuple<int,int,List<VCompDetail>> GetPagedCompDetails(CompFindModel findModel)
  186. {
  187. // 计算偏移量
  188. int offset = (findModel.PageNumber - 1) * findModel.PageSize;
  189. // 动态构造 WHERE 子句
  190. StringBuilder whereClause = new StringBuilder("WHERE comp_id = @CompId");
  191. List<SQLiteParameter> parameters = new List<SQLiteParameter>
  192. {
  193. new SQLiteParameter("@CompId", findModel.CompId),
  194. new SQLiteParameter("@PageSize", findModel.PageSize),
  195. new SQLiteParameter("@Offset", offset)
  196. };
  197. if(findModel.CompFindType == Patch.CompFindType.NERROR_ORIGHT)
  198. {
  199. //新错旧对
  200. whereClause.Append(" AND equal_flag1 != 1 AND equal_flag2 = 1");
  201. }
  202. else if(findModel.CompFindType == Patch.CompFindType.NRIGHT_OERROR)
  203. {
  204. //新对旧错
  205. whereClause.Append(" AND equal_flag1 = 1 AND equal_flag2 != 1");
  206. }
  207. else if(findModel.CompFindType == Patch.CompFindType.NRIGHT_ORIGHT)
  208. {
  209. //新对旧对
  210. whereClause.Append(" AND equal_flag1 = 1 AND equal_flag2 = 1");
  211. }
  212. else if(findModel.CompFindType == Patch.CompFindType.NERROR_OERROR)
  213. {
  214. //新错旧错
  215. whereClause.Append(" AND equal_flag1 != 1 AND equal_flag2 != 1");
  216. }
  217. // 定义 SQL 查询语句,从视图中分页查询
  218. string sql = $@"
  219. SELECT
  220. comp_id,
  221. create_time,
  222. n_patch_id,
  223. o_patch_id,
  224. stand_id,
  225. patch_detail_id1,
  226. create_time1,
  227. stand_detail_id1,
  228. stand_value1,
  229. src_image1,
  230. run_flag1,
  231. run_time1,
  232. dst_image1,
  233. meter_type1,
  234. digit_count1,
  235. pointer_count1,
  236. last_unit1,
  237. result_type1,
  238. raw_value1,
  239. final_value1,
  240. equal_flag1,
  241. ai_ver1,
  242. debug_info1,
  243. log_path1,
  244. memo1,
  245. patch_detail_id2,
  246. create_time2,
  247. stand_detail_id2,
  248. stand_value2,
  249. src_image2,
  250. run_flag2,
  251. run_time2,
  252. dst_image2,
  253. meter_type2,
  254. digit_count2,
  255. pointer_count2,
  256. last_unit2,
  257. result_type2,
  258. raw_value2,
  259. final_value2,
  260. equal_flag2,
  261. ai_ver2,
  262. debug_info2,
  263. log_path2,
  264. memo2
  265. FROM v_comp_detail
  266. {whereClause}
  267. ORDER BY create_time ASC
  268. LIMIT @PageSize OFFSET @Offset;";
  269. // 执行查询并读取数据
  270. List<VCompDetail> compDetails = new List<VCompDetail>();
  271. using (SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql, parameters.ToArray()))
  272. {
  273. while (reader.Read())
  274. {
  275. VCompDetail compDetail = new VCompDetail
  276. {
  277. CompId = reader["comp_id"].ToString(),
  278. CreateTime = reader["create_time"].ToString(),
  279. NPatchId = reader["n_patch_id"].ToString(),
  280. OPatchId = reader["o_patch_id"].ToString(),
  281. StandId = reader["stand_id"].ToString(),
  282. PatchDetail1 = new TPatchDetail
  283. {
  284. //PatchId = reader["n_patch_id"].ToString(),
  285. PatchDetailId = reader["patch_detail_id1"].ToString(),
  286. CreateTime = reader["create_time1"].ToString(),
  287. StandDetailId = reader["stand_detail_id1"].ToString(),
  288. StandValue = reader["stand_value1"].ToString(),
  289. SrcImage = reader["src_image1"].ToString(),
  290. RunFlag = Convert.ToInt32(reader["run_flag1"]),
  291. RunTime = reader["run_time1"].ToString(),
  292. DstImage = reader["dst_image1"].ToString(),
  293. MeterType = Convert.ToInt32(reader["meter_type1"]),
  294. DigitCount = Convert.ToInt32(reader["digit_count1"]),
  295. PointerCount = Convert.ToInt32(reader["pointer_count1"]),
  296. //LastUnit = reader["last_unit1"].ToString(),
  297. ResultType = Convert.ToInt32(reader["result_type1"]),
  298. RawValue = Convert.ToInt64(reader["raw_value1"]),
  299. FinalValue = Convert.ToInt64(reader["final_value1"]),
  300. EqualFlag = Convert.ToInt32(reader["equal_flag1"]),
  301. AiVer = reader["ai_ver1"].ToString(),
  302. DebugInfo = reader["debug_info1"].ToString(),
  303. LogPath = reader["log_path1"].ToString(),
  304. Memo = reader["memo1"].ToString()
  305. },
  306. PatchDetail2 = new TPatchDetail
  307. {
  308. PatchDetailId = reader["patch_detail_id2"].ToString(),
  309. CreateTime = reader["create_time2"].ToString(),
  310. StandDetailId = reader["stand_detail_id2"].ToString(),
  311. StandValue = reader["stand_value2"].ToString(),
  312. SrcImage = reader["src_image2"].ToString(),
  313. RunFlag = Convert.ToInt32(reader["run_flag2"]),
  314. RunTime = reader["run_time2"].ToString(),
  315. DstImage = reader["dst_image2"].ToString(),
  316. MeterType = Convert.ToInt32(reader["meter_type2"]),
  317. DigitCount = Convert.ToInt32(reader["digit_count2"]),
  318. PointerCount = Convert.ToInt32(reader["pointer_count2"]),
  319. //LastUnit = reader["last_unit2"].ToString(),
  320. ResultType = Convert.ToInt32(reader["result_type2"]),
  321. RawValue = Convert.ToInt64(reader["raw_value2"]),
  322. FinalValue = Convert.ToInt64(reader["final_value2"]),
  323. EqualFlag = Convert.ToInt32(reader["equal_flag2"]),
  324. AiVer = reader["ai_ver2"].ToString(),
  325. DebugInfo = reader["debug_info2"].ToString(),
  326. LogPath = reader["log_path2"].ToString(),
  327. Memo = reader["memo2"].ToString()
  328. }
  329. };
  330. compDetails.Add(compDetail);
  331. }
  332. }
  333. // 获取总记录数,用于计算总页数
  334. string countSql = "SELECT COUNT(*) FROM v_comp_detail " + whereClause;
  335. int totalRecords = Convert.ToInt32(SQLiteHelper.GetSingle(countSql, parameters.ToArray()));
  336. // 计算总页数
  337. int totalPages = (int)Math.Ceiling((double)totalRecords / findModel.PageSize);
  338. // 返回总记录数、总页数和分页结果
  339. return Tuple.Create(totalRecords, totalPages, compDetails);
  340. }
  341. //-------------------------------------------------------------------------------
  342. }
  343. }