using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;

namespace CNAS_DBSync
{
    public class CnasDataOperationFact
    {
        //从Config中获取目标数据库的的类型
        private static string strDataType = ConfigurationManager.AppSettings["TargetDBType"].ToString();

        public static ICnasDataOperation CnasDataOperation()
        {
            ICnasDataOperation operation;

            switch (strDataType)
            {
                case "MySQL":
                    operation = new MySQLCNASDataOperation();
                    break;
                case "DB2":
                default:
                    operation = new DB2CNASDataOperation();
                    break;
            }
            return operation;
        }
    }

    public interface ICnasDataOperation
    {
        //测试连接
        bool TestConnect(string strHost, string strName, string strUser, string strPwd,string strPort);

        //获取所有表名称
        DataTable GetAllCNASTablesName(SyncInstrumentItemInfo syncitem);

        //获取第一张表的表结构
        DataTable GetCNASTablesStruct(string strTableName,SyncInstrumentItemInfo syncitem);

        //向第一张表的表中插入数据
        int InsertDataToCNASTable(DataTable dt, SyncInstrumentItemInfo syncitem);

        //获取所有表的数据类型和长度
        DataTable GetCNASTableTypeLenth(string strTableName, SyncInstrumentItemInfo syncitem);
    }

    public class MySQLCNASDataOperation : ICnasDataOperation
    {
        public DataTable GetAllCNASTablesName(SyncInstrumentItemInfo syncitem)
        {
            MySQLBaseDAL mySQLBase = new MySQLBaseDAL();
            mySQLBase.CreateConnectString(syncitem.SyncTargetDBInfo.DBHost,syncitem.SyncTargetDBInfo.DBPort,syncitem.SyncTargetDBInfo.DBName, syncitem.SyncTargetDBInfo.DBUser,syncitem.SyncTargetDBInfo.DBPwd);
            return mySQLBase.GetTableNames(syncitem.SyncTargetDBInfo.DBName);
        }

        public DataTable GetCNASTablesStruct(string strTableName, SyncInstrumentItemInfo syncitem)
        {
            MySQLBaseDAL mySQLBase = new MySQLBaseDAL();
            mySQLBase.CreateConnectString(syncitem.SyncTargetDBInfo.DBHost, syncitem.SyncTargetDBInfo.DBPort, syncitem.SyncTargetDBInfo.DBName, syncitem.SyncTargetDBInfo.DBUser, syncitem.SyncTargetDBInfo.DBPwd);
            return mySQLBase.GetTableStruct(strTableName);
        }

        public DataTable GetCNASTableTypeLenth(string strTableName, SyncInstrumentItemInfo syncitem)
        {
            MySQLBaseDAL mySQLBase = new MySQLBaseDAL();
            mySQLBase.CreateConnectString(syncitem.SyncTargetDBInfo.DBHost, syncitem.SyncTargetDBInfo.DBPort, syncitem.SyncTargetDBInfo.DBName, syncitem.SyncTargetDBInfo.DBUser, syncitem.SyncTargetDBInfo.DBPwd);
            return mySQLBase.GetTableTypeAndLenth(strTableName);
        }

        public int InsertDataToCNASTable(DataTable dt, SyncInstrumentItemInfo syncitem)
        {
            MySQLBaseDAL mySQLBase = new MySQLBaseDAL();
            mySQLBase.CreateConnectString(syncitem.SyncTargetDBInfo.DBHost, syncitem.SyncTargetDBInfo.DBPort, syncitem.SyncTargetDBInfo.DBName, syncitem.SyncTargetDBInfo.DBUser, syncitem.SyncTargetDBInfo.DBPwd);
            return mySQLBase.InsertCnasData(dt,syncitem.LstSyncPramas);
        }

        public bool TestConnect(string strHost, string strName, string strUser, string strPwd, string strPort)
        {
            MySQLBaseDAL mySQLBase = new MySQLBaseDAL();
            mySQLBase.CreateConnectString(strHost, strPort, strName, strUser, strPwd);
            return mySQLBase.LinkCnasTest();
        }
    }

    public class DB2CNASDataOperation : ICnasDataOperation
    {
        public DataTable GetAllCNASTablesName(SyncInstrumentItemInfo syncitem)
        {
            BaseDAL baseDAL = new BaseDAL();
            baseDAL.CreateConnectString(syncitem.SyncTargetDBInfo.DBHost, syncitem.SyncTargetDBInfo.DBName, syncitem.SyncTargetDBInfo.DBUser, syncitem.SyncTargetDBInfo.DBPwd);
            return baseDAL.GetTableNames(syncitem.SyncTargetDBInfo.DBUser);
        }

        public DataTable GetCNASTablesStruct(string strTableName, SyncInstrumentItemInfo syncitem)
        {
            BaseDAL baseDAL = new BaseDAL();
            baseDAL.CreateConnectString(syncitem.SyncTargetDBInfo.DBHost, syncitem.SyncTargetDBInfo.DBName, syncitem.SyncTargetDBInfo.DBUser, syncitem.SyncTargetDBInfo.DBPwd);
            return baseDAL.GetTableStruct(strTableName);
        }

        public DataTable GetCNASTableTypeLenth(string strTableName, SyncInstrumentItemInfo syncitem)
        {
            throw new NotImplementedException();
        }

        public int InsertDataToCNASTable(DataTable dt, SyncInstrumentItemInfo syncitem)
        {
            BaseDAL baseDAL = new BaseDAL();
            baseDAL.CreateConnectString(syncitem.SyncTargetDBInfo.DBHost, syncitem.SyncTargetDBInfo.DBName, syncitem.SyncTargetDBInfo.DBUser, syncitem.SyncTargetDBInfo.DBPwd);
            return baseDAL.InsertCnasData(dt, syncitem.LstSyncPramas);
        }

        public bool TestConnect(string strHost, string strName, string strUser, string strPwd, string strPort)
        {
            BaseDAL basedal = new BaseDAL();
            basedal.CreateConnectString(strHost, strName, strUser, strPwd);
            return basedal.LinkCnasTest();
        }
    }
}
