using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZXDT.CallCenter.MVCWeb.Commons;
using ZXDT.CallCenter.MVCWeb.Models;

namespace ZXDT.CallCenter.MVCWeb.Controllers
{
    public class SMSManagerController : Controller
    {
        BLL.T_SMS_SentSMS smsBLL = new BLL.T_SMS_SentSMS();

        public ActionResult SMSSentList()
        {
            CallScreenModel callScreenModel = new CallScreenModel();
            return View(callScreenModel);
        }

        /// <summary>
        /// 获取数据列表
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <param name="sqlWhere">查询条件</param>
        /// <returns></returns>
        [ActionName("SMSSentData")]
        [HttpGet]
        public string SMSSentData(DateTime? NowDateTime, int page, int limit, string dateParty,
           string phoneNumber, int? status)
        {

            //数据结果集
            ResponseData dataModel = new ResponseData();

            string sql = "";

            //客户电话
            if (!string.IsNullOrEmpty(phoneNumber))
            {
                sql += " and TelNum like '%" + phoneNumber.Trim() + "%'";
            }
        
            //状态
            if (status != null)
            {
                sql += " and State =" + status;
            }
          
          
           



            if (!string.IsNullOrEmpty(dateParty))
            {

                string startDate = dateParty.Substring(0, 19);
                string endDate = dateParty.Substring(21);
                sql += " and SendTime>= '" + startDate + "'";
                sql += " and SendTime<= '" + endDate + "'";
            }

            DataTable dt = new DataTable();
            int recordCount = 0;
            Model.PageData<Model.T_Wo_WorkOrderBase> pageModel = new Model.PageData<Model.T_Wo_WorkOrderBase>();
            dt = BLL.PagerBLL.GetListPager(
                "T_SMS_SentSMS",
                "SMSID",
                " *,CONVERT(varchar,SendTime, 120 ) as SendTimeNew"
                  + " ,(CASE State WHEN 1 THEN '成功' WHEN '0' THEN '失败' ELSE NULL END) as StateName",
                sql,
                "ORDER BY SendTime desc",
                limit,
                page,
                true,
                out recordCount);


            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;

            return JsonConvert.SerializeObject(dataModel);
        }



        /// <summary>
        /// 舆情发送短信接口
        /// </summary>
        /// <param name="MessageCount">近24小时内舆情数量</param>
        /// <returns></returns>
        [ActionName("MessageNotice")]
        [HttpGet]
        public bool MessageNotice(string MessageContent)
        {

            //数据结果集
            bool SendResult = false;
            try
            {
               
                    string smsUrl = "http://www.jc-chn.cn/smsSend.do?username=hry168&password=c1cafe2731b8d88a0f397667facdfbf2&mobile=1863859958&content=";

                    System.Web.HttpUtility.UrlEncode("发现负面舆情！" + MessageContent, System.Text.Encoding.UTF8); 
                    Model.T_SMS_SentSMS model = new Model.T_SMS_SentSMS();
                    model.State = 0;
                    model.SendTime = DateTime.Now;
                    model.MaxSendCount = 5;
                   // model.F_CustomerID = MessageCount;//舆情条数
                    model.Content = MessageContent;
                    for (int i = 1; i <= model.MaxSendCount; i++)
                    {
                        model.CurSentCount = i;
                        string addWxResult = HttpHelper.HttpSMSSend(smsUrl);
                        //判断返回接口是否发送成功！
                        if (!string.IsNullOrEmpty(addWxResult) && !addWxResult.Contains("-") && int.Parse(addWxResult) > 0)
                        {
                            SendResult = true;
                            model.State = 1;//发送成功

                            //将短信发送信息存储到内容中
                            smsBLL.Add(model);
                            break;
                        }
                        //将短信发送信息存储到内容中
                        smsBLL.Add(model);
                    }
                


            }
            catch (Exception ex)
            {

            }

            return SendResult;

        }



    }
}
