CNAS取数仪器端升级
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

255 linhas
12KB

  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 AccessInstrumentData : 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 IAccessService accessDatabaseService { get { return new AccessDBService(); } }
  22. public AccessFormatConfig AccessFormat { get; set; }
  23. public List<string> LstFileFullName { get; set; }
  24. public List<string> LstFileName { get; set; }
  25. public AccessInstrumentData(InstrumentDataSourceInfo dataSourceInfo, object[] vs)
  26. {
  27. try
  28. {
  29. //加载配置信息
  30. AccessFormat = FileOperation.GetFormatConfigData<AccessFormatConfig>("AccessFormatConfig.xml");
  31. //初始化
  32. LstFileFullName = new List<string>();
  33. LstFileName = new List<string>();
  34. if (AccessFormat.ReadAccessFileMode == "0")
  35. {
  36. string strSuffix = Path.GetExtension(dataSourceInfo.Path);
  37. string strFileName = Path.GetFileName(dataSourceInfo.Path);
  38. RemoteFileCopy remoteFileCopy = new RemoteFileCopy(dataSourceInfo);
  39. remoteFileCopy.CopyFileFromRemote(strSuffix, strFileName);
  40. this.StrLocalPath = FileHelper.getBasePath() + "\\Cache\\" + System.Text.RegularExpressions.Regex.Replace(dataSourceInfo.Path, "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", "") + strSuffix;
  41. this.StrUser = dataSourceInfo.UserId;
  42. this.StrPwd = dataSourceInfo.UserPwd;
  43. this.strTableName = vs[0].ToString();
  44. this.strDateColumn = vs[1].ToString();
  45. this.strDate = vs[2].ToString();
  46. LstFileFullName.Add(StrLocalPath);
  47. LstFileName.Add(System.Text.RegularExpressions.Regex.Replace(dataSourceInfo.Path, "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", ""));
  48. AppLog.Error("打印日志 " + this.strTableName);
  49. AppLog.Error("打印日志 " + this.strDateColumn);
  50. AppLog.Error("打印日志 " + this.strDate);
  51. AppLog.Error("打印日志 " + this.StrPwd);
  52. AppLog.Error("打印日志 " + this.StrUser);
  53. AppLog.Error("打印日志 " + this.StrLocalPath);
  54. AppLog.Error("打印日志 " + FileHelper.getBasePath());
  55. AppLog.Error("打印日志 " + dataSourceInfo.Path);
  56. }
  57. else
  58. {
  59. //遍历当前路径,找到所有“今天”的数据文件,复制形成缓冲文件
  60. string strSuffix = AccessFormat.AccessSuffix;
  61. if (dataSourceInfo.Path == "") return;
  62. DirectoryInfo root = new DirectoryInfo(dataSourceInfo.Path);
  63. foreach (FileInfo f in root.GetFiles())
  64. {
  65. string name = f.Name;
  66. string fullName = f.FullName;
  67. string type = f.Extension;
  68. if (type.ToLower() == strSuffix.ToLower() && name.StartsWith(DateTime.Now.ToString(AccessFormat.AccessFileNameFormat))) //“今天”产生的数据
  69. {
  70. RemoteFileCopy remoteFileCopy = new RemoteFileCopy(dataSourceInfo);
  71. remoteFileCopy.CopyFileFromRemote(".mdb", name);
  72. LstFileFullName.Add(FileHelper.getBasePath() + "\\Cache\\" + System.Text.RegularExpressions.Regex.Replace(dataSourceInfo.Path + name, "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", "") + ".mdb");
  73. LstFileName.Add(System.Text.RegularExpressions.Regex.Replace(dataSourceInfo.Path + name, "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", ""));
  74. }
  75. }
  76. this.strTableName = AccessFormat.AccessFileTableNameFormat == null ? vs[0].ToString() : AccessFormat.AccessFileTableNameFormat;
  77. this.strDateColumn = vs[1].ToString();
  78. this.strDate = vs[2].ToString();
  79. }
  80. }
  81. catch (Exception ex)
  82. {
  83. AppLog.Error(ex.Message);
  84. }
  85. }
  86. public override Dictionary<string, DataTable> GetInstrumentData()
  87. {
  88. //return accessDatabaseService.GetInstrumentData(
  89. // new AccessOpenParams
  90. // {
  91. // StrPath = StrLocalPath,
  92. // StrPwd = StrPwd,
  93. // AccessVersionInfo = AccessFormat.AccessFileVersion
  94. // }
  95. // );
  96. Dictionary<string, DataTable> dictReturn = new Dictionary<string, DataTable>();
  97. try
  98. {
  99. foreach (string strPath in LstFileFullName)
  100. {
  101. Dictionary<string, DataTable> dictCurrent = accessDatabaseService.GetInstrumentData(
  102. new AccessOpenParams
  103. {
  104. StrPath = strPath,
  105. StrPwd = StrPwd,
  106. AccessVersionInfo = AccessFormat.AccessFileVersion
  107. }
  108. );
  109. if (dictCurrent.Count > 0)
  110. {
  111. foreach (var item in dictCurrent)
  112. {
  113. if (!dictReturn.ContainsKey(item.Key)) //如果不包含,则直接增加
  114. {
  115. dictReturn.Add(item.Key, item.Value);
  116. }
  117. //else //如果已经存在相同名字的表单,则合并数据
  118. //{
  119. // DataTable dtCompine = item.Value.Clone();
  120. // foreach (DataRow drs in item.Value.Rows)
  121. // {
  122. // DataRow drNew = dictReturn[item.Key].NewRow();
  123. // drNew.ItemArray = drs.ItemArray;
  124. // dictReturn[item.Key].Rows.Add(drNew);
  125. // }
  126. //}
  127. }
  128. }
  129. }
  130. }
  131. catch (Exception ex)
  132. {
  133. AppLog.Error(ex.Message);
  134. }
  135. return dictReturn;
  136. }
  137. public override DataTable GetInstrumentDataByDate()
  138. {
  139. DataTable dtCompine = new DataTable();
  140. List<DataTable> dataTables = new List<DataTable>();
  141. for (int i = 0; i < LstFileFullName.Count; i++)
  142. {
  143. DataTable dt = accessDatabaseService.GetInstrumentDataByDate(
  144. new AccessOpenParams
  145. {
  146. StrPath = LstFileFullName[i],
  147. StrUser = StrUser,
  148. StrPwd = StrPwd,
  149. AccessVersionInfo = AccessFormat.AccessFileVersion,
  150. AccessSpecialDateFormat = AccessFormat.AccessFileDateColumnFormat,
  151. autoSql = AccessFormat.AutoSql
  152. },
  153. new ConditionParams
  154. {
  155. TableName = strTableName,
  156. DateColumn = strDateColumn,
  157. DateValue = strDate
  158. }
  159. );
  160. dataTables.Add(dt);
  161. }
  162. if (dataTables.Count > 0)
  163. {
  164. if (dataTables.Count == 1) //如果只有一个表,不需要再重复循环一次了
  165. dtCompine = dataTables[0];
  166. else
  167. {
  168. dtCompine = dataTables[0].Clone();
  169. try
  170. {
  171. foreach (DataTable dt1 in dataTables)
  172. {
  173. foreach (DataRow dr in dt1.Rows)
  174. {
  175. DataRow drNew = dtCompine.NewRow();
  176. foreach (DataColumn dc in dtCompine.Columns)
  177. {
  178. //if (dc.ColumnName.Trim().ToLower() == strDateColumn.Trim().ToLower())
  179. //{
  180. // DateTime dt = DateTime.Now;
  181. // //AppLog.Info("4-" + dr[dc.ColumnName].ToString());
  182. // if (DateTime.TryParse(dr[dc.ColumnName].ToString(), out dt))
  183. // {
  184. // drNew[dc.ColumnName] = dr[dc.ColumnName].ToString();
  185. // }
  186. // else
  187. // {
  188. // //AppLog.Info("5-" + AccessFormat.AccessFileDateColumnFormat);
  189. // DateTime dtTime = DateTime.ParseExact(dr[dc.ColumnName].ToString(), AccessFormat.AccessFileDateColumnFormat, new System.Globalization.CultureInfo("zh-CN", true));
  190. // if (dtTime.ToString("HH:mm:ss") == "00:00:00")
  191. // drNew[dc.ColumnName] = dtTime.ToString("yyyy-MM-dd");
  192. // else
  193. // drNew[dc.ColumnName] = dtTime.ToString("yyyy-MM-dd HH:mm:ss");
  194. // }
  195. //}
  196. //else
  197. //{
  198. // drNew[dc.ColumnName] = dr[dc.ColumnName];
  199. //}
  200. drNew[dc.ColumnName] = dr[dc.ColumnName];
  201. }
  202. dtCompine.Rows.Add(drNew);
  203. }
  204. }
  205. }
  206. catch (Exception ex)
  207. {
  208. AppLog.Error(ex.Message);
  209. }
  210. }
  211. }
  212. return dtCompine;
  213. }
  214. public override DataTable GetInstrumentDataStruct()
  215. {
  216. return accessDatabaseService.GetInstrumentDataStruct(
  217. new AccessOpenParams
  218. {
  219. StrPath = LstFileFullName[0],
  220. StrUser = StrUser,
  221. StrPwd = StrPwd,
  222. AccessVersionInfo = AccessFormat.AccessFileVersion,
  223. autoSql = AccessFormat.AutoSql
  224. },
  225. new ConditionParams
  226. {
  227. TableName = strTableName
  228. }
  229. );
  230. }
  231. }
  232. }