/*
 * 发送短信记录
 */

using System;
using System.Web;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MySystem.BsModels;
using Library;
using LitJson;
using MySystemLib;

namespace MySystem.Areas.Admin.Controllers
{
    [Area("Admin")]
    [Route("Admin/[controller]/[action]")]
    public class MsgSmsController : BaseController
    {
        public MsgSmsController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
        {
            OtherMySqlConn.connstr = ConfigurationManager.AppSettings["BsSqlConnStr"].ToString();
        }

        #region 发送短信记录列表

        /// <summary>
        /// 根据条件查询发送短信记录列表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index(MsgSms data, string right)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;

            return View();
        }

        #endregion

        #region 根据条件查询发送短信记录列表

        /// <summary>
        /// 发送短信记录列表
        /// </summary>
        /// <returns></returns>
        public JsonResult IndexData(MsgSms data, string CodeSelect, string KindSelect, string SendStatusSelect, int page = 1, int limit = 30)
        {

            Dictionary<string, string> Fields = new Dictionary<string, string>();

            Fields.Add("Mobile", "2"); //手机号
            Fields.Add("SendTime", "3"); //发送时间


            string condition = " and Status>-1";
            //业务编号
            if (!string.IsNullOrEmpty(CodeSelect))
            {
                condition += " and Code=" + CodeSelect;
            }
            //短信类型
            if (!string.IsNullOrEmpty(KindSelect))
            {
                condition += " and Kind=" + KindSelect;
            }
            //发送状态
            if (!string.IsNullOrEmpty(SendStatusSelect))
            {
                condition += " and SendStatus=" + SendStatusSelect;
            }


            Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).IndexData("MsgSms", Fields, "Id desc", "0", page, limit, condition);
            List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
            foreach (Dictionary<string, object> dic in diclist)
            {
                //发送类别
                int SendType = int.Parse(dic["SendType"].ToString());
                if (SendType == 0) dic["SendType"] = "";
                if (SendType == 1) dic["SendType"] = "实时推送";
                if (SendType == 2) dic["SendType"] = "定时推送";
                //短信内容类型
                int SmsType = int.Parse(dic["SmsType"].ToString());
                if (SmsType == 0) dic["SmsType"] = "";
                if (SmsType == 1) dic["SmsType"] = "文本";
                if (SmsType == 2) dic["SmsType"] = "语音";
                if (SmsType == 3) dic["SmsType"] = "营销";
                //校验状态
                int VerifyStatus = int.Parse(dic["VerifyStatus"].ToString());
                if (VerifyStatus == 0) dic["VerifyStatus"] = "未校验";
                if (VerifyStatus == 1) dic["VerifyStatus"] = "已校验";
                if (VerifyStatus == 2) dic["VerifyStatus"] = "校验中";
                if (VerifyStatus == 3) dic["VerifyStatus"] = "校验失败";
                if (VerifyStatus == 4) dic["VerifyStatus"] = "已失效";
                //业务编号
                int Code = int.Parse(dic["Code"].ToString());
                if (Code == 0) dic["Code"] = "";
                if (Code == 1) dic["Code"] = "客小爽";
                //发送状态
                int SendStatus = int.Parse(dic["SendStatus"].ToString());
                if (SendStatus == 0) dic["SendStatus"] = "";
                if (SendStatus == 1) dic["SendStatus"] = "发送成功";
                if (SendStatus == 2) dic["SendStatus"] = "发送失败";
                if (SendStatus == 3) dic["SendStatus"] = "超时";
                if (SendStatus == 4) dic["SendStatus"] = "频率控制拦截";
                if (SendStatus == 5) dic["SendStatus"] = "敏感词过渡";
                if (SendStatus == 6) dic["SendStatus"] = "短信渠道商发给用户成功";
                if (SendStatus == 7) dic["SendStatus"] = "短信渠道商发给用户失败";

            }
            return Json(obj);
        }

        #endregion

        #region 增加发送短信记录

        /// <summary>
        /// 增加或修改发送短信记录信息
        /// </summary>
        /// <returns></returns>
        public IActionResult Add(string right)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;


            return View();
        }

        #endregion

        #region 增加发送短信记录

        /// <summary>
        /// 增加或修改发送短信记录信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Add(MsgSms data)
        {
            Dictionary<string, object> Fields = new Dictionary<string, object>();


            Fields.Add("SeoTitle", data.SeoTitle);
            Fields.Add("SeoKeyword", data.SeoKeyword);
            Fields.Add("SeoDescription", data.SeoDescription);
            int Id = new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Add("MsgSms", Fields, 0);
            AddSysLog(data.Id.ToString(), "MsgSms", "add");
            bsdb.SaveChanges();

            return "success";
        }

        #endregion

        #region 修改发送短信记录

        /// <summary>
        /// 增加或修改发送短信记录信息
        /// </summary>
        /// <returns></returns>
        public IActionResult Edit(string right, int Id = 0)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;


            MsgSms editData = bsdb.MsgSms.FirstOrDefault(m => m.Id == Id) ?? new MsgSms();
            ViewBag.data = editData;
            return View();
        }

        #endregion

        #region 修改发送短信记录

        /// <summary>
        /// 增加或修改发送短信记录信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Edit(MsgSms data)
        {
            Dictionary<string, object> Fields = new Dictionary<string, object>();


            Fields.Add("SeoTitle", data.SeoTitle);
            Fields.Add("SeoKeyword", data.SeoKeyword);
            Fields.Add("SeoDescription", data.SeoDescription);
            new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Edit("MsgSms", Fields, data.Id);
            AddSysLog(data.Id.ToString(), "MsgSms", "update");
            bsdb.SaveChanges();

            return "success";
        }

        #endregion

        #region 删除发送短信记录信息

        /// <summary>
        /// 删除发送短信记录信息
        /// </summary>
        /// <returns></returns>
        public string Delete(string Id)
        {
            string[] idlist = Id.Split(new char[] { ',' });
            AddSysLog(Id, "MsgSms", "del");
            foreach (string subid in idlist)
            {
                int id = int.Parse(subid);
                Dictionary<string, object> Fields = new Dictionary<string, object>();
                Fields.Add("Status", -1);
                new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Edit("MsgSms", Fields, id);
            }
            bsdb.SaveChanges();

            return "success";
        }

        #endregion

        #region 开启

        /// <summary>
        /// 开启
        /// </summary>
        /// <returns></returns>
        public string Open(string Id)
        {
            string[] idlist = Id.Split(new char[] { ',' });
            AddSysLog(Id, "MsgSms", "open");
            foreach (string subid in idlist)
            {
                int id = int.Parse(subid);
                Dictionary<string, object> Fields = new Dictionary<string, object>();
                Fields.Add("Status", 1);
                new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Edit("MsgSms", Fields, id);
            }
            bsdb.SaveChanges();
            return "success";
        }

        #endregion

        #region 关闭

        /// <summary>
        /// 关闭
        /// </summary>
        /// <returns></returns>
        public string Close(string Id)
        {
            string[] idlist = Id.Split(new char[] { ',' });
            AddSysLog(Id, "MsgSms", "close");
            foreach (string subid in idlist)
            {
                int id = int.Parse(subid);
                Dictionary<string, object> Fields = new Dictionary<string, object>();
                Fields.Add("Status", 0);
                new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Edit("MsgSms", Fields, id);
            }
            bsdb.SaveChanges();
            return "success";
        }

        #endregion

        #region 排序
        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="Id"></param>
        public string Sort(int Id, int Sort)
        {
            new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Sort("MsgSms", Sort, Id);

            AddSysLog(Id.ToString(), "MsgSms", "sort");
            return "success";
        }
        #endregion

        #region 导入数据
        /// <summary>
        /// 导入数据
        /// </summary>
        /// <param name="ExcelData"></param>
        public string Import(string ExcelData)
        {
            ExcelData = HttpUtility.UrlDecode(ExcelData);
            JsonData list = JsonMapper.ToObject(ExcelData);
            for (int i = 1; i < list.Count; i++)
            {
                JsonData dr = list[i];

                bsdb.MsgSms.Add(new MsgSms()
                {
                    CreateDate = DateTime.Now,
                    UpdateDate = DateTime.Now,

                });
                bsdb.SaveChanges();
            }
            AddSysLog("0", "MsgSms", "Import");
            return "success";
        }
        #endregion

        #region 导出Excel

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <returns></returns>
        public JsonResult ExportExcel(MsgSms data, string CodeSelect, string KindSelect, string SendStatusSelect)
        {
            Dictionary<string, string> Fields = new Dictionary<string, string>();
            Fields.Add("Mobile", "2"); //手机号
            Fields.Add("SendTime", "3"); //发送时间


            string condition = " and Status>-1";
            //业务编号
            if (!string.IsNullOrEmpty(CodeSelect))
            {
                condition += " and Code=" + CodeSelect;
            }
            //短信类型
            if (!string.IsNullOrEmpty(KindSelect))
            {
                condition += " and Kind=" + KindSelect;
            }
            //发送状态
            if (!string.IsNullOrEmpty(SendStatusSelect))
            {
                condition += " and SendStatus=" + SendStatusSelect;
            }


            Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).IndexData("MsgSms", Fields, "Id desc", "0", 1, 20000, condition, "", false);
            List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
            foreach (Dictionary<string, object> dic in diclist)
            {
                //发送类别
                int SendType = int.Parse(dic["SendType"].ToString());
                if (SendType == 1) dic["SendType"] = "实时推送";
                if (SendType == 2) dic["SendType"] = "定时推送";
                //短信内容类型
                int SmsType = int.Parse(dic["SmsType"].ToString());
                if (SmsType == 1) dic["SmsType"] = "文本";
                if (SmsType == 2) dic["SmsType"] = "语音";
                if (SmsType == 3) dic["SmsType"] = "营销";
                //校验状态
                int VerifyStatus = int.Parse(dic["VerifyStatus"].ToString());
                if (VerifyStatus == 0) dic["VerifyStatus"] = "未校验";
                if (VerifyStatus == 1) dic["VerifyStatus"] = "已校验";
                if (VerifyStatus == 2) dic["VerifyStatus"] = "校验中";
                if (VerifyStatus == 3) dic["VerifyStatus"] = "校验失败";
                if (VerifyStatus == 4) dic["VerifyStatus"] = "已失效";
                //业务编号
                int Code = int.Parse(dic["Code"].ToString());
                if (Code == 1) dic["Code"] = "客小爽";
                //短信类型
                string Kind = dic["Kind"].ToString();
                if (Kind == "合汇邦注册下发验证码") dic["Kind"] = "合汇邦注册下发验证码";
                if (Kind == "FORGOT_YOUR_PAY_PASSWORD_EDP") dic["Kind"] = "FORGOT_YOUR_PAY_PASSWORD_EDP";
                if (Kind == "合汇邦修改结算卡号下发验证码") dic["Kind"] = "合汇邦修改结算卡号下发验证码";
                //发送状态
                int SendStatus = int.Parse(dic["SendStatus"].ToString());
                if (SendStatus == 1) dic["SendStatus"] = "发送成功";
                if (SendStatus == 2) dic["SendStatus"] = "发送失败";
                if (SendStatus == 3) dic["SendStatus"] = "超时";
                if (SendStatus == 4) dic["SendStatus"] = "频率控制拦截";
                if (SendStatus == 5) dic["SendStatus"] = "敏感词过渡";
                if (SendStatus == 6) dic["SendStatus"] = "短信渠道商发给用户成功";
                if (SendStatus == 7) dic["SendStatus"] = "短信渠道商发给用户失败";

            }

            Dictionary<string, object> result = new Dictionary<string, object>();
            result.Add("Status", "1");
            result.Add("Info", "Excel报表-" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + ".xlsx");
            result.Add("Obj", diclist);
            Dictionary<string, object> ReturnFields = new Dictionary<string, object>();

            result.Add("Fields", ReturnFields);
            AddSysLog("0", "MsgSms", "ExportExcel");
            return Json(result);
        }

        #endregion

    }
}
