using CnasSynchronousCommon; using CnasSynchronusDAL; using CnasSynchronusIDAL; using CnasSynchrousModel; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Text; namespace CnasSynchronusClient { internal class TXTInstrumentData : InstrumentData { public string StrLocalPath { get; set; } public string StrUser { get; set; } public string StrPwd { get; set; } public string strTableName { get; set; } public string strDateColumn { get; set; } public string strDate { get; set; } public ITXTService txtDatabaseService { get { return new TXTService(); } } public NormalFileFormatConfig normalFileFormat { get; set; } /// /// 如果配置读取的是文件夹,该属性用来记录需要读取所有文件的缓冲路径 /// public List LstFileFullName { get; set; } public TXTInstrumentData(InstrumentDataSourceInfo dataSourceInfo, object[] vs) { normalFileFormat= FileOperation.GetFormatConfigData("NormalFileFormatConfig.xml"); string strSuffix = normalFileFormat.NormalFileSuffix; try { LstFileFullName = new List(); if (normalFileFormat.ReadNormalFileMode == "0") //此时为读取“文件”模式,指定要读取的具体文件 { RemoteFileCopy remoteFileCopy = new RemoteFileCopy(dataSourceInfo); remoteFileCopy.CopyFileFromRemote(strSuffix); this.StrLocalPath = FileHelper.getBasePath() + "\\Cache\\" + System.Text.RegularExpressions.Regex.Replace(dataSourceInfo.Path, "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", "") + strSuffix; this.strTableName = vs[0].ToString(); this.strDateColumn = vs[1].ToString(); this.strDate = vs[2].ToString(); LstFileFullName.Add(StrLocalPath); } else //此时为读取“文件夹”模式,指定要读取的具体文件 { //遍历当前路径,找到所有“今天”的数据文件,复制形成缓冲文件 DirectoryInfo root = new DirectoryInfo(dataSourceInfo.Path); //if (root.GetFiles() == null) // AppLog.Info($"文件夹文件数量为空"); //else // AppLog.Info($"文件夹文件数量{root.GetFiles().Count()}"); foreach (FileInfo f in root.GetFiles()) { string name = f.Name; string fullName = f.FullName; string type = f.Extension; if (type.ToLower() == strSuffix.ToLower() && name.StartsWith(DateTime.Now.ToString("yyyyMMdd"))) //“今天”产生的数据,并且格式是txt { RemoteFileCopy remoteFileCopy = new RemoteFileCopy(dataSourceInfo); remoteFileCopy.CopyFileFromRemote(strSuffix, name); LstFileFullName.Add(FileHelper.getBasePath() + "\\Cache\\" + System.Text.RegularExpressions.Regex.Replace(dataSourceInfo.Path + name, "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", "") + strSuffix); } } this.strTableName = vs[0].ToString(); this.strDateColumn = vs[1].ToString(); this.strDate = vs[2].ToString(); } } catch (Exception ex) { AppLog.Error(ex.Message); } } public override Dictionary GetInstrumentData() { //读取数据 List dataTables = new List(); foreach (var item in LstFileFullName) { DataTable dt = txtDatabaseService.GetInstrumentData(new TXTOpenParams { StrPath = item, TxtFileMode = normalFileFormat.NormalFileDataMode })["txt"]; dataTables.Add(dt); } //合并数据 DataTable dtCompine = new DataTable(); if (dataTables.Count > 0) { dtCompine = dataTables[0].Clone(); foreach (DataTable dt1 in dataTables) { foreach (DataRow dr in dt1.Rows) { DataRow drNew = dtCompine.NewRow(); drNew.ItemArray = dr.ItemArray; dtCompine.Rows.Add(drNew); } } } return new Dictionary() { { "txt", dtCompine } }; } public override DataTable GetInstrumentDataByDate() { DataTable dtCompine = new DataTable(); try { //读取数据 List dataTables = new List(); foreach (var item in LstFileFullName) { DataTable dt = txtDatabaseService.GetInstrumentDataByDate ( new TXTOpenParams { StrPath = item, TxtFileMode = normalFileFormat.NormalFileDataMode }, new ConditionParams { } ); dataTables.Add(dt); } //合并数据 if (dataTables.Count > 0) { dtCompine = dataTables[0].Clone(); foreach (DataTable dt1 in dataTables) { foreach (DataRow dr in dt1.Rows) { DataRow drNew = dtCompine.NewRow(); foreach (DataColumn dc in dtCompine.Columns) { if (dc.ColumnName == strDateColumn) { DateTime dtTime = DateTime.ParseExact(dr[dc.ColumnName].ToString(), normalFileFormat.NormalFileDateColumnFormat, new System.Globalization.CultureInfo("zh-CN", true)); drNew[dc.ColumnName] = dtTime.ToString("yyyy-MM-dd HH:mm:ss"); } else { drNew[dc.ColumnName] = dr[dc.ColumnName]; } } dtCompine.Rows.Add(drNew); } } } } catch (Exception ex) { AppLog.Info("读取数据时发生异常"); AppLog.Error(ex.Message); } AppLog.Info("读取数据完成,准备返回"); return dtCompine; } public override DataTable GetInstrumentDataStruct() { //读取数据 List dataTables = new List(); foreach (var item in LstFileFullName) { DataTable dt = txtDatabaseService.GetInstrumentData(new TXTOpenParams { StrPath = item, TxtFileMode = normalFileFormat.NormalFileDataMode })["txt"]; dataTables.Add(dt); } //合并数据 DataTable dtCompine = new DataTable(); if (dataTables.Count > 0) { dtCompine = dataTables[0].Clone(); foreach (DataTable dt1 in dataTables) { foreach (DataRow dr in dt1.Rows) { DataRow drNew = dtCompine.NewRow(); drNew.ItemArray = dr.ItemArray; dtCompine.Rows.Add(drNew); } } } return dtCompine; } } }