using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App.BaseApp;
using OpenAuth.App.BaseApp.Request;
using OpenAuth.App.Interface;
using OpenAuth.Repository.Domain;

namespace OpenAuth.WebApi.Controllers.BaseControllers
{
    /// <summary>  
    /// 日 期： 2024-07-06
    /// 描 述： 站内消息
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ImMsgController
    {
        private readonly ImMsgApp _app;
        IAuth _auth;
        public ImMsgController(ImMsgApp app, IAuth auth)
        {
            _app = app;
            _auth = auth;   
        }

        /// <summary>
        /// 获取站内信列表数据
        /// </summary>
        /// <param name="sendUserId">（发送者）</param>
        /// <param name="recvUserId">接收者（当前登录用户）</param>
        /// <param name="keyword">内容</param>
        /// <param name="page">当前页</param>
        /// <param name="limit">每页条数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<ImMsg>>>> LoadImMsgList(string sendUserId, string recvUserId, string keyword, int page=1, int limit=15)
        {
            return await _app.LoadImMsgList(sendUserId, recvUserId,keyword, page, limit);
        }


        /// <summary>
        /// 分页获取系统消息
        /// </summary>
        /// <param name="keyword">内容筛选</param>
        /// <param name="isDelete">是否删除（虚拟删除）</param>
        /// <param name="page">当前页</param>
        /// <param name="limit">每页条数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<ImMsg>>>> LoadSysImMsgList(string keyword, int? isDelete, int page=1, int limit=15)
        {
            return await _app.LoadSysImMsgList(_auth.GetUserId(),"",keyword, isDelete,  page, limit);
        }

        /// <summary>
        /// 获取当前登录用户未读消息
        /// </summary>
        ///<param name="userId">当前登录用户id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<List<ImMsg>>> GetLastList(string userId)
        {
            return await _app.GetLastList(userId);
        }
        /// <summary>
        /// 根据contentid获取任务信息
        /// </summary>
        /// <param name="contentid">消息内容id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<dynamic>> GetInfoByContentId(string contentid)
        {
            return await _app.GetInfoByContentId(contentid);
        }
        /// <summary>
        /// 新增数据
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> SaveEntity(ImMsgReq entity)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.SaveEntity(entity);

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 删除数据源
        /// </summary>
        /// <param name="id">主键</param>
        [HttpPost]
        public async Task<Response<bool>> DeleteEntity(string id)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.DeleteEntity(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        /// <param name="ids">主键list</param>
        [HttpPost]
        public async Task<Response<bool>> DeleteEntitys(List<string> ids)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.DeleteEntitys(ids);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 删除数据源(虚拟删除)
        /// </summary>
        /// <param name="id">主键</param>
        [HttpPost]
        public async Task<Response<bool>> VirtualDeleteEntity(string id)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.VirtualDeleteEntity(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 批量删除数据（虚拟删除）
        /// </summary>
        /// <param name="ids">主键list</param>
        [HttpPost]
        public async Task<Response<bool>> VirtualDeleteEntitys(List<string> ids)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.VirtualDeleteEntitys(ids);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 确认阅读
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> ReadMsg(string id)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.ReadMsg(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        [HttpPost]
        public async Task<Response<bool>> SendSmg(string senduserid, IEnumerable<string> userIdList, string content, string messageType, string contentId = "")
        {
            var result = new Response<bool>();
            try
            {
                await _app.SendMsg(senduserid, userIdList, content, messageType, contentId);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }
    }
}
