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

namespace HySoft.BaseCallCenter.Web.askmanage.ajax
{
    /// <summary>
    /// pageritems 的摘要说明
    /// </summary>
    public class pageritems : 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 "add":
                    context.Response.Write(Add(context));
                    break;
                case "addall":
                    context.Response.Write(AddAll(context));
                    break;
                case "modify":
                    context.Response.Write(Modify(context));
                    break;
                case "move":
                    context.Response.Write(Move(context));
                    break;
                case "delete":
                    context.Response.Write(Delete(context));
                    break;
                case "deleteall":
                    context.Response.Write(DeleteAll(context));
                    break;
                case "gettemplist":
                    context.Response.Write(LoadTempList(context));
                    break;
                case "addtemp":
                    context.Response.Write(AddTemp(context));
                    break;
                case "addalltemp":
                    context.Response.Write(AddAllTemp(context));
                    break;
                case "modifytemp":
                    context.Response.Write(ModifyTemp(context));
                    break;
                case "movetemp":
                    context.Response.Write(MoveTemp(context));
                    break;
                case "deletetemp":
                    context.Response.Write(DeleteTemp(context));
                    break;
                case "deletealltemp":
                    context.Response.Write(DeleteAllTemp(context));
                    break;
            }
        }

        #region 选项数据操作

        #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 questionid = "0";
                try
                {
                    questionid = context.Request.Params["questionid"];
                    if (questionid.Trim() != "")
                    {
                        sql = " and F_QuestionId=" + questionid + " ";
                    }
                }
                catch
                { }
                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_QuestionItems> pageModel = new Model.PageData<Model.T_Ask_QuestionItems>();

                dt = BLL.PagerBLL.GetListPager(
                    "T_Ask_QuestionItems",
                    "F_ItemId",
                    "*",
                    "  " + sql,
                    "ORDER BY F_Sort ",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);

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

                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 Add(HttpContext context)
        {
            string res = "error";
            try
            {
                Model.T_Ask_PagerItems itemmodel = new Model.T_Ask_PagerItems();

                int pagerid = 0;
                try
                {
                    pagerid = Convert.ToInt32(context.Request.Params["id"]);
                }
                catch
                { }
                itemmodel.F_PagerId = pagerid;
                int questionid = 0;
                try
                {
                    questionid = Convert.ToInt32(context.Request.Params["questionid"]);
                }
                catch
                { }
                itemmodel.F_QuestionId = questionid;
                string sort = "0";
                try
                {
                    sort = context.Request.Params["sort"];
                    if (sort.Trim() != "")
                    {
                        itemmodel.F_Sort = Convert.ToInt32(sort) + 1;
                    }
                }
                catch
                { }
                int itemid = new BLL.T_Ask_PagerItems().Add(itemmodel);
                if (itemid > 0)
                {
                    res = "success";
                }
            }
            catch
            { }
            return res;
        }

        #endregion

        #region 批量添加数据

        private string AddAll(HttpContext context)
        {
            string res = "error";
            try
            {
                string arr = context.Request.Params["arrid"];
                string[] arrid = arr.Split(',');
                string sort = "0";
                int intsort = 0;
                try
                {
                    sort = context.Request.Params["sort"];
                    intsort = Convert.ToInt32(sort) + 1;
                }
                catch
                { }
                foreach (string squestionid in arrid)
                {
                    Model.T_Ask_PagerItems model = new Model.T_Ask_PagerItems();

                    int pagerid = 0;
                    try
                    {
                        pagerid = Convert.ToInt32(context.Request.Params["id"]);
                    }
                    catch
                    { }
                    model.F_PagerId = pagerid;

                    int questionid = 0;
                    try
                    {
                        questionid = Convert.ToInt32(squestionid);
                    }
                    catch
                    { }
                    model.F_QuestionId = questionid;

                    try
                    {
                        if (sort.Trim() != "")
                        {
                            model.F_Sort = intsort;
                            intsort++;
                        }
                    }
                    catch
                    { }

                    int i = new BLL.T_Ask_PagerItems().Add(model);
                    if (i > 0)
                    {
                        res = "success";
                    }
                }
            }
            catch
            { }
            return res;
        }

        #endregion

        #region 修改数据

        private string Modify(HttpContext context)
        {
            string res = "error";
            try
            {
                Model.T_Ask_PagerItems model = new BLL.T_Ask_PagerItems().GetModel(Convert.ToInt32(context.Request.Params["itemid"]));
                //model.F_ItemName = context.Request.Params["itemvalue"];
                if (new BLL.T_Ask_PagerItems().Update(model))
                {
                    res = "success";
                }
            }
            catch
            { }
            return res;
        }

        #endregion

        #region 移动排序

        private string Move(HttpContext context)
        {
            string res = "error";
            DataTable dt = new DataTable();
            try
            {
                //"ajax/pageritems.ashx?action=" + optaction + "&pagerid=" + id + "&itemid=" + itemid + "&sort=" + ssort + "&movetype=" + type + "&timeno=" + timeno
                string sql = "";
                string order = "";
                int sort = Convert.ToInt32(context.Request.Params["sort"]);
                int itemid = Convert.ToInt32(context.Request.Params["itemid"]);
                int pagerid = Convert.ToInt32(context.Request.Params["pagerid"]);
                sql += " F_PagerId=" + pagerid + " ";
                int newsort = 0;
                int newitemid = 0;
                if (context.Request.Params["movetype"] == "-1")
                {
                    sql += " and F_Sort<" + sort.ToString();
                    order = " order by F_Sort desc";
                }
                else
                {
                    sql += " and F_Sort>" + sort.ToString();
                    order = " order by F_Sort ";
                }
                dt = new BLL.T_Ask_PagerItems().GetList(sql + " " + order).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    newitemid = Convert.ToInt32(dt.Rows[0]["F_ItemId"].ToString());
                    newsort = Convert.ToInt32(dt.Rows[0]["F_Sort"].ToString());
                }
                Model.T_Ask_PagerItems newmodel = new BLL.T_Ask_PagerItems().GetModel(newitemid);
                newmodel.F_Sort = sort;
                if (new BLL.T_Ask_PagerItems().Update(newmodel))
                {
                    res = "success";
                }
                Model.T_Ask_PagerItems model = new BLL.T_Ask_PagerItems().GetModel(itemid);
                model.F_Sort = newsort;
                if (new BLL.T_Ask_PagerItems().Update(model))
                {
                    res = "success";
                }
            }
            catch
            { }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }
            return res;
        }

        #endregion

        #region 删除数据

        private string Delete(HttpContext context)
        {
            string res = "error";
            try
            {
                if (new BLL.T_Ask_PagerItems().Delete(Convert.ToInt32(context.Request.Params["itemid"])))
                {
                    res = "success";
                }
            }
            catch
            { }
            return res;
        }

        #endregion

        #region 批量删除数据

        private string DeleteAll(HttpContext context)
        {
            string res = "error";
            try
            {
                string arr = context.Request.Params["arrid"];
                string[] arrid = arr.Split(',');
                foreach (string itemid in arrid)
                {
                    if (new BLL.T_Ask_PagerItems().Delete(Convert.ToInt32(itemid)))
                    {
                        res = "success";
                    }
                }
            }
            catch
            { }
            return res;
        }

        #endregion

        #endregion

        #region 选项临时数据操作

        #region 获取临时数据
        private string LoadTempList(HttpContext context)
        {
            string res = "";
            
            return res;
        }
        #endregion

        #region 添加临时数据

        private string AddTemp(HttpContext context)
        {
            string res = "error";
            try
            {
                Model.T_Sys_TempItems model = new Model.T_Sys_TempItems();
                model.F_TempName = "T_Ask_PagerItems";
                string userid = "0";
                try
                {
                    userid = context.Request.Params["userid"];
                    if (userid.Trim() != "")
                    {
                        model.F_UserId = Convert.ToInt32(userid);
                    }
                }
                catch
                { }
                string questionid = "0";
                try
                {
                    questionid = context.Request.Params["questionid"];
                    if (questionid.Trim() != "")
                    {
                        model.ExpandIntField1 = Convert.ToInt32(questionid);
                    }
                }
                catch
                { }
                string sort = "0";
                try
                {
                    sort = context.Request.Params["sort"];
                    if (sort.Trim() != "")
                    {
                        model.ExpandIntField3 = Convert.ToInt32(sort) + 1;
                    }
                }
                catch
                { }

                int i = new BLL.T_Sys_TempItems().Add(model);
                if (i > 0)
                {
                    res = "success";
                }
            }
            catch
            { }
            return res;
        }

        #endregion

        #region 添加临时数据

        private string AddAllTemp(HttpContext context)
        {
            string res = "error";
            try
            {
                string arr = context.Request.Params["arrid"];
                string[] arrid = arr.Split(',');
                string sort = "0";
                int intsort = 0;
                try
                {
                    sort = context.Request.Params["sort"];
                    intsort = Convert.ToInt32(sort) + 1;
                }
                catch
                { }
                foreach (string questionid in arrid)
                {
                    Model.T_Sys_TempItems model = new Model.T_Sys_TempItems();
                    model.F_TempName = "T_Ask_PagerItems";
                    string userid = "0";
                    try
                    {
                        userid = context.Request.Params["userid"];
                        if (userid.Trim() != "")
                        {
                            model.F_UserId = Convert.ToInt32(userid);
                        }
                    }
                    catch
                    { }
                    try
                    {
                        model.ExpandIntField1 = Convert.ToInt32(questionid);
                    }
                    catch
                    { }
                    try
                    {
                        if (sort.Trim() != "")
                        {
                            model.ExpandIntField3 = intsort;
                            intsort++;
                        }
                    }
                    catch
                    { }

                    int i = new BLL.T_Sys_TempItems().Add(model);
                    if (i > 0)
                    {
                        res = "success";
                    }
                }
            }
            catch
            { }
            return res;
        }

        #endregion

        #region 修改临时数据

        private string ModifyTemp(HttpContext context)
        {
            string res = "error";
            try
            {
                Model.T_Sys_TempItems model = new BLL.T_Sys_TempItems().GetModel(Convert.ToInt32(context.Request.Params["itemid"]));
                model.ExpandVchField1 = context.Request.Params["itemvalue"];
                if (new BLL.T_Sys_TempItems().Update(model))
                {
                    res = "success";
                }
            }
            catch
            { }
            return res;
        }

        #endregion

        #region 移动排序

        private string MoveTemp(HttpContext context)
        {
            string res = "error";
            DataTable dt = new DataTable();
            try
            {
                //ajax/questionitems.ashx?action=" + optaction + "&questionid=" + id + "&itemid=" + itemid+"&sort="+sort+"&movetype="+type
                string sql = "";
                string order = "";
                int sort = Convert.ToInt32(context.Request.Params["sort"]);
                int itemid = Convert.ToInt32(context.Request.Params["itemid"]);
                int newsort = 0;
                int newitemid = 0;
                if (context.Request.Params["movetype"] == "-1")
                {
                    sql = " ExpandIntField3<" + sort.ToString();
                    order = " order by ExpandIntField3 desc";
                }
                else
                {
                    sql = " ExpandIntField3>" + sort.ToString();
                    order = " order by ExpandIntField3 ";
                }
                dt = new BLL.T_Sys_TempItems().GetList(sql + " " + order).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    newitemid = Convert.ToInt32(dt.Rows[0]["F_Id"].ToString());
                    newsort = Convert.ToInt32(dt.Rows[0]["ExpandIntField3"].ToString());
                }
                Model.T_Sys_TempItems newmodel = new BLL.T_Sys_TempItems().GetModel(newitemid);
                newmodel.ExpandIntField3 = sort;
                if (new BLL.T_Sys_TempItems().Update(newmodel))
                {
                    res = "success";
                }
                Model.T_Sys_TempItems model = new BLL.T_Sys_TempItems().GetModel(itemid);
                model.ExpandIntField3 = newsort;
                if (new BLL.T_Sys_TempItems().Update(model))
                {
                    res = "success";
                }
            }
            catch
            { }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }
            return res;
        }

        #endregion

        #region 删除临时数据

        private string DeleteTemp(HttpContext context)
        {
            string res = "error";
            try
            {
                if (new BLL.T_Sys_TempItems().Delete(Convert.ToInt32(context.Request.Params["itemid"])))
                {
                    res = "success";
                }
            }
            catch
            { }
            return res;
        }

        #endregion

        #region 删除临时数据

        private string DeleteAllTemp(HttpContext context)
        {
            string res = "error";
            try
            {
                string arr = context.Request.Params["arrid"];
                string[] arrid = arr.Split(',');
                foreach (string itemid in arrid)
                {
                    if (new BLL.T_Sys_TempItems().Delete(Convert.ToInt32(itemid)))
                    {
                        res = "success";
                    }
                }
            }
            catch
            { }
            return res;
        }

        #endregion

        #endregion

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