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

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

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = CommonRequest.GetQueryString("action");
            switch (action)
            {
                case "getF_Namelist":
                    context.Response.Write(getF_Namelist(context));
                    break;
                case "getF_TypeNamelist":
                    context.Response.Write(getF_TypeNamelist(context));
                    break;
                case "gettreelist":
                    context.Response.Write(gettreelist(context));
                    break;
                case "getSERVICEMETHODtree":
                    context.Response.Write(getSERVICEMETHODtree(context));
                    break;
                case "getSERVICETYPElist":
                    context.Response.Write(getSERVICETYPElist(context));
                    break;
                case "getVISITOPINIONlist":
                    context.Response.Write(getVISITOPINIONlist(context));
                    break;
                case "getCUSTOMEROPINIONlist":
                    context.Response.Write(getCUSTOMEROPINIONlist(context));
                    break;
                case "getSOLVElist":
                    context.Response.Write(getSOLVElist(context));
                    break;
                case "getSTANDARDADDRESSlist":
                    context.Response.Write(getSTANDARDADDRESSlist(context));
                    break;
                case "getORDERTYPElist":
                    context.Response.Write(getORDERTYPElist(context));
                    break;
                case "getF_RoleName":
                    context.Response.Write(getF_RoleNamelist(context));
                    break;
            }
        }
        #region 获取数据
        private string getF_Namelist(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = " ";

            try
            {

                dt = new BLL.T_Wo_WorkOrderState().GetList("  ").Tables[0];

                System.Collections.Generic.List<Model.TreeModel> modelList = BindZXZTree(dt, "0");
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List<Model.TreeModel>));

                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;
        }

        private string getSERVICEMETHODtree(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = " ";

            try
            {

                dt = new BLL.T_Sys_DictionaryValue().GetList(" F_DictionaryFlag='fwfs' ").Tables[0];

                System.Collections.Generic.List<Model.TreeModel> modelList = Tree(dt, "0");
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List<Model.TreeModel>));

                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;
        }

        private string getSERVICETYPElist(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = " ";

            try
            {

                dt = new BLL.T_Sys_DictionaryValue().GetList(" F_DictionaryFlag='fwllx' ").Tables[0];

                System.Collections.Generic.List<Model.TreeModel> modelList = Tree(dt, "0");
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List<Model.TreeModel>));

                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;
        }

        private string getVISITOPINIONlist(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = " ";

            try
            {

                dt = new BLL.T_Sys_DictionaryValue().GetList(" F_DictionaryFlag='hfjy' ").Tables[0];

                System.Collections.Generic.List<Model.TreeModel> modelList = Tree(dt, "0");
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List<Model.TreeModel>));

                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;
        }

        private string getCUSTOMEROPINIONlist(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = " ";

            try
            {

                dt = new BLL.T_Sys_DictionaryValue().GetList(" F_DictionaryFlag='khyjfl' ").Tables[0];

                System.Collections.Generic.List<Model.TreeModel> modelList = Tree(dt, "0");
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List<Model.TreeModel>));

                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;
        }

        private string getSOLVElist(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = " ";

            try
            {

                dt = new BLL.T_Sys_DictionaryValue().GetList(" F_DictionaryFlag='jjqk' ").Tables[0];

                System.Collections.Generic.List<Model.TreeModel> modelList = Tree(dt, "0");
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List<Model.TreeModel>));

                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;
        }

        private string getSTANDARDADDRESSlist(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = " ";

            try
            {

                dt = new BLL.T_Sys_DictionaryValue().GetList(" F_DictionaryFlag='bxrxz' ").Tables[0];

                System.Collections.Generic.List<Model.TreeModel> modelList = Tree(dt, "0");
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List<Model.TreeModel>));

                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;
        }

        private string getORDERTYPElist(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = " ";

            try
            {

                dt = new BLL.T_Sys_DictionaryValue().GetList(" F_DictionaryFlag='wtlb' ").Tables[0];

                System.Collections.Generic.List<Model.TreeModel> modelList = Tree(dt, "0");
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List<Model.TreeModel>));

                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;
        }

        private string gettreelist(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = " ";

            try
            {

                dt = new BLL.T_Sys_DictionaryValue().GetList(" F_DictionaryFlag='fwlx' ").Tables[0];

                System.Collections.Generic.List<Model.TreeModel> modelList = Tree(dt, "0");
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List<Model.TreeModel>));

                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;
        }

        private string getF_TypeNamelist(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = " ";

            try
            {

                dt = new BLL.T_Wo_WorkOrderType().GetList("  F_ParentId=1").Tables[0];

                System.Collections.Generic.List<Model.TreeModel> modelList = BindTree(dt, "0");
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List<Model.TreeModel>));

                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;
        }

        private string getF_RoleNamelist(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = " ";

            try
            {

                dt = new BLL.T_Sys_RoleInfo().GetList("  F_RoleId in (30,31)").Tables[0];

                System.Collections.Generic.List<Model.TreeModel> modelList = RoleNameBindTree(dt, "0");
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List<Model.TreeModel>));

                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;
        }

        public List<Model.TreeModel> BindZXZTree(DataTable tab, string parentid)
        {
            if (tab != null && tab.Rows.Count > 0)
            {
                System.Collections.Generic.List<Model.T_Wo_WorkOrderState> categorylist = new BLL.T_Wo_WorkOrderState().DataTableToList(tab);
                System.Collections.Generic.List<Model.TreeModel> modelList = new List<Model.TreeModel>(categorylist.Count);
                for (int i = 0; i < categorylist.Count; i++)
                {
                    Model.TreeModel model = new Model.TreeModel();
                    string currentID = categorylist[i].F_WorkOrderStateId.ToString();//当前功能ID
                    model.id = currentID;
                    model.IconCls = categorylist[i].F_Des;
                    model.text = categorylist[i].F_Name;
                    modelList.Add(model);
                }
                return modelList;

            }
            else
            {
                return null;
            }

        }

        public List<Model.TreeModel> Tree(DataTable tab, string parentid)
        {
            if (tab != null && tab.Rows.Count > 0)
            {
                System.Collections.Generic.List<Model.T_Sys_DictionaryValue> categorylist = new BLL.T_Sys_DictionaryValue().DataTableToList(tab);
                System.Collections.Generic.List<Model.TreeModel> modelList = new List<Model.TreeModel>(categorylist.Count);
                for (int i = 0; i < categorylist.Count; i++)
                {
                    Model.TreeModel model = new Model.TreeModel();
                    string currentID = categorylist[i].F_DictionaryValueId.ToString();//当前功能ID
                    model.id = currentID;
                    model.IconCls = categorylist[i].F_DictionaryFlag;
                    model.text = categorylist[i].F_Name;
                    modelList.Add(model);
                }
                return modelList;

            }
            else
            {
                return null;
            }

        }
        public List<Model.TreeModel> RoleNameBindTree(DataTable tab, string parentid)
        {
            if (tab != null && tab.Rows.Count > 0)
            {
                System.Collections.Generic.List<Model.T_Sys_RoleInfo> categorylist = new BLL.T_Sys_RoleInfo().DataTableToList(tab);
                System.Collections.Generic.List<Model.TreeModel> modelList = new List<Model.TreeModel>(categorylist.Count);
                for (int i = 0; i < categorylist.Count; i++)
                {
                    Model.TreeModel model = new Model.TreeModel();
                    string currentID = categorylist[i].F_RoleId.ToString();//当前功能ID
                    model.id = currentID;
                    model.text = categorylist[i].F_RoleName;
                    modelList.Add(model);
                }
                return modelList;

            }
            else
            {
                return null;
            }

        }
        public List<Model.TreeModel> BindTree(DataTable tab, string parentid)
        {
            if (tab != null && tab.Rows.Count > 0)
            {
                System.Collections.Generic.List<Model.T_Wo_WorkOrderType> categorylist = new BLL.T_Wo_WorkOrderType().DataTableToList(tab);
                System.Collections.Generic.List<Model.TreeModel> modelList = new List<Model.TreeModel>(categorylist.Count);
                for (int i = 0; i < categorylist.Count; i++)
                {
                    Model.TreeModel model = new Model.TreeModel();
                    string currentID = categorylist[i].F_WorkOrderTypeId.ToString();//当前功能ID
                    model.id = currentID;
                    model.IconCls = categorylist[i].F_Ename;
                    model.text = categorylist[i].F_Name;
                    modelList.Add(model);
                }
                return modelList;

            }
            else
            {
                return null;
            }

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