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

namespace HySoft.BaseCallCenter.Web.workordermanage.ajax
{
    /// <summary>
    /// CommonDataList 的摘要说明
    /// </summary>
    public class CommonDataList : 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;
            }
        }
        #region 获取数据
        private string LoadList(HttpContext context)
        {
            string intenseid = CommonRequest.GetQueryString("intenseid");
            string res = "";
            DataTable dt = new DataTable();
            string sql = " ";

            if (intenseid.Trim() != "")
            {
                sql += " and F_INSTANCEID = '" + intenseid.Trim() + "' ";
            }
            try
            {

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

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

                dt = BLL.PagerBLL.GetListPager(
                    "V_WorkOrderHistory",
                    "F_HISTORYID",
                    "*",
                    sql,
                    "ORDER BY F_HISTORYID ",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);

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

                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;
        }
        private string LoadList(string intenseid)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = "";
            try
            {
                if (intenseid.Trim() != "")
                {
                    sql += " and F_INSTANCEID = '" + intenseid.Trim() + "' ";
                }
                Model.PageData<Model.T_Wo_WorkOrderHistory> pageModel = new Model.PageData<Model.T_Wo_WorkOrderHistory>();
                dt = (new  BLL.T_Wo_WorkOrderHistory()).GetListView(" 1=1 " + sql + " ORDER BY F_HISTORYID").Tables[0];
    //            int recordCount = 0;
    //            dt = BLL.PagerBLL.GetListPager(
    //"V_WorkOrderHistory",
    //"F_HISTORYID",
    //"*",
    //sql,
    //"ORDER BY F_HISTORYID ",
    //10,
    //1,
    //true,
    //out recordCount);
                System.Collections.Generic.List<Model.T_Wo_WorkOrderHistory> modelList = new BLL.T_Wo_WorkOrderHistory().DataTableToListDetail(dt);
                //pageModel.Rows = modelList;
                //pageModel.Total = dt.Rows.Count;
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(System.Collections.Generic.List<Model.T_Wo_WorkOrderHistory>));
                using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
                {                    //JSON序列化
                    serializer.WriteObject(stream, modelList);
                    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;
            }
        }
    }
}