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 { /// /// 同步表当前id /// public int id; /// /// 当前数据唯一标识 /// public string uuid; /// /// 数据传输方向 /// public DataDirection dataDirection; /// /// 数据源字段解析 /// public string[] targetStrs; /// /// 同步字段解析 /// public string[] synchroStrs; /// /// 同步日志表字段解析 /// public string[] synLogStrs; /// /// 双方主键 /// public string[] primaryKeys; #region 中航方相关信息 /// /// 中航方表名 /// public string afkTabName; /// /// 中航方列名 /// public string afkTargeFields; /// /// 中航方主键 /// public string afkPrimaryKey; #endregion #region 朗速方相关信息 /// /// 朗速表名 /// public string lsTabName; /// /// 朗速字段名 /// public string lsTargeFields; /// /// 朗速主键 /// public string lsPrimaryKey; #endregion #region 日志表相关信息 /// /// 日志表名 /// public string logTabName; /// /// 日志不同列名 /// public string[] logFields; /// /// 日志不同列别名 /// public string[] logAsFields; #endregion #region 其他信息 /// /// 是否为批量传递类型 /// public bool isBatch; /// /// 是否为批量传递基础档案提前类型 /// public bool isBefore; /// /// 批量传递类型验证sql /// public string batchAfterSql; /// /// 执行后aftersql /// public string afterSql; /// /// tagid是3或者4,指定字段改变 /// public string disableFieids; /// /// 朗速字段属性表 /// public DataTable SQLQueryPropertySheet; /// /// 中航字段属性表 /// public DataTable MYSQLPropertySheet; #endregion /// /// 数据源where条件 /// public string sourceWhereCond; /// /// 数据源表名 /// public string sourceTabNale; /// /// 接受数据表表名 /// public string getSourceTabName; /// /// 数据源字段 /// public string sourceSelectFields; /// /// 批量同步到朗速临时表名 /// public string lsTempTabName; /// /// 数据源字段 /// 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 } } }