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.

287 line
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. }
  49. else
  50. {
  51. //遍历当前路径,找到所有“今天”的数据文件,复制形成缓冲文件
  52. string strSuffix = AccessFormat.AccessSuffix;
  53. if (dataSourceInfo.Path == "") return;
  54. DirectoryInfo root = new DirectoryInfo(dataSourceInfo.Path);
  55. foreach (FileInfo f in root.GetFiles())
  56. {
  57. string name = f.Name;
  58. string fullName = f.FullName;
  59. string type = f.Extension;
  60. if (type.ToLower() == strSuffix.ToLower() && name.StartsWith(DateTime.Now.ToString(AccessFormat.AccessFileNameFormat))) //“今天”产生的数据
  61. {
  62. RemoteFileCopy remoteFileCopy = new RemoteFileCopy(dataSourceInfo);
  63. remoteFileCopy.CopyFileFromRemote(".mdb", name);
  64. LstFileFullName.Add(FileHelper.getBasePath() + "\\Cache\\" + System.Text.RegularExpressions.Regex.Replace(dataSourceInfo.Path + name, "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", "") + ".mdb");
  65. LstFileName.Add(System.Text.RegularExpressions.Regex.Replace(dataSourceInfo.Path + name, "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", ""));
  66. }
  67. }
  68. this.strTableName = AccessFormat.AccessFileTableNameFormat == null ? vs[0].ToString() : AccessFormat.AccessFileTableNameFormat;
  69. this.strDateColumn = vs[1].ToString();
  70. this.strDate = vs[2].ToString();
  71. }
  72. }
  73. catch (Exception ex)
  74. {
  75. AppLog.Error(ex.Message);
  76. }
  77. }
  78. public override Dictionary<string, DataTable> GetInstrumentData()
  79. {
  80. //return accessDatabaseService.GetInstrumentData(
  81. // new AccessOpenParams
  82. // {
  83. // StrPath = StrLocalPath,
  84. // StrPwd = StrPwd,
  85. // AccessVersionInfo = AccessFormat.AccessFileVersion
  86. // }
  87. // );
  88. Dictionary<string, DataTable> dictReturn = new Dictionary<string, DataTable>();
  89. try
  90. {
  91. foreach (string strPath in LstFileFullName)
  92. {
  93. Dictionary<string, DataTable> dictCurrent = accessDatabaseService.GetInstrumentData(
  94. new AccessOpenParams
  95. {
  96. StrPath = strPath,
  97. StrPwd = StrPwd,
  98. AccessVersionInfo = AccessFormat.AccessFileVersion
  99. }
  100. );
  101. if (dictCurrent.Count > 0)
  102. {
  103. foreach (var item in dictCurrent)
  104. {
  105. if (!dictReturn.ContainsKey(item.Key)) //如果不包含,则直接增加
  106. {
  107. dictReturn.Add(item.Key, item.Value);
  108. }
  109. else //如果已经存在相同名字的表单,则合并数据
  110. {
  111. DataTable dtCompine = item.Value.Clone();
  112. foreach (DataRow drs in item.Value.Rows)
  113. {
  114. DataRow drNew = dictReturn[item.Key].NewRow();
  115. drNew.ItemArray = drs.ItemArray;
  116. dictReturn[item.Key].Rows.Add(drNew);
  117. }
  118. }
  119. }
  120. }
  121. }
  122. }
  123. catch (Exception ex)
  124. {
  125. AppLog.Error(ex.Message);
  126. }
  127. return dictReturn;
  128. }
  129. public override DataTable GetInstrumentDataByDate()
  130. {
  131. ///*获取数据源数据*/
  132. //DataTable dt = accessDatabaseService.GetInstrumentDataByDate(
  133. // new AccessOpenParams
  134. // {
  135. // StrPath = StrLocalPath,
  136. // StrUser = StrUser,
  137. // StrPwd = StrPwd,
  138. // AccessVersionInfo = AccessFormat.AccessFileVersion,
  139. // AccessSpecialDateFormat = AccessFormat.AccessFileDateColumnFormat,
  140. // autoSql = AccessFormat.AutoSql
  141. // },
  142. // new ConditionParams
  143. // {
  144. // TableName = strTableName,
  145. // DateColumn = strDateColumn,
  146. // DateValue = strDate
  147. // }
  148. // );
  149. ///*如果日期关键字段格式不合法,将不无法使用,此时需要将其转换为可识别的格式*/
  150. //DataTable dtReturn = new DataTable();
  151. //try
  152. //{
  153. // if (AccessFormat.AccessFileDateColumnFormat != "")
  154. // {
  155. // dtReturn = dt.Clone();
  156. // foreach (DataRow dr in dt.Rows)
  157. // {
  158. // DataRow drNew = dtReturn.NewRow();
  159. // foreach (DataColumn dc in dtReturn.Columns)
  160. // {
  161. // if (dc.ColumnName.Trim().ToLower() == strDateColumn.Trim().ToLower())
  162. // {
  163. // DateTime dtTime = DateTime.ParseExact(dr[dc.ColumnName].ToString(), AccessFormat.AccessFileDateColumnFormat, new System.Globalization.CultureInfo("zh-CN", true));
  164. // drNew[dc.ColumnName] = dtTime.ToString("yyyy-MM-dd HH:mm:ss");
  165. // }
  166. // else
  167. // {
  168. // drNew[dc.ColumnName] = dr[dc.ColumnName];
  169. // }
  170. // }
  171. // dtReturn.Rows.Add(drNew);
  172. // }
  173. // }
  174. // else
  175. // dtReturn = dt;
  176. //}
  177. //catch (Exception ex)
  178. //{
  179. // AppLog.Error(ex.Message);
  180. //}
  181. //return dtReturn;
  182. DataTable dtCompine = new DataTable();
  183. List<DataTable> dataTables = new List<DataTable>();
  184. for (int i = 0; i < LstFileFullName.Count; i++)
  185. {
  186. DataTable dt = accessDatabaseService.GetInstrumentDataByDate(
  187. new AccessOpenParams
  188. {
  189. StrPath = LstFileFullName[i],
  190. StrUser = StrUser,
  191. StrPwd = StrPwd,
  192. AccessVersionInfo = AccessFormat.AccessFileVersion,
  193. AccessSpecialDateFormat = AccessFormat.AccessFileDateColumnFormat,
  194. autoSql = AccessFormat.AutoSql
  195. },
  196. new ConditionParams
  197. {
  198. TableName = strTableName,
  199. DateColumn = strDateColumn,
  200. DateValue = strDate
  201. }
  202. );
  203. dataTables.Add(dt);
  204. }
  205. if (dataTables.Count > 0)
  206. {
  207. dtCompine = dataTables[0].Clone();
  208. try
  209. {
  210. foreach (DataTable dt1 in dataTables)
  211. {
  212. foreach (DataRow dr in dt1.Rows)
  213. {
  214. DataRow drNew = dtCompine.NewRow();
  215. foreach (DataColumn dc in dtCompine.Columns)
  216. {
  217. if (dc.ColumnName.Trim().ToLower() == strDateColumn.Trim().ToLower())
  218. {
  219. DateTime dt = DateTime.Now;
  220. if (DateTime.TryParse(dr[dc.ColumnName].ToString(), out dt))
  221. {
  222. drNew[dc.ColumnName] = dr[dc.ColumnName].ToString();
  223. }
  224. else
  225. {
  226. DateTime dtTime = DateTime.ParseExact(dr[dc.ColumnName].ToString(), AccessFormat.AccessFileDateColumnFormat, new System.Globalization.CultureInfo("zh-CN", true));
  227. }
  228. }
  229. else
  230. {
  231. drNew[dc.ColumnName] = dr[dc.ColumnName];
  232. }
  233. }
  234. dtCompine.Rows.Add(drNew);
  235. }
  236. }
  237. }
  238. catch (Exception ex)
  239. {
  240. AppLog.Error(ex.Message);
  241. }
  242. }
  243. return dtCompine;
  244. }
  245. public override DataTable GetInstrumentDataStruct()
  246. {
  247. return accessDatabaseService.GetInstrumentDataStruct(
  248. new AccessOpenParams
  249. {
  250. StrPath = LstFileFullName[0],
  251. StrUser = StrUser,
  252. StrPwd = StrPwd,
  253. AccessVersionInfo = AccessFormat.AccessFileVersion,
  254. autoSql = AccessFormat.AutoSql
  255. },
  256. new ConditionParams
  257. {
  258. TableName = strTableName
  259. }
  260. );
  261. }
  262. }
  263. }