using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Edu.Common.API;
using Edu.Common.Plugin;
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 SystemLogController : AppBaseController
    {
        private readonly MsgLogModule msgLogModule = new MsgLogModule();

        /// <summary>
        /// 获取自己的系统消息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetSystemLogPageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = new RB_System_Log_ViewModel()
            {
                SelectSendState = 1
            };
            query.Group_Id = base.AppUserInfo.Group_Id;
            query.Student_Id = base.AppUserInfo.Id;
            List<object> result = new List<object>();
            var list = msgLogModule.GetSystemLogPageListRepository(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            foreach (var item in list)
            {
                result.Add(new
                {
                    item.ID,
                    item.SendTitle,
                    item.PicUrl,
                    item.SendStatus
                });
            }
            pageModel.Count = rowsCount;
            pageModel.PageData = result;
            return ApiResult.Success(data: pageModel);
        }
        /// <summary>
        /// 获取自己的系统消息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetSystemLogModel()
        {
            var ID = base.ParmJObj.GetInt("ID", 0);
            if (ID == 0)
            {
                return ApiResult.Failed("请传入您要阅读的系统消息id");
            }
            var model = msgLogModule.GetSystemLogListRepository(new RB_System_Log_ViewModel { ID = ID }).FirstOrDefault();

            return ApiResult.Success(data: model);
        }

        /// <summary>
        /// 获取自己的系统消息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetSystemLogCount()
        {
            var query = new RB_System_Log_ViewModel()
            {
                SelectSendState = 1
            };
            query.Group_Id = base.AppUserInfo.Group_Id;
            query.Student_Id = base.AppUserInfo.Id;

            var msgCount = msgLogModule.GetSystemLogListRepository(query).Count();

            return ApiResult.Success(data: new { msgCount });
        }

    }
}
