using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Edu.Common.API;
using Edu.Common.Enum.App;
using Edu.Common.Plugin;
using Edu.Model.Entity.App;
using Edu.Model.Entity.System;
using Edu.Model.ViewModel.App;
using Edu.Model.ViewModel.System;
using Edu.Module.System;
using Edu.Repository.App;
using Edu.WebApi.Filter;
using Microsoft.AspNetCore.Authorization;
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 AppPublicController : BaseController
    {

        /// <summary>
        /// 短信处理类对象
        /// </summary>
        private readonly AppHomePageModule appHomePageModule = AOP.AOPHelper.CreateAOPObject<AppHomePageModule>();
        /// <summary>
        /// app学生系统消息处理类对象
        /// </summary>
        private readonly AppSystemMsgModule appSystemMsgModule = new AppSystemMsgModule();


        #region 首页轮播图
        [HttpPost]
        public ApiResult GetBannerIndex()
        {
            var query = new RB_HomePage_Banner()
            {
                Group_Id = base.UserInfo.Group_Id,
            };
            var list = appHomePageModule.GetHomePageBannerList(query).OrderBy(x => x.Sort);

            return ApiResult.Success("", list);

        }


        /// <summary>
        /// 添加修改轮播图信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetBannerIndex()
        {
            var list = Common.Plugin.JsonHelper.DeserializeObject<List<RB_HomePage_Banner>>(RequestParm.Msg.ToString());
            if (list == null || !list.Any())
            {
                return ApiResult.Failed("请传入轮播图信息");
            }
            else if (list.Any(x => string.IsNullOrWhiteSpace(x.BannerPic)))
            {
                return ApiResult.Failed("请传入轮播图信息");
            }
            list.Where(x => x.BannerId == 0).ToList().ForEach(x => x.CreateBy = base.UserInfo.Id);
            list.Where(x => x.BannerId == 0).ToList().ForEach(x => x.CreateTime = System.DateTime.Now);
            list.ForEach(x => x.UpdateTime = System.DateTime.Now);
            list.ForEach(x => x.UpdateBy = base.UserInfo.Id);
            list.ForEach(x => x.School_Id = base.UserInfo.School_Id);
            list.ForEach(x => x.Group_Id = base.UserInfo.Group_Id);
            list.ForEach(x => x.Status = Common.Enum.DateStateEnum.Normal);
            bool flag = appHomePageModule.SetHomePage(list);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }
        #endregion



        #region 首页标签
        [HttpPost]
        public ApiResult GetLableIndex()
        {
            var query = new RB_HomePage_Lable()
            {
                Group_Id = base.UserInfo.Group_Id,
            };
            var list = appHomePageModule.GetHomePageLableList(query).OrderBy(x => x.Sort);

            return ApiResult.Success("", list);

        }


        /// <summary>
        /// 添加修改首页标签信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetLableIndex()
        {
            var list = Common.Plugin.JsonHelper.DeserializeObject<List<RB_HomePage_Lable>>(RequestParm.Msg.ToString());
            if (list == null || !list.Any())
            {
                return ApiResult.Failed("请传入标签信息");
            }
            else if (list.Any(x => string.IsNullOrWhiteSpace(x.LableName)))
            {
                return ApiResult.Failed("请输入标签名称");
            }
            list.Where(x => x.LableId == 0).ToList().ForEach(x => x.CreateBy = base.UserInfo.Id);
            list.Where(x => x.LableId == 0).ToList().ForEach(x => x.CreateTime = System.DateTime.Now);
            list.ForEach(x => x.UpdateTime = System.DateTime.Now);
            list.ForEach(x => x.UpdateBy = base.UserInfo.Id);
            list.ForEach(x => x.School_Id = base.UserInfo.School_Id);
            list.ForEach(x => x.Group_Id = base.UserInfo.Group_Id);
            list.ForEach(x => x.Status = Common.Enum.DateStateEnum.Normal);
            bool flag = appHomePageModule.SetHomePageLable(list);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }
        #endregion


        #region app 系统消息
        /// <summary>
        /// 获取系统消息分页
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetSystemMsgPageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = new RB_Student_SystemMsg_ViewModel()
            {
                IsRead = base.ParmJObj.GetInt("IsRead", -1),
                MsgType = (MsgTypeEnum)base.ParmJObj.GetInt("MsgType", 0),
                StudentType = (StudentTypeEnum)base.ParmJObj.GetInt("StudentType", 0),
            };
            query.Group_Id = base.UserInfo.Group_Id;
            query.Student_Id = base.UserInfo.Id;
            var list = appSystemMsgModule.GetStudentSystemMsgPageList(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            var retult = list.Select(x => new
            {
                x.MsgId,
                x.IsRead,
                x.Title,
                x.Content,
                x.Pic,
                CreateTimeStr = x.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),
                MsgTypeStr = (x.MsgType.HasValue && x.MsgType.Value > 0) ? Common.Plugin.EnumHelper.ToName(x.MsgType) : "",
                StudentTypeStr = (x.StudentType.HasValue && x.StudentType.Value > 0) ? Common.Plugin.EnumHelper.ToName(x.StudentType) : "",
                x.MsgType,
                x.StudentType
            });
            pageModel.Count = rowsCount;
            pageModel.PageData = retult;
            return ApiResult.Success(data: pageModel);
        }




        public ApiResult SetSystemMsg()
        {
            var query = new RB_Student_SystemMsg_ViewModel()
            {
                Title = base.ParmJObj.GetStringValue("Title"),
                Content = base.ParmJObj.GetStringValue("Content"),
                Pic = base.ParmJObj.GetStringValue("Pic"),
                StudentIds = base.ParmJObj.GetStringValue("StudentIds"),
                MsgType = (MsgTypeEnum)base.ParmJObj.GetInt("MsgType", 0),
                StudentType = (StudentTypeEnum)base.ParmJObj.GetInt("StudentType", 0),
            };

            if (string.IsNullOrWhiteSpace(query.StudentIds))
            {
                return ApiResult.Failed("请选择您要发送的学生");
            }

            List<RB_Student_SystemMsg_ViewModel> list = new List<RB_Student_SystemMsg_ViewModel>();
            foreach (var item in query.StudentIds.Split(","))
            {
                if (!string.IsNullOrWhiteSpace(item))
                {
                    list.Add(new RB_Student_SystemMsg_ViewModel
                    {
                        MsgId = 0,
                        Group_Id = base.UserInfo.Group_Id,
                        School_Id = base.UserInfo.School_Id,
                        CreateBy = base.UserInfo.Id,
                        CreateTime = System.DateTime.Now,
                        Status = Common.Enum.DateStateEnum.Normal,
                        Student_Id = Convert.ToInt32(item),
                        IsRead = 0,
                        MsgType = query.MsgType,
                        Title = query.Title,
                        StudentType = query.StudentType,
                        Pic = query.Pic,
                        Content = query.Content
                    });
                }
            }
            if (list == null || !list.Any())
            {
                return ApiResult.Failed("请选择您要发送的学生");
            }

            var retult = appSystemMsgModule.BatchSetStudentSystemMsg(list);

            return retult ? ApiResult.Success("新增系统消息成功") : ApiResult.Failed("新增系统消息失败");
        }

        /// <summary>
        /// 系统消息枚举类型
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public ApiResult GetMsgTypeEnumList()
        {
            var list = Common.Plugin.EnumHelper.EnumToList(typeof(MsgTypeEnum));

            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 学校提醒枚举类型
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public ApiResult GetStudentTypeEnumList()
        {
            var list = Common.Plugin.EnumHelper.EnumToList(typeof(StudentTypeEnum));

            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 批量删除系统消息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult DelStudentMsg()
        {
            var msgIds = base.ParmJObj.GetStringValue("MsgIds");
            if (string.IsNullOrWhiteSpace(msgIds))
            {
                return ApiResult.Failed("请传入您要删除的消息id");
            }
            var retult = appSystemMsgModule.DelStudentMsgById(msgIds, base.UserInfo.Group_Id);
            return retult ? ApiResult.Success("删除成功") : ApiResult.Failed("删除失败");
        }

        /// <summary>
        /// 获取系统消息详情
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetSystemMsgDetails()
        {
            var msgId = base.ParmJObj.GetInt("MsgId", 0);
            if (msgId == 0)
            {
                return ApiResult.Failed("参数错误");
            }
            var query = new RB_Student_SystemMsg_ViewModel()
            {
                MsgId = msgId
            };
            query.Group_Id = base.UserInfo.Group_Id;
            var model = appSystemMsgModule.GetStudentSystemMsgList(query).FirstOrDefault();
            if (model == null || model.MsgId == 0)
            {
                return ApiResult.Failed("消息不存在");
            }
            var retult = new
            {
                model.MsgId,
                model.IsRead,
                model.Title,
                model.Content,
                model.Pic,
                CreateTimeStr = model.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),
                MsgTypeStr = (model.MsgType.HasValue && model.MsgType.Value > 0) ? Common.Plugin.EnumHelper.ToName(model.MsgType) : "",
                StudentTypeStr = (model.StudentType.HasValue && model.StudentType.Value > 0) ? Common.Plugin.EnumHelper.ToName(model.StudentType) : "",
                model.MsgType,
                model.StudentType
            };
            return ApiResult.Success(data: retult);
        }

        #endregion
    }
}