
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>
    /// customer 的摘要说明
    /// </summary>
    public class customer : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //取得处事类型
            string action = CommonRequest.GetQueryString("action");
            switch (action)
            {
                case "getcustomerlist":
                    context.Response.Write(LoadCustomerList(context));
                    break;
                case "delete":
                    context.Response.Write(DeleteCustomer(context));
                    break;
                case "fastadd":
                    context.Response.Write(FastAddCustomer(context));
                    break;
            }
        }
        #region 加载客户列表
        private string LoadCustomerList(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            try
            {
                string sql = "";
                string key = context.Request.Params["key"];
                string customernature = context.Request.Params["customernature"];
                string customerclass = context.Request.Params["customerclass"];
                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_DeleteFlag=0 ";
                if (key != null)
                {
                    sql += " and F_CustomerName like '%" + key + "%' ";
                }
                if (customernature != null)
                {
                    sql += " and F_CustomerNature like '%" + customernature + "%' ";
                }
                if (customerclass != null)
                {
                    sql += " and F_CustomerClass like '%" + customerclass + "%' ";
                }
                int recordCount = 0;

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

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

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

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


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

        #endregion
        #region 快速添加客户
        private bool FastAddCustomer(HttpContext context)
        {
            if (CommonRequest.GetQueryString("cn") != null && CommonRequest.GetQueryString("cn") != "")
            {
                string cname = CommonRequest.GetQueryString("cn");
                int b = new BLL.T_Cus_CustomerBase().Add(new Model.T_Cus_CustomerBase()
                {
                    F_CustomerName = cname
                });
                if (b > 0) return true;
                else return false;
            }
            else return false;
        }
        #endregion
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}