using System.Collections.Generic;
using System.Data;

namespace Stat.BLL
{
    /// <summary>
    /// 用户
    /// </summary>
    public class Users
    {
        private static IDAL.IUsers dal = DALFactory.AbstractFactory.GetInstance().CreateUsers();


        /// <summary>
        /// 用户检索 分页
        /// </summary>
        /// <param name="userModel"></param>
        /// <param name="pageInfo"></param>
        /// <returns></returns>
        public static IList<Model.Users> Get(Model.Users userModel, ref Model.PageInfo pageInfo)
        {
            return dal.Get(userModel, ref pageInfo);
        }
        /// <summary>
        /// 用户总数
        /// </summary>
        /// <returns></returns>
        public static int GetUserSum()
        {
            return dal.GetTotalitUser();
        }
        /// <summary>
        /// 查询 用户
        /// </summary>
        /// <returns></returns>
        public static DataTable GetList(string key)
        {
            return dal.GetList(key);
        }
        /// <summary>
        /// 获取某个用户的信息
        /// </summary>
        /// <returns></returns>
        public static Model.Users GetUserInfo(int uid)
        {
            return dal.GetUserInfo(uid);
        }
        /// <summary>
        /// 添加新的用户
        /// </summary>
        /// <returns></returns>
        public static bool Add(Stat.Model.Users model)
        {
            if (model.IsPwdMD5) { model.UserPwd = Plugin.Common.MD5(model.UserPwd); }
            model.UserState = 1;
            model.UserPower = model.UserPower == null ? "" : model.UserPower;
            return dal.Add(model);
        }
        /// <summary>
        /// 修改资料
        /// </summary>
        public static bool Edit(Stat.Model.Users model)
        {
            if (model.UserPwd.Length > 0)
            {
                if (model.IsPwdMD5) { model.UserPwd = Plugin.Common.MD5(model.UserPwd); }
            }
            return dal.Edit(model);
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        public static bool EditPWD(Stat.Model.Users model)
        {
            if (model.UserPwd.Length > 0)
            {
                if (model.IsPwdMD5) { model.UserPwd = Plugin.Common.MD5(model.UserPwd); }
            }
            return dal.EditPWD(model);
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        public static bool Delete(string Userid)
        {
            if (Userid == "1")
                return false; //保护 1:admin 避免误删
            else
                return dal.Delete(Userid);
        }
        /// <summary>
        /// 修改权限
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool EditPower(Stat.Model.Users model)
        {

           return dal.EditPower(model);
        }


        /// <summary>
        /// 检查用户是否存在
        /// </summary>
        /// <param name="uName"></param>
        /// <returns></returns>
        public static int CheckUserName(string uName)
        {
            return dal.CheckUserName(uName);
        }

        /// <summary>
        /// 获取 用户信息
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public static  Model.Users Get(string userName)
        {
            return dal.Get(userName);
        }
    }
}
