using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 UUN;
using System.Data.SqlClient;

public partial class Admin_YongHu_HuiYuan : System.Web.UI.Page
{
    private 访问 访问;
    private String SQL = "SELECT [ID],[性别],[昵称],[积分],[等级],[所在位置],[是否加黑],[今日访问次数],[最后登录地区] FROM [" + 程序.数据表前缀 + "社区_用户_用户] WHERE [用户类型]='会员'";
    private String 数量SQL = "SELECT COUNT(ID) FROM [" + 程序.数据表前缀 + "社区_用户_用户] WHERE [用户类型]='会员'";
    private String 排序 = " [ID] DESC";
    public int 总访问数;
    public String 用户组代码;
    public int 当前页;
    protected void Page_Load(object sender, EventArgs e)
    {
        访问 = new 访问(this.Context);
        访问.开始处理("会员管理", "网站后台", "会员管理", new 访问.回调委托(ZhiXing));
        访问.关闭连接();
    }
    private void ZhiXing()
    {
        访问.验证管理员();
        DataSet 用户组数据 = (DataSet)Application["UUN_UUN3G_YongHuZu"];
        用户组代码 = "<select name=\"yhz\">" +
            "<option value=\"\">不限</option>";
        for (int i = 0; i < 用户组数据.Tables[0].Rows.Count; i++)
        {
            DataRow 行 = 用户组数据.Tables[0].Rows[i];
            if(行["用户类型"].ToString()=="会员")
                用户组代码 += "<option value=\"" + Server.HtmlEncode(行["组名称"].ToString()) + "\">" + Server.HtmlEncode(行["组名称"].ToString()) + "</option>";
        }
        用户组代码 += "</select>";
        生成查询条件();
        生成排序();
        if (!this.IsPostBack)
            生成数据();
    }
    private void 生成排序()
    {
        String 字段 = Request.QueryString["zd"];
        if (字段 == null || 字段 == "")
            return;
        bool 方向 = Convert.ToBoolean(Request.QueryString["fx"]);
        排序 = " [" + 字段 + "]" + (方向 ? " DESC" : "");
        if (!this.IsPostBack)
        {
            PaiXuZiDuan.Value = 字段;
            PaiXuFangXiang.Value = 方向.ToString();
        }
    }
    private String 用户;
    private int 类型;
    private String 用户组;
    private int 最后访问;
    private int 条件;
    private SqlParameter 参数1;
    private SqlParameter 参数2;
    private SqlParameter 参数3;
    private void 生成查询条件()
    {
        用户 = Request.QueryString["yh"];
        类型 = Convert.ToInt32(Request.QueryString["lx"]);
        用户组 = Request.QueryString["yhz"];
        最后访问=Convert.ToInt32(Request.QueryString["fw"]);
        条件=Convert.ToInt32(Request.QueryString["tj"]);

        if(用户!=""&&用户!=null)
        {
            switch(类型)
            {
                case 0:
                    SQL += " AND [昵称] LIKE @昵称";
                    数量SQL += " AND [昵称] LIKE @昵称";
                    参数1 = new SqlParameter("@昵称", SqlDbType.NVarChar);
                    参数1.Value = "%" + 用户.Replace(" ", "%") + "%";
                    break;
                case 1:
                    SQL += " AND [ID]=" + Convert.ToInt32(用户);
                    数量SQL += " AND [ID]=" + Convert.ToInt32(用户);
                    break;
                case 2:
                    SQL += " AND [手机号]=@手机号";
                    数量SQL += " AND [手机号]=@手机号";
                    参数1 = new SqlParameter("@手机号", SqlDbType.NVarChar);
                    参数1.Value = 用户;
                    break;
                default:
                    SQL += " AND [标识]=@标识";
                    数量SQL += " AND [标识]=@标识";
                    参数1 = new SqlParameter("@标识", SqlDbType.NVarChar);
                    参数1.Value = 用户;
                    break;
            }
        }
        if (用户组 != ""&&用户组!=null)
        {
            SQL += " AND [用户组]=@用户组";
            数量SQL += " AND [用户组]=@用户组";
            参数2 = new SqlParameter("@用户组", SqlDbType.NVarChar);
            参数2.Value = 用户组;
        }
        if (最后访问 != 0)
        {
            SQL += " AND [最后访问时间]" + (条件 == 0 ? ">" : "<") + "@最后访问时间";
            参数3 = new SqlParameter("@最后访问时间", SqlDbType.DateTime);
            switch (最后访问)
            {
                case 1:
                    参数3.Value = DateTime.Now - new TimeSpan(0, Convert.ToInt32(程序.设置["离线时间"]), 0);
                    break;
                case 2:
                    参数3.Value = DateTime.Now - new TimeSpan(1,0, 0, 0);
                    break;
                case 3:
                    参数3.Value = DateTime.Now - new TimeSpan(7, 0, 0, 0);
                    break;
                case 4:
                    参数3.Value = DateTime.Now - new TimeSpan(30, 0, 0, 0);
                    break;
                case 5:
                    参数3.Value = DateTime.Now - new TimeSpan(100, 0, 0, 0);
                    break;
                case 6:
                    参数3.Value = DateTime.Now - new TimeSpan(180, 0, 0, 0);
                    break;
                default:
                    参数3.Value = DateTime.Now - new TimeSpan(365, 0, 0, 0);
                    break;
            }
        }
    }
    private void 生成数据()
    {
        SqlCommand 语句 = new SqlCommand(数量SQL, 访问.连接);
        if (参数1 != null)
            语句.Parameters.Add(参数1);
        if (参数2 != null)
            语句.Parameters.Add(参数2);
        if (参数3 != null)
            语句.Parameters.Add(参数3);

        后台分页 分页 = new 后台分页(Convert.ToInt32(语句.ExecuteScalar()),
            Convert.ToInt32(Request.QueryString["page"]),
            10,
            "HuiYuan.aspx?zd=" + Server.UrlEncode(PaiXuZiDuan.Value) +
            "&fx=" + Server.UrlEncode(PaiXuFangXiang.Value)+
            "&yh="+Server.UrlEncode(用户)+
            "&lx="+类型+
            "&yhz="+Server.UrlEncode(用户组)+
            "&fw="+最后访问+
            "&tj="+条件);
        当前页 = 分页.当前页;

        DataSet 数据 = new DataSet();
        语句 = new SqlCommand(SQL + " ORDER BY " + 排序, 访问.连接);
        if (参数1 != null)
            语句.Parameters.Add(参数1.ParameterName, 参数1.SqlDbType).Value = 参数1.Value;
        if (参数2 != null)
            语句.Parameters.Add(参数2.ParameterName, 参数2.SqlDbType).Value = 参数2.Value;
        if (参数3 != null)
            语句.Parameters.Add(参数3.ParameterName, 参数3.SqlDbType).Value = 参数3.Value;
        SqlDataAdapter 填充 = new SqlDataAdapter(语句);
        填充.Fill(数据, (分页.当前页 - 1) * 10, 10, "用户");

        GridView1.DataSource = 数据;
        GridView1.DataBind();

        FenYeDaiMa.Text = 分页.生成代码();
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        排序 = "[" + e.SortExpression + "]";

        if (PaiXuZiDuan.Value == e.SortExpression && (!Convert.ToBoolean(PaiXuFangXiang.Value)))
        {
            排序 += " DESC";
            PaiXuFangXiang.Value = "true";
        }
        else
        {
            排序 += "";
            PaiXuFangXiang.Value = "false";
        }

        PaiXuZiDuan.Value = e.SortExpression;

        访问.连接.Open();

        访问.排错调用(new 访问.回调委托(生成数据));

        访问.关闭连接();
    }
}
