using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Data;
namespace CitySunlight
{
    public static class UserManager
    {
        private const String tbname = "tb_user";
        private static Dictionary<String, int> trytimes = new Dictionary<string, int>();
        private static SqlHelper sql = new SqlHelper("Data Source=.;Initial Catalog=myApplication;User ID=sa;Password=325325");
        /// <summary>
        /// 登录错误增加错误次数
        /// </summary>
        /// <param name="username">用户名</param>
        /// <returns>错误次数</returns>
        public static int ErrLogin(String username)
        {
            return UpdateTimes(username, true);
        }
        /// <summary>
        /// 获取错误次数
        /// </summary>
        /// <param name="username">用户名</param>
        /// <returns></returns>
        public static int GetTimes(String username)
        {
            return UpdateTimes(username, false);
        }
        /// <summary>
        /// 更新错误次数
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="add">是否添加</param>
        /// <returns></returns>
        private static int UpdateTimes(String username, bool add)
        {
            if (!trytimes.ContainsKey(username))
            {
                trytimes.Add(username, add ? 1 : 0);
            }
            else
            {
                if (add) { trytimes[username] += 1; };
            }
            return trytimes[username];
        }
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <returns>是否登录成功</returns>
        public static bool LoginUser(String username, String password)
        {
            DataTable dt = sql.ExecuteDataTable("select * from " + tbname + " where UserName='" + username + "' and PassWord='" + password + "'", CommandType.Text);
            //return "select * from user where username='" + username + "' and password='" + password + "'";
            if (dt.Rows.Count > 0)
            {
                return true;
            }
            UpdateTimes(username, true);
            return false;
        }
        /// <summary>
        /// 检查用户是否存在
        /// </summary>
        /// <param name="username">用户名</param>
        /// <returns>是否存在</returns>
        public static bool isExist(String username)
        {
            DataTable dt = sql.ExecuteDataTable("select * from " + tbname + " where UserName='" + username + "'", CommandType.Text);
            if (dt.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 注册用户
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <returns>是否注册成功</returns>
        public static bool RegisterUser(String username, String password)
        {
            int insert = sql.ExecuteNonQuery("insert into " + tbname + "(UserName,PassWord) values('" + username + "','" + password + "')");
            if (insert > 0)
            {
                return true;
            }
            return false;
        }
    }
}