594e020e24
SVN-Revision: r1064
853 lines
48 KiB
C#
853 lines
48 KiB
C#
using MySql.Data.MySqlClient;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace AfkDataService
|
|
{
|
|
public class SynchData
|
|
{
|
|
public FrmMain frmMain;
|
|
/// <summary>
|
|
/// sqlserver连接对象
|
|
/// </summary>
|
|
public AfkDataService.SqlHelper SqlHelper;
|
|
/// <summary>
|
|
/// sql连接字符串
|
|
/// </summary>
|
|
public string SqlConnectStr;
|
|
/// <summary>
|
|
/// mysql连接对象
|
|
/// </summary>
|
|
public AfkDataService.MySqlHelper MySqlHelper;
|
|
/// <summary>
|
|
/// mysql连接字符串
|
|
/// </summary>
|
|
public string MySqlConnectStr;
|
|
public SynchData(AfkDataService.SqlHelper sqlHelper, AfkDataService.MySqlHelper mySqlHelper)
|
|
{
|
|
this.SqlHelper = sqlHelper;
|
|
this.MySqlHelper = mySqlHelper;
|
|
}
|
|
/// <summary>
|
|
/// 判断表是否存在
|
|
/// </summary>
|
|
/// <param name="abutmentModel"></param>
|
|
private void CreateTable(AbutmentModel abutmentModel)
|
|
{
|
|
if (abutmentModel.dataDirection == DataDirection.AfkToLs)//afk到ls
|
|
{
|
|
// 获取lsSQL表结构
|
|
DataTable schemaTable = SqlHelper.ExecuteDataTable($"select * from information_schema.columns where table_name = '{abutmentModel.lsTabName}'");
|
|
if (!CheckIfTableExists(abutmentModel.lsTempTabName, SqlHelper))
|
|
{
|
|
CreateSqlServerTableWithSpecialFields(abutmentModel.lsTempTabName, schemaTable, SqlHelper);
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 同步数据表
|
|
/// </summary>
|
|
/// <param name="abutmentModel"></param>
|
|
public bool SyncDataTable(AbutmentModel abutmentModel)
|
|
{
|
|
bool isSuccess = false;
|
|
abutmentModel.allCount = 0;
|
|
if (abutmentModel.dataDirection == DataDirection.AfkToLs)//afk到ls
|
|
{
|
|
try
|
|
{
|
|
CreateTable(abutmentModel);
|
|
// 同步数据
|
|
using (var bulkCopy = new SqlBulkCopy(SqlHelper._connection))
|
|
{
|
|
bulkCopy.DestinationTableName = abutmentModel.lsTempTabName;
|
|
bulkCopy.BulkCopyTimeout = 0;
|
|
string sourceSelectFields = GetSelectFieldsStr(abutmentModel).ToString();
|
|
abutmentModel.allCount = Convert.ToInt32(MySqlHelper.ExecuteScalar($"select count(*) from {abutmentModel.afkTabName} {abutmentModel.sourceWhereCond}") + "");
|
|
for (int i = 0; i <= abutmentModel.allCount; i += 10000)
|
|
{
|
|
try
|
|
{
|
|
string selectSourceSql = string.Format("select {0} from {1} {2}", sourceSelectFields.TrimEnd(','), abutmentModel.afkTabName, abutmentModel.sourceWhereCond + $" limit {i},10000");
|
|
DataTable dataTable = MySqlHelper.ExecuteDataTable(selectSourceSql);
|
|
bulkCopy.ColumnMappings.Clear();
|
|
foreach (DataColumn item in dataTable.Columns)
|
|
{
|
|
bulkCopy.ColumnMappings.Add(item.ColumnName, item.ColumnName);
|
|
}
|
|
bulkCopy.WriteToServer(dataTable);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
frmMain.SendMessage(frmMain.toLsTextBox, $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}:同步表{abutmentModel.afkTabName}==>{abutmentModel.lsTempTabName}成功\r\n");
|
|
isSuccess = true;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
frmMain.SendMessage(frmMain.toLsTextBox, $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}:同步表{abutmentModel.afkTabName}==>{abutmentModel.lsTempTabName}失败,原因:{ex.Message}\r\n后续步骤已终止\r\n");
|
|
string remarkSql = $"insert into p_remarktable (lstablename,primkey,primkeyvalue,messages,recordingtime) values " +
|
|
$"('{abutmentModel.lsTempTabName}','uuid','{abutmentModel.uuid}','批量同步到朗速失败,原因:{ex.Message.Replace("'", "''")}','{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}')";
|
|
try
|
|
{
|
|
SqlHelper.ExecuteNonQuery(remarkSql);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.Message);
|
|
}
|
|
}
|
|
}
|
|
else if (abutmentModel.dataDirection == DataDirection.LsToAfk)
|
|
{
|
|
try
|
|
{
|
|
// 同步数据
|
|
using (MySqlConnector.MySqlConnection MySqlConnection = new MySqlConnector.MySqlConnection(MySqlConnectStr + "AllowLoadLocalInfile=true;"))
|
|
{
|
|
var mySqlBulkCopy = new MySqlConnector.MySqlBulkCopy(MySqlConnection);
|
|
mySqlBulkCopy.DestinationTableName = abutmentModel.afkTabName;
|
|
string sourceSelectFields = GetSelectFieldsStr(abutmentModel).ToString();
|
|
string dourceSql = string.Format("select {0} from {1}{2}", sourceSelectFields.TrimEnd(','), abutmentModel.lsTabName, abutmentModel.sourceWhereCond);
|
|
DataTable dataTable = SqlHelper.ExecuteDataTable(dourceSql);
|
|
abutmentModel.allCount = dataTable.Rows.Count;
|
|
for (int i = 0; i < dataTable.Columns.Count; i++)
|
|
{
|
|
DataColumn item = dataTable.Columns[i];
|
|
MySqlConnector.MySqlBulkCopyColumnMapping mySqlBulkCopyColumnMapping = new MySqlConnector.MySqlBulkCopyColumnMapping();
|
|
mySqlBulkCopyColumnMapping.SourceOrdinal = i;
|
|
mySqlBulkCopyColumnMapping.DestinationColumn = item.ColumnName;
|
|
mySqlBulkCopy.ColumnMappings.Add(mySqlBulkCopyColumnMapping);
|
|
}
|
|
mySqlBulkCopy.WriteToServer(dataTable);
|
|
frmMain.SendMessage(frmMain.toAfkTextBox, $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}:同步表{abutmentModel.lsTabName}==>{abutmentModel.afkTabName}成功\r\n");
|
|
isSuccess = true;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
frmMain.SendMessage(frmMain.toAfkTextBox, $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}:同步表{abutmentModel.lsTabName}==>{abutmentModel.afkTabName}失败,原因:{ex.Message}\r\n后续步骤已终止\r\n");
|
|
string remarkSql = $"insert into p_remarktable (lstablename,primkey,primkeyvalue,messages,recordingtime) values " +
|
|
$"('{ abutmentModel.afkTabName}','uuid','{abutmentModel.uuid}','批量同步到中航失败,原因:{ex.Message.Replace("'", "''")}','{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}')";
|
|
try
|
|
{
|
|
SqlHelper.ExecuteNonQuery(remarkSql);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.Message);
|
|
}
|
|
}
|
|
}
|
|
return isSuccess;
|
|
}
|
|
/// <summary>
|
|
/// 执行同步后的业务存储过程
|
|
/// </summary>
|
|
/// <param name="abutmentModel"></param>
|
|
/// <returns></returns>
|
|
public bool ExecBatchAfter(AbutmentModel abutmentModel)
|
|
{
|
|
bool isSucess = false;
|
|
if (abutmentModel.dataDirection == DataDirection.AfkToLs)//afk到ls
|
|
{
|
|
if (!string.IsNullOrEmpty(abutmentModel.batchAfterSql))
|
|
{
|
|
try
|
|
{
|
|
int result = SqlHelper.ExecuteNonQuery(abutmentModel.batchAfterSql.Replace("{uuid}", abutmentModel.uuid));
|
|
frmMain.SendMessage(frmMain.toLsTextBox, $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}:业务逻辑判断完成\r\n");
|
|
isSucess = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
frmMain.SendMessage(frmMain.toLsTextBox, $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}:业务逻辑判断失败,原因:{ex.Message}\r\n后续步骤已终止\r\n");
|
|
string remarkSql = $"insert into p_remarktable (lstablename,primkey,primkeyvalue,messages,recordingtime) values " +
|
|
$"('{abutmentModel.batchAfterSql.Replace("'", "''")}','uuid','{abutmentModel.uuid}','执行业务存储过程失败,原因:{ex.Message.Replace("'", "''")}','{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}')";
|
|
try
|
|
{
|
|
SqlHelper.ExecuteNonQuery(remarkSql);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.Message);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
frmMain.SendMessage(frmMain.toLsTextBox, $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}:业务逻辑判断完成\r\n");
|
|
isSucess = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
isSucess = true;
|
|
}
|
|
return isSucess;
|
|
}
|
|
/// <summary>
|
|
/// 发送日志
|
|
/// </summary>
|
|
/// <param name="abutmentModel"></param>
|
|
/// <returns></returns>
|
|
public bool SendLog(AbutmentModel abutmentModel)
|
|
{
|
|
bool isSuccess = false;
|
|
if (abutmentModel.dataDirection == DataDirection.AfkToLs)//afk到ls
|
|
{
|
|
try
|
|
{
|
|
string sourceSelectFields = GetSelectFieldsStr(abutmentModel, true).ToString();
|
|
if (!string.IsNullOrWhiteSpace(sourceSelectFields))
|
|
{
|
|
using (MySqlConnector.MySqlConnection MySqlConnection = new MySqlConnector.MySqlConnection(MySqlConnectStr + "AllowLoadLocalInfile=true;"))
|
|
{
|
|
var mySqlBulkCopy = new MySqlConnector.MySqlBulkCopy(MySqlConnection);
|
|
mySqlBulkCopy.DestinationTableName = abutmentModel.logTabName;
|
|
mySqlBulkCopy.BulkCopyTimeout = 0;
|
|
string logSourceSql = string.Format("select {0} from {1} where uuid = '{2}'", sourceSelectFields.TrimEnd(','), abutmentModel.lsTempTabName, abutmentModel.uuid);
|
|
DataTable logDataTable = SqlHelper.ExecuteDataTable(logSourceSql);
|
|
int count = logDataTable.Rows.Count;
|
|
if (count != abutmentModel.allCount)
|
|
{
|
|
throw new Exception($"同步数据与日志数据条数不一致,同步数据{abutmentModel.allCount}条,日志数据{count}条,uuid:{abutmentModel.uuid}");
|
|
}
|
|
for (int i = 0; i < logDataTable.Columns.Count; i++)
|
|
{
|
|
DataColumn item = logDataTable.Columns[i];
|
|
MySqlConnector.MySqlBulkCopyColumnMapping mySqlBulkCopyColumnMapping = new MySqlConnector.MySqlBulkCopyColumnMapping();
|
|
mySqlBulkCopyColumnMapping.SourceOrdinal = i;
|
|
mySqlBulkCopyColumnMapping.DestinationColumn = item.ColumnName;
|
|
mySqlBulkCopy.ColumnMappings.Add(mySqlBulkCopyColumnMapping);
|
|
}
|
|
mySqlBulkCopy.WriteToServer(logDataTable);
|
|
frmMain.SendMessage(frmMain.toLsTextBox, $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}:反写日志==>{abutmentModel.logTabName}成功\r\n");
|
|
isSuccess = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//未配置日志表
|
|
frmMain.SendMessage(frmMain.toLsTextBox, $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}:反写日志==>{abutmentModel.logTabName}成功,但未查询到对应日志表\r\n");
|
|
isSuccess = true;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
frmMain.SendMessage(frmMain.toLsTextBox, $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}:反写日志==>{abutmentModel.logTabName}失败,原因{ex.Message}\r\n后续步骤已终止\r\n");
|
|
string remarkSql = $"insert into p_remarktable (lstablename,primkey,primkeyvalue,messages,recordingtime) values " +
|
|
$"('{abutmentModel.logTabName}','uuid','{abutmentModel.uuid}','批量同步到朗速的数据记录到中航日志表失败,原因:{ex.Message.Replace("'", "''")}','{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}')";
|
|
try
|
|
{
|
|
SqlHelper.ExecuteNonQuery(remarkSql);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.Message);
|
|
}
|
|
}
|
|
}
|
|
else if (abutmentModel.dataDirection == DataDirection.LsToAfk)
|
|
{
|
|
try
|
|
{
|
|
string sourceSelectFields = GetSelectFieldsStr(abutmentModel, true).ToString();
|
|
if (!string.IsNullOrWhiteSpace(sourceSelectFields))
|
|
{
|
|
using (MySqlConnector.MySqlConnection MySqlConnection = new MySqlConnector.MySqlConnection(MySqlConnectStr + "AllowLoadLocalInfile=true;"))
|
|
{
|
|
var mySqlBulkCopy = new MySqlConnector.MySqlBulkCopy(MySqlConnection);
|
|
mySqlBulkCopy.DestinationTableName = abutmentModel.logTabName;
|
|
string logSourceSql = string.Format("select {0} from {1}{2}", sourceSelectFields.TrimEnd(','), abutmentModel.lsTabName, abutmentModel.sourceWhereCond);
|
|
DataTable logDataTable = SqlHelper.ExecuteDataTable(logSourceSql);
|
|
int count = logDataTable.Rows.Count;
|
|
if (count != abutmentModel.allCount)
|
|
{
|
|
throw new Exception($"同步数据与日志数据条数不一致,同步数据{abutmentModel.allCount}条,日志数据{count}条,uuid:{abutmentModel.uuid}");
|
|
}
|
|
for (int i = 0; i < logDataTable.Columns.Count; i++)
|
|
{
|
|
DataColumn item = logDataTable.Columns[i];
|
|
MySqlConnector.MySqlBulkCopyColumnMapping mySqlBulkCopyColumnMapping = new MySqlConnector.MySqlBulkCopyColumnMapping();
|
|
mySqlBulkCopyColumnMapping.SourceOrdinal = i;
|
|
mySqlBulkCopyColumnMapping.DestinationColumn = item.ColumnName;
|
|
mySqlBulkCopy.ColumnMappings.Add(mySqlBulkCopyColumnMapping);
|
|
}
|
|
mySqlBulkCopy.WriteToServer(logDataTable);
|
|
frmMain.SendMessage(frmMain.toAfkTextBox, $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}:反写日志==>{abutmentModel.logTabName}成功\r\n");
|
|
isSuccess = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//未配置日志表
|
|
frmMain.SendMessage(frmMain.toAfkTextBox, $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}:反写日志==>{abutmentModel.logTabName}成功,但未查询到对应日志表\r\n");
|
|
isSuccess = true;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
frmMain.SendMessage(frmMain.toAfkTextBox, $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}:反写日志==>{abutmentModel.logTabName}失败,原因{ex.Message}\r\n后续步骤已终止\r\n");
|
|
string remarkSql = $"insert into p_remarktable (lstablename,primkey,primkeyvalue,messages,recordingtime) values " +
|
|
$"('{ abutmentModel.logTabName}','uuid','{abutmentModel.uuid}','批量同步到中航的数据记录到中航日志表失败,原因:{ex.Message.Replace("'", "''")}','{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}')";
|
|
try
|
|
{
|
|
SqlHelper.ExecuteNonQuery(remarkSql);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.Message);
|
|
}
|
|
}
|
|
}
|
|
return isSuccess;
|
|
}
|
|
/// <summary>
|
|
/// 同步完成后更改状态
|
|
/// </summary>
|
|
/// <param name="abutmentModel"></param>
|
|
/// <returns></returns>
|
|
public bool AfterSync(AbutmentModel abutmentModel)
|
|
{
|
|
bool isSuccess = false;
|
|
if (abutmentModel.dataDirection == DataDirection.AfkToLs)//afk到ls
|
|
{
|
|
//所有操作完成后删除中航中间表已同步完成数据
|
|
string deleteSql = string.Format("delete from {0} {1}", abutmentModel.afkTabName, abutmentModel.sourceWhereCond);
|
|
try
|
|
{
|
|
MySqlHelper.ExecuteNonQuery(deleteSql);
|
|
isSuccess = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
string remarkSql = $"insert into p_remarktable (lstablename,primkey,primkeyvalue,messages,recordingtime) values " +
|
|
$"('{abutmentModel.afkTabName}','uuid','{abutmentModel.uuid}','删除已同步中间表数据失败,原因:{ex.Message.Replace("'", "''")}','{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}')";
|
|
try
|
|
{
|
|
SqlHelper.ExecuteNonQuery(remarkSql);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.Message);
|
|
}
|
|
}
|
|
}
|
|
else if (abutmentModel.dataDirection == DataDirection.LsToAfk)
|
|
{
|
|
//所有操作完成后更改朗速中间表已同步完成数据同步标记
|
|
string updateSql = string.Format("update {0} set directTag = '2' {1}", abutmentModel.lsTabName, abutmentModel.sourceWhereCond);
|
|
try
|
|
{
|
|
SqlHelper.ExecuteNonQuery(updateSql);
|
|
isSuccess = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
string remarkSql = $"insert into p_remarktable (lstablename,primkey,primkeyvalue,messages,recordingtime) values " +
|
|
$"('{abutmentModel.afkTabName}','uuid','{abutmentModel.uuid}','删除已同步中间表数据失败,原因:{ex.Message.Replace("'", "''")}','{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}')";
|
|
try
|
|
{
|
|
SqlHelper.ExecuteNonQuery(remarkSql);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.Message);
|
|
}
|
|
}
|
|
}
|
|
return isSuccess;
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:创建Afk向朗速传递数据的同步方法</para>
|
|
/// <para>创建人:王一帆</para>
|
|
/// <para>创建日期:2023-11-10 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="sqlServerTableName">Ls方需要构建的表</param>
|
|
/// <param name="mysqlTableName">Afk方需要构建的表</param>
|
|
/// <param name="specialFieldNames">特殊不同字段集合</param>
|
|
public void CreateAndSyncTable(AbutmentModel abutmentModel, bool isSync = true, bool isExecAfter = true, bool isSendLog = true, bool isDeleteSync = true)
|
|
{
|
|
if (abutmentModel.dataDirection == DataDirection.AfkToLs)//afk到ls
|
|
{
|
|
// 获取MySQL表结构
|
|
DataTable schemaTable = GetSchemaTable(abutmentModel.afkTabName, MySqlHelper);
|
|
if (!CheckIfTableExists(abutmentModel.lsTempTabName, SqlHelper))
|
|
{
|
|
CreateSqlServerTableWithSpecialFields(abutmentModel.lsTempTabName, schemaTable, SqlHelper);
|
|
}
|
|
if (isSync)
|
|
{
|
|
try
|
|
{
|
|
// 同步数据
|
|
using (var bulkCopy = new SqlBulkCopy(SqlHelper._connection))
|
|
{
|
|
bulkCopy.DestinationTableName = abutmentModel.lsTempTabName;
|
|
bulkCopy.BulkCopyTimeout = 0;
|
|
string sourceSelectFields = GetSelectFieldsStr(abutmentModel).ToString();
|
|
string selectSourceSql = string.Format("select {0} from {1}{2}", sourceSelectFields.TrimEnd(','), abutmentModel.afkTabName, abutmentModel.sourceWhereCond);
|
|
DataTable dataTable = MySqlHelper.ExecuteDataTable(selectSourceSql);
|
|
foreach (DataColumn item in dataTable.Columns)
|
|
{
|
|
bulkCopy.ColumnMappings.Add(item.ColumnName, item.ColumnName);
|
|
}
|
|
bulkCopy.WriteToServer(dataTable);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
string remarkSql = $"insert into p_remarktable (lstablename,primkey,primkeyvalue,messages,recordingtime) values " +
|
|
$"('{abutmentModel.lsTempTabName}','uuid','{abutmentModel.uuid}','批量同步到朗速失败,原因:{ex.Message.Replace("'", "''")}','{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}')";
|
|
try
|
|
{
|
|
SqlHelper.ExecuteNonQuery(remarkSql);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.Message);
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
//同步后执行存储过程
|
|
if (isExecAfter)
|
|
{
|
|
if (!string.IsNullOrEmpty(abutmentModel.batchAfterSql))
|
|
{
|
|
try
|
|
{
|
|
int result = SqlHelper.ExecuteNonQuery(abutmentModel.batchAfterSql.Replace("{uuid}", abutmentModel.uuid));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
string remarkSql = $"insert into p_remarktable (lstablename,primkey,primkeyvalue,messages,recordingtime) values " +
|
|
$"('{abutmentModel.batchAfterSql.Replace("'", "''")}','uuid','{abutmentModel.uuid}','执行业务存储过程失败,原因:{ex.Message.Replace("'", "''")}','{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}')";
|
|
try
|
|
{
|
|
SqlHelper.ExecuteNonQuery(remarkSql);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.Message);
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
//同步到ls后向中航方写入日志数据
|
|
if (isSendLog)
|
|
{
|
|
try
|
|
{
|
|
string sourceSelectFields = GetSelectFieldsStr(abutmentModel, true).ToString();
|
|
if (!string.IsNullOrWhiteSpace(sourceSelectFields))
|
|
{
|
|
using (MySqlConnector.MySqlConnection MySqlConnection = new MySqlConnector.MySqlConnection(MySqlConnectStr + "AllowLoadLocalInfile=true;"))
|
|
{
|
|
var mySqlBulkCopy = new MySqlConnector.MySqlBulkCopy(MySqlConnection);
|
|
mySqlBulkCopy.DestinationTableName = abutmentModel.logTabName;
|
|
mySqlBulkCopy.BulkCopyTimeout = 0;
|
|
string logSourceSql = string.Format("select {0} from {1} where uuid = '{2}'", sourceSelectFields.TrimEnd(','), abutmentModel.lsTempTabName, abutmentModel.uuid);
|
|
DataTable logDataTable = SqlHelper.ExecuteDataTable(logSourceSql);
|
|
mySqlBulkCopy.WriteToServer(logDataTable);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//未配置日志表
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
string remarkSql = $"insert into p_remarktable (lstablename,primkey,primkeyvalue,messages,recordingtime) values " +
|
|
$"('{abutmentModel.logTabName}','uuid','{abutmentModel.uuid}','批量同步到朗速的数据记录到中航日志表失败,原因:{ex.Message.Replace("'", "''")}','{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}')";
|
|
try
|
|
{
|
|
SqlHelper.ExecuteNonQuery(remarkSql);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.Message);
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
//所有操作完成后删除中航中间表已同步完成数据
|
|
if (isDeleteSync)
|
|
{
|
|
//string deleteSql = string.Format("delete from {0} {1}", abutmentModel.afkTabName, abutmentModel.sourceWhereCond);
|
|
//try
|
|
//{
|
|
// MySqlHelper.ExecuteNonQuery(deleteSql);
|
|
//}
|
|
//catch (Exception ex)
|
|
//{
|
|
// string remarkSql = $"insert into p_remarktable (lstablename,primkey,primkeyvalue,messages,recordingtime) values " +
|
|
// $"('{abutmentModel.afkTabName}','uuid','{abutmentModel.uuid}','删除已同步中间表数据失败,原因:{ex.Message.Replace("'", "''")}','{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}')";
|
|
// try
|
|
// {
|
|
// SqlHelper.ExecuteNonQuery(remarkSql);
|
|
// }
|
|
// catch (Exception e)
|
|
// {
|
|
// Console.WriteLine(e.Message);
|
|
// }
|
|
// return;
|
|
//}
|
|
}
|
|
}
|
|
else if (abutmentModel.dataDirection == DataDirection.LsToAfk)//ls到afk
|
|
{
|
|
try
|
|
{
|
|
// 同步数据
|
|
using (MySqlConnector.MySqlConnection MySqlConnection = new MySqlConnector.MySqlConnection(MySqlConnectStr))
|
|
{
|
|
var mySqlBulkCopy = new MySqlConnector.MySqlBulkCopy(MySqlConnection);
|
|
mySqlBulkCopy.DestinationTableName = abutmentModel.afkTabName;
|
|
string sourceSelectFields = GetSelectFieldsStr(abutmentModel).ToString();
|
|
string dourceSql = string.Format("select {0} from {1}{2}", sourceSelectFields.TrimEnd(','), abutmentModel.lsTabName, abutmentModel.sourceWhereCond);
|
|
DataTable dataTable = SqlHelper.ExecuteDataTable(dourceSql);
|
|
mySqlBulkCopy.WriteToServer(dataTable);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
string remarkSql = $"insert into p_remarktable (lstablename,primkey,primkeyvalue,messages,recordingtime) values " +
|
|
$"('{ abutmentModel.afkTabName}','uuid','{abutmentModel.uuid}','批量同步到中航失败,原因:{ex.Message.Replace("'", "''")}','{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}')";
|
|
try
|
|
{
|
|
SqlHelper.ExecuteNonQuery(remarkSql);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.Message);
|
|
}
|
|
return;
|
|
}
|
|
//同步到afk后向中航方写入日志数据
|
|
try
|
|
{
|
|
string sourceSelectFields = GetSelectFieldsStr(abutmentModel, true).ToString();
|
|
if (!string.IsNullOrWhiteSpace(sourceSelectFields))
|
|
{
|
|
using (MySqlConnector.MySqlConnection MySqlConnection = new MySqlConnector.MySqlConnection(MySqlConnectStr))
|
|
{
|
|
var mySqlBulkCopy = new MySqlConnector.MySqlBulkCopy(MySqlConnection);
|
|
mySqlBulkCopy.DestinationTableName = abutmentModel.logTabName;
|
|
string logSourceSql = string.Format("select {0} from {1}{2}", sourceSelectFields.TrimEnd(','), abutmentModel.lsTabName, abutmentModel.sourceWhereCond);
|
|
DataTable logDataTable = MySqlHelper.ExecuteDataTable(logSourceSql);
|
|
mySqlBulkCopy.WriteToServer(logDataTable);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//未配置日志表
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
string remarkSql = $"insert into p_remarktable (lstablename,primkey,primkeyvalue,messages,recordingtime) values " +
|
|
$"('{ abutmentModel.logTabName}','uuid','{abutmentModel.uuid}','批量同步到中航的数据记录到中航日志表失败,原因:{ex.Message.Replace("'", "''")}','{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}')";
|
|
try
|
|
{
|
|
SqlHelper.ExecuteNonQuery(remarkSql);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.Message);
|
|
}
|
|
return;
|
|
}
|
|
//所有操作完成后更改朗速中间表已同步完成数据同步标记
|
|
//string updateSql = string.Format("update {0} set directTag = '2' where {1}", abutmentModel.lsTabName, abutmentModel.sourceWhereCond);
|
|
//try
|
|
//{
|
|
// SqlHelper.ExecuteNonQuery(updateSql);
|
|
//}
|
|
//catch (Exception ex)
|
|
//{
|
|
// string remarkSql = $"insert into p_remarktable (lstablename,primkey,primkeyvalue,messages,recordingtime) values " +
|
|
// $"('{abutmentModel.afkTabName}','uuid','{abutmentModel.uuid}','删除已同步中间表数据失败,原因:{ex.Message.Replace("'", "''")}','{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}')";
|
|
// try
|
|
// {
|
|
// SqlHelper.ExecuteNonQuery(remarkSql);
|
|
// }
|
|
// catch (Exception e)
|
|
// {
|
|
// Console.WriteLine(e.Message);
|
|
// }
|
|
// return;
|
|
//}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:获取mysql表中的列信息方便调用时候直接取用</para>
|
|
/// <para>创建人:王一帆</para>
|
|
/// <para>创建日期:2023-11-10 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="tableName">mysql表名</param>
|
|
/// <param name="connection">MySqlConnection的连接</param>
|
|
/// <returns></returns>
|
|
private DataTable GetSchemaTable(string tableName, AfkDataService.MySqlHelper mySqlHelper)
|
|
{
|
|
DataTable schemaTable = new DataTable();
|
|
try
|
|
{
|
|
schemaTable = mySqlHelper.ExecuteDataTable($"DESCRIBE {tableName}");
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
}
|
|
return schemaTable;
|
|
}
|
|
private DataTable GetSchemaTable(string tableName, AfkDataService.SqlHelper sqlHelper)
|
|
{
|
|
DataTable schemaTable = new DataTable();
|
|
try
|
|
{
|
|
schemaTable = sqlHelper.ExecuteDataTable($"Select * FROM SysColumns Where id = Object_Id ('{tableName}')");
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
}
|
|
return schemaTable;
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:判断Ls数据库中是否存在需要同步的表名</para>
|
|
/// <para>创建人:王一帆</para>
|
|
/// <para>创建日期:2023-11-10 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="tableName">表名</param>
|
|
/// <param name="connection">SqlConnection的连接</param>
|
|
/// <returns></returns>
|
|
private bool CheckIfTableExists(string tableName, AfkDataService.SqlHelper sqlHelper)
|
|
{
|
|
bool result = false;
|
|
try
|
|
{
|
|
result = 1 == (int)sqlHelper.ExecuteScalar($"SELECT CASE WHEN EXISTS((SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '{tableName}')) THEN 1 ELSE 0 END");
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
return result;
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:通过对应的数据结构进行同步</para>
|
|
/// <para>创建人:王一帆</para>
|
|
/// <para>创建日期:2023-11-10 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="tableName">Ls方需要构建的表名</param>
|
|
/// <param name="schemaTable"></param>
|
|
/// <param name="specialFieldNames"></param>
|
|
/// <param name="connection"></param>
|
|
private void CreateSqlServerTableWithSpecialFields(string tableName, DataTable schemaTable, SqlHelper sqlHelper)
|
|
{
|
|
StringBuilder sqlBuilder = new StringBuilder();
|
|
// 创建表的SQL语句
|
|
sqlBuilder.Append($"CREATE TABLE {tableName} (");
|
|
|
|
foreach (DataRow row in schemaTable.Rows)
|
|
{
|
|
var columnName = row["column_name"] + "";
|
|
var columnType = row["data_type"] + "";
|
|
var length = row["character_maximum_length"] + "";
|
|
var number = row["NUMERIC_SCALE"] + "";
|
|
//跳过自增长列
|
|
//if (extra.Equals("auto_increment", StringComparison.OrdinalIgnoreCase))
|
|
//{
|
|
// continue;
|
|
//}
|
|
// 转换MySQL数据类型到SQL Server数据类型
|
|
if (columnType.StartsWith("varchar"))
|
|
{
|
|
sqlBuilder.Append($"[{columnName}] varchar({length}), ");
|
|
}
|
|
else if (columnType.StartsWith("int"))
|
|
{
|
|
sqlBuilder.Append($"[{columnName}] int, ");
|
|
}
|
|
else if (columnType.StartsWith("decimal"))
|
|
{
|
|
sqlBuilder.Append($"[{columnName}] decimal(18,{number}), ");
|
|
}
|
|
else if (columnType.StartsWith("datetime"))
|
|
{
|
|
sqlBuilder.Append($"[{columnName}] datetime, ");
|
|
}
|
|
else
|
|
{
|
|
sqlBuilder.Append($"[{columnName}] varchar(max), ");
|
|
}
|
|
}
|
|
sqlBuilder.Append($"[synchroSuccess] varchar(1000), ");
|
|
sqlBuilder.Append($"[synchroMessage] varchar(1000), ");
|
|
sqlBuilder.Append($"[uuid] varchar(1000), ");
|
|
sqlHelper.ExecuteNonQuery(sqlBuilder.ToString().TrimEnd(',', ' ') + ")");
|
|
}
|
|
/// <summary>
|
|
/// 获取查询列字段集合
|
|
/// </summary>
|
|
/// <param name="abutmentModel"></param>
|
|
/// <returns></returns>
|
|
private StringBuilder GetSelectFieldsStr(AbutmentModel abutmentModel, bool isLog = false)
|
|
{
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
try
|
|
{
|
|
string[] lsTargeFieldsArray = abutmentModel.lsTargeFields.Split(',');
|
|
string[] afkTargeFieldsArray = abutmentModel.afkTargeFields.Split(',');
|
|
DataTable mySqlSchemaTable = new DataTable();
|
|
DataTable sqlServerSchemaTable = new DataTable();
|
|
if (abutmentModel.dataDirection == DataDirection.AfkToLs)
|
|
{
|
|
sqlServerSchemaTable = SqlHelper.ExecuteDataTable($"Select name FROM SysColumns Where id = Object_Id ('{abutmentModel.lsTempTabName}')");
|
|
if (!isLog)
|
|
{
|
|
mySqlSchemaTable = MySqlHelper.ExecuteDataTable($"DESCRIBE {abutmentModel.afkTabName}");
|
|
for (int i = 0; i < afkTargeFieldsArray.Length; i++)
|
|
{
|
|
string columnName = afkTargeFieldsArray[i].Trim();
|
|
DataRow colDataRow = mySqlSchemaTable.Rows.Cast<DataRow>().Where(n => (n["Field"] + "").Equals(columnName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
|
|
string lsColName = lsTargeFieldsArray[i].Trim();
|
|
DataRow selectRow = sqlServerSchemaTable.Rows.Cast<DataRow>().Where(n => (n["name"] + "").Equals(lsColName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
|
|
if (colDataRow != null && selectRow != null)
|
|
{
|
|
stringBuilder.Append($"{columnName} as {selectRow["name"]},");
|
|
}
|
|
}
|
|
stringBuilder.Append($"'{abutmentModel.uuid}' as uuid,");
|
|
}
|
|
else
|
|
{
|
|
if (string.IsNullOrWhiteSpace(abutmentModel.logTabName))
|
|
{
|
|
return stringBuilder;
|
|
}
|
|
mySqlSchemaTable = MySqlHelper.ExecuteDataTable($"DESCRIBE {abutmentModel.logTabName}");
|
|
for (int i = 0; i < lsTargeFieldsArray.Length; i++)
|
|
{
|
|
string columnName = lsTargeFieldsArray[i].Trim();
|
|
DataRow colDataRow = sqlServerSchemaTable.Rows.Cast<DataRow>().Where(n => (n["name"] + "").Equals(columnName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
|
|
string afkColName = afkTargeFieldsArray[i].Trim();
|
|
DataRow selectRow = mySqlSchemaTable.Rows.Cast<DataRow>().Where(n => !(n["Extra"] + "").Equals("auto_increment", StringComparison.OrdinalIgnoreCase) && (n["Field"] + "").Equals(afkColName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
|
|
if (colDataRow != null && selectRow != null)
|
|
{
|
|
stringBuilder.Append($"{columnName} as {selectRow["Field"]},");
|
|
}
|
|
}
|
|
for (int i = 0; i < abutmentModel.logAsFields.Length; i++)
|
|
{
|
|
string logAsField = abutmentModel.logAsFields[i];
|
|
DataRow selectRow = mySqlSchemaTable.Rows.Cast<DataRow>().Where(n => !(n["Extra"] + "").Equals("auto_increment", StringComparison.OrdinalIgnoreCase) && (n["Field"] + "").Equals(abutmentModel.logFields[i], StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
|
|
if (logAsField.Equals("@ErrorType", StringComparison.OrdinalIgnoreCase) && sqlServerSchemaTable.Rows.Cast<DataRow>().Where(n => (n["name"] + "").Equals("synchroSuccess", StringComparison.OrdinalIgnoreCase)).Count() > 0)
|
|
{
|
|
stringBuilder.Append($"(case when synchroSuccess = '成功' then '0' else '1' end) as {selectRow["Field"]},");
|
|
}
|
|
else if (logAsField.Equals("@ErrorMessage", StringComparison.OrdinalIgnoreCase) && sqlServerSchemaTable.Rows.Cast<DataRow>().Where(n => (n["name"] + "").Equals("synchroMessage", StringComparison.OrdinalIgnoreCase)).Count() > 0)
|
|
{
|
|
stringBuilder.Append($"(synchroSuccess + synchroMessage) as {selectRow["Field"]},");
|
|
}
|
|
else if (logAsField.Equals("@TransferLogo", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
stringBuilder.Append($"(case when synchroSuccess = '成功' then '1' else '2' end) as {selectRow["Field"]},");
|
|
}
|
|
else if (logAsField.Equals("getdate()", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
stringBuilder.Append($"getdate() as {selectRow["Field"]},");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else if (abutmentModel.dataDirection == DataDirection.LsToAfk)
|
|
{
|
|
sqlServerSchemaTable = SqlHelper.ExecuteDataTable($"Select name FROM SysColumns Where id = Object_Id ('{abutmentModel.lsTabName}')");
|
|
if (!isLog)
|
|
{
|
|
mySqlSchemaTable = MySqlHelper.ExecuteDataTable($"DESCRIBE {abutmentModel.afkTabName}");
|
|
for (int i = 0; i < lsTargeFieldsArray.Length; i++)
|
|
{
|
|
string columnName = lsTargeFieldsArray[i].Trim();
|
|
DataRow colDataRow = sqlServerSchemaTable.Rows.Cast<DataRow>().Where(n => (n["name"] + "").Equals(columnName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
|
|
string afkColName = afkTargeFieldsArray[i].Trim();
|
|
DataRow selectRow = mySqlSchemaTable.Rows.Cast<DataRow>().Where(n => !(n["Extra"] + "").Equals("auto_increment", StringComparison.OrdinalIgnoreCase) && (n["Field"] + "").Equals(afkColName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
|
|
if (colDataRow != null && selectRow != null)
|
|
{
|
|
stringBuilder.Append($"{columnName} as {selectRow["Field"]},");
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (string.IsNullOrWhiteSpace(abutmentModel.logTabName))
|
|
{
|
|
return stringBuilder;
|
|
}
|
|
mySqlSchemaTable = MySqlHelper.ExecuteDataTable($"DESCRIBE {abutmentModel.logTabName}");
|
|
for (int i = 0; i < lsTargeFieldsArray.Length; i++)
|
|
{
|
|
string columnName = lsTargeFieldsArray[i].Trim();
|
|
DataRow colDataRow = sqlServerSchemaTable.Rows.Cast<DataRow>().Where(n => (n["name"] + "").Equals(columnName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
|
|
string afkColName = afkTargeFieldsArray[i].Trim();
|
|
DataRow selectRow = mySqlSchemaTable.Rows.Cast<DataRow>().Where(n => !(n["Extra"] + "").Equals("auto_increment", StringComparison.OrdinalIgnoreCase) && (n["Field"] + "").Equals(afkColName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
|
|
if (selectRow != null)
|
|
{
|
|
stringBuilder.Append($"{columnName} as {selectRow["Field"]},");
|
|
}
|
|
}
|
|
for (int i = 0; i < abutmentModel.logAsFields.Length; i++)
|
|
{
|
|
string logAsField = abutmentModel.logAsFields[i];
|
|
DataRow selectRow = mySqlSchemaTable.Rows.Cast<DataRow>().Where(n => !(n["Extra"] + "").Equals("auto_increment", StringComparison.OrdinalIgnoreCase) && (n["Field"] + "").Equals(abutmentModel.logFields[i], StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
|
|
if (logAsField.Equals("@ErrorType", StringComparison.OrdinalIgnoreCase) && sqlServerSchemaTable.Rows.Cast<DataRow>().Where(n => (n["name"] + "").Equals("synchroSuccess", StringComparison.OrdinalIgnoreCase)).Count() > 0)
|
|
{
|
|
stringBuilder.Append($"0 as {selectRow["Field"]},");
|
|
}
|
|
else if (logAsField.Equals("@ErrorMessage", StringComparison.OrdinalIgnoreCase) && sqlServerSchemaTable.Rows.Cast<DataRow>().Where(n => (n["name"] + "").Equals("synchroMessage", StringComparison.OrdinalIgnoreCase)).Count() > 0)
|
|
{
|
|
stringBuilder.Append($"'' as {selectRow["Field"]},");
|
|
}
|
|
else if (logAsField.Equals("@TransferLogo", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
stringBuilder.Append($"1 as {selectRow["Field"]},");
|
|
}
|
|
else if (logAsField.Equals("getdate()", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
stringBuilder.Append($"getdate() as {selectRow["Field"]},");
|
|
}
|