/// <summary> /// 创建 <see cref="MissivePage"/> /// </summary> /// <param name="currentUserId">当前登录用户 ID</param> /// <param name="dbContext"><see cref="KeylolDbContext"/></param> /// <param name="cachedData"><see cref="CachedDataProvider"/></param> /// <returns><see cref="MissivePage"/></returns> public static async Task <MissivePage> CreateAsync(string currentUserId, KeylolDbContext dbContext, CachedDataProvider cachedData) { var messages = await PostOfficeMessageList.CreateAsync(typeof(MissivePage), currentUserId, 1, true, dbContext, cachedData); return(new MissivePage { MessagePageCount = messages.Item2, Messages = messages.Item1 }); }
/// <summary> /// 获取公函消息列表 /// </summary> /// <param name="page">分页页码</param> /// <param name="dbContext"><see cref="KeylolDbContext"/></param> /// <param name="cachedData"><see cref="CachedDataProvider"/></param> /// <returns><see cref="PostOfficeMessageList"/></returns> public static async Task <PostOfficeMessageList> GetMessages(int page, [Injected] KeylolDbContext dbContext, [Injected] CachedDataProvider cachedData) { return((await PostOfficeMessageList.CreateAsync(typeof(MissivePage), StateTreeHelper.GetCurrentUserId(), page, false, dbContext, cachedData)).Item1); }
/// <summary> /// 创建 <see cref="PostOfficeMessageList"/> /// </summary> /// <param name="pageType">邮政页面类型</param> /// <param name="currentUserId">当前登录用户 ID</param> /// <param name="page">分页页码</param> /// <param name="returnPageCount">是否返回总页数</param> /// <param name="dbContext"><see cref="KeylolDbContext"/></param> /// <param name="cachedData"><see cref="CachedDataProvider"/></param> /// <returns>Item1 表示 <see cref="PostOfficeMessageList"/>,Item2 表示总页数</returns> public static async Task <Tuple <PostOfficeMessageList, int> > CreateAsync(Type pageType, string currentUserId, int page, bool returnPageCount, KeylolDbContext dbContext, CachedDataProvider cachedData) { Expression <Func <Message, bool> > condition; if (pageType == typeof(UnreadPage)) { condition = m => m.ReceiverId == currentUserId; } else if (pageType == typeof(CommentPage)) { condition = m => m.ReceiverId == currentUserId && (int)m.Type >= 100 && (int)m.Type <= 199; } else if (pageType == typeof(LikePage)) { condition = m => m.ReceiverId == currentUserId && m.Type >= 0 && (int)m.Type <= 99; } else if (pageType == typeof(SubscriberPage)) { condition = m => m.ReceiverId == currentUserId && (int)m.Type >= 300 && (int)m.Type <= 399; } else if (pageType == typeof(MissivePage)) { condition = m => m.ReceiverId == currentUserId && (int)m.Type >= 200 && (int)m.Type <= 299; } else { throw new ArgumentOutOfRangeException(nameof(pageType)); } var messages = await dbContext.Messages.Include(m => m.Article) .Include(m => m.Article.Author) .Include(m => m.Activity) .Include(m => m.Activity.Author) .Include(m => m.Operator) .Include(m => m.ArticleComment) .Include(m => m.ArticleComment.Article) .Include(m => m.ArticleComment.Article.Author) .Include(m => m.ActivityComment) .Include(m => m.ActivityComment.Activity) .Include(m => m.ActivityComment.Activity.Author) .Where(condition) .OrderByDescending(m => m.Unread) .ThenByDescending(m => m.Sid) .TakePage(page, RecordsPerPage) .ToListAsync(); var result = new PostOfficeMessageList(messages.Count); var markReadCount = 0; foreach (var m in messages) { var item = new PostOfficeMessage { Type = m.Type, CreateTime = m.CreateTime, Unread = m.Unread }; if (m.Type.IsMissiveMessage()) { item.Id = m.Id; } else { item.OperatorIdCode = m.Operator.IdCode; item.OperatorAvatarImage = m.Operator.AvatarImage; item.OperatorUserName = m.Operator.UserName; } if (!string.IsNullOrWhiteSpace(m.Reasons)) { item.Reasons = m.Reasons.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToList(); } if (m.ArticleId != null) { item.ArticleAuthorIdCode = m.Article.Author.IdCode; item.ArticleSidForAuthor = m.Article.SidForAuthor; item.ArticleTitle = CollapleArticleTitle(m.Article.Title); } else if (m.ActivityId != null) { item.ActivityAuthorIdCode = m.Activity.Author.IdCode; item.ActivitySidForAuthor = m.Activity.SidForAuthor; item.ActivityContent = CollapseActivityContent(m.Activity); } else if (m.ArticleCommentId != null) { item.CommentContent = CollapseCommentContent(m.ArticleComment.UnstyledContent); item.CommentSidForParent = m.ArticleComment.SidForArticle; item.ArticleAuthorIdCode = m.ArticleComment.Article.Author.IdCode; item.ArticleSidForAuthor = m.ArticleComment.Article.SidForAuthor; item.ArticleTitle = CollapleArticleTitle(m.ArticleComment.Article.Title); } else if (m.ActivityCommentId != null) { item.CommentContent = CollapseCommentContent(m.ActivityComment.Content); item.CommentSidForParent = m.ActivityComment.SidForActivity; item.ActivityAuthorIdCode = m.ActivityComment.Activity.Author.IdCode; item.ActivitySidForAuthor = m.ActivityComment.Activity.SidForAuthor; item.ActivityContent = CollapseActivityContent(m.ActivityComment.Activity); } if (m.Count > 0) { item.Count = m.Count; } if (m.SecondCount > 0) { item.SecondCount = m.SecondCount; } result.Add(item); if (m.Unread) { m.Unread = false; markReadCount++; } } await dbContext.SaveChangesAsync(KeylolDbContext.ConcurrencyStrategy.ClientWin); await cachedData.Messages.IncreaseUserUnreadMessageCountAsync(currentUserId, -markReadCount); NotificationProvider.Hub <MessageHub, IMessageHubClient>().User(currentUserId)? .OnUnreadCountChanged(await cachedData.Messages.GetUserUnreadMessageCountAsync(currentUserId)); var pageCount = 1; if (returnPageCount) { var totalCount = await dbContext.Messages.CountAsync(condition); pageCount = totalCount > 0 ? (int)Math.Ceiling(totalCount / (double)RecordsPerPage) : 1; } return(new Tuple <PostOfficeMessageList, int>(result, pageCount)); }