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 { ///     /// 群组消息     /// 版 本:V1.20.15     /// 版 权:Wesley(https://www.NCCsoft.com)     /// 作 者:NCC开发平台组     /// 日 期:2022-03-16     /// [ApiDescriptionSettings(Tag = "群组消息", Name = "RoomIM", Order = 240)] [Route("api/RoomIM/[controller]")] public class RoomIMContentService : IRoomIMContentService, ITransient { private readonly ISqlSugarRepository _IMContentRepository; private readonly ISqlSugarRepository _roomMessageRepository; /// /// /// public RoomIMContentService(ISqlSugarRepository IMContentRepository, ISqlSugarRepository roomMessageRepository) { _IMContentRepository = IMContentRepository; _roomMessageRepository = roomMessageRepository; } /// /// 获取群组/房间消息列表 /// /// 分页参数 /// 发送者 /// 房间号/群组 /// 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.SqlSugarPageResult(list); } /// /// 发送消息 /// /// /// /// /// /// 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 房间 /// /// 添加房间 /// /// public async Task 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(); entity.Id = YitIdHelper.NextId().ToString(); entity.IsRead = 0; entity.RoomNo = YitIdHelper.NextId().ToString(); await _roomMessageRepository.Ado.Context.Insertable(entity).CallEntityMethod(m => m.Create()).ExecuteCommandAsync(); return entity; } #endregion } }