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-18</remarks>
    class Users : IDAL.IUsers
    {
        private const string PARM_USERID = "@UserId";
        private const string PARM_USERNAME = "@UserName";
        private const string PARM_USERPWD = "@UserPwd";
        private const string PARM_USERSTATE = "@UserState";
        private const string PARM_USERGROUP = "@UserGroup";
        private const string PARM_USERPOWER = "@UserPower";
        private const string PARM_USEREMAIL = "@UserEmail";
        private const string PARM_USERQQ = "@UserQQ";

        #region Methods

        /// <summary>
        /// 获取某个用户的信息
        /// </summary>
        /// <returns></returns>
        public Model.Users GetUserInfo(int userid)
        {

            SqlParameter parm = new SqlParameter("@uid", SqlDbType.Int);
            parm.Value = userid;
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_UserLoadByUserId", parm);


            if (dt != null && dt.Rows.Count > 0)
            {
                return GetModel(dt.Rows[0]);
            }
            return null;
        }
        /// <summary>
        /// 查询 用户
        /// </summary>
        /// <returns></returns>
        public DataTable GetList(string key)
        {
            SqlParameter parm = new SqlParameter("@key", SqlDbType.NVarChar);
            parm.Value = key;
            return SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_UsersLoadByKey", parm);
        }
        /// <summary>
        /// 添加新的用户
        /// </summary>
        /// <param name="u"></param>
        /// <returns></returns>
        public bool Add(Model.Users model)
        {
            SqlParameter[] parms = SetParmeters(model);
            int rInt = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_UserInsert", parms);

            return rInt > 0;
        }
        /*----------------------------------------------------------------*/
        /// <summary>
        /// 用户总数
        /// </summary>
        /// <returns></returns>
        public int GetTotalitUser()
        {
            SqlParameter parm = new SqlParameter("@uCount", SqlDbType.Int);
            parm.Direction = ParameterDirection.Output;

            object obj = SqlHelper.ExecuteReturnParmeter(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_UserLoadCount", parm);

            if (obj != null)
            {
                int rInt;
                Int32.TryParse(obj.ToString(), out rInt);
                return rInt;
            }
            return 0;
        }
        /// <summary>
        /// 检查用户是否存在
        /// </summary>
        /// <param name="uName"></param>
        /// <returns></returns>
        public int CheckUserName(string uName)
        {

            SqlParameter[] parms = new SqlParameter[] { 
            new SqlParameter("@uCount", SqlDbType.Int),
            new SqlParameter(PARM_USERNAME, SqlDbType.NVarChar)            
            };
            parms[0].Direction = ParameterDirection.Output;
            parms[1].Value = uName;
            object obj = SqlHelper.ExecuteReturnParmeter(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_UserCheckUserName", parms);

            if (obj != null)
            {
                Int32 rInt;
                Int32.TryParse(obj.ToString(), out rInt);
                return rInt;
            }
            return 0;
        }

        /// <summary>
        /// 修改资料
        /// </summary>
        public bool Edit(Model.Users model)
        {
            SqlParameter[] parms = SetParmeters(model);
            int rInt = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_UserUpdate", parms);
            return rInt > 0;
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        public bool EditPWD(Model.Users model)
        {
            SqlParameter[] parms = SetParmeters(model);
            int rInt = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_UserUpdateUserPwd", parms);
            return rInt > 0;
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        public bool Delete(string Userid)
        {

            SqlParameter parm = new SqlParameter(PARM_USERID, SqlDbType.Int);
            parm.Value = Userid;
            int rInt = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_UserDelete", parm);

            return rInt > 0;
        }


        /// <summary>
        /// 按用户名称获取该用户信息
        /// </summary>
        /// <param name="userName">用户名称</param>
        /// <returns></returns>
        public Model.Users Get(string userName)
        {

            SqlParameter parm = new SqlParameter(PARM_USERNAME, SqlDbType.NVarChar);
            parm.Value = userName;

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

            if (dt != null && dt.Rows.Count > 0)
            {
                Model.Users user = GetModel(dt.Rows[0]);
                return user;
            }
            return null;
        }
        public bool EditPower(Stat.Model.Users model)
        {
            SqlParameter[] parms = SetParmeters(model);

            int rInt = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_UserUpdateUserPower", parms);
            return rInt > 0;

        }
        #endregion
        #region ：Page Index
        /// <summary>
        /// 检索用户分页
        /// </summary>
        /// <param name="searchInfo"></param>
        /// <param name="pageInfo"></param>
        /// <returns></returns>
        public IList<global::Stat.Model.Users> Get(global::Stat.Model.Users usersModel, ref global::Stat.Model.PageInfo pageInfo)
        {
            //表名
            string tableName = "Stat_Users";
            //要返回的列
            string fidles = "*";
            //查询条件
            string sqlWhere = "UserGroup=" + usersModel.UserGroup;
            /*-----------------------------------------------------------------*/
            if (usersModel.UserId > 0)
            {
                sqlWhere += " and (UserId = " + usersModel.UserId + "%'";
            }
            if (usersModel.UserName != null && usersModel.UserName.Length > 0)
            {
                sqlWhere += " and (UserName like '%" + usersModel.UserName + "%'";
            }
            if (usersModel.UserQQ != null && usersModel.UserQQ.Length > 0)
            {
                sqlWhere += " or UserQQ like '%" + usersModel.UserQQ + "%'";
            }
            if (usersModel.UserEmail != null && usersModel.UserEmail.Length > 0)
            {
                sqlWhere += " or UserEmail like '%" + usersModel.UserEmail + "%')";
            }
            /*-------------------------------------*/
            //排序字段
            string SortFidle = "UserId";
            /*------------------------------------------------------------------------------------------------------------*/
            int RecordCount = 0;
            DataTable dt = JPageIndex.ProGetList.GetFromDataTable(tableName, fidles, sqlWhere, SortFidle, pageInfo.SortType, pageInfo.PageSize, pageInfo.PageIndex, ref RecordCount, "Stat_PageIndex",SqlHelper.ConnectionStringMaster);
            pageInfo.RecordCount = RecordCount;
            return GetList(dt);
        }

        #endregion
        #region ：Set Parmeters
        private static SqlParameter[] SetParmeters(Model.Users model)
        {
            SqlParameter[] parms = GetParmeters();           
            parms[0].Value = model.UserName;
            parms[1].Value = model.UserPwd;
            parms[2].Value = model.UserState;
            parms[3].Value = model.UserGroup;          
            parms[4].Value = model.UserEmail ?? "";
            parms[5].Value = model.UserQQ ?? "";
            parms[6].Value = model.UserId;
            parms[7].Value = model.UserPower ?? "";
            return parms;
        }

        private static SqlParameter[] GetParmeters()
        {
            SqlParameter[] parms = new SqlParameter[] {
            new SqlParameter(PARM_USERNAME, SqlDbType.NVarChar),
            new SqlParameter(PARM_USERPWD, SqlDbType.NVarChar),
            new SqlParameter(PARM_USERSTATE, SqlDbType.TinyInt),
            new SqlParameter(PARM_USERGROUP, SqlDbType.TinyInt),        
            new SqlParameter(PARM_USEREMAIL, SqlDbType.NVarChar),
            new SqlParameter(PARM_USERQQ, SqlDbType.NVarChar),
            new SqlParameter(PARM_USERID, SqlDbType.Int),
            new SqlParameter(PARM_USERPOWER, SqlDbType.NVarChar)
            };
            return parms;
        }
        #endregion
        #region Utilites
        /// <summary>
        /// 获取一条实体
        /// </summary>
        ///<param name="row"></param>
        /// <returns></returns>
        private Model.Users GetModel(System.Data.DataRow row)
        {
            Model.Users model = new global::Stat.Model.Users();
            if (row != null)
            {
                if (row["UserId"] != DBNull.Value)
                    model.UserId = Convert.ToInt32(row["UserId"]);
                if (row["UserName"] != DBNull.Value)
                    model.UserName = row["UserName"].ToString();
                if (row["UserPwd"] != DBNull.Value)
                    model.UserPwd = row["UserPwd"].ToString();
                if (row["UserPower"] != DBNull.Value)
                    model.UserPower = row["UserPower"].ToString();
                if (row["UserEmail"] != DBNull.Value)
                    model.UserEmail = row["UserEmail"].ToString();
                if (row["UserState"] != DBNull.Value)
                    model.UserState = Convert.ToInt32(row["UserState"]);
                if (row["UserGroup"] != DBNull.Value)
                    model.UserGroup = Convert.ToInt32(row["UserGroup"]);
                if (row["UserQQ"] != DBNull.Value)
                    model.UserQQ = row["UserQQ"].ToString();
                if (row["UserEmail"] != DBNull.Value)
                    model.UserEmail = row["UserEmail"].ToString();
                if (row["AddTime"] != DBNull.Value)
                    model.AddTime = Convert.ToDateTime(row["AddTime"]);
                if (row["SystemID"] != DBNull.Value)
                    model.SystemID = Convert.ToInt32(row["SystemID"]);
            }
            return model;
        }
        /// <summary>
        /// 获取 列表
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        private IList<Model.Users> GetList(System.Data.DataTable table)
        {
            List<Model.Users> list = new List<global::Stat.Model.Users>();
            if (table != null)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    list.Add(GetModel(table.Rows[i]));
                }
            }
            return list;
        }


        #endregion

    }
}
