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

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

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

        #region 根据来电号码获取客户信息

        private string getCustomerInfoByPhone(HttpContext context)
        {
            string res = "";
            DataTable dt=new DataTable();
            DataTable dtc = new DataTable();
            try
            {
                string phone = CommonRequest.GetQueryString("tel").Trim();
                if (phone != "")
                { 
                    //先查联系人表存在联系人记录
                    dt = new BLL.T_Cus_ContactPerson().GetList(" F_Telephone like '%" + phone + "%' or F_Mobile like '%" + phone + "%' ").Tables[0];
                    if (dt.Rows.Count > 0)
                    {
                        res = getCustomerInfoById(dt.Rows[0]["F_CustomerId"].ToString().Trim());
                    }
                    else
                    {
                        dtc = new BLL.T_Cus_CustomerBase().GetList(" F_Telephone like '%" + phone + "%' or F_Mobile like '%" + phone + "%' or F_ChargeTelephone like '%" + phone + "%' ").Tables[0];

                        if (dtc.Rows.Count > 0)
                        {
                            res = getCustomerInfoById(dtc.Rows[0]["F_CustomerId"].ToString().Trim());
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                res = "";
            }
            finally
            {
                dt.Clear();
                dt.Dispose();
                dtc.Clear();
                dtc.Dispose();
            }
            return res;
        }

        private string getCustomerInfoById(string id)
        {
            string res = "";
            Model.T_Cus_CustomerBase model = new BLL.T_Cus_CustomerBase().GetModel(Convert.ToInt32(id));
            if (model != null)
            {
                //F_CustomerName~F_CustomerCode!
                //res = model.F_CustomerName.Trim() + "~" + model.F_CustomerCode.Trim() + "~" + model.F_CustomerName.Trim() + "~";
                //res += model.F_CustomerName.Trim() + "~" + model.F_CustomerName.Trim() + "~" + model.F_CustomerName.Trim() + "~";
                //res += model.F_CustomerName.Trim() + "~" + model.F_CustomerName.Trim() + "~" + model.F_CustomerName.Trim() + "";
            }
            return res;
        }

        #endregion

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