using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;

/// <summary>
/// UserManager 的摘要说明
/// </summary>
public static class UserManager
{
    public static string AddUser(UserInfo u)
    {
        string sql = string.Format("INSERT INTO UserInfo VALUES ('{0}','{1}','{2}','{3}','{4}')",u.UId,u.UName,u.UPwd,u.UQQ,u.URegTime);
        int i = DBHelper.ExecuteCommand(sql);
        if (i > 0)
        {
            return "注册成功";
        }
        else
        {
            return "注册失败";
        }
    }

    public static string ExistUserName(string userName)
    {
        string sql = string.Format("SELECT COUNT(UId) FROM UserInfo WHERE UName='{0}'", userName);
        int i = DBHelper.ExecuteCommand(sql);
        if (i > 0)
        {
            return "此用户名已存在";
        }
        else
        {
            return "该用户名未被使用";
        }
    }

    public static string UserLogin(string uname, string upwd)
    {
        string sql = string.Format("SELECT COUNT(UId) FROM UserInfo WHERE UName='{0}' AND UPwd='{1}'",uname,upwd);
        int i = DBHelper.GetScalar(sql);
        if (i > 0)
        {
            return "登录成功";
        }
        else
        {
            return "用户名或密码不正确";
        }
    }

    public static List<UserInfo> ShowUserList()
    {
        string sql = "SELECT * FROM UserInfo";
        List<UserInfo> list = new List<UserInfo>();
        using (SqlDataReader Reader = DBHelper.GetReader(sql))
        {
            if (!Reader.HasRows)
            {
                list = null;
            }
            else
            {
                while (Reader.Read())
                {
                    UserInfo u = new UserInfo();
                    u.UId = (Guid)Reader["UId"];
                    u.UName = Reader["UName"].ToString();
                    u.UPwd = Reader["UPwd"].ToString();
                    u.UQQ = Reader["UQQ"].ToString();
                    u.URegTime = Convert.ToDateTime(Reader["URegTime"]);
                    list.Add(u);
                }
            }
        }
        return list;
    }

    public static List<UserInfo> ShowUserList(string uname)
    {
        string sql = "SELECT * FROM UserInfo WHERE UName LIKE '%" + uname + "%'";
        List<UserInfo> list = new List<UserInfo>();
        using (SqlDataReader Reader = DBHelper.GetReader(sql))
        {
            if (!Reader.HasRows)
            {
                list = null;
            }
            else
            {
                while (Reader.Read())
                {
                    UserInfo u = new UserInfo();
                    u.UId = (Guid)Reader["UId"];
                    u.UName = Reader["UName"].ToString();
                    u.UPwd = Reader["UPwd"].ToString();
                    u.UQQ = Reader["UQQ"].ToString();
                    u.URegTime = Convert.ToDateTime(Reader["URegTime"]);
                    list.Add(u);
                }
            }
        }
        return list;
    }

    public static string DeleteUserByUId(string uid)
    {
        string sql = string.Format("DELETE UserInfo WHERE UId='{0}'", uid);
        int i = DBHelper.ExecuteCommand(sql);
        if (i > 0)
        {
            return "删除成功";
        }
        else
        {
            return "删除失败";
        }
    }
}
