CNAS取数仪器端升级
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
9.5KB

  1. using CnasSynchronousCommon;
  2. using CnasSynchronusDAL;
  3. using CnasSynchronusIDAL;
  4. using CnasSynchrousModel;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Data;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. namespace CnasSynchronusClient
  12. {
  13. public class SQLiteInstrumentData : InstrumentData
  14. {
  15. public string StrLocalPath { get; set; }
  16. public string StrUser { get; set; }
  17. public string StrPwd { get; set; }
  18. public string strTableName { get; set; }
  19. public string strDateColumn { get; set; }
  20. public string strDate { get; set; }
  21. public ISqliteService sqliteDataBaseService { get { return new SqliteDBService(); } }
  22. public SqliteFormatConfig SqliteFormat { get; set; }
  23. public List<string> LstFileFullName { get; set; }
  24. public List<string> LstFileName { get; set; }
  25. public SQLiteInstrumentData(InstrumentDataSourceInfo dataSourceInfo, object[] vs)
  26. {
  27. try
  28. {
  29. SqliteFormat = FileOperation.GetFormatConfigData<SqliteFormatConfig>("SqliteFormatConfig.xml");
  30. //初始化变量
  31. LstFileFullName = new List<string>();
  32. LstFileName = new List<string>();
  33. if (SqliteFormat.ReadSqliteFileMode == "0")
  34. {
  35. RemoteFileCopy remoteFileCopy = new RemoteFileCopy(dataSourceInfo);
  36. remoteFileCopy.CopyFileFromRemote(".db");
  37. this.StrLocalPath = FileHelper.getBasePath() + "\\Cache\\" + System.Text.RegularExpressions.Regex.Replace(dataSourceInfo.Path, "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", "") + ".db";
  38. this.StrUser = dataSourceInfo.UserId;
  39. this.StrPwd = dataSourceInfo.UserPwd;
  40. this.strTableName = vs[0].ToString();
  41. this.strDateColumn = vs[1].ToString();
  42. this.strDate = vs[2].ToString();
  43. LstFileFullName.Add(StrLocalPath);
  44. LstFileName.Add(System.Text.RegularExpressions.Regex.Replace(dataSourceInfo.Path, "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", ""));
  45. }
  46. else
  47. {
  48. //遍历当前路径,找到所有“今天”的数据文件,复制形成缓冲文件
  49. string strSuffix = ".db";
  50. DirectoryInfo root = new DirectoryInfo(dataSourceInfo.Path);
  51. foreach (FileInfo f in root.GetFiles())
  52. {
  53. string name = f.Name;
  54. string fullName = f.FullName;
  55. string type = f.Extension;
  56. if (type.ToLower() == strSuffix.ToLower() && name.StartsWith(DateTime.Now.ToString(SqliteFormat.SqliteFileNameFormat))) //“今天”产生的数据
  57. {
  58. RemoteFileCopy remoteFileCopy = new RemoteFileCopy(dataSourceInfo);
  59. remoteFileCopy.CopyFileFromRemote(strSuffix, name);
  60. LstFileFullName.Add(FileHelper.getBasePath() + "\\Cache\\" + System.Text.RegularExpressions.Regex.Replace(dataSourceInfo.Path + name, "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", "") + strSuffix);
  61. LstFileName.Add(System.Text.RegularExpressions.Regex.Replace(dataSourceInfo.Path + name, "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", ""));
  62. }
  63. }
  64. this.strTableName = SqliteFormat.SqliteFileTableNameFormat == null ? vs[0].ToString() : SqliteFormat.SqliteFileTableNameFormat;
  65. this.strDateColumn = vs[1].ToString();
  66. this.strDate = vs[2].ToString();
  67. }
  68. }
  69. catch (Exception ex)
  70. {
  71. AppLog.Error(ex.Message);
  72. }
  73. }
  74. public override Dictionary<string, DataTable> GetInstrumentData()
  75. {
  76. Dictionary<string, DataTable> dictReturn = new Dictionary<string, DataTable>();
  77. try
  78. {
  79. foreach (string strPath in LstFileFullName)
  80. {
  81. Dictionary<string, DataTable> dictCurrent = sqliteDataBaseService.GetInstrumentData
  82. (
  83. new SqliteOpenParams
  84. {
  85. StrPath = strPath,
  86. StrPwd = StrPwd
  87. }
  88. );
  89. if (dictCurrent.Count > 0)
  90. {
  91. foreach (var item in dictCurrent)
  92. {
  93. if (!dictReturn.ContainsKey(item.Key)) //如果不包含,则直接增加
  94. {
  95. dictReturn.Add(item.Key, item.Value);
  96. }
  97. else //如果已经存在相同名字的表单,则合并数据
  98. {
  99. DataTable dtCompine = item.Value.Clone();
  100. foreach (DataRow drs in item.Value.Rows)
  101. {
  102. DataRow drNew = dictReturn[item.Key].NewRow();
  103. drNew.ItemArray = drs.ItemArray;
  104. dictReturn[item.Key].Rows.Add(drNew);
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }
  111. catch (Exception ex)
  112. {
  113. AppLog.Error(ex.Message);
  114. }
  115. return dictReturn;
  116. }
  117. public override DataTable GetInstrumentDataByDate()
  118. {
  119. DataTable dtCompine = new DataTable();
  120. try
  121. {
  122. List<DataTable> dataTables = new List<DataTable>();
  123. for (int i = 0; i < LstFileFullName.Count; i++)
  124. {
  125. DataTable dt = sqliteDataBaseService.GetInstrumentDataByDate(
  126. new SqliteOpenParams
  127. {
  128. StrPath = LstFileFullName[i],
  129. StrPwd = StrPwd,
  130. autoSql = SqliteFormat.AutoSql
  131. },
  132. new ConditionParams
  133. {
  134. TableName = strTableName,
  135. DateColumn = strDateColumn,
  136. DateValue = strDate
  137. }); ;
  138. dataTables.Add(dt);
  139. }
  140. if (dataTables.Count > 0)
  141. {
  142. dtCompine = dataTables[0].Clone();
  143. foreach (DataTable dt1 in dataTables)
  144. {
  145. foreach (DataRow dr in dt1.Rows)
  146. {
  147. DataRow drNew = dtCompine.NewRow();
  148. foreach (DataColumn dc in dtCompine.Columns)
  149. {
  150. if (dc.ColumnName.Trim().ToLower() == strDateColumn.Trim().ToLower())
  151. {
  152. DateTime dt = DateTime.Now;
  153. if (DateTime.TryParse(dr[dc.ColumnName].ToString(), out dt))
  154. {
  155. drNew[dc.ColumnName] = dr[dc.ColumnName].ToString();
  156. }
  157. else
  158. {
  159. DateTime dtTime = DateTime.ParseExact(dr[dc.ColumnName].ToString(), SqliteFormat.SqliteFileDateColumnFormat, new System.Globalization.CultureInfo("zh-CN", true));
  160. if (dtTime.ToString("HH:mm:ss") == "00:00:00")
  161. drNew[dc.ColumnName] = dtTime.ToString("yyyy-MM-dd");
  162. else
  163. drNew[dc.ColumnName] = dtTime.ToString("yyyy-MM-dd HH:mm:ss");
  164. }
  165. }
  166. else
  167. {
  168. drNew[dc.ColumnName] = dr[dc.ColumnName];
  169. }
  170. }
  171. dtCompine.Rows.Add(drNew);
  172. }
  173. }
  174. }
  175. }
  176. catch (Exception ex)
  177. {
  178. AppLog.Error(ex.Message);
  179. }
  180. return dtCompine;
  181. }
  182. public override DataTable GetInstrumentDataStruct()
  183. {
  184. return sqliteDataBaseService.GetInstrumentDataStruct(
  185. new SqliteOpenParams
  186. {
  187. StrPath = LstFileFullName[0],
  188. StrPwd = StrPwd,
  189. autoSql = SqliteFormat.AutoSql
  190. },
  191. new ConditionParams
  192. {
  193. TableName = strTableName
  194. });
  195. }
  196. }
  197. }