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

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

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

        #region 获取数据
        private string getentitylist(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            try
            {
                string strpageindex = context.Request.Params["page"];
                int pageindex = 1;
                string strpagesize = context.Request.Params["pagesize"];
                int pagesize = 10;
                string sql = "";
                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_Form_EntityInfo> pageModel = new Model.PageData<Model.T_Form_EntityInfo>();
                dt = BLL.PagerBLL.GetListPager(
                    "T_Form_EntityInfo",
                    "F_EntityId",
                    "*",
                    sql,
                    "ORDER BY F_EntityId desc",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);
                System.Collections.Generic.List<Model.T_Form_EntityInfo> modelList = new BLL.T_Form_EntityInfo().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_Form_EntityInfo>));
                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 getentityattributelist(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            try
            {
                string entityid = context.Request.Params["id"];
                string strpageindex = context.Request.Params["page"];
                int pageindex = 1;
                string strpagesize = context.Request.Params["pagesize"];
                int pagesize = 10;
                string sql = " and F_EntityId="+entityid.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_Form_EntityAttribute> pageModel = new Model.PageData<Model.T_Form_EntityAttribute>();
                dt = BLL.PagerBLL.GetListPager(
                    "T_Form_EntityAttribute",
                    "F_AttributeId",
                    "*",
                    sql,
                    "ORDER BY F_AttributeId desc",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);
                System.Collections.Generic.List<Model.T_Form_EntityAttribute> modelList = new BLL.T_Form_EntityAttribute().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_Form_EntityAttribute>));
                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;
            }
        }
    }
}