using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Edu.Common.API;
using Edu.Common.Plugin;
using Edu.Model.Entity.System;
using Edu.Model.ViewModel.System;
using Edu.Module.System;
using Edu.WebApi.Filter;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;

namespace Edu.WebApi.Controllers.Public
{
    /// <summary>
    /// 系统公用接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class MsgController : BaseController
    {

        /// <summary>
        /// 短信处理类对象
        /// </summary>
        private readonly MsgLogModule msgLogModule = new MsgLogModule();

        #region 短信记录信息
        /// <summary>
        /// 获取短信记录分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetMsgLogPageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Msg_Log_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            query.School_Id = base.UserInfo.School_Id;
            var list = msgLogModule.GetPageListRepository(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            pageModel.Count = rowsCount;
            pageModel.PageData = list;
            return ApiResult.Success(data: pageModel);
        }


        /// <summary>
        /// 获取触发事件枚举
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetMsgSendStatusEnumList()
        {
            var list = EnumHelper.EnumToList(typeof(Common.Enum.System.MsgSendStatusEnum));
            return ApiResult.Success("", list);
        }

        #endregion

        #region 短信配置


        /// <summary>
        /// 获取短信配置页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetMsgBasePageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Msg_Base_Function_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            var list = msgLogModule.GetMsgBasePageListModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            foreach (var item in list)
            {
                if (!string.IsNullOrWhiteSpace(item.MsgConfigure))
                {
                    item.MsgBase = Common.Plugin.JsonHelper.DeserializeObject<RB_Msg_Base_ViewModel>(item.MsgConfigure);
                }
            }
            pageModel.Count = rowsCount;
            pageModel.PageData = list;

            return ApiResult.Success(data: pageModel);
        }

        /// <summary>
        /// 获取短信配置列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetMsgBaseList()
        {
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Msg_Base_Function_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            var list = msgLogModule.GetMsgBaseModule(query);

            if (!string.IsNullOrWhiteSpace(list.MsgConfigure))
            {
                list.MsgBase = Common.Plugin.JsonHelper.DeserializeObject<RB_Msg_Base_ViewModel>(list.MsgConfigure);
            }
            return ApiResult.Success(data: list);
        }




        /// <summary>
        /// 添加修改短信配置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetMsgBase()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Msg_Base_Function_ViewModel>(RequestParm.Msg.ToString());
            var model = new RB_Msg_Base
            {
                CreateDate = DateTime.Now,
                CreateBy = UserInfo.Id,
                Group_Id = base.UserInfo.Group_Id,
                ID = extModel.ID,
                StoreType = extModel.StoreType
            };
            if (extModel.MsgBase != null)
            {
                model.MsgConfigure = Common.Plugin.JsonHelper.Serialize(extModel.MsgBase);
            }
            bool flag = msgLogModule.SetMsgBaseModule(model);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取短信配置实体
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetMsgBase()
        {
            var ID = base.ParmJObj.GetInt("ID", 0);
            var extModel = msgLogModule.GetMsgBaseModule(ID);
            if (!string.IsNullOrWhiteSpace(extModel.MsgConfigure))
            {
                extModel.MsgBase = Common.Plugin.JsonHelper.DeserializeObject<RB_Msg_Base_ViewModel>(extModel.MsgConfigure);
            }
            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 删除短信配置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveMsgBase()
        {
            var ClassId = base.ParmJObj.GetInt("ID", 0);
            var flag = msgLogModule.RemoveMsgBaseModule(ClassId);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        #endregion





        #region 短信模板配置


        /// <summary>
        /// 获取触发事件枚举
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetBaseTemplateTypeEnumList()
        {
            var list = EnumHelper.EnumToList(typeof(Common.Enum.System.BaseTemplateTypeEnum));
            return ApiResult.Success("", list);
        }


        /// <summary>
        /// 获取短信模板配置页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetMsgBaseTemplatePageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Msg_BaseTemplate_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            var list = msgLogModule.GetMsgBaseTemplatePageListModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            foreach (var item in list)
            {
                item.BaseTemplateTypeStr = EnumHelper.ToName(item.BaseTemplateType);
            }
            pageModel.Count = rowsCount;
            pageModel.PageData = list;

            return ApiResult.Success(data: pageModel);
        }

        /// <summary>
        /// 获取短信模板配置列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetMsgBaseTemplateList()
        {
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Msg_BaseTemplate_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            var list = msgLogModule.GetMsgBaseTemplateModule(query);


            return ApiResult.Success(data: list);
        }




        /// <summary>
        /// 添加修改短信模板配置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetMsgBaseTemplate()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Msg_BaseTemplate_ViewModel>(RequestParm.Msg.ToString());
            extModel.CreateDate = DateTime.Now;
            extModel.CreateBy = UserInfo.Id;
            extModel.Group_Id = base.UserInfo.Group_Id;
            bool flag = msgLogModule.SetMsgBaseTemplateModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取短信模板配置实体
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetMsgBaseTemplate()
        {
            var ID = base.ParmJObj.GetInt("ID", 0);
            var extModel = msgLogModule.GetMsgBaseTemplateModule(ID);

            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 删除短信模板配置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveMsgBaseTemplate()
        {
            var ClassId = base.ParmJObj.GetInt("ID", 0);
            var flag = msgLogModule.RemoveMsgBaseTemplateModule(ClassId);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        #endregion


        #region 发短信测试
        [HttpPost]
        public ApiResult TestSendMsg()
        {

            object PhoneMessage = new
            {
                OrderNo = "1111111111111",
                Name = "李三",
                Mobile = "13551132418",
                TripDate = "2020-11-30",//item.TripSTime.Value.ToString("yyyy-MM-dd HH:mm") + " " + ((item.UseDay ?? 0) == Convert.ToDecimal(0.5) ? "半" : Convert.ToInt32(item.UseDay ?? 0).ToString()) + "日",
                Address = "阳光新业2号楼",
                Number = "李四"
            };

            //新增短信记录信息
            var msgLogModel = new RB_Msg_Log
            {
                Group_Id = 100000,
                School_Id = 1,
                Student_Id = 2,
                CreateDate = DateTime.Now,
                SendStatus = Common.Enum.System.MsgSendStatusEnum.InReceipt,
                CreateBy = 0,
                CreateByName = "系统自动发送",
                SendContent = "测试内容",
                ReceiverName = "张学生",
                ReceiverPhone = "13551132417"
            };

            msgLogModule.SendMsg(PhoneMessage, Common.Enum.System.BaseTemplateTypeEnum.OrderSuccess, msgLogModel);

            // ThirdCore.Message.SMSService.SendMsg("13551132417", PhoneMessage, "SMS_201722097", "印象之旅", "dysmsapi.aliyuncs.com", "LTAIwE7l9dImZSa3", "j47Ajn0d0WzUCIX8Biyj3P2r8QDltI", "cn-hangzhou");
            return ApiResult.Success("");
        }

        [HttpPost]
        public ApiResult TestUpdateSendMsg()
        {

            msgLogModule.UpdateMsgSendStatus();

            return ApiResult.Success("");
        }

        #endregion
    }
}
