RoomIMContentService.cs 4.46 KB
using NCC.Common.Filter;
using NCC.Dependency;
using NCC.LinqBuilder;
using NCC.Message.Entitys;
using NCC.Message.Entitys.Dto.IM;
using NCC.Message.Entitys.Model.IM;
using NCC.Message.Interfaces.Message;
using Mapster;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using Yitter.IdGenerator;
using NCC.Message.Entitys.Dto.Message;
using NCC.FriendlyException;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace NCC.Message.Service
{
    /// <summary>
        /// 群组消息
        /// 版 本:V1.20.15
        /// 版 权:Wesley(https://www.NCCsoft.com)
        /// 作 者:NCC开发平台组
        /// 日 期:2022-03-16 
        /// </summary>
    [ApiDescriptionSettings(Tag = "群组消息", Name = "RoomIM", Order = 240)]
    [Route("api/RoomIM/[controller]")]
    public class RoomIMContentService : IRoomIMContentService, ITransient
    {
        private readonly ISqlSugarRepository<RoomIMContentEntity> _IMContentRepository;
        private readonly ISqlSugarRepository<RoomMessageEntity> _roomMessageRepository;

        /// <summary>
        /// 
        /// </summary>
        public RoomIMContentService(ISqlSugarRepository<RoomIMContentEntity> IMContentRepository, ISqlSugarRepository<RoomMessageEntity> roomMessageRepository)
        {
            _IMContentRepository = IMContentRepository;
            _roomMessageRepository = roomMessageRepository;
        }

        /// <summary>
        /// 获取群组/房间消息列表
        /// </summary>
        /// <param name="input">分页参数</param>
        /// <param name="sendUserId">发送者</param>
        /// <param name="roomNo">房间号/群组</param>
        /// <returns></returns>
        public dynamic GetMessageList(string sendUserId, string roomNo, PageInputBase input)
        {
            var orderByType = input.sort == "asc" ? OrderByType.Asc : OrderByType.Desc;
            var list = _IMContentRepository.Entities.Select(it => new RoomIMContentListOutput { id = it.Id, sendUserId = it.SendUserId, sendTime = it.SendTime, content = it.Content, contentType = it.ContentType, state = it.State, roomNo = it.RoomNo }).MergeTable()
                .OrderBy(it => it.sendTime, orderByType)
                .WhereIF(!string.IsNullOrEmpty(input.keyword), it => it.content.Contains(input.keyword))
                .Where(i => (i.sendUserId == sendUserId && i.roomNo == roomNo))
                .ToPagedList(input.currentPage, input.pageSize);
            if (input.sort == "desc")
            {
                list.list = list.list.ToList().OrderBy(it => it.sendTime);
            }
            return PageResult<RoomIMContentListOutput>.SqlSugarPageResult(list);
        }

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="sendUserId"></param>
        /// <param name="roomNo"></param>
        /// <param name="message"></param>
        /// <param name="messageType"></param>
        /// <returns></returns>
        public int SendMessage(string sendUserId, string roomNo, string message, string messageType)
        {
            RoomIMContentEntity entity = new RoomIMContentEntity();
            entity.Id = YitIdHelper.NextId().ToString();
            entity.SendUserId = sendUserId;
            entity.SendTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            entity.RoomNo = roomNo;
            entity.State = 0;
            entity.Content = message;
            entity.ContentType = messageType;


            return _IMContentRepository.Insert(entity);
        }
        #region 房间

        /// <summary>
        /// 添加房间
        /// </summary> 
        /// <returns></returns>
        public async Task<object> AddRoom(GroupMessageCrInput input)
        {
            if (input == null) throw NCCException.Oh("添加失败!");
            //if (input.roomNo.IsNullOrEmpty()) throw NCCException.Oh("房间号必填!");
            //if (_roomMessageRepository.Any(o => o.RoomNo == input.roomNo)) throw NCCException.Oh("房间已存在!");
            RoomMessageEntity entity = input.Adapt<RoomMessageEntity>();
            entity.Id = YitIdHelper.NextId().ToString();
            entity.IsRead = 0;
            entity.RoomNo = YitIdHelper.NextId().ToString();
            await _roomMessageRepository.Ado.Context.Insertable<RoomMessageEntity>(entity).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
            return entity;
        }

        #endregion
    }
}