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

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

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

        #region 获取数据
        private string LoadList(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            try
            {
                string strpageindex = context.Request.Params["page"];
                int pageindex = 1;
                string strpagesize = context.Request.Params["pagesize"];
                int pagesize = 10;
                string sql = "";
                string phone = HttpUtility.UrlDecode(CommonRequest.GetQueryString("phone"));
                string callstate = HttpUtility.UrlDecode(CommonRequest.GetQueryString("callstate"));
                string usercode = HttpUtility.UrlDecode(CommonRequest.GetQueryString("usercode"));
                string calltype = HttpUtility.UrlDecode(CommonRequest.GetQueryString("calltype"));
                string starttime = HttpUtility.UrlDecode(CommonRequest.GetQueryString("starttime"));
                string endtime = HttpUtility.UrlDecode(CommonRequest.GetQueryString("endtime"));
                if (phone.Trim() != "")
                {
                    sql += " and CallNumber='" + phone + "'";
                }
                if (callstate.Trim() != "")
                {
                    sql += " and CallState='" + callstate + "'";
                }
                if (usercode.Trim() != "")
                {
                    sql += " and UserCode='" + usercode + "'";
                }
                if (calltype.Trim() != "")
                {
                    sql += " and CallType='" + calltype + "'";
                }
                if (starttime.Trim() != "")
                {
                    sql += " and BeginTime>='" + starttime + "'";
                }
                if (endtime.Trim() != "")
                {
                    sql += " and EndTime<='" + endtime + "'";
                }
                if (strpageindex.Trim() != "")
                {
                    try
                    {
                        pageindex = Convert.ToInt32(strpageindex);
                    }
                    catch
                    { }
                }
                if (strpagesize.Trim() != "")
                {
                    try
                    {
                        pagesize = Convert.ToInt32(strpagesize);
                    }
                    catch
                    { }
                }
                int recordCount = 0;
                Model.PageData<Model.T_Call_CallRecords_QC> pageModel = new Model.PageData<Model.T_Call_CallRecords_QC>();
                dt = BLL.PagerBLL.GetListPager(
                    "T_Call_CallRecords",
                    "CallRecordsId",
                    "*",
                    sql,
                    "ORDER BY CallRecordsId desc",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);
                System.Collections.Generic.List<Model.T_Call_CallRecords_QC> modelList = new BLL.T_Call_CallRecords_QC().DataTableToList(dt);
                pageModel.Rows = modelList;
                pageModel.Total = recordCount;
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(Model.PageData<Model.T_Call_CallRecords_QC>));
                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;
        }
        #endregion

        #region 获取数据
        private string LoadYZJList(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            try
            {
                string strpageindex = context.Request.Params["page"];
                int pageindex = 1;
                string strpagesize = context.Request.Params["pagesize"];
                int pagesize = 10;
                string sql = " and F_QCState=1 ";
                string phone = HttpUtility.UrlDecode(CommonRequest.GetQueryString("phone"));
                string callstate = HttpUtility.UrlDecode(CommonRequest.GetQueryString("callstate"));
                string usercode = HttpUtility.UrlDecode(CommonRequest.GetQueryString("usercode"));
                string calltype = HttpUtility.UrlDecode(CommonRequest.GetQueryString("calltype"));
                string starttime = HttpUtility.UrlDecode(CommonRequest.GetQueryString("starttime"));
                string endtime = HttpUtility.UrlDecode(CommonRequest.GetQueryString("endtime"));
                if (phone.Trim() != "")
                {
                    sql += " and CallNumber='" + phone + "'";
                }
                if (callstate.Trim() != "")
                {
                    sql += " and CallState='" + callstate + "'";
                }
                if (usercode.Trim() != "")
                {
                    sql += " and UserCode='" + usercode + "'";
                }
                if (calltype.Trim() != "")
                {
                    sql += " and CallType='" + calltype + "'";
                }
                if (starttime.Trim() != "")
                {
                    sql += " and BeginTime>='" + starttime + "'";
                }
                if (endtime.Trim() != "")
                {
                    sql += " and EndTime<='" + endtime + "'";
                }
                if (strpageindex.Trim() != "")
                {
                    try
                    {
                        pageindex = Convert.ToInt32(strpageindex);
                    }
                    catch
                    { }
                }
                if (strpagesize.Trim() != "")
                {
                    try
                    {
                        pagesize = Convert.ToInt32(strpagesize);
                    }
                    catch
                    { }
                }
                int recordCount = 0;
                Model.PageData<Model.T_Call_CallRecords_QC> pageModel = new Model.PageData<Model.T_Call_CallRecords_QC>();
                dt = BLL.PagerBLL.GetListPager(
                    "T_Call_CallRecords",
                    "CallRecordsId",
                    "*",
                    sql,
                    "ORDER BY CallRecordsId desc",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);
                System.Collections.Generic.List<Model.T_Call_CallRecords_QC> modelList = new BLL.T_Call_CallRecords_QC().DataTableToList(dt);
                pageModel.Rows = modelList;
                pageModel.Total = recordCount;
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(Model.PageData<Model.T_Call_CallRecords_QC>));
                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;
        }
        #endregion

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