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

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = CommonRequest.GetQueryString("action");
            string tel = CommonRequest.GetQueryString("tel");
              
            switch (action) {
                case "getLoadlist":
                    context.Response.Write(getLoadList(context, tel));
                    break;
                case "getLoadAlrlist":
                    context.Response.Write(getLoadAlrlist(context, tel));
                    break;
                case "ResendSms":
                    context.Response.Write(getResendSms(context));
                    break;
                case "delete":
                    context.Response.Write(Delete(context));
                    break;
            }
        }



        public string getResendSms(HttpContext context) {
            bool refg=false;
            string str="";
            string smsSnetTask=CommonRequest.GetQueryString("ResendId");
            Model.T_SMS_SentSMS sentSmsModel = new Model.T_SMS_SentSMS();
            BLL.T_SMS_SentSMS sentSmsBll=new BLL.T_SMS_SentSMS();
            try{
            if (!string.IsNullOrEmpty(smsSnetTask)) { 
            Model.T_SMS_SendSMSTask smsSendTaskModel = new Model.T_SMS_SendSMSTask();
            smsSendTaskModel=new BLL.T_SMS_SendSMSTask().GetModel(Convert.ToInt32(smsSnetTask));
            if (smsSendTaskModel != null)
            {
                sentSmsModel.TelNum = smsSendTaskModel.TelNum;
                sentSmsModel.Content = smsSendTaskModel.Content;
                sentSmsModel.CommitTime = DateTime.Now;
                sentSmsModel.State = 0;
                sentSmsModel.SendTime = DateTime.Now;
                sentSmsModel.MaxSendCount = 3;
                sentSmsModel.CurSentCount = 0;
                sentSmsModel.Info = smsSendTaskModel.Info;
                sentSmsModel.SendModemIMEI = "管理员";
                sentSmsModel.F_UserID = 1;
                sentSmsModel.F_CustomerID = 3;
                sentSmsModel.F_Name = "8000";
                refg = sentSmsBll.Add(sentSmsModel);
                new BLL.T_SMS_SendSMSTask().Delete(Convert.ToInt32(smsSnetTask));
                if (refg)
                {
                    str = "success";
                }
                else
                {
                    str = "error";
                }
            }
            else {
                str = "acss";
            }

            }
            }catch{
              str="error";
            }
            return str;
        }



        #region 删除数据

        private string Delete(HttpContext context)
        {
            bool res = false;
            string arr = CommonRequest.GetQueryString("arrid");
            if (arr.IndexOf(",") == -1)
            {
                res = new BLL.T_SMS_SendSMSTask().Delete(Convert.ToInt32(arr));
            }
            else
            {
                res = new BLL.T_SMS_SendSMSTask().DeleteList(arr);
            }


            return res ? "success" : "field";
        }
        #endregion
        /// <summary>
        /// 未发送短信信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="sql"></param>
        /// <returns></returns>
        public string getLoadList(HttpContext context,string sql) {

            string res = "";
            DataTable dt = new DataTable();
            try
            {
                if (sql != "")
                {
                    sql = " and TelNum like '%" + sql + "%'";
                }
                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 State=0";

                int recordCount = 0;

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

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

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

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


        /// <summary>
        /// 已发送短信信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="sql"></param>
        /// <returns></returns>
        public string getLoadAlrlist(HttpContext context, string sql)
        {

            string res = "";
            DataTable dt = new DataTable();
            try
            {
                if (sql != "")
                {
                    sql = " and TelNum like '%" + sql + "%'";
                }
                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 State!=0";

                int recordCount = 0;

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

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

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

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