CNAS取数仪器端升级
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

4 місяці тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. using CnasLocalIDAL;
  2. using CnasSynchronousCommon;
  3. using CnasSynchrousModel;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Data.SQLite;
  8. using System.Linq;
  9. using System.Text;
  10. namespace SyncLocalDAL
  11. {
  12. public class LaboratoryTestDAL: LocalDBbase, ILaboratoryTest
  13. {
  14. //读取数据
  15. public List<LaboratoryTest> GetAllLaboratoryTests()
  16. {
  17. List<LaboratoryTest> laboratoryTests = new List<LaboratoryTest>();
  18. try
  19. {
  20. //构建数据库连接
  21. SQLiteHelper.SetConnectionString(ConnectString);
  22. string strSql = "select * from LaboratoryTest";
  23. SQLiteDataReader reader = SQLiteHelper.ExecuteReader(strSql, new SQLiteParameter[] { });
  24. while (reader.Read())
  25. {
  26. LaboratoryTest laboratoryTest = new LaboratoryTest();
  27. AddLaboratoryTestValue(reader,laboratoryTest);
  28. laboratoryTests.Add(laboratoryTest);
  29. }
  30. reader.Close();
  31. }
  32. catch (Exception ex)
  33. {
  34. AppLog.Error(ex.Message);
  35. throw ex;
  36. }
  37. return laboratoryTests;
  38. }
  39. public List<LaboratoryTest> GetLaboratoryTestsByType(string strType)
  40. {
  41. List<LaboratoryTest> laboratoryTests = new List<LaboratoryTest>();
  42. try
  43. {
  44. //构建数据库连接
  45. SQLiteHelper.SetConnectionString(ConnectString);
  46. string strSql = $"select * from LaboratoryTest where datatype='{strType}'";
  47. SQLiteDataReader reader = SQLiteHelper.ExecuteReader(strSql, new SQLiteParameter[] { });
  48. while (reader.Read())
  49. {
  50. LaboratoryTest laboratoryTest = new LaboratoryTest();
  51. AddLaboratoryTestValue(reader, laboratoryTest);
  52. laboratoryTests.Add(laboratoryTest);
  53. }
  54. reader.Close();
  55. }
  56. catch (Exception ex)
  57. {
  58. AppLog.Error(ex.Message);
  59. throw ex;
  60. }
  61. return laboratoryTests;
  62. }
  63. public DataTable GetAllLaboratoryTestsToDT()
  64. {
  65. DataTable dt = new DataTable();
  66. try
  67. {
  68. //构建数据库连接
  69. SQLiteHelper.SetConnectionString(ConnectString);
  70. string strSql = "select * from LaboratoryTest";
  71. dt = SQLiteHelper.ExecuteQuery(strSql, new SQLiteParameter[] { });
  72. }
  73. catch (Exception ex)
  74. {
  75. AppLog.Error(ex.Message);
  76. throw ex;
  77. }
  78. return dt;
  79. }
  80. public DataTable GetLaboratoryTestsByTypeToDT(string strType)
  81. {
  82. DataTable dt = new DataTable();
  83. try
  84. {
  85. //构建数据库连接
  86. SQLiteHelper.SetConnectionString(ConnectString);
  87. string strSql = $"select * from LaboratoryTest where datatype='{strType}'";
  88. dt = SQLiteHelper.ExecuteQuery(strSql, new SQLiteParameter[] { });
  89. }
  90. catch (Exception ex)
  91. {
  92. AppLog.Error(ex.Message);
  93. throw ex;
  94. }
  95. return dt;
  96. }
  97. //插入一行数据
  98. public int InsertLaboratory(LaboratoryTest test)
  99. {
  100. int iReturn = 0;
  101. string strSql = @"INSERT INTO LaboratoryTest (GUID
  102. ,Sample_Number
  103. ,Crucible_Number
  104. ,Instrument_Number
  105. ,Auto_Code
  106. ,DataType
  107. ,Empty_Crucible_Weight
  108. ,Sample_Weight
  109. ,AddSample_Weight
  110. ,Drying_Weight
  111. ,SecondDrying_Weight
  112. ,ThirdDrying_Weight
  113. ,BurningResidue_Weight
  114. ,SecondBurningResidue_Weight
  115. ,ThirdBurningResidue_Weight
  116. ,Crucible_Location_Number
  117. ,Cinder_Characteristics
  118. ,Operator
  119. ,DataMold
  120. ,WeighingType
  121. ,OperaDateTime
  122. ,IfEnabled
  123. ,Mad
  124. ,Granularity)
  125. VALUES(@GUID
  126. ,@Sample_Number
  127. ,@Crucible_Number
  128. ,@Instrument_Number
  129. ,@Auto_Code
  130. ,@DataType
  131. ,@Empty_Crucible_Weight
  132. ,@Sample_Weight
  133. ,@AddSample_Weight
  134. ,@Drying_Weight
  135. ,@SecondDrying_Weight
  136. ,@ThirdDrying_Weight
  137. ,@BurningResidue_Weight
  138. ,@SecondBurningResidue_Weight
  139. ,@ThirdBurningResidue_Weight
  140. ,@Crucible_Location_Number
  141. ,@Cinder_Characteristics
  142. ,@Operator
  143. ,@DataMold
  144. ,@WeighingType
  145. ,@OperaDateTime
  146. ,@IfEnabled
  147. ,@Mad
  148. ,@Granularity)";
  149. try
  150. {
  151. //构建数据库连接
  152. SQLiteHelper.SetConnectionString(ConnectString);
  153. int index = 0;
  154. SQLiteParameter[] parameters = new SQLiteParameter[test.GetType().GetProperties().Count()];
  155. for (int i = 0; i < test.GetType().GetProperties().Count(); i++)
  156. {
  157. parameters[index] = new SQLiteParameter(test.GetType().GetProperties()[i].Name, test.GetType().GetProperties()[i].GetValue(test, null));
  158. index++;
  159. }
  160. //执行SQL语句
  161. iReturn += SQLiteHelper.ExecuteNonQuery(strSql, parameters);
  162. }
  163. catch(Exception ex)
  164. {
  165. AppLog.Error(ex.Message);
  166. throw ex;
  167. }
  168. return iReturn;
  169. }
  170. //更新数据
  171. public int UpdateLaboratory(LaboratoryTest test)
  172. {
  173. int iReturn = 0;
  174. string strSql = @"UPDATE LaboratoryTest set Sample_Number=@Sample_Number
  175. ,Crucible_Number=@Crucible_Number
  176. ,Instrument_Number=@Instrument_Number
  177. ,Auto_Code=@Auto_Code
  178. ,DataType=@DataType
  179. ,Empty_Crucible_Weight=@Empty_Crucible_Weight
  180. ,Sample_Weight=@Sample_Weight
  181. ,AddSample_Weight=@AddSample_Weight
  182. ,Drying_Weight=@Drying_Weight
  183. ,SecondDrying_Weight=@SecondDrying_Weight
  184. ,ThirdDrying_Weight=@ThirdDrying_Weight
  185. ,BurningResidue_Weight=@BurningResidue_Weight
  186. ,SecondBurningResidue_Weight=@SecondBurningResidue_Weight
  187. ,ThirdBurningResidue_Weight=@ThirdBurningResidue_Weight
  188. ,Crucible_Location_Number=@Crucible_Location_Number
  189. ,Cinder_Characteristics=@Cinder_Characteristics
  190. ,Operator=@Operator
  191. ,DataMold=@DataMold
  192. ,WeighingType=@WeighingType
  193. ,OperaDateTime=@OperaDateTime
  194. ,IfEnabled=@IfEnabled
  195. ,Mad=@Mad
  196. ,Granularity=@Granularity
  197. WHERE GUID=@GUID";
  198. try
  199. {
  200. //构建数据库连接
  201. SQLiteHelper.SetConnectionString(ConnectString);
  202. SQLiteParameter[] parameters = new SQLiteParameter[test.GetType().GetProperties().Count()];
  203. for (int i = 0; i < test.GetType().GetProperties().Count(); i++)
  204. {
  205. parameters[i] = new SQLiteParameter(test.GetType().GetProperties()[i].Name, test.GetType().GetProperties()[i].GetValue(test, null));
  206. }
  207. //执行SQL语句
  208. iReturn += SQLiteHelper.ExecuteNonQuery(strSql, parameters);
  209. }
  210. catch (Exception ex)
  211. {
  212. AppLog.Error(ex.Message);
  213. throw ex;
  214. }
  215. return iReturn;
  216. }
  217. //删除数据
  218. public int DeleteLaboratory(LaboratoryTest test)
  219. {
  220. int iReturn = 0;
  221. try
  222. {
  223. //删除数据时要删除记录表中相关数据,避免LaboratoryTestID重复导致无法正常增加记录。
  224. string strSql = $"DELETE FROM UpLoadRecord WHERE LaboratoryTestGUID='{test.GUID}';DELETE FROM LaboratoryTest WHERE GUID='{test.GUID}';";
  225. //构建数据库连接
  226. SQLiteHelper.SetConnectionString(ConnectString);
  227. //执行SQL语句
  228. iReturn += SQLiteHelper.ExecuteNonQuery(strSql, new SQLiteParameter[] { });
  229. }
  230. catch (Exception ex)
  231. {
  232. AppLog.Error(ex.Message);
  233. throw ex;
  234. }
  235. return iReturn;
  236. }
  237. private void AddLaboratoryTestValue(SQLiteDataReader reader, LaboratoryTest laboratoryTest)
  238. {
  239. laboratoryTest.GUID = reader["GUID"].ToString();
  240. laboratoryTest.Sample_Number = reader["Sample_Number"].ToString();
  241. laboratoryTest.Crucible_Number = reader["Crucible_Number"].ToString();
  242. laboratoryTest.Crucible_Location_Number = reader["Crucible_Location_Number"].ToString();
  243. laboratoryTest.Instrument_Number = reader["Instrument_Number"].ToString();
  244. laboratoryTest.Auto_Code = reader["Auto_Code"].ToString();
  245. laboratoryTest.DataType = reader["DataType"].ToString();
  246. laboratoryTest.Cinder_Characteristics = reader["Cinder_Characteristics"].ToString();
  247. laboratoryTest.Operator = reader["Operator"].ToString();
  248. laboratoryTest.DataMold = reader["DataMold"].ToString();
  249. laboratoryTest.WeighingType = reader["WeighingType"].ToString();
  250. laboratoryTest.Granularity = reader["Granularity"].ToString();
  251. double defaultdouble = 0;
  252. if (double.TryParse(reader["Empty_Crucible_Weight"].ToString(), out defaultdouble))
  253. laboratoryTest.Empty_Crucible_Weight = defaultdouble;
  254. if (double.TryParse(reader["Sample_Weight"].ToString(), out defaultdouble))
  255. laboratoryTest.Sample_Weight = defaultdouble;
  256. if (double.TryParse(reader["AddSample_Weight"].ToString(), out defaultdouble))
  257. laboratoryTest.AddSample_Weight = defaultdouble;
  258. if (double.TryParse(reader["Drying_Weight"].ToString(), out defaultdouble))
  259. laboratoryTest.Drying_Weight = defaultdouble;
  260. if (double.TryParse(reader["SecondDrying_Weight"].ToString(), out defaultdouble))
  261. laboratoryTest.SecondDrying_Weight = defaultdouble;
  262. if (double.TryParse(reader["ThirdDrying_Weight"].ToString(), out defaultdouble))
  263. laboratoryTest.ThirdDrying_Weight = defaultdouble;
  264. if (double.TryParse(reader["BurningResidue_Weight"].ToString(), out defaultdouble))
  265. laboratoryTest.BurningResidue_Weight = defaultdouble;
  266. if (double.TryParse(reader["SecondBurningResidue_Weight"].ToString(), out defaultdouble))
  267. laboratoryTest.SecondBurningResidue_Weight = defaultdouble;
  268. if (double.TryParse(reader["ThirdBurningResidue_Weight"].ToString(), out defaultdouble))
  269. laboratoryTest.ThirdBurningResidue_Weight = defaultdouble;
  270. if (double.TryParse(reader["Mad"].ToString(), out defaultdouble))
  271. laboratoryTest.Mad = defaultdouble;
  272. DateTime defaultdate = DateTime.Now;
  273. if (DateTime.TryParse(reader["OperaDateTime"].ToString(), out defaultdate))
  274. laboratoryTest.OperaDateTime = defaultdate;
  275. laboratoryTest.IfEnabled = reader["IfEnabled"].ToString().ToLower() == "true" ? true : false;
  276. }
  277. /// <summary>
  278. /// 根据每种类型CNAS库里面的最大化验时间获取数据
  279. /// </summary>
  280. /// <param name="dictType"></param>
  281. /// <returns></returns>
  282. public List<LaboratoryTest> GetLaboratoryTestsByTypeAndDate(Dictionary<string,string> dictType)
  283. {
  284. List<LaboratoryTest> laboratoryTests = new List<LaboratoryTest>();
  285. try
  286. {
  287. //select* from laboratorytest where(datatype= 'ARB' and OperaDateTime> '2019-08-01') or datatype not in('ARB');
  288. //构建数据库连接
  289. SQLiteHelper.SetConnectionString(ConnectString);
  290. string strsql = "select * from LaboratoryTest where";
  291. string strsql_2 = "";
  292. foreach (var item in dictType)
  293. {
  294. strsql += "(datatype='" + item.Key + "' and OperaDateTime>'" + item.Value + "') or";
  295. strsql_2 += "'" + item.Key + "',";
  296. }
  297. string strSql = strsql +" datatype not in(" +strsql_2.Substring(0, strsql_2.Length - 1)+")";
  298. SQLiteDataReader reader = SQLiteHelper.ExecuteReader(strSql, new SQLiteParameter[] { });
  299. while (reader.Read())
  300. {
  301. LaboratoryTest laboratoryTest = new LaboratoryTest();
  302. AddLaboratoryTestValue(reader, laboratoryTest);
  303. laboratoryTests.Add(laboratoryTest);
  304. }
  305. reader.Close();
  306. }
  307. catch (Exception ex)
  308. {
  309. AppLog.Error(ex.Message);
  310. throw ex;
  311. }
  312. return laboratoryTests;
  313. }
  314. /// <summary>
  315. /// 获取未上传的所有数据
  316. /// </summary>
  317. /// <param name="dictType"></param>
  318. /// <returns></returns>
  319. public List<LaboratoryTest> GetLaboratoryTestsByNoUpLoad()
  320. {
  321. List<LaboratoryTest> laboratoryTests = new List<LaboratoryTest>();
  322. try
  323. {
  324. //select* from laboratorytest where(datatype= 'ARB' and OperaDateTime> '2019-08-01') or datatype not in('ARB');
  325. //构建数据库连接
  326. SQLiteHelper.SetConnectionString(ConnectString);
  327. string strsql = "select * from LaboratoryTest A left join UpLoadRecord B on A.GUID=B.LaboratoryTestGUID where B.GUID is null;";
  328. SQLiteDataReader reader = SQLiteHelper.ExecuteReader(strsql, new SQLiteParameter[] { });
  329. while (reader.Read())
  330. {
  331. LaboratoryTest laboratoryTest = new LaboratoryTest();
  332. AddLaboratoryTestValue(reader, laboratoryTest);
  333. laboratoryTests.Add(laboratoryTest);
  334. }
  335. reader.Close();
  336. }
  337. catch (Exception ex)
  338. {
  339. AppLog.Error(ex.Message);
  340. throw ex;
  341. }
  342. return laboratoryTests;
  343. }
  344. }
  345. }