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.

228 lines
9.9KB

  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. internal class FoxProInstrumentData : 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 IFoxProService foxproDatabaseService { get { return new FoxProDBService(); } }
  22. /// <summary>
  23. /// 如果配置读取的是文件夹,该属性用来记录需要读取所有文件的缓冲路径
  24. /// </summary>
  25. public List<string> LstFileFullName { get; set; }
  26. public List<string> LstFileName { get; set; }
  27. //public SystemFormatConfig SystemFormat { get; set; }
  28. public FoxProFormatConfig FoxProFormat { get; set; }
  29. public FoxProInstrumentData(InstrumentDataSourceInfo dataSourceInfo, object[] vs)
  30. {
  31. try
  32. {
  33. //加载配置信息
  34. FoxProFormat = FileOperation.GetFormatConfigData<FoxProFormatConfig>("FoxProFormatConfig.xml");
  35. //初始化变量
  36. LstFileFullName = new List<string>();
  37. LstFileName = new List<string>();
  38. if (FoxProFormat.ReadFoxProFileMode == "0")
  39. {
  40. RemoteFileCopy remoteFileCopy = new RemoteFileCopy(dataSourceInfo);
  41. remoteFileCopy.CopyFileFromRemote(".dbf");
  42. this.StrLocalPath = FileHelper.getBasePath() + "\\Cache\\" + System.Text.RegularExpressions.Regex.Replace(dataSourceInfo.Path, "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", "") + ".dbf";
  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 = ".dbf";
  53. DirectoryInfo root = new DirectoryInfo(dataSourceInfo.Path);
  54. foreach (FileInfo f in root.GetFiles())
  55. {
  56. string name = f.Name;
  57. string fullName = f.FullName;
  58. string type = f.Extension;
  59. if (type.ToLower() == strSuffix.ToLower() && name.StartsWith(DateTime.Now.ToString(FoxProFormat.FoxProFileNameFormat))) //“今天”产生的数据
  60. {
  61. RemoteFileCopy remoteFileCopy = new RemoteFileCopy(dataSourceInfo);
  62. remoteFileCopy.CopyFileFromRemote(strSuffix, name);
  63. LstFileFullName.Add(FileHelper.getBasePath() + "\\Cache\\" + System.Text.RegularExpressions.Regex.Replace(dataSourceInfo.Path + name, "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", "") + strSuffix);
  64. LstFileName.Add(System.Text.RegularExpressions.Regex.Replace(dataSourceInfo.Path + name, "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", ""));
  65. }
  66. }
  67. this.strTableName = vs[0].ToString();
  68. this.strDateColumn = vs[1].ToString();
  69. this.strDate = vs[2].ToString();
  70. }
  71. }
  72. catch (Exception ex)
  73. {
  74. AppLog.Error(ex.Message);
  75. }
  76. }
  77. public override Dictionary<string, DataTable> GetInstrumentData()
  78. {
  79. Dictionary<string, DataTable> dictReturn = new Dictionary<string, DataTable>();
  80. try
  81. {
  82. foreach (string strPath in LstFileFullName)
  83. {
  84. Dictionary<string, DataTable> dictCurrent = foxproDatabaseService.GetInstrumentData
  85. (
  86. new FoxProOpenParams
  87. {
  88. StrPath = strPath,
  89. StrPwd = StrPwd,
  90. specialMethod = FoxProFormat.SpecialDtOpera
  91. }
  92. );
  93. if (dictCurrent.Count > 0)
  94. {
  95. foreach (var item in dictCurrent)
  96. {
  97. if (!dictReturn.ContainsKey(item.Key)) //如果不包含,则直接增加
  98. {
  99. dictReturn.Add(item.Key, item.Value);
  100. }
  101. else //如果已经存在相同名字的表单,则合并数据
  102. {
  103. DataTable dtCompine = item.Value.Clone();
  104. foreach (DataRow drs in item.Value.Rows)
  105. {
  106. DataRow drNew = dictReturn[item.Key].NewRow();
  107. drNew.ItemArray = drs.ItemArray;
  108. dictReturn[item.Key].Rows.Add(drNew);
  109. }
  110. }
  111. }
  112. }
  113. }
  114. }
  115. catch (Exception ex)
  116. {
  117. AppLog.Error(ex.Message);
  118. }
  119. return dictReturn;
  120. }
  121. public override DataTable GetInstrumentDataByDate()
  122. {
  123. DataTable dtCompine = new DataTable();
  124. try
  125. {
  126. List<DataTable> dataTables = new List<DataTable>();
  127. for (int i = 0; i < LstFileFullName.Count; i++)
  128. {
  129. DataTable dt = foxproDatabaseService.GetInstrumentDataByDate(
  130. new FoxProOpenParams
  131. {
  132. StrPath = LstFileFullName[i],
  133. StrUser = StrUser,
  134. StrPwd = StrPwd,
  135. timeFormat = FoxProFormat.FoxProFileDateColumnFormat,
  136. autosql = FoxProFormat.AutoSql,
  137. specialMethod = FoxProFormat.SpecialDtOpera
  138. },
  139. new ConditionParams
  140. {
  141. TableName = LstFileName[i],
  142. DateColumn = strDateColumn,
  143. DateValue = strDate
  144. });
  145. dataTables.Add(dt);
  146. }
  147. if (dataTables.Count > 0)
  148. {
  149. dtCompine = dataTables[0].Clone();
  150. foreach (DataTable dt1 in dataTables)
  151. {
  152. foreach (DataRow dr in dt1.Rows)
  153. {
  154. DataRow drNew = dtCompine.NewRow();
  155. foreach (DataColumn dc in dtCompine.Columns)
  156. {
  157. if (dc.ColumnName.Trim().ToLower() == strDateColumn.Trim().ToLower())
  158. {
  159. DateTime dt = DateTime.Now;
  160. if (DateTime.TryParse(dr[dc.ColumnName].ToString(), out dt))
  161. {
  162. drNew[dc.ColumnName] = dr[dc.ColumnName].ToString();
  163. }
  164. else
  165. {
  166. DateTime dtTime = DateTime.ParseExact(dr[dc.ColumnName].ToString().Trim(), FoxProFormat.FoxProFileDateColumnFormat, new System.Globalization.CultureInfo("zh-CN", true));
  167. if (dtTime.ToString("HH:mm:ss") == "00:00:00")
  168. drNew[dc.ColumnName] = dtTime.ToString("yyyy-MM-dd");
  169. else
  170. drNew[dc.ColumnName] = dtTime.ToString("yyyy-MM-dd HH:mm:ss");
  171. }
  172. }
  173. else
  174. {
  175. drNew[dc.ColumnName] = dr[dc.ColumnName];
  176. }
  177. }
  178. dtCompine.Rows.Add(drNew);
  179. }
  180. }
  181. }
  182. }
  183. catch (Exception ex)
  184. {
  185. AppLog.Error(ex.Message);
  186. }
  187. return dtCompine;
  188. }
  189. public override DataTable GetInstrumentDataStruct()
  190. {
  191. //认为所有表的表结构是一样的
  192. return foxproDatabaseService.GetInstrumentDataStruct(
  193. new FoxProOpenParams
  194. {
  195. StrPath = LstFileFullName[0],
  196. StrUser = StrUser,
  197. StrPwd = StrPwd,
  198. autosql = FoxProFormat.AutoSql,
  199. specialMethod = FoxProFormat.SpecialDtOpera
  200. },
  201. new ConditionParams
  202. {
  203. TableName = LstFileName[0]
  204. });
  205. }
  206. }
  207. }