using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using Stat.DBUtility;
namespace Stat.SQLServerDAL
{
    /// <summary>
    /// 
    /// </summary>
    /// <remarks>JUJU 2011-3-22</remarks>
    class WebsiteStructure : IDAL.IWebsiteStructure
    {

        private const string PARM_DATATABLE_ACTION = "@DataTable_Action_";
        private const string PARM_ID = "@id";
        private const string PARM_WEBID = "@Webid";
        private const string PARM_NAME = "@Name";
        private const string PARM_RULE = "@Rule";
        private const string PARM_TYPE = "@Type";
        private const string PARM_SORT = "@Sort";
        private const string PARM_ISUSE = "@IsUse";
        private const string PARM_ADD_USERID = "@AddUserId";


        #region ：Methods
        public int WebsiteStructureInsertUpdateDelete(Stat.Model.WebsiteStructure model)
        {
            Int32 rInt = -1;
            SqlParameter[] parms = GetParmeters();
            SetParmeters(model);
            rInt = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_WebsiteStructure_InsertUpdateDelete", parms));
            return rInt;
        }

        public IList<Stat.Model.WebsiteStructure> GetList(int id)
        {
            Model.WebsiteStructure model = new Stat.Model.WebsiteStructure();
            Model.PageInfo pageModel = new Stat.Model.PageInfo();
            model.id = id;
            return GetList(ref pageModel, model);
        }


        public IList<Model.StatStructure> GetRule(int webId)
        {
            SqlParameter parm = new SqlParameter(PARM_WEBID, SqlDbType.Int);
            parm.Value = webId;
            return GetStatList(SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_WebsiteStructureLoadForStatistics", parm));

        }
        public IList<Stat.Model.WebsiteStructure> GetRuleList(int webId)
        {
            SqlParameter parm = new SqlParameter(PARM_WEBID, SqlDbType.Int);
            parm.Value = webId;
            return GetList(SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_WebsiteStructureLoadByWebid", parm));

        }

        #endregion

        #region：Set Parmeters

        private static SqlParameter[] SetParmeters(Stat.Model.WebsiteStructure model)
        {
            SqlParameter[] parms = GetParmeters();
            //设置参数
            parms[0].Value = model.Data_Action;
            parms[1].Value = model.id;  //id
            parms[2].Value = model.Webid;  //Webid
            parms[3].Value = model.Name;  //Rule
            parms[4].Value = model.Rule;  //Rule
            parms[5].Value = model.Type;  //Type
            parms[6].Value = model.Sort;  //Sort
            parms[7].Value = model.IsUse;  //IsUse
            parms[8].Value = model.AddUserId;  //AddUserId
            return parms;
        }

        private static SqlParameter[] GetParmeters()
        {
            SqlParameter[] parms = new SqlParameter[] { 
            new SqlParameter(PARM_DATATABLE_ACTION, SqlDbType.VarChar),
            new SqlParameter(PARM_ID, SqlDbType.Int),
            new SqlParameter(PARM_WEBID, SqlDbType.Int),
            new SqlParameter(PARM_NAME, SqlDbType.NVarChar),
            new SqlParameter(PARM_RULE, SqlDbType.NText),
            new SqlParameter(PARM_TYPE, SqlDbType.Int),
            new SqlParameter(PARM_SORT, SqlDbType.Int),
            new SqlParameter(PARM_ISUSE, SqlDbType.Int),
            new SqlParameter(PARM_ADD_USERID, SqlDbType.Int)
            };
            return parms;
        }

        #endregion

        #region ：Page Index

        public System.Collections.Generic.IList<Stat.Model.WebsiteStructure> GetList(ref Stat.Model.PageInfo pageModel, Stat.Model.WebsiteStructure structureModel)
        {
            string sqlWhere = "1=1";

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

            if (structureModel.Name != null && structureModel.Name.Length > 0)
            {
                sqlWhere += " And Name like '%" + structureModel.Name + "%'";
            }
            if (structureModel.Rule != null && structureModel.Rule.Length > 0)
            {
                sqlWhere += " And Rule like '%" + structureModel.Rule + "%'";
            }
            if (structureModel.Type > 0)
            {
                sqlWhere += " And Type = " + structureModel.Type + "";
            }
            if (structureModel.Webid > 0)
            {
                sqlWhere += " And Webid = " + structureModel.Webid + "";
            }
            if (structureModel.IsUse > 0)
            {
                sqlWhere += " And IsUse = " + structureModel.IsUse + "";
            }

            int recordCount = 0;
            IList<global::Stat.Model.WebsiteStructure> list = GetList(JPageIndex.ProGetList.GetFromDataTable("Stat_WebsiteStructure", "*", sqlWhere, "Sort", 0, pageModel.PageSize, pageModel.PageIndex, ref recordCount, "Stat_PageIndex",SqlHelper.ConnectionStringMaster));

            pageModel.RecordCount = recordCount;

            return list;
        }








        #endregion

        #region Utilities
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        private Model.WebsiteStructure GetModel(System.Data.DataRow dr)
        {
            Model.WebsiteStructure model = new Stat.Model.WebsiteStructure();

            if (dr.Table.Columns.Contains("id") && !Convert.IsDBNull(dr["id"])) model.id = Convert.ToInt32(dr["id"]); // id
            if (dr.Table.Columns.Contains("Webid") && !Convert.IsDBNull(dr["Webid"])) model.Webid = Convert.ToInt32(dr["Webid"]); // Webid
            if (dr.Table.Columns.Contains("Name") && !Convert.IsDBNull(dr["Name"])) model.Name = Convert.ToString(dr["Name"]).Trim(); // Rule
            if (dr.Table.Columns.Contains("Rule") && !Convert.IsDBNull(dr["Rule"])) model.Rule = Convert.ToString(dr["Rule"]).Trim(); // Rule
            if (dr.Table.Columns.Contains("Type") && !Convert.IsDBNull(dr["Type"])) model.Type = Convert.ToInt32(dr["Type"]); // Type
            if (dr.Table.Columns.Contains("Sort") && !Convert.IsDBNull(dr["Sort"])) model.Sort = Convert.ToInt32(dr["Sort"]); // Sort
            if (dr.Table.Columns.Contains("IsUse") && !Convert.IsDBNull(dr["IsUse"])) model.IsUse = Convert.ToInt32(dr["IsUse"]); // IsUse
            if (dr.Table.Columns.Contains("AddUserId") && !Convert.IsDBNull(dr["AddUserId"])) model.AddUserId = Convert.ToInt32(dr["AddUserId"]); // AddUserId
            if (dr.Table.Columns.Contains("AddTime") && !Convert.IsDBNull(dr["AddTime"])) model.AddTime = Convert.ToDateTime(dr["AddTime"]); // AddTime
            return model;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        private IList<Model.WebsiteStructure> GetList(System.Data.DataTable table)
        {
            List<Model.WebsiteStructure> list = new List<Model.WebsiteStructure>();
            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></returns>
        private Model.StatStructure GetStatModel(System.Data.DataRow dr)
        {
            Model.StatStructure model = new Stat.Model.StatStructure();
            if (dr.Table.Columns.Contains("id") && !Convert.IsDBNull(dr["id"])) model.id = Convert.ToInt32(dr["id"]); // id
            if (dr.Table.Columns.Contains("Rule") && !Convert.IsDBNull(dr["Rule"])) model.Rule = Convert.ToString(dr["Rule"]).Replace("\r\n", "").Trim(); // Rule
            if (dr.Table.Columns.Contains("Type") && !Convert.IsDBNull(dr["Type"])) model.Type = Convert.ToInt32(dr["Type"]); // Type
            return model;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        private IList<Model.StatStructure> GetStatList(System.Data.DataTable table)
        {
            List<Model.StatStructure> list = new List<Model.StatStructure>();
            if (table != null)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    list.Add(GetStatModel(table.Rows[i]));
                }
            }
            return list;
        }

        #endregion


    }
}
