using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using JuSNS.Profile;
using JuSNS.Factory;
using JuSNS.Config;
using JuSNS.Model;

namespace JuSNS.SQLServer
{
    public class Manager : DbBase, IManager
    {
        /// <summary>
        /// 检查登录
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        EnumLoginState IManager.CheckLogin(int adminId, string password)
        {
            SqlConnection cn = new SqlConnection(DBConfig.CnString);
            cn.Open();
            try
            {
                string pwd = string.Empty;
                EnumUserState us = EnumUserState.Lock;
                bool exist = false;
                string Sql = "select Password,State from NT_Admin where State=5 and AdminID=" + adminId;
                IDataReader rd = DbHelper.ExecuteReader(cn, CommandType.Text, Sql, null);
                if (rd.Read())
                {
                    pwd = rd.GetString(0);
                    us = (EnumUserState)rd.GetInt32(1);
                    exist = true;
                }
                rd.Close();
                if (!exist || pwd == string.Empty || pwd != password)
                    return EnumLoginState.Err_NameOrPwdError;
                if (us == EnumUserState.Register)
                    return EnumLoginState.Err_UnActivation;
                else if (us == EnumUserState.Lock)
                    return EnumLoginState.Err_AdminLocked;
                return EnumLoginState.Succeed;
            }
            finally
            {
                if (cn.State == ConnectionState.Open)
                    cn.Close();
            }
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="Email">用户名</param>
        /// <param name="Password">密码</param>
        /// <returns></returns>
        EnumLoginState IManager.Login(string loginName, string md5Password, out int adminId, out string trueName)
        {
            adminId = -1;
            trueName = string.Empty;
            SqlConnection cn = new SqlConnection(DBConfig.CnString);
            cn.Open();
            try
            {
                string pwd = string.Empty;
                EnumUserState state = EnumUserState.Lock;
                SqlParameter param = new SqlParameter("@LoginName", loginName);
                bool exist = false;
                string Sql = "select AdminID,TrueName,Password,State from NT_Admin where LoginName=@LoginName";
                IDataReader rd = DbHelper.ExecuteReader(cn, CommandType.Text, Sql, param);
                if (rd.Read())
                {
                    adminId = rd.GetInt32(0);
                    trueName = rd.GetString(1);
                    pwd = rd.GetString(2);
                    state = (EnumUserState)rd.GetInt32(3);
                    exist = true;
                }
                rd.Close();
                if (!exist || pwd == string.Empty || pwd != md5Password)
                    return EnumLoginState.Err_NameOrPwdError;
                else if (state == EnumUserState.Lock)
                    return EnumLoginState.Err_Locked;
                Sql = "update NT_Admin set LastLoginTime='" + DateTime.Now + "',LastLoginIP='" + JuSNS.Common.Public.GetClientIP() + "',LoginTimes=LoginTimes+1 where AdminID=" + adminId;
                DbHelper.ExecuteNonQuery(cn, CommandType.Text, Sql, null);
                return EnumLoginState.Succeed;
            }
            finally
            {
                if (cn.State == ConnectionState.Open)
                    cn.Close();
            }
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="adminId"></param>
        /// <param name="oldPassword">未经md5的旧密码</param>
        /// <param name="newPassword">未经md5的新密码</param>
        /// <returns>0,成功,1,该管理员ID不存在,2旧密码错误</returns>
        int IManager.ChangePasswrod(int adminId, string oldPassword, string newPassword)
        {
            SqlConnection cn = new SqlConnection(DBConfig.CnString);
            cn.Open();
            try
            {
                string Sql = "select Password from NT_Admin where AdminID=" + adminId;
                object obj = DbHelper.ExecuteScalar(cn, CommandType.Text, Sql, null);
                if (obj == null)
                {
                    return 1;
                }
                if ((string)obj != Common.Input.MD5(oldPassword, false))
                    return 2;
                Sql = "update NT_Admin set Password='" + Common.Input.MD5(newPassword, false) + "' where AdminID=" + adminId;
                int n = DbHelper.ExecuteNonQuery(cn, CommandType.Text, Sql, null);
                if (n > 0)
                    return 0;
                else
                    return 3;
            }
            finally
            {
                if (cn.State == ConnectionState.Open)
                    cn.Close();
            }
        }

        public int AddAdmin(string AdminName, string Password, string TrueName, int state)
        {
            string gSQL = "select count(AdminID) from NT_Admin where LoginName='" + AdminName + "'";
            int n = Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, gSQL, null).ToString());
            if (n > 0)
            {
                return 2;
            }
            string sql = "insert into NT_Admin(LoginName,TrueName,Password,LastLoginTime,LastLoginIP,LoginTimes,State)values('" + AdminName + "','" + TrueName + "','" + JuSNS.Common.Input.MD5(Password, false) + "','','" + JuSNS.Common.Public.GetClientIP() + "',0," + state + ")";
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, null);
        }

        public int updateAdmin(string AdminName, string Password, string TrueName, int state,int AdminID)
        {
            string sql = "update NT_Admin set Password='" + Password + "',TrueName='" + TrueName + "',State=" + state + " where AdminID=" + AdminID;
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, null);
        }

        public DataTable GetAdminInfo(int ID)
        {
            string sql = "select * from NT_Admin where AdminID=" + ID;
            return DbHelper.ExecuteTable(CommandType.Text, sql, null);
        }

        public DataTable AdminList()
        {
            string sql = "select * from NT_Admin order by adminID desc";
            return DbHelper.ExecuteTable(CommandType.Text, sql, null);
        }
    }
}
