Blame view

netcore/src/Modularity/Message/NCC.Message/Service/RoomIMContentService.cs 4.46 KB
de2bd2f9   “wangming”   项目初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
  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
          ///  权:Wesleyhttps://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
      }
  }