|
-
- using CnasSynchronousCommon;
- using CnasSynchronusClient;
- using CnasSynchrousModel;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Timers;
- using System.Xml.Serialization;
-
- namespace CNAS_SyncService
- {
- public class SyncServiceOperation
- {
- System.Timers.Timer timerSync;
- ServiceConfig config = new ServiceConfig();
- bool bRunComplete = true;
- public void Start()
- {
- AppLog.ServiceInfo("SyncSerivce:服务启动");
- config = ReadServiceConfigData();
- if (config.Minutes != 0)
- {
- // 启动定时器
- timerSync = new System.Timers.Timer();
- timerSync.Interval = config.Minutes * 60 * 1000; //设置计时器事件间隔执行时间
- timerSync.Elapsed += new System.Timers.ElapsedEventHandler(timerSync_Elapsed);
- timerSync.Enabled = true;
- }
- else
- {
- //写入日志
- AppLog.ServiceInfo("SyncSerivce:未能成功读取定时器时间,本次同步失败。");
- }
- }
-
- private void timerSync_Elapsed(object sender, ElapsedEventArgs e)
- {
- AppLog.ServiceInfo("定时器启动");
- if (bRunComplete)
- {
- bRunComplete = false;
- //执行同步程序
- //1.读取配置信息
- List<SyncInstrumentItemInfo> lstSyncInstrument = ReadSyncInstruments();
- //2 遍历仪器列表,逐个列表进行同步
- foreach (var instrumentItem in lstSyncInstrument)
- {
- RunSyncByInstrument(instrumentItem);
- }
- bRunComplete = true;
-
- //回收垃圾
- GC.Collect();
- }
- }
-
- private List<SyncInstrumentItemInfo> ReadSyncInstruments()
- {
- List<SyncInstrumentItemInfo> lstSyncInstrument = new List<SyncInstrumentItemInfo>();
- lstSyncInstrument = FileOperation.GetLocalSyncInStrumentData();
- AppLog.ServiceInfo($"读取到本地配置信息行数{lstSyncInstrument.Count}");
- return lstSyncInstrument;
- }
-
- private void RunSyncByInstrument(SyncInstrumentItemInfo syncInstrumentItem)
- {
- //0.读取目标库表数据的最大时间 1.读取来源的数据 2.对读取到的数据进行逻辑处理,整理格式 3.执行同步
- AppLog.ServiceInfo("准备根据日期字段查询筛选");
- var query = syncInstrumentItem.LstSyncPramas.Where(s => s.IfDateField == true).ToList<SyncParamasInfo>();
- if (query.Count == 1)
- {
- string strCompareTime = "";
- string strDateTime = CnasDataOperationFact.CnasDataOperation().GetMaxTimeByTableName(syncInstrumentItem.SyncTargetDBInfo, syncInstrumentItem.LstSyncPramas[0].TargetTable, query[0].TargetField, syncInstrumentItem.CnasInstrumentColumn,syncInstrumentItem.GUID);
-
- //strDateTime = "2016-07-01 00:00:00";
- if (strDateTime == "1899-1-1")
- {
- AppLog.ServiceInfo($"读取上次最晚执行时间数据库连接失败。");
- return;
- }
- else if (strDateTime == "")
- {
- strCompareTime = Convert.ToDateTime(config.InitalDT.ToString()).ToString("yyyy-MM-dd HH:mm:ss");
- }
- else
- strCompareTime = Convert.ToDateTime(strDateTime).AddDays(-3).ToString("yyyy-MM-dd HH:mm:ss");
- AppLog.ServiceInfo($"读取上次最晚执行时间为{strCompareTime}");
-
- object[] obj = new object[]
- {
- syncInstrumentItem.LstSyncPramas[0].SourceTable,
- query[0].SourceField,
- strCompareTime
- };
- AppLog.ServiceInfo($"准备构建数据源读取工厂");
- InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(syncInstrumentItem.SyncInstrumentDSInfo, obj);
- AppLog.ServiceInfo($"构建数据源读取工厂完成,准备读取数据源");
- DataTable dtReadySource = instrumentData.GetInstrumentDataByDate();
- AppLog.ServiceInfo($"根据日期读取准备插入的来源数据,共{dtReadySource.Rows.Count}条数据");
-
- string strMsg = "";
- RunUpLoad(syncInstrumentItem, dtReadySource, ref strMsg);
- if (strMsg != "")
- {
- //写入日志
- AppLog.ServiceInfo(strMsg);
- }
- }
- else
- {
- AppLog.ServiceInfo("SyncSerivce:未能成功读取日期字段,可能不存在或存在多个日期字段,本次同步失败。");
- }
- }
-
- private void RunUpLoad(SyncInstrumentItemInfo syncInstrumentItem,DataTable dtReadySource,ref string strMsg)
- {
- //根据映射字段获取准备上传的所有数据
- if (dtReadySource == null || dtReadySource.Rows.Count <= 0) return;
-
- //构建准备插入的数据
- DataTable dtTarget = CnasDataOperationFact.CnasDataOperation().GetCNASTablesStruct(syncInstrumentItem.LstSyncPramas[0].TargetTable, syncInstrumentItem.SyncTargetDBInfo);
- AppLog.ServiceInfo("读取目标数据的数据结构");
-
- if (dtTarget.Columns.Count <= 0)
- {
- strMsg="未能成功读取CNAS数据库列,请检查数据库配置。";
- return;
- }
-
- CnasInsertOperation insertOperation = new CnasInsertOperation()
- {
- syncInstrumentItem = syncInstrumentItem
- };
- AppLog.ServiceInfo("遍历所有来源数据,构造准备插入的数据");
- strMsg=insertOperation.CreateInputData(dtReadySource, ref dtTarget);
- if (strMsg != "") return;
- if (dtTarget.Rows.Count <= 0)
- {
- strMsg = "创建准备插入的数据行为空。";
- return;
- }
-
- AppLog.ServiceInfo("检查准备插入的数据是否合法");
- insertOperation.CheckInsertDataFormat(dtTarget, ref strMsg);
- if (strMsg != "") return;
-
- //逐行执行插入
- List<DataRow> lstError = new List<DataRow>();
- int ErrorCount = 0;
- int SuccessCount = 0;
- int OtherCount = 0;
- AppLog.ServiceInfo($"逐行插入目标数据,最终准备插入(更新)数据行{dtTarget.Rows.Count}");
- foreach (DataRow dr in dtTarget.Rows)
- {
- dr.AcceptChanges();
- int iReturn = CnasDataOperationFact.CnasDataOperation().InsertDataToCNASTable(GlobalCommonOperation.ConvertDataRowToTable(dr), syncInstrumentItem.SyncTargetDBInfo, syncInstrumentItem.LstSyncPramas, syncInstrumentItem.CnasInstrumentColumn, syncInstrumentItem.lstFixedValue);
- if (iReturn <= 0) //此时出现问题
- {
- if (iReturn == -1)
- {
- AppLog.ServiceInfo("数据库连接中断,终止本次上传。");
- break; //此时数据库连接中断,直接跳出循环,结束本次数据同步传输
- }
- else if (iReturn == -2) //等于-2表示插入准备更新时发现数据一致,不再执行
- {
- OtherCount++;
- }
- else
- {
- ErrorCount++;
- //lstError.Add(GlobalCommonOperation.ConvertDataRowToTable(dr).Rows[0]);
- }
- }
- else
- {
- SuccessCount++;
- AppLog.Info("成功插入(更新)1条数据。");
- }
- }
-
- //if (lstError.Count <= 0)
- //{
- // strMsg = "上传完成!...................................................................................................................................";
- //}
- //else
- //{
- // //log.ErrorFormat("未成功上传的数据如下:{0}", ListToString(lstError));
- // strMsg = "上传过程中发生异常,存在部分数据未成功上传,请联系管理员!";
- //}
- strMsg= $"上传操作完成!其中成功{SuccessCount}条,失败{ErrorCount}条,其他{OtherCount}条。............................................................................................................";
- }
- private ServiceConfig ReadServiceConfigData()
- {
- ServiceConfig config = new ServiceConfig();
- try
- {
- //读取本地文件中存储的配置信息
- XmlSerializer serializer = new XmlSerializer(config.GetType());
- FileStream stream = new FileStream(FileHelper.getBasePath() + "/Data/SyncServiceData.xml", FileMode.Open);
- config = (ServiceConfig)serializer.Deserialize(stream);
- stream.Close();
- }
- catch (Exception ex)
- {
- AppLog.ServiceInfo(ex.Message);
- }
- return config;
- }
- }
- }
|