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 { public class AccessInstrumentData : 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 IAccessService accessDatabaseService { get { return new AccessDBService(); } } public AccessFormatConfig AccessFormat { get; set; } public List LstFileFullName { get; set; } public List LstFileName { get; set; } public AccessInstrumentData(InstrumentDataSourceInfo dataSourceInfo, object[] vs) { try { //加载配置信息 AccessFormat = FileOperation.GetFormatConfigData("AccessFormatConfig.xml"); //初始化 LstFileFullName = new List(); LstFileName = new List(); if (AccessFormat.ReadAccessFileMode == "0") { string strSuffix = Path.GetExtension(dataSourceInfo.Path); string strFileName = Path.GetFileName(dataSourceInfo.Path); RemoteFileCopy remoteFileCopy = new RemoteFileCopy(dataSourceInfo); remoteFileCopy.CopyFileFromRemote(strSuffix, strFileName); this.StrLocalPath = FileHelper.getBasePath() + "\\Cache\\" + System.Text.RegularExpressions.Regex.Replace(dataSourceInfo.Path, "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", "") + strSuffix; this.StrUser = dataSourceInfo.UserId; this.StrPwd = dataSourceInfo.UserPwd; this.strTableName = vs[0].ToString(); this.strDateColumn = vs[1].ToString(); this.strDate = vs[2].ToString(); LstFileFullName.Add(StrLocalPath); LstFileName.Add(System.Text.RegularExpressions.Regex.Replace(dataSourceInfo.Path, "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", "")); AppLog.Error("打印日志 " + this.strTableName); AppLog.Error("打印日志 " + this.strDateColumn); AppLog.Error("打印日志 " + this.strDate); AppLog.Error("打印日志 " + this.StrPwd); AppLog.Error("打印日志 " + this.StrUser); AppLog.Error("打印日志 " + this.StrLocalPath); AppLog.Error("打印日志 " + FileHelper.getBasePath()); AppLog.Error("打印日志 " + dataSourceInfo.Path); } else { //遍历当前路径,找到所有“今天”的数据文件,复制形成缓冲文件 string strSuffix = AccessFormat.AccessSuffix; if (dataSourceInfo.Path == "") return; DirectoryInfo root = new DirectoryInfo(dataSourceInfo.Path); 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(AccessFormat.AccessFileNameFormat))) //“今天”产生的数据 { RemoteFileCopy remoteFileCopy = new RemoteFileCopy(dataSourceInfo); remoteFileCopy.CopyFileFromRemote(".mdb", name); LstFileFullName.Add(FileHelper.getBasePath() + "\\Cache\\" + System.Text.RegularExpressions.Regex.Replace(dataSourceInfo.Path + name, "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", "") + ".mdb"); LstFileName.Add(System.Text.RegularExpressions.Regex.Replace(dataSourceInfo.Path + name, "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", "")); } } this.strTableName = AccessFormat.AccessFileTableNameFormat == null ? vs[0].ToString() : AccessFormat.AccessFileTableNameFormat; this.strDateColumn = vs[1].ToString(); this.strDate = vs[2].ToString(); } } catch (Exception ex) { AppLog.Error(ex.Message); } } public override Dictionary GetInstrumentData() { //return accessDatabaseService.GetInstrumentData( // new AccessOpenParams // { // StrPath = StrLocalPath, // StrPwd = StrPwd, // AccessVersionInfo = AccessFormat.AccessFileVersion // } // ); Dictionary dictReturn = new Dictionary(); try { foreach (string strPath in LstFileFullName) { Dictionary dictCurrent = accessDatabaseService.GetInstrumentData( new AccessOpenParams { StrPath = strPath, StrPwd = StrPwd, AccessVersionInfo = AccessFormat.AccessFileVersion } ); if (dictCurrent.Count > 0) { foreach (var item in dictCurrent) { if (!dictReturn.ContainsKey(item.Key)) //如果不包含,则直接增加 { dictReturn.Add(item.Key, item.Value); } //else //如果已经存在相同名字的表单,则合并数据 //{ // DataTable dtCompine = item.Value.Clone(); // foreach (DataRow drs in item.Value.Rows) // { // DataRow drNew = dictReturn[item.Key].NewRow(); // drNew.ItemArray = drs.ItemArray; // dictReturn[item.Key].Rows.Add(drNew); // } //} } } } } catch (Exception ex) { AppLog.Error(ex.Message); } return dictReturn; } public override DataTable GetInstrumentDataByDate() { DataTable dtCompine = new DataTable(); List dataTables = new List(); for (int i = 0; i < LstFileFullName.Count; i++) { DataTable dt = accessDatabaseService.GetInstrumentDataByDate( new AccessOpenParams { StrPath = LstFileFullName[i], StrUser = StrUser, StrPwd = StrPwd, AccessVersionInfo = AccessFormat.AccessFileVersion, AccessSpecialDateFormat = AccessFormat.AccessFileDateColumnFormat, autoSql = AccessFormat.AutoSql }, new ConditionParams { TableName = strTableName, DateColumn = strDateColumn, DateValue = strDate } ); dataTables.Add(dt); } if (dataTables.Count > 0) { if (dataTables.Count == 1) //如果只有一个表,不需要再重复循环一次了 dtCompine = dataTables[0]; else { dtCompine = dataTables[0].Clone(); try { foreach (DataTable dt1 in dataTables) { foreach (DataRow dr in dt1.Rows) { DataRow drNew = dtCompine.NewRow(); foreach (DataColumn dc in dtCompine.Columns) { //if (dc.ColumnName.Trim().ToLower() == strDateColumn.Trim().ToLower()) //{ // DateTime dt = DateTime.Now; // //AppLog.Info("4-" + dr[dc.ColumnName].ToString()); // if (DateTime.TryParse(dr[dc.ColumnName].ToString(), out dt)) // { // drNew[dc.ColumnName] = dr[dc.ColumnName].ToString(); // } // else // { // //AppLog.Info("5-" + AccessFormat.AccessFileDateColumnFormat); // DateTime dtTime = DateTime.ParseExact(dr[dc.ColumnName].ToString(), AccessFormat.AccessFileDateColumnFormat, new System.Globalization.CultureInfo("zh-CN", true)); // if (dtTime.ToString("HH:mm:ss") == "00:00:00") // drNew[dc.ColumnName] = dtTime.ToString("yyyy-MM-dd"); // else // drNew[dc.ColumnName] = dtTime.ToString("yyyy-MM-dd HH:mm:ss"); // } //} //else //{ // drNew[dc.ColumnName] = dr[dc.ColumnName]; //} drNew[dc.ColumnName] = dr[dc.ColumnName]; } dtCompine.Rows.Add(drNew); } } } catch (Exception ex) { AppLog.Error(ex.Message); } } } return dtCompine; } public override DataTable GetInstrumentDataStruct() { return accessDatabaseService.GetInstrumentDataStruct( new AccessOpenParams { StrPath = LstFileFullName[0], StrUser = StrUser, StrPwd = StrPwd, AccessVersionInfo = AccessFormat.AccessFileVersion, autoSql = AccessFormat.AutoSql }, new ConditionParams { TableName = strTableName } ); } } }