Blame view

netcore/src/Modularity/Message/NCC.Message/Service/ImReplyService.cs 4.53 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
  using NCC.Common.Core.Manager;
  using NCC.Dependency;
  using NCC.DynamicApiController;
  using NCC.JsonSerialization;
  using NCC.Message.Entitys;
  using NCC.Message.Entitys.Dto.ImReply;
  using NCC.Message.Extensions;
  using NCC.Message.Interfaces;
  using NCC.System.Entitys.Permission;
  using Mapster;
  using Microsoft.AspNetCore.Mvc;
  using SqlSugar;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  
  namespace NCC.Message.Service
  {
      /// <summary>
      /// 消息会话接口
      /// </summary>
      [ApiDescriptionSettings(Tag = "Message", Name = "imreply", Order = 163)]
      [Route("api/message/[controller]")]
      public class ImReplyService : IImReplyService, IDynamicApiController, ITransient
      {
          private readonly ISqlSugarRepository<ImReplyEntity> _imReplyRepository;
          private readonly IUserManager _userManager;
          private readonly SqlSugarScope _db;
  
          /// <summary>
          /// 初始化一个<see cref="ImReplyService"/>类型的新实例
          /// </summary>
          public ImReplyService(ISqlSugarRepository<ImReplyEntity> imReplyRepository, IUserManager userManager)
          {
              _imReplyRepository = imReplyRepository;
              _userManager = userManager;
              _db = _imReplyRepository.Context;
          }
  
          /// <summary>
          /// 获取消息会话列表
          /// </summary>
          /// <returns></returns>
          [HttpGet("")]
          public async Task<dynamic> GetList()
          {
              var userInfo = await _userManager.GetUserInfo();
              var newObjectUserList = new List<ImReplyListOutput>();
              //获取全部聊天对象列表
              var objectList = _db.UnionAll(_db.Queryable<ImReplyEntity>().Where(i => i.ReceiveUserId == userInfo.userId).Select(it => new { userId = it.UserId, latestDate = it.ReceiveTime }).MergeTable().Select<ImReplyObjectIdOutput>(),
                   _db.Queryable<ImReplyEntity>().Where(i => i.UserId == userInfo.userId).Select(it => new { userId = it.ReceiveUserId, latestDate = it.ReceiveTime }).MergeTable().Select<ImReplyObjectIdOutput>()).MergeTable().GroupBy(it => new { it.userId }).Select(it => new { it.userId, latestDate = SqlFunc.AggregateMax(it.latestDate) }).ToList();
              var objectUserList = objectList.Adapt<List<ImReplyListOutput>>();
              if (objectUserList.Count > 0)
              {
                  var userList = await _db.Queryable<UserEntity>().In(it => it.Id, objectUserList.Select(it => it.userId).ToArray()).ToListAsync();
                  //将用户信息补齐
                  userList.ForEach(item =>
                  {
                      objectUserList.ForEach(it =>
                      {
                          if (it.userId == item.Id)
                          {
                              it.account = item.Account;
                              it.id = it.userId;
                              it.realName = item.RealName;
                              it.headIcon = "/api/File/Image/userAvatar/" + item.HeadIcon;
  
                              var imContent = _db.Queryable<IMContentEntity>().Where(i => (i.SendUserId == userInfo.userId && i.ReceiveUserId == it.userId) || (i.SendUserId == it.userId && i.ReceiveUserId == userInfo.userId)).Where(i => i.SendTime.Equals(it.latestDate)).ToList().FirstOrDefault();
                              //获取最信息
                              if (imContent != null)
                              {
                                  it.latestMessage = imContent.Content;
                                  it.messageType = imContent.ContentType;
                              }
                              it.unreadMessage = _db.Queryable<IMContentEntity>().Where(i => i.SendUserId == it.userId && i.ReceiveUserId == userInfo.userId).Where(i => i.State == 0).Count();
                          }
                      });
                  });
              }
              var output = objectUserList.OrderByDescending(x => x.latestDate).ToList();
              return new { list = output };
          }
  
          /// <summary>
          /// 
          /// </summary>
          /// <param name="connectionId"></param>
          [NonAction]
          public void ForcedOffline(string connectionId)
          {
              var onlineUser = WebSocketClientCollection._clients.Find(q => q.ConnectionId == connectionId);
              if (onlineUser != null)
              {
                  onlineUser.SendMessageAsync(new { method = "logout", msg = "此账号已在其他地方登陆" }.Serialize());
                  WebSocketClientCollection._clients.RemoveAll((x) => x.ConnectionId == connectionId);
              }
          }
      }
  }