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.sysmanage.ajax
{
    /// <summary>
    /// systemConfigAction 的摘要说明
    /// </summary>
    public class systemConfigAction : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = CommonRequest.GetQueryString("action");
            string keyWord = CommonRequest.GetQueryString("keyWords");
            string taotlCount = CommonRequest.GetQueryString("taolCount");
            switch (action)
            {
                case "getSystemlist":
                    context.Response.Write(GetSystemList(context, keyWord));
                    break;
                case "delete":
                    context.Response.Write(GetUserDel(context, taotlCount));
                    break;
                //case "Getexpstate":
                //    context.Response.Write(Deptexpstate(context));
                //    break;
                case "clear":
                    break;
            }


        }
        
        /// <summary>
        /// 删除当前的参数信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="taotlCount"></param>
        /// <returns></returns>
        private string GetUserDel(HttpContext context, string taotlCount)
        {
            int res = 0;
            string sql = "update T_Sys_SystemConfig set F_State=1 where F_ParamId in(" + taotlCount.Trim(',') + ")";
            if (!string.IsNullOrEmpty(taotlCount))
            {
                res = DBUtility.DbHelperSQL.ExecuteSql(sql);
            }
            if (res > 0)
            {
                return "success";
            }
            else
            {
                return "error";
            }
        }

        /// <summary>
        /// 获取当前的参数信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="keyWord"></param>
        /// <returns></returns>
        private string GetSystemList(HttpContext context, string keyWord)
        {
            string result = "";
            DataTable dt = new DataTable();
            string keyWordCon = "";
            try
            {
                if (!string.IsNullOrEmpty(keyWord))
                {
                    keyWordCon += " and F_ParamCode like '%" + keyWord + "%'";
                }
                string strpageindex = context.Request.Params["page"];
                int pageindex = 1;
                string strpagesize = context.Request.Params["pagesize"];
                int pagesize = 10;


                int recordCount = 0;
                Model.PageData<Model.T_Sys_SystemConfig> pageModel = new Model.PageData<Model.T_Sys_SystemConfig>();

                dt = BLL.PagerBLL.GetListPager(
                    "T_Sys_SystemConfig  ",
                    "F_ParamId",
                    "*",
                     " and F_State=0" + keyWordCon,
                    "ORDER BY F_ParamId desc",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);

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

                using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
                {
                    //JSON序列化
                    serializer.WriteObject(stream, pageModel);
                    result = System.Text.Encoding.UTF8.GetString(stream.ToArray());
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }

            return result;



        }

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