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.ViewModel.App;
using Edu.Model.ViewModel.System;
using Edu.Module.System;
using Edu.WebApi.Filter;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Edu.WebApi.Controllers.APP
{
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class StudentSystemMsgController : AppBaseController
    {
        private readonly AppSystemMsgModule appSystemMsgModule = new AppSystemMsgModule();

        /// <summary>
        /// 获取系统消息总数
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetSystemLogList()
        {
            var query = new RB_Student_SystemMsg_ViewModel()
            {
                IsRead = base.ParmJObj.GetInt("IsRead", 0),
            };
            query.Group_Id = base.AppUserInfo.Group_Id;
            query.Student_Id = base.AppUserInfo.Id;
            var list = appSystemMsgModule.GetStudentSystemMsgList(query);
            var msgTypeList = Common.Plugin.EnumHelper.EnumToList(typeof(MsgTypeEnum));
            List<object> retult = new List<object>();
            foreach (var item in msgTypeList)
            {
                var nowModel = list.Where(x => (int)x.MsgType == item.Id).OrderByDescending(x => x.CreateTime).FirstOrDefault();
                retult.Add(new
                {
                    MsgType = item.Id,
                    Count = list.Where(x => (int)x.MsgType == item.Id).Count(),
                    Title = nowModel?.Title ?? "暂无消息",
                    CreateTiemStr = (nowModel != null && nowModel.MsgId > 0) ? StringHelper.DateFormatToString(nowModel.CreateTime) : ""
                });
            }
            return ApiResult.Success(data: retult);
        }


        /// <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.AppUserInfo.Group_Id;
            query.Student_Id = base.AppUserInfo.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);
        }



        /// <summary>
        /// 获取自己的系统消息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult UpdateStudentMsg()
        {
            var msgType = base.ParmJObj.GetInt("MsgType", 0);
            if (msgType == 0)
            {
                return ApiResult.Failed("请传入消息类型");
            }

            var retult = appSystemMsgModule.UpdateStudentMsg(msgType, base.AppUserInfo.Group_Id, base.AppUserInfo.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.AppUserInfo.Group_Id;
            query.Student_Id = base.AppUserInfo.Id;
            var model = appSystemMsgModule.GetStudentSystemMsgList(query).FirstOrDefault();
            if (model == null || model.MsgId == 0)
            {
                return ApiResult.Failed("消息不存在");
            }
            if (model.IsRead == 0)//更新当前的系统消息为已读
            {
                try
                {
                    appSystemMsgModule.UpdateStudentMsgById(model.MsgId, model.Group_Id, model.Student_Id);
                }
                catch (Exception ex)
                {
                    Common.Plugin.LogHelper.Write(ex, "GetSystemMsgDetails");
                }
            }
            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);
        }

    }
}
