using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Stat.DBUtility;
namespace Stat.SQLServerDAL
{
    class GatherServer : IDAL.IGatherServer
    {
        private const string PARM_ID = "@ID";
        private const string PARM_SERVER_NAME = "@ServerName";
        private const string PARM_SERVER_DOMAIN = "@ServerDomain";
        private const string PARM_ISUSE = "@IsUse";
        private const string PARM_ISOPEN = "@IsOpen";
        private const string PARM_EXPANSION = "@expansion";
        private const string PARM_DATATABLE_ACTION = "@DataTable_Action_";

        #region IGatherServer 成员

        public int InsertUpdateDelete(Stat.Model.GatherServer model)
        {
            SqlParameter[] parms = SetSqlParmeter(model);

            return SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_GatherServer_InsertUpdateDelete", parms);
        }



        public IList<Stat.Model.GatherServer> Get(Stat.Model.GatherServer gatherModel, ref Stat.Model.PageInfo pageModel)
        {
            string sqlWhere = "1=1";

            if (gatherModel.ID > 0)
            {
                sqlWhere += " And ID = " + gatherModel.ID + "";
            }

            if (gatherModel.ServerName != null && gatherModel.ServerName.Length > 0)
            {
                sqlWhere += " And ServerName like '%" + gatherModel.ServerName + "%'";
            }
            if (gatherModel.ServerDomain != null && gatherModel.ServerDomain.Length > 0)
            {
                sqlWhere += " And ServerDomain like '%" + gatherModel.ServerDomain + "%'";
            }
            if (gatherModel.IsOpen > 0)
            {
                sqlWhere += " And IsOpen = " + gatherModel.IsOpen + "";
            }
            int recordCount = 0;
            IList<global::Stat.Model.GatherServer> dt = GetList(JPageIndex.ProGetList.GetFromDataTable("Stat_GatherServer", "*", sqlWhere, "Id", 0, pageModel.PageSize, pageModel.PageIndex, ref recordCount, "Stat_PageIndex", SqlHelper.ConnectionStringMaster));

            pageModel.RecordCount = recordCount;

            return dt;
        }

        #endregion












        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private List<Stat.Model.GatherServer> GetList(System.Data.DataTable table)
        {
            List<Stat.Model.GatherServer> list = new List<Stat.Model.GatherServer>();
            if (table != null)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    list.Add(GetModel(table.Rows[i]));
                }
            }
            return list;
        }
        /// <summary>
        /// 将记录集转为实体类
        /// </summary>
        /// <param name="dr">记录集</param> 
        /// <returns>Model.Log</returns>
        private Stat.Model.GatherServer GetModel(System.Data.DataRow dr)
        {
            Stat.Model.GatherServer model = new Stat.Model.GatherServer();

            if (dr.Table.Columns.Contains("ID") && !Convert.IsDBNull(dr["ID"])) model.ID = Convert.ToInt32(dr["ID"]); // ID
            if (dr.Table.Columns.Contains("ServerName") && !Convert.IsDBNull(dr["ServerName"])) model.ServerName = Convert.ToString(dr["ServerName"]).Trim(); //ServerName
            if (dr.Table.Columns.Contains("ServerDomain") && !Convert.IsDBNull(dr["ServerDomain"])) model.ServerDomain = Convert.ToString(dr["ServerDomain"]).Trim(); // ServerDomain
            if (dr.Table.Columns.Contains("IsUse") && !Convert.IsDBNull(dr["IsUse"])) model.IsUse = Convert.ToInt32(dr["IsUse"]); // IsUse
            if (dr.Table.Columns.Contains("IsOpen") && !Convert.IsDBNull(dr["IsOpen"])) model.IsOpen = Convert.ToInt32(dr["IsOpen"]); // IsOpen  

            if (dr.Table.Columns.Contains("expansion") && !Convert.IsDBNull(dr["expansion"])) model.Expansion = Convert.ToString(dr["expansion"]).Trim(); // expansion
            //
            model.Expansion += "|||";
            string[] arr = model.Expansion.Split(new string[] { "|||" }, StringSplitOptions.None);
            model.Speed = Convert.ToInt32("0" + arr[1]);
            //
            return model;
        }

        #region IGatherServer 成员


        public Stat.Model.GatherServer Get(int id)
        {
            SqlParameter parm = new SqlParameter(PARM_ID, SqlDbType.Int);
            parm.Value = id;

            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_GatherServer_id_Get", parm);


            if (dt != null && dt.Rows.Count > 0)
            {
                return GetModel(dt.Rows[0]);
            }
            else
            {
                return null;
            }
        }

        #endregion

        #region ：Set SqlParmeter
        private static SqlParameter[] SetSqlParmeter(Stat.Model.GatherServer model)
        {


            SqlParameter[] parms = GetSqlParmeter();
            parms[0].Value = model.ID;
            parms[1].Value = model.ServerName;
            parms[2].Value = model.ServerDomain;
            parms[3].Value = model.IsUse;
            parms[4].Value = model.IsOpen;
            parms[5].Value = model.Expansion + "|||" + model.Speed;
            parms[6].Value = model.DbAction;

            return parms;
        }

        private static SqlParameter[] GetSqlParmeter()
        {
            //设置参数
            SqlParameter[] parms = new SqlParameter[]{                               
             new SqlParameter(PARM_ID, SqlDbType.Int),
             new SqlParameter(PARM_SERVER_NAME, SqlDbType.NVarChar),
             new SqlParameter(PARM_SERVER_DOMAIN, SqlDbType.NText),
             new SqlParameter(PARM_ISUSE, SqlDbType.Int),
             new SqlParameter(PARM_ISOPEN, SqlDbType.Int),
             new SqlParameter(PARM_EXPANSION, SqlDbType.NText),
             new SqlParameter(PARM_DATATABLE_ACTION, SqlDbType.VarChar)
          };
            return parms;
        }

        #endregion
    }
}
