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

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

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = CommonRequest.GetQueryString("action");
            string sqlWhere = CommonRequest.GetQueryString("keyWords");
            string sendSms = CommonRequest.GetQueryString("TypeModelId");
           switch(action){
               case "getUserList":
                   context.Response.Write(GetUserList(context, sqlWhere));
                break;
               case "getSendContext":
                context.Response.Write(SendContext(context, sendSms));
                break;
           }
        }
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public string GetUserList(HttpContext context, string sqlWhere)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = sqlWhere;

            try
            {
                string id = HttpUtility.UrlDecode(CommonRequest.GetQueryString("id"));
                string strpageindex = context.Request.Params["page"];
                int pageindex = 1;
                string strpagesize = context.Request.Params["pagesize"];
                int pagesize = 10;
                sql += " and F_DeleteFlag=0";
                if (id.Trim() != "" && id.Trim() != "undefined")
                {
                    sql = "";
                    sql += " and F_DeleteFlag=0";
                    sql += " and F_DeptId=" + id.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.UserAccount> pageModel = new Model.PageData<Model.UserAccount>();

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

                System.Collections.Generic.List<Model.UserAccount> modelList = new BLL.UserAccount().DataTableToList(dt);
                #region MyRegion
                //System.Collections.Generic.List<Model.UserAccount> newmodelList = new List<Model.UserAccount>();
                //foreach (Model.T_Sys_UserAccount tsu in modelList)
                //{
                //    newmodelList.Add(new Model.UserAccount()
                //    {
                //        F_UserId = tsu.F_UserId,
                //        F_UserCode = tsu.F_UserCode,
                //        F_WorkNumber = tsu.F_WorkNumber,
                //        F_UserName = tsu.F_UserName,
                //        F_Telephone = tsu.F_Telephone,
                //        F_Mobile = tsu.F_Mobile,
                //        F_SeatFlag = tsu.F_SeatFlag,
                //        F_DeptName = new BLL.T_Sys_Department().GetModel(tsu.F_DeptId).F_DeptName,
                //        F_RoleName = new BLL.T_Sys_RoleInfo().GetModel(tsu.F_RoleId).F_RoleName
                //    });
                //} 
                #endregion
                pageModel.Rows = modelList;
                pageModel.Total = recordCount;
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer =
                    new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(Model.PageData<Model.UserAccount>));

                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;
        }
        public string SendContext(HttpContext context,string sendSmsModel) {
            string strSendName="";
            try
            {
                if (!string.IsNullOrEmpty(sendSmsModel))
                {
                    strSendName = DBUtility.DbHelperSQL.GetSingle(" select Contents from t_sms_smsmodel where modelid='" + sendSmsModel + "'").ToString();
                }
            }
            catch
            {
            }
            return strSendName;
        }




        public string LoadUserList(HttpContext context, string sql) {



            string res = "";
            DataTable dt = new DataTable();
            try
            {

                string strpageindex = context.Request.Params["page"];
                int pageindex = 1;
                string strpagesize = context.Request.Params["pagesize"];
                int pagesize = 5;
                if (strpageindex.Trim() != "")
                {
                    try
                    {
                        pageindex = Convert.ToInt32(strpageindex);
                    }
                    catch
                    { }
                }

                if (strpagesize.Trim() != "")
                {
                    try
                    {
                        pagesize = Convert.ToInt32(strpagesize);
                    }
                    catch
                    { }
                }

                sql += " and F_DeleteFlag=0";
                int recordCount = 0;

                Model.PageData<Model.T_Sys_UserAccount> pageModel = new Model.PageData<Model.T_Sys_UserAccount>();

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

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

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






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