using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Stat.DBUtility;
namespace Stat.SQLServerDAL
{
    /// <summary>
    /// 友情链接
    /// </summary>
    /// <remarks>Juju ：2011-3-20</remarks>
    class FriendLink : IDAL.IFriendLink
    {
        private const string PARM_LINKNAME = "@LinkName";
        private const string PARM_LINKIMG_PATH = "@LinkImgPath";
        private const string PARM_LINKURL = "@LinkURL";
        private const string PARM_LINKINFO = "@LinkInfo";
        private const string PARM_ORDERNUM = "@OrderNum";
        private const string PARM_STATE = "@State";
        private const string PARM_STYLE = "@Style";
        private const string PARM_CTYPE = "@cType";
        private const string PARM_POSITION = "@Position";
        private const string PARM_ID = "@ID";
        private const string PARM_NUM = "@num";

        #region Utilities
        /// <summary>
        /// 获取一条友情链接实体
        /// </summary>
        ///<param name="row"></param>
        /// <returns></returns>
        private Model.FriendLink GetModel(System.Data.IDataReader rdr)
        {
            Model.FriendLink model = new Model.FriendLink();
            if (rdr != null)
            {
                model.id = SqlHelper.GetInt(rdr, "ID");
                model.LinkName = SqlHelper.GetString(rdr, "LinkName");
                model.LinkURL = SqlHelper.GetString(rdr, "LinkURL");
                model.LinkInfo = SqlHelper.GetString(rdr, "LinkInfo");
                model.State = SqlHelper.GetInt(rdr, "State");
                model.Style = SqlHelper.GetInt(rdr, "Style");
                model.cType = SqlHelper.GetInt(rdr, "cType");
                model.OrderNum = SqlHelper.GetInt(rdr, "OrderNum");
                model.Position = SqlHelper.GetInt(rdr, "Position");
                model.LinkImgPath = SqlHelper.GetString(rdr, "LinkImgPath");
            }
            return model;
        }
        /// <summary>
        /// 获取 友情链接 列表
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        private List<Model.FriendLink> GetList(System.Data.IDataReader rdr)
        {
            List<Model.FriendLink> list = new List<Model.FriendLink>();
            while (rdr.Read())
            {
                list.Add(GetModel(rdr));
            }

            return list;
        }

        #endregion

        #region Methods
        public bool Insert(Model.FriendLink model)
        {
            SqlParameter[] parms = SetParmeters(model);
            int rInt = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_FriendLinkInsert", parms);
            return rInt > 0;
        }
        public bool Update(Model.FriendLink model)
        {

            SqlParameter[] parms = SetParmeters(model);

            int rInt = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_FriendLinkUpdate", parms);

            return rInt > 0;

        }
        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_FriendLinkDelete", parm);
            return rInt > 0;

        }
        public IList<Model.FriendLink> Get()
        {
            return GetList(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_FriendLinkLoadAll", null));

        }
        public IList<Model.FriendLink> Get(int id)
        {
            SqlParameter parm = new SqlParameter(PARM_ID, SqlDbType.Int);
            parm.Value = id;
            return GetList(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_FriendLinkLoadById", parm));
        }
        public IList<Model.FriendLink> GetcType(int cType)
        {
            SqlParameter parm = new SqlParameter(PARM_CTYPE, SqlDbType.Int);
            parm.Value = cType;
            return GetList(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_FriendLinkLoadByCType", parm));
        }

        /// 获取友情链接
        /// </summary>
        /// <param name="num">数目</param>
        /// <param name="state">状态：1，已审核</param>
        /// <param name="style">样式：0，文字；1，图片</param>
        /// <param name="cType"></param>
        /// <returns></returns>
        public IList<Model.FriendLink> Get(int num, int state, int style, int cType)
        {
            SqlParameter[] parms = SetParmetersForLoad(num, state, style, cType, 0);

            return GetList(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_FriendLinkLoadByTopStyleStateCType", parms));

        }


        /// <summary>
        /// 获取友情链接
        /// </summary>
        /// <param name="num">数目</param>
        /// <param name="state">状态：1，已审核</param>
        /// <param name="style">样式：0，文字；1，图片</param>
        /// <param name="cType">2：小LOgo广告</param>
        /// <param name="Position">位置：0,1,2,3</param>
        /// <returns></returns>
        public IList<Model.FriendLink> Get(int num, int state, int style, int cType, int Position)
        {

            SqlParameter[] parms = SetParmetersForLoad(num, state, style, cType, Position);

            return GetList(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_FriendLinkLoadByTopPositionStyleStateCType", parms));

        }



        #endregion

        #region ：Set Parmeters


        private static SqlParameter[] SetParmeters(Model.FriendLink model)
        {
            SqlParameter[] parms = GetParmeters();
            parms[0].Value = model.LinkName;
            parms[1].Value = model.LinkImgPath;
            parms[2].Value = model.LinkURL;
            parms[3].Value = model.LinkInfo;
            parms[4].Value = model.OrderNum;
            parms[5].Value = model.State;
            parms[6].Value = model.Style;
            parms[7].Value = model.cType;
            parms[8].Value = model.Position;
            parms[9].Value = model.id;
            return parms;
        }

        private static SqlParameter[] GetParmeters()
        {
            SqlParameter[] parms = new SqlParameter[] { 
            new SqlParameter(PARM_LINKNAME, SqlDbType.NVarChar),
            new SqlParameter(PARM_LINKIMG_PATH, SqlDbType.NVarChar),
            new SqlParameter(PARM_LINKURL, SqlDbType.NVarChar),
            new SqlParameter(PARM_LINKINFO, SqlDbType.NVarChar),
            new SqlParameter(PARM_ORDERNUM, SqlDbType.Int),
            new SqlParameter(PARM_STATE, SqlDbType.Int),
            new SqlParameter(PARM_STYLE, SqlDbType.Int),
            new SqlParameter(PARM_CTYPE, SqlDbType.Int),
            new SqlParameter(PARM_POSITION, SqlDbType.Int),
            new SqlParameter(PARM_ID, SqlDbType.Int)
            };
            return parms;
        }
        private static SqlParameter[] SetParmetersForLoad(int num, int state, int style, int cType, int Position)
        {
            SqlParameter[] parms = new SqlParameter[]{
            new SqlParameter(PARM_NUM, SqlDbType.Int),
            new SqlParameter(PARM_STYLE, SqlDbType.Int),
            new SqlParameter(PARM_STATE, SqlDbType.Int),
            new SqlParameter(PARM_CTYPE, SqlDbType.Int),
            new SqlParameter(PARM_POSITION, SqlDbType.Int)
            };
            parms[0].Value = num;
            parms[1].Value = style;
            parms[2].Value = state;
            parms[3].Value = cType;
            parms[4].Value = Position;
            return parms;
        }
        #endregion

    }
}
