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

namespace HySoft.BaseCallCenter.Web.calloutmanage.ajax
{
    /// <summary>
    /// callplantelnum 的摘要说明
    /// </summary>
    public class callplantelnum : 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 "getwfplist":
                    context.Response.Write(LoadWFPList(context));
                    break;
                case "getmylist":
                    context.Response.Write(LoadMyList(context));
                    break;
                case "getmyresultlist":
                    context.Response.Write(LoadMyResultList(context));
                    break;
                case "getsearchresultlist":
                    context.Response.Write(LoadSearchResultList(context));
                    break;
                case "deletetelnum":
                    context.Response.Write(DeleteList(context));
                    break;
                case "qxfptelnum":
                    context.Response.Write(QXFPList(context));
                    break;
            }
        }

        #region 批量取消分配数据

        private string QXFPList(HttpContext context)
        {
            string res = "error";
            try
            {
                string arr = context.Request.Params["arrid"];
                if (new BLL.T_Call_TaskTelNum().UpdateQXFP(arr))
                {
                    res = "success";
                }
            }
            catch
            { }
            return res;
        }

        #endregion

        #region 批量删除数据

        private string DeleteList(HttpContext context)
        {
            string res = "error";
            try
            {
                string arr = context.Request.Params["arrid"];
                if (new BLL.T_Call_TaskTelNum().DeleteList(arr))
                {
                    res = "success";
                }
            }
            catch
            { }
            return res;
        }

        #endregion

        private string LoadMyList(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = " and F_HCState=0 ";

            try
            {
                string strpageindex = context.Request.Params["page"];
                int pageindex = 1;
                string strpagesize = context.Request.Params["pagesize"];
                int pagesize = 10;
                string key = context.Request.Params["key"];
                string taskid = context.Request.Params["taskid"];
                string userid = context.Request.Params["userid"];

                if (userid.Trim() != "")
                {
                    sql += " and F_UserId=" + userid + " ";
                }
                else
                {
                    sql += " and 1=0 ";
                }

                if (taskid.Trim() != "")
                {
                    sql += " and F_TaskId=" + taskid + " ";
                }
                if (key.Trim() != "")
                {
                    sql += " and (F_Phone like '%" + key.Trim() + "%' or F_Customer like '%" + key.Trim() + "%' ) ";
                }

                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_TaskTelNum> pageModel = new Model.PageData<Model.T_Call_TaskTelNum>();

                dt = BLL.PagerBLL.GetListPager(
                    "vw_Call_TaskTelNum",
                    "F_Id",
                    "*",
                    " " + sql,
                    "ORDER BY F_Id desc",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);

                System.Collections.Generic.List<Model.T_Call_TaskTelNum> modelList = new BLL.T_Call_TaskTelNum().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_TaskTelNum>));

                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 LoadMyResultList(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = "  and F_HCState=1 ";

            try
            {
                string strpageindex = context.Request.Params["page"];
                int pageindex = 1;
                string strpagesize = context.Request.Params["pagesize"];
                int pagesize = 10;
                string key = context.Request.Params["key"];
                string taskid = context.Request.Params["taskid"];
                string userid = context.Request.Params["userid"];

                try
                {
                    string fpstate = context.Request.Params["fpstate"];
                    string hcstate = context.Request.Params["hcstate"];
                    string yjstate = context.Request.Params["yjstate"];
                    string hjjg = CommonRequest.GetQueryString("hjjg");
                    if (fpstate.Trim() != "")
                    {
                        sql += " and F_FPState=" + fpstate + " ";
                    }
                    if (hcstate.Trim() != "")
                    {
                        sql += " and F_HCState=" + hcstate + " ";
                    }
                    if (yjstate.Trim() != "")
                    {
                        sql += " and F_YJState=" + yjstate + " ";
                    }
                    if (hjjg.Trim() != "")
                    {
                        sql += " and F_HJJGId=" + hjjg + " ";
                    }
                }
                catch
                {
                }

                try
                {
                    string starttime = HttpUtility.UrlDecode(CommonRequest.GetQueryString("starttime"));
                    string endtime = HttpUtility.UrlDecode(CommonRequest.GetQueryString("endtime"));
                    string timesql = "";
                    int t = 0;
                    if (starttime.Trim() != "")
                    {
                        t = 1;
                        timesql += " and datediff(day,BeginTime,'" + starttime + "')<=0 ";
                    }
                    if (endtime.Trim() != "")
                    {
                        t = 1;
                        timesql += " and datediff(day,BeginTime,'" + endtime + "')>=0 ";
                    }
                    if (t > 0)
                    {

                        sql += " and F_Id in(SELECT distinct OperateObject FROM [T_Call_CallRecords] where OperateType=7 and CallType=1 " + timesql + ") ";
                    }

                }
                catch
                { }

                if (userid.Trim() != "")
                {
                    sql += " and F_UserId=" + userid + " ";
                }
                else
                {
                    sql += " and 1=0 ";
                }

                if (taskid.Trim() != "")
                {
                    sql += " and F_TaskId=" + taskid + " ";
                }
                if (key.Trim() != "")
                {
                    sql += " and (F_Phone like '%" + key.Trim() + "%' or F_Customer like '%" + key.Trim() + "%' ) ";
                }

                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_TaskTelNumRes> pageModel = new Model.PageData<Model.T_Call_TaskTelNumRes>();

                dt = BLL.PagerBLL.GetListPager(
                    "vw_Call_TaskTelNum",
                    "F_Id",
                    "*",
                    " " + sql,
                    "ORDER BY F_Id desc",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);

                System.Collections.Generic.List<Model.T_Call_TaskTelNumRes> modelList = new BLL.T_Call_TaskTelNum().DataTableToListRes(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_TaskTelNumRes>));

                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 LoadSearchResultList(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = "  ";

            try
            {
                string strpageindex = context.Request.Params["page"];
                int pageindex = 1;
                string strpagesize = context.Request.Params["pagesize"];
                int pagesize = 10;
                string key = context.Request.Params["key"];
                string taskid = context.Request.Params["taskid"];
                string userid = context.Request.Params["userid"];

                try
                {
                    string fpstate = context.Request.Params["fpstate"];
                    string hcstate = context.Request.Params["hcstate"];
                    string yjstate = context.Request.Params["yjstate"];
                    string hjjg = CommonRequest.GetQueryString("hjjg");
                    if (fpstate.Trim() != "")
                    {
                        sql += " and F_FPState=" + fpstate + " ";
                    }
                    if (hcstate.Trim() != "")
                    {
                        sql += " and F_HCState=" + hcstate + " ";
                    }
                    if (yjstate.Trim() != "")
                    {
                        sql += " and F_YJState=" + yjstate + " ";
                    }
                    if (hjjg.Trim() != "")
                    {
                        sql += " and F_HJJGId=" + hjjg + " ";
                    }
                }
                catch
                {
                }

                try
                {
                    string starttime = HttpUtility.UrlDecode(CommonRequest.GetQueryString("starttime"));
                    string endtime = HttpUtility.UrlDecode(CommonRequest.GetQueryString("endtime"));
                    string timesql = "";
                    int t = 0;
                    if (starttime.Trim() != "")
                    {
                        t = 1;
                        timesql += " and datediff(day,BeginTime,'" + starttime + "')<=0 ";
                    }
                    if (endtime.Trim() != "")
                    {
                        t = 1;
                        timesql += " and datediff(day,BeginTime,'" + endtime + "')>=0 ";
                    }
                    if (t > 0)
                    {
                        
                        sql += " and F_Id in(SELECT distinct OperateObject FROM [T_Call_CallRecords] where OperateType=7 and CallType=1 " + timesql + ") ";
                    }

                }
                catch
                { }

                if (userid.Trim() != "")
                {
                    sql += " and F_UserId=" + userid + " ";
                }

                if (taskid.Trim() != "")
                {
                    sql += " and F_TaskId=" + taskid + " ";
                }
                if (key.Trim() != "")
                {
                    sql += " and (F_Phone like '%" + key.Trim() + "%' or F_Customer like '%" + key.Trim() + "%' ) ";
                }

                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_TaskTelNumRes> pageModel = new Model.PageData<Model.T_Call_TaskTelNumRes>();

                dt = BLL.PagerBLL.GetListPager(
                    "vw_Call_TaskTelNum",
                    "F_Id",
                    "*",
                    " " + sql,
                    "ORDER BY F_Id desc",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);

                System.Collections.Generic.List<Model.T_Call_TaskTelNumRes> modelList = new BLL.T_Call_TaskTelNum().DataTableToListRes(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_TaskTelNumRes>));

                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;
        }

        #region 获取数据
        private string LoadList(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = " ";

            try
            {
                string strpageindex = context.Request.Params["page"];
                int pageindex = 1;
                string strpagesize = context.Request.Params["pagesize"];
                int pagesize = 10;
                string key = context.Request.Params["key"];
                string taskid = context.Request.Params["taskid"];
                try
                {
                    string fpstate = context.Request.Params["fpstate"];
                    string hcstate = context.Request.Params["hcstate"];
                    if (fpstate.Trim() != "")
                    {
                        sql += " and F_FPState=" + fpstate + " ";
                    }
                    if (hcstate.Trim() != "")
                    {
                        sql += " and F_HCState=" + hcstate + " ";
                    }
                }
                catch
                { 
                }

                if (taskid.Trim() != "")
                {
                    sql += " and F_TaskId=" + taskid + " ";
                }
                else
                {
                    sql += " and 1=0 ";
                }
                if (key.Trim() != "")
                {
                    sql += " and (F_Phone like '%" + key.Trim() + "%' or F_Customer like '%" + key.Trim() + "%' ) ";
                }

                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_TaskTelNum> pageModel = new Model.PageData<Model.T_Call_TaskTelNum>();

                dt = BLL.PagerBLL.GetListPager(
                    "vw_Call_TaskTelNum",
                    "F_Id",
                    "*",
                    " " + sql,
                    "ORDER BY F_Id desc",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);

                System.Collections.Generic.List<Model.T_Call_TaskTelNum> modelList = new BLL.T_Call_TaskTelNum().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_TaskTelNum>));

                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 LoadWFPList(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = " and F_FPState=0 ";

            try
            {
                string strpageindex = context.Request.Params["page"];
                int pageindex = 1;
                string strpagesize = context.Request.Params["pagesize"];
                int pagesize = 10;
                string key = context.Request.Params["key"];
                string taskid = context.Request.Params["taskid"];

                if (taskid.Trim() != "")
                {
                    sql += " and F_TaskId=" + taskid + " ";
                }
                else
                {
                    sql += " and 1=0 ";
                }

                try
                {
                    string field = context.Request.Params["field"];
                    if (field.Trim() != "")
                    {
                        if (key.Trim() != "")
                        {
                            sql += " and " + field + " like '%" + key.Trim() + "%' ";
                        }
                    }
                    else
                    {
                        if (key.Trim() != "")
                        {
                            sql += " and (F_Phone like '%" + key.Trim() + "%' or F_Customer like '%" + key.Trim() + "%' ) ";
                        }
                    }
                }
                catch
                { 
                }

                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_TaskTelNum> pageModel = new Model.PageData<Model.T_Call_TaskTelNum>();

                dt = BLL.PagerBLL.GetListPager(
                    "vw_Call_TaskTelNum",
                    "F_Id",
                    "*",
                    " " + sql,
                    "ORDER BY F_Id desc",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);

                System.Collections.Generic.List<Model.T_Call_TaskTelNum> modelList = new BLL.T_Call_TaskTelNum().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_TaskTelNum>));

                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;
            }
        }
    }
}