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

namespace CNAS_DBSync
{
    public class BaseDAL
    {
        private static log4net.ILog log = log4net.LogManager.GetLogger("BaseDAL");

        public void CreateConnectString(string strHost, string strName, string strUser, string strPwd)
        {
            SQLDB2Helper.createConnectString(strHost, strName, strUser, strPwd);
        }

        //获取所有表单名称
        public DataTable GetTableNames(string strUser)
        {
            DataTable dt = new DataTable();
            string strSql = string.Format("SELECT TABNAME FROM SYSCAT.TABLES Where TABSCHEMA = '{0}'",strUser);
            try
            {
                dt= SQLDB2Helper.getDB2Table(strSql);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }
            return dt;
        }

        /// <summary>
        /// 获取某表的表结构
        /// </summary>
        /// <param name="strTableName"></param>
        /// <returns></returns>
        public DataTable GetTableStruct(string strTableName)
        {
            DataTable dt = new DataTable();
            string strSql = string.Format("SELECT * FROM {0} Where 1=0", strTableName);
            try
            {
                dt = SQLDB2Helper.getDB2Table(strSql);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }
            return dt;
        }


        /// <summary>
        /// 逐行批量插入数据
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public int InsertCnasData(DataTable dt, List<SyncParamasInfo> syncParamasInfos)
        {
            int iReturn = 0;
            if (dt.Rows.Count <= 0) return 0;
            try
            {
                //构建SQL语句
                string strSql_part1 = "";
                string strSql_part2 = "";
                foreach (var item in syncParamasInfos)
                {
                    strSql_part1 += item.TargetField + ",";

                    strSql_part2 += "?,";
                }
                string strSql = string.Format("insert into {0}({1}) values({2})",syncParamasInfos[0].TargetTable, strSql_part1.Substring(0, strSql_part1.Length - 1), strSql_part2.Substring(0, strSql_part2.Length - 1));

                foreach (DataRow dr in dt.Rows)
                {

                    OleDbParameter[] parameters = new OleDbParameter[syncParamasInfos.Count];
                    for (int i = 0; i < syncParamasInfos.Count; i++)
                    {
                        parameters[i] = new OleDbParameter(syncParamasInfos[i].TargetField, dr[syncParamasInfos[i].TargetField]);
                    }

                    //执行SQL语句
                    iReturn += SQLDB2Helper.ExequeryDB2Table(strSql, parameters);

                }
            }
            catch (Exception ex)
            {
                //此处添加错误日志
                log.Error(ex.Message);
            }

            return iReturn;
        }

        public bool LinkCnasTest()
        {
            return SQLDB2Helper.TestCnasLink();
        }

    }
}
