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

namespace HySoft.BaseCallCenter.Web.askmanage.ajax
{
    /// <summary>
    /// question 的摘要说明
    /// </summary>
    public class question : 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 "getlistbypager":
                    context.Response.Write(LoadListByPager(context));
                    break;
                case "getselectlist":
                    context.Response.Write(LoadSelectList(context));
                    break;
                case "delete":
                    context.Response.Write(DeleteById(context));
                    break;
            }
        }

        #region 删除试题

        private string DeleteById(HttpContext context)
        {
            string str = "error";
            try
            {
                if (new BLL.T_Ask_Question().Delete(Convert.ToInt32(context.Request.Params["id"])))
                {
                    if (new BLL.T_Ask_QuestionItems().DeleteByQuestionId(Convert.ToInt32(context.Request.Params["id"])))
                    {
                        str = "success";
                    }
                    else
                    {
                        str = "error";
                    }
                    str = "success";
                }
                else
                {
                    str = "error";
                }
            }
            catch (Exception ex)
            {
            }
            return str;
        }

        #endregion

        #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 categoryid = context.Request.Params["categoryid"];
                string selecttype = context.Request.Params["selecttype"];
                string key = context.Request.Params["key"];
                if (categoryid.Trim() != "")
                {
                    sql += " and F_CategoryId=" + categoryid + " ";
                }
                if (selecttype.Trim() != "")
                {
                    sql += " and F_Type=" + selecttype + " ";
                }

                if (key.Trim() != "")
                {
                    sql += " and (F_Title like '%" + key.Trim() + "%' or F_Content 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_Ask_Question> pageModel = new Model.PageData<Model.T_Ask_Question>();

                dt = BLL.PagerBLL.GetListPager(
                    "vw_Ask_Question",
                    "F_QuestionId",
                    "*",
                    " and F_DeleteFlag=0 " + sql,
                    "ORDER BY F_QuestionId desc",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);

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

                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 LoadListByPager(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = " ";

            try
            {
                string pagerid = context.Request.Params["pagerid"];
                string userid = context.Request.Params["userid"];
                string strpageindex = context.Request.Params["page"];
                int pageindex = 1;
                string strpagesize = context.Request.Params["pagesize"];
                int pagesize = 10;

                string categoryid = context.Request.Params["categoryid"];
                string selecttype = context.Request.Params["selecttype"];
                string key = context.Request.Params["key"];
                if (categoryid.Trim() != "")
                {
                    sql += " and F_CategoryId=" + categoryid + " ";
                }
                if (selecttype.Trim() != "")
                {
                    sql += " and F_Type=" + selecttype + " ";
                }

                if (key.Trim() != "")
                {
                    sql += " and (F_Title like '%" + key.Trim() + "%' or F_Content like '%" + key.Trim() + "%') ";
                }

                if (pagerid.Trim() != "")
                {
                    sql += " and F_QuestionId not in (SELECT [F_QuestionId] FROM [T_Ask_PagerItems] where [F_PagerId]=" + pagerid + ") ";
                }
                else
                {
                    sql += " and F_QuestionId not in (SELECT [ExpandIntField1] FROM [T_Sys_TempItems] where [F_UserId]=" + userid + " and F_TempName='T_Ask_PagerItems') ";
                }

                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_Ask_Question> pageModel = new Model.PageData<Model.T_Ask_Question>();

                dt = BLL.PagerBLL.GetListPager(
                    "vw_Ask_Question",
                    "F_QuestionId",
                    "*",
                    " and F_DeleteFlag=0 " + sql,
                    "ORDER BY F_QuestionId desc",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);

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

                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 LoadSelectList(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = " ";

            try
            {
                string pagerid = context.Request.Params["pagerid"];
                string userid = context.Request.Params["userid"];
                string strpageindex = context.Request.Params["page"];
                int pageindex = 1;
                string strpagesize = context.Request.Params["pagesize"];
                int pagesize = 10;
                //string arrid = context.Request.Params["arrid"];
                //string categoryid = context.Request.Params["categoryid"];
                //string selecttype = context.Request.Params["selecttype"];
                //string key = context.Request.Params["key"];
                //if (categoryid.Trim() != "")
                //{
                //    sql += " and F_CategoryId=" + categoryid + " ";
                //}
                //if (selecttype.Trim() != "")
                //{
                //    sql += " and F_Type=" + selecttype + " ";
                //}

                //if (key.Trim() != "")
                //{
                //    sql += " and (F_Title like '%" + key.Trim() + "%' or F_Content like '%" + key.Trim() + "%') ";
                //}
                //if (arrid.Trim() != "")
                //{
                //    sql += " and F_QuestionId in(" + arrid + ") ";
                //}
                //else
                //{
                //    sql += " and 1=0 ";
                //}


                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_Ask_PagerItemsQuestion> pageModel = new Model.PageData<Model.T_Ask_PagerItemsQuestion>();

                if (pagerid.Trim() != "")
                {
                    sql += " and [F_PagerId]=" + pagerid + " ";

                    dt = BLL.PagerBLL.GetListPager(
                    "vw_Ask_PagerQuestion",
                    "F_ItemId",
                    "*",
                    " and F_DeleteFlag=0 " + sql,
                    "ORDER BY F_Sort ",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);
                }
                else
                {
                    sql += " and F_QuestionId in (SELECT [ExpandIntField1] FROM [T_Sys_TempItems] where [F_UserId]=" + userid + " and F_TempName='T_Ask_PagerItems') ";

                    dt = BLL.PagerBLL.GetListPager(
                    "vw_Ask_Question LEFT OUTER JOIN T_Sys_TempItems ON vw_Ask_Question.F_QuestionId = T_Sys_TempItems.ExpandIntField1",
                    "F_QuestionId",
                    "vw_Ask_Question.*,T_Sys_TempItems.ExpandIntField3 as F_Sort,T_Sys_TempItems.F_Id as F_ItemId",
                    " and F_DeleteFlag=0 " + sql,
                    "ORDER BY T_Sys_TempItems.ExpandIntField3 ",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);
                }
                               

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

                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

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