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

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = CommonRequest.GetQueryString("action");
            switch (action)
            {
                case "getlist":
                    context.Response.Write(LoadList(context));
                    break;
                case "delete":
                    context.Response.Write(DeleteById(context));
                    break;
                case "clearphone":
                    context.Response.Write(ClearById(context));
                    break;
                case "start":
                    context.Response.Write(StartById(context));
                    break;
                case "stop":
                    context.Response.Write(StopById(context));
                    break;
            }
        }

        #region 获取数据
        private string LoadList(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = " ";

            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"];

                if (key.Trim() != "")
                {
                    sql += " and (TaskName 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.vw_CTI_TaskPager> pageModel = new Model.PageData<Model.vw_CTI_TaskPager>();

                dt = BLL.PagerBLL.GetListPager(
                    "vw_CTI_TaskPager",
                    "TaskID",
                    "*",
                    " " + sql,
                    "ORDER BY AddTime desc",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);

                System.Collections.Generic.List<Model.vw_CTI_TaskPager> modelList = new BLL.T_CTI_Task().DataTableToListPager(dt);
                pageModel.Rows = modelList;
                pageModel.Total = recordCount;
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(Model.PageData<Model.vw_CTI_TaskPager>));

                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 DeleteById(HttpContext context)
        {
            int res = 0;
            string str = "error";

            //DataTable dt = new DataTable();
            try
            {
                //dt = new BLL.T_Ask_Question().GetList(" F_CategoryId=" + context.Request.Params["id"] + " ").Tables[0];
                //res = dt.Rows.Count;
                //if (res > 0)
                //{
                //    str = res.ToString();
                //}
                //else
                //{
                    if (new BLL.T_CTI_Task().Delete(Convert.ToInt32(context.Request.Params["id"])))
                    {
                        str = "success";
                    }
                    else
                    {
                        str = "error";
                    }
                //}
            }
            catch (Exception ex)
            {
            }
            finally
            {
                //dt.Clear();
                //dt.Dispose();
            }
            return str;
        }

        #endregion

        #region 清空

        private string ClearById(HttpContext context)
        {
            string str = "error";
            try
            {
                int r=new BLL.T_Call_TaskTelNum().ClearPhone(Convert.ToInt32(context.Request.Params["id"]));
                if (r > 0)
                {
                    try
                    {
                        DBUtility.DbHelperSQL.ExecuteSql("update T_CTI_Task set y_HMCount=(y_HMCount-" + r + ") where TaskID=" + context.Request.Params["id"]);
                    }
                    catch { }
                    str = "success";
                }
                else
                {
                    str = "error";
                }
                str = r.ToString();
            }
            catch (Exception ex)
            {
            }
            finally
            {
            }
            return str;
        }

        #endregion

        #region 启动

        private string StartById(HttpContext context)
        {
            string str = "error";
            try
            {
                if (new BLL.T_CTI_Task().UpdateState(Convert.ToInt32(context.Request.Params["id"]), 1))
                {
                    str = "success";
                }
                else
                {
                    str = "error";
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
            }
            return str;
        }

        #endregion

        #region 停止

        private string StopById(HttpContext context)
        {
            string str = "error";
            try
            {
                if (new BLL.T_CTI_Task().UpdateState(Convert.ToInt32(context.Request.Params["id"]), 0))
                {
                    str = "success";
                }
                else
                {
                    str = "error";
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
            }
            return str;
        }

        #endregion

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