using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Stat.DBUtility;
namespace Stat.SQLServerDAL
{
    /// <summary>
    /// 系统配置
    /// </summary>
    /// <remarks>Juju 2011-4-28</remarks>
    class Systems : IDAL.ISystems
    {
        private const string PARM_ID="@ID";
         private const string PARM_WEBNAME="@WebName";
         private const string PARM_WEBURL="@WebUrl";
         private const string PARM_WEBLOGO="@WebLogo";
         private const string PARM_WEBFOOT="@WebFoot";
         private const string PARM_ISREG="@IsReg";
         private const string PARM_ISUSE="@IsUse";
         private const string PARM_STAT="@Stat";
         private const string PARM_WEBCOPY="@WebCopy";
         private const string PARM_SHOWID="@ShowID";
         private const string PARM_EXPANSION = "@expansion";

        #region Methods
        /// <summary>
        /// 添加系统配置
        /// </summary>
        /// <returns></returns>
        /// <remarks>Juju 2011-3-11</remarks>
        public bool Insert(Model.System model)
        {
            SqlParameter[] parms = SetParmeters(model);
            int rInt = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_SystemInsert", parms);

            return rInt > 0;

        }
                /// <summary>
        /// 更新系统配置
        /// </summary>
        /// <returns></returns>
        public bool Update(Model.System model)
        {
            SqlParameter[] parms=SetParmeters(model);
            int rInt = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_SystemUpdate", parms);
            return rInt>0;
        }
                /// <summary>
        ///删除系统配置
        /// </summary>
        /// <returns></returns>
        public bool Delete(int id)
        {
            SqlParameter parm=new SqlParameter(PARM_ID, SqlDbType.Int);
            parm.Value = id;
            int rInt=  SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringMaster,CommandType.StoredProcedure,"Stat_SystemDelete",parm);
            return rInt>0;
        }
                /// <summary>
        /// 获取系统配置
        /// </summary>
        /// <returns></returns>
        public IList<Model.System> Get()
        {
             return GetList(SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringMaster,CommandType.StoredProcedure,"Stat_SystemLoadAll"));
        }
        /// <summary>
        /// 获取系统配置
        /// </summary>
        /// <returns></returns>
        public IList<Model.System> Get(int id)
        {
            SqlParameter parm = new SqlParameter(PARM_ID, SqlDbType.Int);
            parm.Value = id;
            return GetList(SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_SystemLoadById",parm));
        }
        #endregion
        #region：Set SqlParmeters
        private static SqlParameter[] SetParmeters(Model.System model)
        {
            model.Deferred = model.Deferred < 1000 ? 1000 : model.Deferred;
            model.Expansion += model.IpTaskMode + "‖" + model.Deferred + "‖";
            model.Expansion += model.GatherServer + "‖";
            //
            SqlParameter[] parms = GetParmeters();
            parms[0].Value = model.WebName;
            parms[1].Value = model.WebUrl;
            parms[2].Value = model.WebLogo;
            parms[3].Value = model.WebFoot;
            parms[4].Value = model.IsReg;
            parms[5].Value = model.IsUse;
            parms[6].Value = model.Stat;
            parms[7].Value = model.WebCopy;
            parms[8].Value = model.ShowID;
            parms[9].Value = model.Expansion;
            parms[10].Value = model.ID;
            return parms;
        }

        private static SqlParameter[] GetParmeters()
        {
            SqlParameter[] parms = new SqlParameter[] {             
            new SqlParameter(PARM_WEBNAME, SqlDbType.NVarChar),
            new SqlParameter(PARM_WEBURL, SqlDbType.NVarChar),
            new SqlParameter(PARM_WEBLOGO, SqlDbType.NVarChar),
            new SqlParameter(PARM_WEBFOOT, SqlDbType.NVarChar),
            new SqlParameter(PARM_ISREG, SqlDbType.Int),
            new SqlParameter(PARM_ISUSE, SqlDbType.Int),
            new SqlParameter(PARM_STAT, SqlDbType.NVarChar),
            new SqlParameter(PARM_WEBCOPY, SqlDbType.NVarChar),
            new SqlParameter(PARM_SHOWID, SqlDbType.Int),
            new SqlParameter(PARM_EXPANSION, SqlDbType.NText),
            new SqlParameter(PARM_ID, SqlDbType.Int)
            };
            return parms;
        }

         #endregion
        #region Utilites
        /// <summary>
        /// 获取一条系统配置实体
        /// </summary>
        ///<param name="row"></param>
        /// <returns></returns>
        private Model.System GetModel(System.Data.DataRow row)
        {
            Model.System model = new Model.System();
            if (row != null)
            {
                if (row["ID"] != DBNull.Value) model.ID = Convert.ToInt32(row["ID"]);
                if (row["WebName"] != DBNull.Value) model.WebName = row["WebName"].ToString();
                if (row["WebUrl"] != DBNull.Value) model.WebUrl = row["WebUrl"].ToString();
                if (row["WebLogo"] != DBNull.Value) model.WebLogo = row["WebLogo"].ToString();
                if (row["WebFoot"] != DBNull.Value) model.WebFoot = row["WebFoot"].ToString();
                if (row["IsReg"] != DBNull.Value) model.IsReg = Convert.ToInt32(row["IsReg"]);
                if (row["IsUse"] != DBNull.Value) model.IsUse = Convert.ToInt32(row["IsUse"]);
                if (row["Stat"] != DBNull.Value) model.Stat = row["Stat"].ToString();
                if (row["WebCopy"] != DBNull.Value) model.WebCopy = row["WebCopy"].ToString();
                if (row["ShowID"] != DBNull.Value) model.ShowID = Convert.ToInt32(row["ShowID"]);
                if (row["RegisterNumber"] != DBNull.Value) model.SystemMD5 = row["RegisterNumber"].ToString();
    
                if (row.Table.Columns.Contains("expansion") && !Convert.IsDBNull(row["expansion"]))
                {
                    model.Expansion = row["expansion"].ToString(); //expansion
                    model.Expansion += "‖‖‖‖‖‖‖‖‖‖‖‖‖‖‖‖‖‖‖‖‖‖‖";
                    string[] setting = model.Expansion.Split(new string[] { "‖" }, System.StringSplitOptions.None);
                    model.IpTaskMode = setting[0] == "" ? 0 : Convert.ToInt32(setting[0]);
                    //
                    model.Deferred = setting[2] == "" ? 3000 : Convert.ToDouble(setting[2]);
                    model.Deferred = model.Deferred < 2000 ? 2000 : model.Deferred;
                   //
                    model.GatherServer = setting[3];
                }

            }
            return model;
        }
        /// <summary>
        /// 获取 系统配置 列表
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        private List<Model.System> GetList(System.Data.DataTable table)
        {
            List<Model.System> list = new List<Model.System>();
            if (table != null)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    list.Add(GetModel(table.Rows[i]));
                }
            }
            return list;
        }
        /// <summary>
        /// 未开发
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public int Totali_Active_Website(DateTime dt)
        {
            
            return 0;
        }
        /// <summary>
        /// 未开发
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public int Totali_Active_IP(DateTime dt)
        {

            return 0;
        }
        /// <summary>
        /// 未开发
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public int Totali_Active_Keyword(DateTime dt)
        {


            return 0;
        }
        #endregion

    }
}