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

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

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //取得处事类型
            string action = CommonRequest.GetQueryString("action");
            switch (action)
            {
                case "getallpersonlist":
                    context.Response.Write(LoadAllPersonList(context));
                    break;
                case "getpersonlist":
                    context.Response.Write(LoadPersonList(context));
                    break;
                case "getcustomerinfo":
                    context.Response.Write(LoadCustomerInfo(context));
                    break;
                case "delete":
                    context.Response.Write(DeletePerson(context));
                    break;
            }
        }
        private string LoadCustomerInfo(HttpContext context)
        {
            string res = "";
            if (context.Request.QueryString["type"] != null && context.Request.QueryString["id"] != null)
            {
                int id = Convert.ToInt32(context.Request.QueryString["id"]);
                string type = context.Request.QueryString["type"];
                if (type == "code")
                {
                    res = new BLL.T_Cus_CustomerBase().GetModel(id).F_CustomerCode;
                }
                if (type == "name")
                {
                    res = new BLL.T_Cus_CustomerBase().GetModel(id).F_CustomerName;
                }
            }
            return res;
        }
        #region 加载联系人列表
        private string LoadPersonList(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            try
            {
                string sql = "";
                string strpageindex = context.Request.Params["page"];
                int pageindex = 1;
                string strpagesize = context.Request.Params["pagesize"];
                int pagesize = 10;
                if (strpageindex.Trim() != "")
                {
                    try
                    {
                        pageindex = Convert.ToInt32(strpageindex);
                    }
                    catch
                    { }
                }

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

                sql = " and F_DeletionStateCode=0 and (F_CustomerId IS NULL or F_CustomerId<=0)";
                string customerId = context.Request.Params["cId"];
                if (customerId.Trim() != "")
                    sql = " and F_CustomerId=" + customerId;
                //else return "";
                int recordCount = 0;

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

                dt = BLL.PagerBLL.GetListPager(
                    "T_Cus_ContactPerson",
                    "F_ManId",
                    "*",
                    sql,
                    "ORDER BY F_ManId DESC",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);

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

                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
        private string LoadAllPersonList(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            try
            {
                string keywords = CommonRequest.GetQueryString("keywords");
                string sql = "";
                string strpageindex = context.Request.Params["page"];
                int pageindex = 1;
                string strpagesize = context.Request.Params["pagesize"];
                int pagesize = 10;
                if (strpageindex.Trim() != "")
                {
                    try
                    {
                        pageindex = Convert.ToInt32(strpageindex);
                    }
                    catch
                    { }
                }

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

                sql += " and F_DeletionStateCode=0";//自己没有被删除的
                sql += " and F_CustomerId in (select F_CustomerId from [dbo].[T_Cus_CustomerBase] WHERE F_DeleteFlag=0)";//自己所属的客户也没有被删除
                if (keywords != "")
                {
                    string s = string.Format(" and (F_Name like '%{0}%' or F_Sex like '%{1}%' or F_Duty like '%{2}%' or F_Mobile like '%{3}%' or F_Telephone like '%{4}%' or F_Email like '%{5}%' or F_QQ like '%{6}%' or F_Fax like '%{7}%' or F_Remark like '%{8}%')",
                        keywords, keywords, keywords, keywords, keywords, keywords, keywords, keywords, keywords);
                    sql += s;
                }
                int recordCount = 0;

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

                dt = BLL.PagerBLL.GetListPager(
                    "T_Cus_ContactPerson",
                    "F_ManId",
                    "*",
                    sql,
                    "ORDER BY F_ManId DESC",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);

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

                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 DeletePerson(HttpContext context)
        {
            bool res = false;
            string arr = CommonRequest.GetQueryString("arrid");
            if (arr.IndexOf(",") == -1)
            {
                res = new BLL.T_Cus_ContactPerson().Delete(Convert.ToInt32(arr));
            }
            else
            {
                res = new BLL.T_Cus_ContactPerson().DeleteList(arr);
            }


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

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