ab56a9bcf7
SVN-Revision: r240
231 lines
11 KiB
C#
231 lines
11 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;
|
|
using System.Threading.Tasks;
|
|
using static AFKAbutment.EnumClass;
|
|
|
|
namespace AFKAbutment
|
|
{
|
|
public class AbutmentModel
|
|
{
|
|
/// <summary>
|
|
/// 同步表当前id
|
|
/// </summary>
|
|
public int id;
|
|
/// <summary>
|
|
/// 当前数据唯一标识
|
|
/// </summary>
|
|
public string uuid;
|
|
/// <summary>
|
|
/// 数据传输方向
|
|
/// </summary>
|
|
public DataDirection dataDirection;
|
|
/// <summary>
|
|
/// 数据源字段解析
|
|
/// </summary>
|
|
public string[] targetStrs;
|
|
/// <summary>
|
|
/// 同步字段解析
|
|
/// </summary>
|
|
public string[] synchroStrs;
|
|
/// <summary>
|
|
/// 同步日志表字段解析
|
|
/// </summary>
|
|
public string[] synLogStrs;
|
|
/// <summary>
|
|
/// 双方主键
|
|
/// </summary>
|
|
public string[] primaryKeys;
|
|
#region 中航方相关信息
|
|
/// <summary>
|
|
/// 中航方表名
|
|
/// </summary>
|
|
public string afkTabName;
|
|
/// <summary>
|
|
/// 中航方列名
|
|
/// </summary>
|
|
public string afkTargeFields;
|
|
/// <summary>
|
|
/// 中航方主键
|
|
/// </summary>
|
|
public string afkPrimaryKey;
|
|
#endregion
|
|
#region 朗速方相关信息
|
|
/// <summary>
|
|
/// 朗速表名
|
|
/// </summary>
|
|
public string lsTabName;
|
|
/// <summary>
|
|
/// 朗速字段名
|
|
/// </summary>
|
|
public string lsTargeFields;
|
|
/// <summary>
|
|
/// 朗速主键
|
|
/// </summary>
|
|
public string lsPrimaryKey;
|
|
#endregion
|
|
#region 日志表相关信息
|
|
/// <summary>
|
|
/// 日志表名
|
|
/// </summary>
|
|
public string logTabName;
|
|
/// <summary>
|
|
/// 日志不同列名
|
|
/// </summary>
|
|
public string[] logFields;
|
|
/// <summary>
|
|
/// 日志不同列别名
|
|
/// </summary>
|
|
public string[] logAsFields;
|
|
#endregion
|
|
#region 其他信息
|
|
/// <summary>
|
|
/// 是否为批量传递类型
|
|
/// </summary>
|
|
public bool isBatch;
|
|
/// <summary>
|
|
/// 是否为批量传递基础档案提前类型
|
|
/// </summary>
|
|
public bool isBefore;
|
|
/// <summary>
|
|
/// 批量传递类型验证sql
|
|
/// </summary>
|
|
public string batchAfterSql;
|
|
/// <summary>
|
|
/// 执行后aftersql
|
|
/// </summary>
|
|
public string afterSql;
|
|
/// <summary>
|
|
/// tagid是3或者4,指定字段改变
|
|
/// </summary>
|
|
public string disableFieids;
|
|
/// <summary>
|
|
/// 朗速字段属性表
|
|
/// </summary>
|
|
public DataTable SQLQueryPropertySheet;
|
|
/// <summary>
|
|
/// 中航字段属性表
|
|
/// </summary>
|
|
public DataTable MYSQLPropertySheet;
|
|
#endregion
|
|
/// <summary>
|
|
/// 数据源where条件
|
|
/// </summary>
|
|
public string sourceWhereCond;
|
|
/// <summary>
|
|
/// 数据源表名
|
|
/// </summary>
|
|
public string sourceTabNale;
|
|
/// <summary>
|
|
/// 接受数据表表名
|
|
/// </summary>
|
|
public string getSourceTabName;
|
|
/// <summary>
|
|
/// 数据源字段
|
|
/// </summary>
|
|
public string sourceSelectFields;
|
|
/// <summary>
|
|
/// 批量同步到朗速临时表名
|
|
/// </summary>
|
|
public string lsTempTabName;
|
|
/// <summary>
|
|
/// 数据源字段
|
|
/// </summary>
|
|
public DataTable sourceTab;
|
|
public AbutmentModel(DataRow _item, DataDirection _dataDirection, string afkDataBase, string sqlHelperConStr, string mySqlHelperConStr)
|
|
{
|
|
NMySqlHelper MySqlHelper = new NMySqlHelper(new MySqlConnection(mySqlHelperConStr));
|
|
NSqlHelper SqlHelper = new NSqlHelper(new SqlConnection(sqlHelperConStr));
|
|
this.dataDirection = _dataDirection;
|
|
this.id = Convert.ToInt32(_item["id"] + "");
|
|
this.targetStrs = (_item["targetTabSql"] + "").ToLower().Split('^');//数据源字段解析
|
|
this.synchroStrs = (_item["synchroSql"] + "").ToLower().Split('^');//同步字段解析
|
|
this.synLogStrs = (_item["synLogSql"] + "").ToLower().Split('^');//同步日志表字段解析
|
|
this.primaryKeys = null;
|
|
if (this.synchroStrs.Length == 4)
|
|
{
|
|
this.primaryKeys = this.synchroStrs[3].Replace(" ", "").Split('=');
|
|
}
|
|
#region 中航方相关信息
|
|
this.afkTabName = this.dataDirection == 0 ? this.targetStrs[0].Replace(" ", "") : this.synchroStrs[0].Replace(" ", "");//表名
|
|
this.afkTargeFields = this.dataDirection == 0 ? this.synchroStrs[2].Replace(" ", "") : this.synchroStrs[1].Replace(" ", "");//字段名
|
|
this.afkPrimaryKey = this.dataDirection == 0 ? (this.primaryKeys != null && this.primaryKeys.Length == 2 ? this.primaryKeys[1] : "") : (this.primaryKeys != null && this.primaryKeys.Length == 2 ? this.primaryKeys[0] : "");
|
|
#endregion
|
|
#region 朗速方相关信息
|
|
this.lsTabName = this.dataDirection == 0 ? this.synchroStrs[0].Replace(" ", "") : this.targetStrs[0].Replace(" ", "");
|
|
this.lsTargeFields = this.dataDirection == 0 ? this.synchroStrs[1].Replace(" ", "") : this.synchroStrs[2].Replace(" ", "");
|
|
this.lsPrimaryKey = this.dataDirection == 0 ? (this.primaryKeys != null && this.primaryKeys.Length == 2 ? this.primaryKeys[0] : "") : (this.primaryKeys != null && this.primaryKeys.Length == 2 ? this.primaryKeys[1] : "");
|
|
#endregion
|
|
#region 日志表相关信息
|
|
this.logTabName = this.synLogStrs.Length == 3 ? this.synLogStrs[0].Replace(" ", "") : "";
|
|
this.logFields = this.synLogStrs.Length == 3 ? this.synLogStrs[1].Replace(" ", "").Split(',') : null;
|
|
this.logAsFields = this.synLogStrs.Length == 3 ? this.synLogStrs[2].Replace(" ", "").Split(',') : null;
|
|
#endregion
|
|
#region 其他信息
|
|
this.lsTempTabName = string.Format("{0}_copy", this.lsTabName);
|
|
this.isBatch = _item.Table.Columns.Contains("isBatch") ? (_item["isBatch"] + "").Equals("true") : false;
|
|
this.isBefore = _item.Table.Columns.Contains("isBefore") ? (_item["isBefore"] + "").Equals("true") : false;
|
|
this.batchAfterSql = _item.Table.Columns.Contains("batchAfterSql") ? _item["batchAfterSql"] + "" : "";
|
|
this.afterSql = (_item["afterSql"] + "").ToLower();//同步完成后执行的sql
|
|
this.disableFieids = (_item["disableField"] + "").ToLower();//tagid是3或者4,指定字段改变
|
|
//数据库属性表
|
|
this.SQLQueryPropertySheet = SqlHelper.ExecuteDataTable(string.Format("select COLUMN_NAME,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH,IS_NULLABLE from information_schema.columns where table_name = '{0}'", this.lsTabName));
|
|
this.MYSQLPropertySheet = MySqlHelper.ExecuteDataTable(string.Format("show full columns from {0}", this.afkTabName));
|
|
#endregion
|
|
string lsExistTagStr = string.Format(@"if not exists(select * from syscolumns
|
|
where id=object_id('{0}') and name='directTag')
|
|
begin
|
|
alter table {0} add directTag int
|
|
end", this.lsTabName);
|
|
SqlHelper.ExecuteNonQuery(lsExistTagStr);//判断朗速方是否存在同步完成字段
|
|
string afkExistTagStr = string.Format(@"select CASE when (select count(1)
|
|
FROM information_schema.COLUMNS
|
|
WHERE table_schema = '{0}'
|
|
and table_name = '{1}'
|
|
AND column_name = 'isSynchro') > 0
|
|
then '1'
|
|
else '2'
|
|
end
|
|
", afkDataBase, this.afkTabName);
|
|
string afkExistResult = MySqlHelper.ExecuteScalar(afkExistTagStr) + "";//判断中航方有无标识列
|
|
if (afkExistResult.Equals("2"))
|
|
{
|
|
string afkAddTagStr = string.Format("alter table {0} add column isSynchro varchar(30);", this.afkTabName); //如果不存在则添加标识列
|
|
MySqlHelper.ExecuteNonQuery(afkAddTagStr);
|
|
}
|
|
if (this.targetStrs.Length == 1)
|
|
this.sourceWhereCond = this.dataDirection == 0 ? "where isSynchro=1" : " where directTag<>2";//数据源是afk则默认条件字段没有意义,数据源是ls则需获取默认条件字段不为2的数据
|
|
else
|
|
this.sourceWhereCond = string.Format(" where {0}", this.dataDirection == 0 ? this.targetStrs[1] + " and isSynchro=1 " : this.targetStrs[1]);
|
|
if (dataDirection == 0 && !isBatch && !isBefore)//如果是ls获取数据,要先把数据源中当前的符合条件的数据的isSynchro变为1
|
|
{
|
|
string conditions = string.Empty;
|
|
conditions = this.targetStrs.Length == 2 ? " where isSynchro is null and " + this.targetStrs[1] : "";
|
|
string sql = string.Format("update {0} set isSynchro='1' {1} ", this.afkTabName, conditions);
|
|
MySqlHelper.ExecuteNonQuery(sql);
|
|
}
|
|
#region 获取数据源
|
|
this.sourceTabNale = this.dataDirection == 0 ? this.afkTabName : this.lsTabName;//数据源表名
|
|
this.getSourceTabName = this.dataDirection == 0 ? this.lsTabName : this.afkTabName;//接受数据表表名
|
|
this.sourceSelectFields = this.dataDirection == 0 ? this.afkTargeFields : this.lsTargeFields;//数据源字段
|
|
if (this.sourceSelectFields.Contains("getdate()"))//处理各种特殊字段
|
|
this.sourceSelectFields = this.sourceSelectFields.Replace("getdate()", this.dataDirection == 0 ? "now() as as_senddate" : "getdate() as as_senddate");
|
|
//if (this.sourceSelectFields.Contains("newid()"))
|
|
// this.sourceSelectFields = this.sourceSelectFields.Replace("newid()", string.Format("'{0}' as as_uuid", uuid));
|
|
if (this.sourceSelectFields.Contains("#add$_"))
|
|
this.sourceSelectFields = this.sourceSelectFields.Replace("#add$_", "");
|
|
string selectSourceSql = string.Format("select {0} from {1}{2}", sourceSelectFields, sourceTabNale, sourceWhereCond);//查询数据源sql
|
|
this.sourceTab = this.dataDirection == 0 ? MySqlHelper.ExecuteDataTable(selectSourceSql) : SqlHelper.ExecuteDataTable(selectSourceSql);//获取数据源
|
|
if (!this.sourceTab.Columns.Contains("suuid"))
|
|
{
|
|
this.sourceTab.Columns.Add("suuid");
|
|
}
|
|
#endregion
|
|
}
|
|
}
|
|
}
|