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

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

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = CommonRequest.GetQueryString("action");
            switch (action)
            {
                case "resulstdata":
                    context.Response.Write(LoadReport(context));
                    break;
            }
        }

        #region 获取数据分析

        /// <summary>
        /// 获取数据分析
        /// </summary>
        /// <returns></returns>
        protected string LoadReport(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = "  ";
            
            DataTable dtpage = new DataTable();
            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"];
                    if (fpstate.Trim() != "")
                    {
                        sql += " and F_FPState=" + fpstate + " ";
                    }
                    if (hcstate.Trim() != "")
                    {
                        sql += " and F_HCState=" + hcstate + " ";
                    }
                    if (yjstate.Trim() != "")
                    {
                        sql += " and F_YJState=" + yjstate + " ";
                    }
                }
                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 + " ";
                    string pagerid = "";
                    Model.T_CTI_Task model = new BLL.T_CTI_Task().GetModel(Convert.ToInt32(taskid));
                    if (model != null)
                    {
                        if (model.y_TkModelId != 0)
                        {
                            pagerid = model.y_TkModelId.ToString();
                        }
                    }
                    string itemtable = "T_Ask_PagerItems";
                    string questionidname = "F_QuestionId";
                    string sortname = "F_Sort";
                    string itemidname = "F_ItemId";
                    string pkey = "T_Ask_PagerItems.F_ItemId";
                    string psql = "";
                    if (pagerid.Trim() != "")
                    {
                        itemtable = "T_Ask_PagerItems";
                        questionidname = "F_QuestionId";
                        sortname = "F_Sort";
                        itemidname = "F_ItemId";
                        pkey = "T_Ask_PagerItems.F_ItemId";
                        psql += " and [T_Ask_PagerItems].[F_PagerId]=" + pagerid + " ";
                    }
                    int precordCount = 0;
                    dtpage = BLL.PagerBLL.GetListPager(
                        "vw_Ask_Question LEFT OUTER JOIN " + itemtable + " ON vw_Ask_Question.F_QuestionId = " + itemtable + "." + questionidname + " ",
                        pkey,
                        "vw_Ask_Question.*," + itemtable + "." + sortname + " as F_Sort," + itemtable + "." + itemidname + " as F_ItemId",
                        " and F_DeleteFlag=0 " + psql,
                        "ORDER BY " + itemtable + "." + sortname + " ",
                        100,
                        1,
                        true,
                        out precordCount);

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

                res += "";
            }
            catch (Exception err)
            {
                //res = err.ToString(); 
            }
            finally
            {
                dtpage.Clear();
                dtpage.Dispose();
                dt.Clear();
                dt.Dispose();
            }
            res += "</table><br />";
            return res;
        }
        #endregion


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