protected SyncData(OracleDbOperation targetDb, OracleDbOperation localDb, string targetCode, string localCode, FtpHelper targetFtp, FtpHelper localFtp, StringBuilder sbLog) { this.targetDb = targetDb; this.localDb = localDb; this.targetCode = targetCode; this.localCode = localCode; this.targetFtp = targetFtp; this.localFtp = localFtp; this.sbLog = sbLog; dtOperation = new DataTableOperation(); deleteAction = () => { LogOperation.Append(sbLog, "删除成功"); }; totalAction = (tableName, count) => { LogOperation.Append(sbLog, "统计:" + tableName + "表共同步了" + count + "条数据"); }; deleteFileAction = (itemCode, fileName) => { LogOperation.Append(sbLog, string.Format("项目编号:{0}, 删除文件:{1}", itemCode, fileName)); }; ftpCopyAction = (itemCode, fileCount) => { LogOperation.Append(sbLog, string.Format("项目编号:{0}, 拷贝文件数:{1}", itemCode, fileCount.ToString())); }; }
/// <summary> /// 同步。 /// </summary> /// <param name="setting">同步配置。</param> /// <param name="log">日志信息。</param> /// <returns>同步是否成功。</returns> public bool Sync(SyncConfig syncConfig, SyncConfigTarget targetConfig, SyncConfigLocal localConfig, ref StringBuilder sbLog) { bool result = false; LogOperation.Append(sbLog, string.Format("开始同步数据:{0}({1}) 开始时间:{2}", targetConfig.Name, targetConfig.Code, DateTime.Now)); OracleDbOperation localDb = new OracleDbOperation(localConfig.DbConfig.ConnStr); FtpHelper localFtp = new FtpHelper(localConfig.FtpConfig.Ip, localConfig.FtpConfig.Port, localConfig.FtpConfig.Username, localConfig.FtpConfig.Password); OracleDbOperation targetDb = new OracleDbOperation(targetConfig.DbConfig.ConnStr); FtpHelper targetFtp = new FtpHelper(targetConfig.FtpConfig.Ip, targetConfig.FtpConfig.Port, targetConfig.FtpConfig.Username, targetConfig.FtpConfig.Password); try { targetDb.BeginTransaction(); localDb.BeginTransaction(); List <SyncData> syncDataList = new List <SyncData>(); ////系统数据 syncDataList.Add(new SyncDataSystemInfo(targetDb, localDb, targetConfig.Code, localConfig.Code, targetFtp, localFtp, sbLog)); ////项目数据 syncDataList.Add(new SyncDataItem(syncConfig, targetDb, localDb, targetConfig.Code, localConfig.Code, targetFtp, localFtp, sbLog)); ////流程数据 syncDataList.Add(new SyncDataWorkflow(targetDb, localDb, targetConfig.Code, localConfig.Code, targetFtp, localFtp, sbLog)); ////Syncs.Add(new SyncXtxx(targetDbOperation, localDbOperation, targetSetting.Code, localSetting.Code, targetFtpOperation, localFtpOperation, sbLog)); foreach (var syncData in syncDataList) { syncData.Sync(); } targetDb.Commit(); localDb.Commit(); result = true; } catch (Exception e) { targetDb.Rollback(); localDb.Rollback(); LogOperation.WriteExceptionLog(sbLog, e); } sbLog.AppendLine(); sbLog.AppendLine("结束时间:" + DateTime.Now); return(result); }
public override void Sync() { LogOperation.WriteTitle(sbLog, "-------------------------------------同步流程信息开始-------------------------------------"); string companyCodeConditionString = "ccode = '" + targetCode + "'"; List <string> workflowId = new List <string>(); LogOperation.WriteSyncTableTitle(sbLog, "wf_workflow", SyncType.AddAfterDelete, companyCodeConditionString); SyncByAddAfterDelete("wf_workflow", companyCodeConditionString, deleteAction, (dr) => { LogOperation.Append(sbLog, "流程ID:" + dr["FLOWID"] + "。"); workflowId.Add("'" + dr["FLOWID"] + "'"); }, totalAction); string workflowConditionString = "flowid in (" + string.Join(",", workflowId.ToArray()) + ")"; LogOperation.WriteSyncTableTitle(sbLog, "wf_node", SyncType.AddAfterDelete, workflowConditionString); SyncByAddAfterDelete("wf_node", workflowConditionString, deleteAction, (dr) => { LogOperation.Append(sbLog, "流程ID:" + dr["FLOWID"] + "," + "环节ID" + dr["NODEID"] + "。"); }, totalAction); LogOperation.WriteSyncTableTitle(sbLog, "wf_file", SyncType.Comparer, string.Empty); SyncByComparer("wf_file", string.Empty, new List <string>() { "FILECODE" }, false, (dr) => { LogOperation.Append(sbLog, "添加 文档编码:" + dr["FILECODE"]); }, (dr) => { LogOperation.Append(sbLog, "删除 文档编码:" + dr["FILECODE"]); }, (drTarget, drLocal) => { LogOperation.Append(sbLog, "更新 文档编码:" + drTarget["FILECODE"]); }); LogOperation.WriteTitle(sbLog, "-------------------------------------同步流程信息结束-------------------------------------"); }
public override void Sync() { MessageManage messageManage = new MessageManage(); Action <DataRow> actionAddLocalMessageFile = new Action <DataRow>((dr) => { string xxbh = dr["xxbh"].ToString(); string messageFtpPath = CommonManage.GetFtpMessagePath(); LogOperation.Append(sbLog, "从" + targetCode + "到" + localCode + "同步系统消息(" + xxbh + ")的文件。"); targetFtp.CopyDirectory(messageFtpPath, xxbh, localFtp, messageFtpPath, xxbh, null); }); Action <DataRow> actionAddTargetMessageFile = new Action <DataRow>((dr) => { string xxbh = dr["xxbh"].ToString(); string messageFtpPath = CommonManage.GetFtpMessagePath(); LogOperation.Append(sbLog, "从" + localCode + "到" + targetCode + "同步系统消息(" + xxbh + ")的文件。"); localFtp.CopyDirectory(messageFtpPath, xxbh, targetFtp, messageFtpPath, xxbh, null); }); Action <DataRow> actionDeleteLocalMessageFile = new Action <DataRow>((dr) => { string xxbh = dr["xxbh"].ToString(); LogOperation.Append(sbLog, "删除" + localCode + "的系统消息(" + xxbh + ")的文件。"); localFtp.DeleteDirectory(CommonManage.GetFtpMessagePath(xxbh)); }); Action <DataRow> actionDeleteTargetMessageFile = new Action <DataRow>((dr) => { string xxbh = dr["xxbh"].ToString(); LogOperation.Append(sbLog, "删除" + targetCode + "的系统消息(" + xxbh + ")的文件。"); targetFtp.DeleteDirectory(CommonManage.GetFtpMessagePath(xxbh)); }); LogOperation.WriteTitle(sbLog, "同步系统消息开始(从" + targetCode + "到" + localCode + ")。"); SyncXxbh(targetDb, localDb, localCode, actionAddLocalMessageFile, actionDeleteTargetMessageFile, actionDeleteLocalMessageFile); LogOperation.WriteTitle(sbLog, "同步系统消息结束(从" + targetCode + "到" + localCode + ")。"); LogOperation.WriteTitle(sbLog, "同步系统消息开始(从" + localCode + "到" + targetCode + ")。"); SyncXxbh(localDb, targetDb, targetCode, actionAddTargetMessageFile, actionDeleteLocalMessageFile, actionDeleteTargetMessageFile); LogOperation.WriteTitle(sbLog, "同步系统消息结束(从" + localCode + "到" + targetCode + ")。"); }
private void SyncXxbh(OracleDbOperation sourceDbOperation, OracleDbOperation targetDbOperation, string targetCode, Action <DataRow> actionAddTargetMessageFile, Action <DataRow> actionDeleteSourceMessageFile, Action <DataRow> actionDeleteTargetMessageFile) { string sql; List <OracleParameter> parameters = new List <OracleParameter>(); DataTable dt; //用指定的行政区代码去系统信息接收表中查询所有与接收人行政区代码相等的记录,即需要同步的信息编号。 sql = "select distinct(jsxxbh) from xtxxjs where jsrxzdm = :jsrxzdm"; parameters.Clear(); parameters.Add(new OracleParameter("jsrxzdm", targetCode)); dt = sourceDbOperation.ExecuteDataTable(sql, "temp", parameters); var xxbhs = (from DataRow dr in dt.Rows select "'" + dr[0] + "'").ToArray(); if (xxbhs.Length > 0) { //分别取源系统和目标系统取出需要同步的信息接收数据。 string xxbhsCondition = string.Join(",", xxbhs); sql = "select * from xtxxjs where jsxxbh in (" + xxbhsCondition + ")"; DataTable dtSourceXtxxjs = sourceDbOperation.ExecuteDataTable(sql, "xtxxjs", null); DataTable dtTargetXtxxjs = targetDbOperation.ExecuteDataTable(sql, "xtxxjs", null); string xtxxCondition = "xxbh in (" + xxbhsCondition + ")"; //进行比对同步。 LogOperation.WriteSyncTableTitle(sbLog, "xtxx", SyncType.Comparer, xtxxCondition); SyncByComparer(sourceDbOperation, targetDbOperation, "xtxx", xtxxCondition, new List <string>() { "xxbh" }, true, (dr) => { string xxbh = dr["xxbh"].ToString(); LogOperation.Append(sbLog, "添加目标系统信息!信息编号:" + xxbh); foreach (var needAddXtxxjs in dtSourceXtxxjs.Select("jsxxbh = '" + xxbh + "' and jsrxzdm = '" + targetCode + "'")) { DataRow newXtxxjs = dtTargetXtxxjs.NewRow(); Copy(needAddXtxxjs, newXtxxjs); dtTargetXtxxjs.Rows.Add(newXtxxjs); } if (actionAddTargetMessageFile != null) { actionAddTargetMessageFile(dr); } }, (dr) => { LogOperation.Append(sbLog, "删除垃圾系统信息!信息编号:" + dr["XXBH"]); }, (drSource, drTarget) => { string xxbh = drSource["XXBH"].ToString(); LogOperation.Append(sbLog, "更新系统信息!信息编号:" + xxbh); if (actionAddTargetMessageFile != null) { actionAddTargetMessageFile(drSource); } //判断是否需要删除邮件。 if (drSource["FSRSC"].ToString() == "1") { var querySource = from DataRow dr in dtSourceXtxxjs.Rows where dr["JSXXBH"].ToString() == xxbh select dr; var queryTarget = from DataRow dr in dtTargetXtxxjs.Rows where dr["JSXXBH"].ToString() == xxbh select dr; bool needDelete = true; foreach (var itemSource in querySource) { string jsr = itemSource["JSR"].ToString(); var queryTargetJsr = (from dr in queryTarget where dr["JSR"].ToString() == jsr select dr).FirstOrDefault(); if (queryTargetJsr == null) { if (itemSource["JSRSC"].ToString() == "0") { needDelete = false; break; } } else { if (queryTargetJsr["JSRSC"].ToString() == "0") { needDelete = false; break; } } } //删除邮件。 if (needDelete) { dtOperation.DeleteDbDataRow(drSource, (dr) => { dtOperation.DeleteDbDataRows(querySource, null); LogOperation.Append(sbLog, "删除源系统信息,因为收发双方都已删除此信息!信息编号:" + xxbh); actionDeleteSourceMessageFile(drSource); }); dtOperation.DeleteDbDataRow(drTarget, (dr) => { dtOperation.DeleteDbDataRows(queryTarget, null); LogOperation.Append(sbLog, "删除目标系统信息,因为收发双方都已删除此信息!信息编号:" + xxbh); actionDeleteSourceMessageFile(drTarget); }); } } }); //保存信息接收信息。 sourceDbOperation.UpdateDataTable(dtSourceXtxxjs); targetDbOperation.UpdateDataTable(dtTargetXtxxjs); } }
public override void Sync() { LogOperation.WriteTitle(sbLog, "-------------------------------------同步系统信息开始-------------------------------------"); Action <DataRow> companyDetailAction = (dr) => { LogOperation.Append(sbLog, "单位编码:" + dr["CCODE"]); }; Action <DataRow> departmentDetailAction = (dr) => { LogOperation.Append(sbLog, "部门编码:" + dr["DEPTCODE"]); }; Action <DataRow> userDetailAction = (dr) => { LogOperation.Append(sbLog, "用户ID:" + dr["USERID"]); }; string targetCompanyCodeConditionString = "ccode like '" + targetCode.Substring(0, 4) + "%'"; LogOperation.Append(sbLog, "同步系统信息(从" + targetCode + "到" + localCode + ")。"); LogOperation.WriteSyncTableTitle(sbLog, "company", SyncType.Comparer, targetCompanyCodeConditionString); SyncByComparer(targetDb, localDb, "company", targetCompanyCodeConditionString, new List <string>() { "CCODE" }, true, (dr) => { LogOperation.Append(sbLog, "添加行政区单位!单位编码:" + dr["CCODE"]); }, (dr) => { LogOperation.Append(sbLog, "删除行政区单位!单位编码:" + dr["CCODE"]); }, (drTarget, drLocal) => { LogOperation.Append(sbLog, "更新行政区单位!单位编码:" + drTarget["CCODE"]); }); LogOperation.WriteSyncTableTitle(sbLog, "department", SyncType.AddAfterDelete, targetCompanyCodeConditionString); SyncByAddAfterDelete(targetDb, localDb, "department", targetCompanyCodeConditionString, deleteAction, departmentDetailAction, totalAction); LogOperation.WriteSyncTableTitle(sbLog, "users", SyncType.AddAfterDelete, targetCompanyCodeConditionString); SyncByAddAfterDelete(targetDb, localDb, "users", targetCompanyCodeConditionString, deleteAction, userDetailAction, totalAction); //string localCompanyCodeConditionString = "ccode like '" + localCode.Substring(0, 4) + "%'"; //LogOperation.Append(sbLog, "同步系统信息(从" + localCode + "到" + targetCode + ")。"); //LogOperation.WriteSyncTableTitle(sbLog, "company", SyncType.AddAfterDelete, localCompanyCodeConditionString); //SyncByComparer(localDb, targetDb, "company", localCompanyCodeConditionString, new List<string>() { "CCODE" }, true, // (dr) => // { // LogOperation.Append(sbLog, "添加行政区单位!单位编码:" + dr["CCODE"]); // }, // (dr) => // { // LogOperation.Append(sbLog, "删除行政区单位!单位编码:" + dr["CCODE"]); // }, // (drTarget, drLocal) => // { // LogOperation.Append(sbLog, "更新行政区单位!单位编码:" + drTarget["CCODE"]); // }); //LogOperation.WriteSyncTableTitle(sbLog, "department", SyncType.AddAfterDelete, localCompanyCodeConditionString); //SyncByAddAfterDelete(localDb, targetDb, "department", localCompanyCodeConditionString, deleteAction, departmentDetailAction, totalAction); //LogOperation.WriteSyncTableTitle(sbLog, "users", SyncType.AddAfterDelete, localCompanyCodeConditionString); //SyncByAddAfterDelete(localDb, targetDb, "users", localCompanyCodeConditionString, deleteAction, userDetailAction, totalAction); LogOperation.WriteTitle(sbLog, "-------------------------------------同步系统信息结束-------------------------------------"); }
public override void Sync() { LogOperation.WriteTitle(sbLog, "-------------------------------------同步项目信息开始-------------------------------------"); LogOperation.WriteTitle(sbLog, string.Format("同步数据方式:{0}", syncConfig.SyncType.Equals(1) ? "增量同步" : "全部同步")); Action <DataRow, DataRow> checkTbzt = (drA, drB) => { ChangeTbzt(drA); if (ChangeTbzt(drB)) { LogOperation.Append(sbLog, "标记成已同步项目:" + drB["ITEMCODE"].ToString() + ""); } }; //在目标数据库中查询需要同步的项目信息。 string strWhere = string.Empty; if (syncConfig.SyncType == 1) { strWhere = "and TBZT != 1"; } string queryTargetUnSyncXmxx = string.Format("select * from xm_xmxx where 1=1 {0} order by itemCode", strWhere); DataTable dtXmxxTarget = targetDb.ExecuteDataTable(queryTargetUnSyncXmxx, "xm_xmxx", null); int rowCount = dtXmxxTarget.Rows.Count; if (rowCount > 0) { string[] itemCodes = new string[rowCount]; string[] conditonItemCodes = new string[rowCount]; for (int i = 0; i < rowCount; i++) { string itemCode = dtXmxxTarget.Rows[i]["ITEMCODE"].ToString(); conditonItemCodes[i] = "'" + itemCode + "'"; itemCodes[i] = itemCode; } string itemCodeConditionString = " itemcode in (" + string.Join(",", conditonItemCodes) + ")"; #region 步项目信息 LogOperation.WriteSyncTableTitle(sbLog, "xm_xmxx", SyncType.Comparer, itemCodeConditionString); SyncByComparer("xm_xmxx", itemCodeConditionString, new List <string>() { "ITEMCODE" }, true, (dr) => { LogOperation.Append(sbLog, "添加 项目编号:" + dr["ITEMCODE"]); }, (dr) => { LogOperation.Append(sbLog, "删除 项目编号:" + dr["ITEMCODE"]); }, (drTarget, drLocal) => { LogOperation.Append(sbLog, "更新 项目编号:" + drTarget["ITEMCODE"]); }, checkTbzt); #endregion #region 步项目相关信息 List <string> dkxxIds = new List <string>(); SyncByAddAfterDelete("gis_data", itemCodeConditionString, deleteAction, (dr) => { dkxxIds.Add("'" + dr["GisId"] + "'"); }, totalAction); if (dkxxIds.Count > 0) { string dkjzdConditionString = " GisId in (" + string.Join(",", dkxxIds.ToArray()) + ")"; SyncByAddAfterDelete("gis_dk", dkjzdConditionString, deleteAction, null, totalAction); SyncByAddAfterDelete("gis_jzd", dkjzdConditionString, deleteAction, null, totalAction); SyncByAddAfterDelete("gis_dltb", dkjzdConditionString, deleteAction, null, totalAction); SyncByAddAfterDelete("gis_xzdw", dkjzdConditionString, deleteAction, null, totalAction); } string xzfConditionString = "XZDM = " + targetCode; SyncByAddAfterDelete("xm_xzf", xzfConditionString, deleteAction, null, totalAction); //SyncByAddAfterDelete("xm_xmxx", itemCodeConditionString, deleteAction, null, totalAction); SyncByAddAfterDelete("xm_sb_jbxx", itemCodeConditionString, deleteAction, null, totalAction); SyncByAddAfterDelete("xm_ky_jbxx", itemCodeConditionString, deleteAction, null, totalAction); SyncByAddAfterDelete("xm_ghsjysxx", itemCodeConditionString, deleteAction, null, totalAction); SyncByAddAfterDelete("xm_gcxx", itemCodeConditionString, deleteAction, null, totalAction); SyncByAddAfterDelete("xm_gdxx", itemCodeConditionString, deleteAction, null, totalAction); SyncByAddAfterDelete("xm_jsxx", itemCodeConditionString, deleteAction, null, totalAction); SyncByAddAfterDelete("xm_ss_bgxx", itemCodeConditionString, deleteAction, null, totalAction); SyncByAddAfterDelete("xm_ss_gcjl_jlht", itemCodeConditionString, deleteAction, null, totalAction); SyncByAddAfterDelete("xm_ss_gcjl_jlry", itemCodeConditionString, deleteAction, null, totalAction); SyncByAddAfterDelete("xm_ss_jdgz_xmbb", itemCodeConditionString, deleteAction, null, totalAction); SyncByAddAfterDelete("xm_ss_zjbf", itemCodeConditionString, deleteAction, null, totalAction); SyncByAddAfterDelete("xm_ss_ztb_jbxx", itemCodeConditionString, deleteAction, null, totalAction); SyncByAddAfterDelete("xm_ss_ztb_sght", itemCodeConditionString, deleteAction, null, totalAction); SyncByAddAfterDelete("xm_ss_ztb_zbqk", itemCodeConditionString, deleteAction, null, totalAction); SyncByAddAfterDelete("xm_xmdw", itemCodeConditionString, deleteAction, null, totalAction); SyncByAddAfterDelete("xm_xmzj", itemCodeConditionString, deleteAction, null, totalAction); SyncByAddAfterDelete("xm_ysxx", itemCodeConditionString, deleteAction, null, totalAction); SyncByAddAfterDelete("wf_instance", itemCodeConditionString, deleteAction, null, totalAction); SyncByAddAfterDelete("item_file", itemCodeConditionString, deleteAction, null, totalAction); //清除垃圾文件。 LogOperation.WriteTitle(sbLog, "--------开始检查项目垃圾文件--------"); foreach (var itemCode in itemCodes) { string filePath = CommonManage.GetFtpUploadPath(targetCode) + itemCode; string itemFileTableName = "item_file"; string queryFileSql = "select * from " + itemFileTableName + " where itemcode = '" + itemCode + "'"; DataTable dtItemFile = targetDb.ExecuteDataTable(queryFileSql, itemFileTableName, null); if (dtItemFile.Rows.Count > 0) { List <string> exceptions = (from DataRow dr in dtItemFile.Rows select dr["FILENAME"].ToString()).ToList(); targetFtp.ClearDirectory(itemCode, filePath, exceptions, deleteFileAction); } } LogOperation.WriteTitle(sbLog, "--------结束检查项目垃圾文件--------"); //LogOperation.WriteSyncTableTitle(sbLog, "item_file", SyncType.AddAfterDelete, itemCodeConditionString); LogOperation.WriteTitle(sbLog, "--------开始同步项目文件(FTP)--------"); foreach (var itemCode in itemCodes) { string filePath = CommonManage.GetFtpUploadPath(targetCode); try { targetFtp.CopyDirectory(filePath, itemCode, localFtp, filePath, itemCode, ftpCopyAction); } catch (Exception ex) { LogOperation.Append(sbLog, string.Format("异常 项目编号:{0}; 原因:{1}", itemCode, ex.Message)); continue; } //string gisPath = CommonManage.GetFtpGisQueryPath() + itemCode + ".xml"; //targetFtpOperation.CopyFile(gisPath, localFtpOperation, gisPath); } LogOperation.WriteTitle(sbLog, "--------结束同步项目文件(FTP)--------"); #endregion ////同步GIS数据 if (this.syncConfig.SyncGisData) { LogOperation.WriteTitle(sbLog, "--------开始调用GIS服务--------"); foreach (var itemCode in itemCodes) { ////得到连接字符串 Gis_Data <Gis_Dk> gisdata = new BusiItemManage_Gis().GetNewGisInfo(itemCode, targetDb.ConnectionStr, false); IGBXM <GBDK> gisEntity = new BusiItemManage_Gis().Conversion(gisdata); gisEntity.Ccode = itemCode.Substring(0, 6); string wsUrl = this.syncConfig.ServiceUrl; string method = this.syncConfig.ServiceMethod; object[] args = new object[1]; args[0] = JsonConvert.SerializeObject(gisEntity); try { bool succ = (bool)WebServiceHelper.InvokeWebService(wsUrl, method, args); if (!succ) { LogOperation.Append(sbLog, "服务返回失败:" + itemCode); } } catch (Exception ex) { LogOperation.Append(sbLog, string.Format("异常 项目编号:{0}; 原因:{1}", itemCode, ex.Message)); continue; } } LogOperation.WriteTitle(sbLog, "--------结束调用GIS服务--------"); } } LogOperation.WriteTitle(sbLog, "-------------------------------------同步项目信息结束-------------------------------------"); }