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

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

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = CommonRequest.GetQueryString("action");
            string tel = CommonRequest.GetQueryString("tel");
            int uid = 0;
            if (!string.IsNullOrEmpty(CommonRequest.GetQueryString("uid")))
            {
                uid = Convert.ToInt32(CommonRequest.GetQueryString("uid"));
            }

            switch (action)
            {
                case "getLoadlist":
                    context.Response.Write(getLoadList(context, tel, uid));
                    break;
                case "getLoadAlrlist":
                    context.Response.Write(getLoadAlrlist(context, tel, uid));
                    break;
                case "getLoadRecvlist":
                    context.Response.Write(getLoadRecvlist(context, tel, uid));
                    break;
                case "getLoadPubliclist":
                    context.Response.Write(getLoadPubliclist(context, tel));
                    break;
                case "deleteSendTask":
                    context.Response.Write(deleteSendTask(context));
                    break;
                case "deleteAlready":
                    context.Response.Write(deleteAlready(context));
                    break;
                case "deleteReceive":
                    context.Response.Write(deleteReceive(context));
                    break;
                case "againSend":
                    context.Response.Write(againSend(context));
                    break;
            }
        }

        #region 删除数据
        /// <summary>
        /// 删除待发传真
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private string deleteSendTask(HttpContext context)
        {
            bool res = false;
            string arr = CommonRequest.GetQueryString("arrid");
            if (arr.IndexOf(",") == -1)
            {
                res = new BLL.T_Fax_SendFaxTask().Delete(Convert.ToInt32(arr));
            }
            else
            {
                res = new BLL.T_Fax_SendFaxTask().DeleteList(arr);
            }

            return res ? "success" : "field";
        }
        /// <summary>
        /// 删除已发传真
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private string deleteAlready(HttpContext context)
        {
            bool res = false;
            string arr = CommonRequest.GetQueryString("arrid");
            if (arr.IndexOf(",") == -1)
            {
                res = new BLL.T_Fax_SentFax().Delete(Convert.ToInt32(arr));
            }
            else
            {
                res = new BLL.T_Fax_SentFax().DeleteList(arr);
            }

            return res ? "success" : "field";
        }
        /// <summary>
        /// 删除接收传真
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private string deleteReceive(HttpContext context)
        {
            bool res = false;
            string arr = CommonRequest.GetQueryString("arrid");
            if (arr.IndexOf(",") == -1)
            {
                res = new BLL.T_Fax_RecvFax().Delete(Convert.ToInt32(arr));
            }
            else
            {
                res = new BLL.T_Fax_RecvFax().DeleteList(arr);
            }

            return res ? "success" : "field";
        }

        #endregion
        /// <summary>
        /// 待发送传真信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="sql"></param>
        /// <returns></returns>
        public string getLoadList(HttpContext context, string sql, int uid)
        {

            string res = "";
            DataTable dt = new DataTable();
            try
            {
                if (sql != "")
                {
                    sql = " and TelNum like '%" + sql + "%'";
                }
                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
                    { }
                }
                sql += " and State=0 and F_UserId=" + uid;

                int recordCount = 0;

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

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

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

                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;

        }

        /// <summary>
        /// 已发送传真信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="sql"></param>
        /// <returns></returns>
        public string getLoadAlrlist(HttpContext context, string sql, int uid)
        {

            string res = "";
            DataTable dt = new DataTable();
            try
            {
                if (sql != "")
                {
                    sql = " and TelNum like '%" + sql + "%'";
                }
                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
                    { }
                }
                sql += " and F_UserId=" + uid;

                int recordCount = 0;

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

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

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

                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)
            {

            }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }
            return res;
        }

        /// <summary>
        /// 已接收传真信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="sql"></param>
        /// <returns></returns>
        public string getLoadRecvlist(HttpContext context, string sql, int uid)
        {

            string res = "";
            DataTable dt = new DataTable();
            try
            {
                if (sql != "")
                {
                    sql = " and TelNum like '%" + sql + "%'";
                }
                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
                    { }
                }
                sql += " and F_UserId=" + uid;

                int recordCount = 0;

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

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

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

                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)
            {

            }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }
            return res;
        }

        /// <summary>
        /// 公海传真信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="sql"></param>
        /// <returns></returns>
        public string getLoadPubliclist(HttpContext context, string sql)
        {

            string res = "";
            DataTable dt = new DataTable();
            try
            {
                if (sql != "")
                {
                    sql = " and TelNum like '%" + sql + "%'";
                }
                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
                    { }
                }

                sql += " and F_UserId is NULL";

                int recordCount = 0;

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

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

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

                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)
            {

            }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }
            return res;
        }

        /// <summary>
        /// 重新发送
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private string againSend(HttpContext context)
        {
            int res ;
            string arr = CommonRequest.GetQueryString("arrid");
            res = new BLL.T_Fax_SentFax().ReSend(arr);

            return res > 0 ? "success" : "field";
        }

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