using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HySoft.Common;
using System.Data;

namespace HySoft.BaseCallCenter.Web.telmanage.ajax
{
    /// <summary>
    /// selectuser 的摘要说明
    /// </summary>
    public class selectuser : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //取得处事类型
            string action = CommonRequest.GetQueryString("action");
            switch (action)
            {
                case "getaftersalelist":
                    context.Response.Write(LoadAfterSaleList(context));
                    break;
                case "getuserbyname":
                    context.Response.Write(LoadByNameList(context));
                    break;
            }
        }

        private string LoadByNameList(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            try
            {
                string sql = "";
                string name = CommonRequest.GetQueryString("name");
                if (name != "")
                {
                    sql = " and (F_UserCode like '%" + name + "%' or F_UserName like '%" + name + "%') ";
                }
                int recordCount = 0;
                string strpageindex = context.Request.Params["page"];
                int pageindex = 1;
                string strpagesize = context.Request.Params["pagesize"];
                int pagesize = 5;
                if (strpageindex.Trim() != "")
                {
                    try
                    {
                        pageindex = Convert.ToInt32(strpageindex);
                    }
                    catch
                    { }
                }

                if (strpagesize.Trim() != "")
                {
                    try
                    {
                        pagesize = Convert.ToInt32(strpagesize);
                    }
                    catch
                    { }
                }

                Model.PageData<Model.UserAccount> pageModel = new Model.PageData<Model.UserAccount>();

                dt = BLL.PagerBLL.GetListPager(
                    "T_Sys_UserAccount",
                    "F_UserId",
                    "*",
                    " and F_DeleteFlag=0 " + sql,
                    "ORDER BY F_UserId desc",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);

                System.Collections.Generic.List<Model.UserAccount> modelList = new BLL.UserAccount().DataTableToList(dt);
                pageModel.Rows = modelList;
                pageModel.Total = recordCount;
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer =
                    new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(Model.PageData<Model.UserAccount>));

                using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
                {
                    //JSON序列化
                    serializer.WriteObject(stream, pageModel);
                    res = System.Text.Encoding.UTF8.GetString(stream.ToArray());
                }
            }
            catch (Exception err)
            {
                //res = err.ToString(); 
            }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }
            return res;
        }

        private string LoadAfterSaleList(HttpContext context)
        {
            string res = "";
            try
            {
                string sql = "";
                string deptid = CommonRequest.GetQueryString("deptid");
                if (deptid != "")
                {
                    sql = " F_DeptId=" + deptid;
                }
                List<Model.T_Sys_UserAccount> pageModel = new List<Model.T_Sys_UserAccount>();
                //if (context.Request.Params["param"] != null)
                pageModel = new BLL.T_Sys_UserAccount().GetModelList(sql);
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List<Model.T_Sys_UserAccount>));

                using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
                {
                    //JSON序列化
                    serializer.WriteObject(stream, pageModel);
                    res = System.Text.Encoding.UTF8.GetString(stream.ToArray());
                }
            }
            catch (Exception err)
            {
                //res = err.ToString(); 
            }
            finally
            {

            }
            return res;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}