Beispiel #1
0
        private IQueryable <Notification> CreateQuery(XM.NotificationPageSearchCriteria criteria)
        {
            IQueryable <Notification> query = _context.Notification;

            query = query.WhereIf(criteria.FromUserId.HasValue, m => m.FromUserId == criteria.FromUserId);
            query = query.WhereIf(criteria.ToUserId.HasValue, m => m.ToUserId == criteria.ToUserId);
            if (criteria.CreationTimeBegin.HasValue)
            {
                var begin = criteria.CreationTimeBegin.Value.Date;
                query = query.Where(m => m.CreationTime >= begin);
            }
            if (criteria.CreationTimeEnd.HasValue)
            {
                var end = criteria.CreationTimeEnd.Value.Date.AddDays(1);
                query = query.Where(m => m.CreationTime < end);
            }
            if (criteria.Keyword != null)
            {
                var keyword = criteria.Keyword.Trim();
                if (keyword.Length != 0)
                {
                    query = query.Where(m => m.Title.Contains(keyword));
                }
            }
            return(query);
        }
Beispiel #2
0
        /// <summary>
        /// GetPageAsync
        /// </summary>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public async Task <Page <XM.NotificationUser> > GetPageAsync(XM.NotificationPageSearchCriteria criteria)
        {
            if (criteria.ToUserId.HasValue)
            {
                return(await GetNotificationUserPageAsync(criteria));
            }

            // 备注:忽略搜索条件的 IsReaded, ToUserId
            // 备注:因为查询所有 ToUserId, 所有不会标记已读未读

            IQueryable <Notification>        query = CreateQuery(criteria);
            IOrderedQueryable <Notification> orderedQuery;

            if (criteria.PagingInfo.SortInfo != null && !criteria.PagingInfo.SortInfo.Sort.IsNullOrWhiteSpace())
            {
                orderedQuery = query.Order(criteria.PagingInfo.SortInfo.Sort, criteria.PagingInfo.SortInfo.SortDir == SortDir.DESC);
            }
            else
            {
                // 默认排序
                orderedQuery = query.OrderByDescending(m => m.NotificationId);
            }

            var page = await orderedQuery.Select(_notificationUserSelector).GetPageAsync(criteria.PagingInfo);

            return(page);
        }
Beispiel #3
0
        /// <summary>
        /// GetNotificationUserPageAsync
        /// </summary>
        /// <param name="criteria"></param>
        /// <returns></returns>
        private async Task <Page <XM.NotificationUser> > GetNotificationUserPageAsync(XM.NotificationPageSearchCriteria criteria)
        {
            if (!criteria.ToUserId.HasValue)
            {
                throw new ArgumentNullException(nameof(criteria.ToUserId), "必须输入 ToUserId");
            }
            var userCreationTime = await _context.User.AsNoTracking().Where(m => m.UserId == criteria.ToUserId.Value).Select(m => m.CreationTime).FirstOrDefaultAsync();

            // 备注:查询发送给所有人的以及本人的、未删除的记录
            var query1 = CreateQuery(criteria);

            query1 = from n in query1
                     where n.CreationTime > userCreationTime && (!n.ToUserId.HasValue || n.ToUserId == criteria.ToUserId.Value)
                     select n;

            // 剔除已逻辑删除的记录
            var query2 = from m in query1
                         from pu in m.NotificationUser.Where(n => n.UserId == criteria.ToUserId.Value).DefaultIfEmpty()
                         where pu == null || !pu.DeleteTime.HasValue
                         //join pu in m.NotificationUsers.Where(n=>n.UserId == criteria.ToUserId.Value) on m equals pu.Notification into purd
                         //from x in purd.DefaultIfEmpty()
                         //where x == null || !x.DeleteTime.HasValue
                         select new XM.NotificationUser
            {
                NotificationId = m.NotificationId,
                FromUser       = new XM.UserInfoWarpper
                {
                    UserId      = m.FromUserId.HasValue ? m.FromUser.UserId : 0,
                    Username    = m.FromUserId.HasValue ? m.FromUser.Username : "",
                    DisplayName = m.FromUserId.HasValue ? m.FromUser.DisplayName : "",
                    HeadUrl     = m.FromUserId.HasValue ? m.FromUser.HeadUrl : "",
                    LogoUrl     = m.FromUserId.HasValue ? m.FromUser.LogoUrl : "",
                },
                ToUser = new XM.UserInfoWarpper
                {
                    UserId      = m.ToUserId.HasValue ? m.ToUser.UserId : 0,
                    Username    = m.ToUserId.HasValue ? m.ToUser.Username : "",
                    DisplayName = m.ToUserId.HasValue ? m.ToUser.DisplayName : "",
                    HeadUrl     = m.ToUserId.HasValue ? m.ToUser.HeadUrl : "",
                    LogoUrl     = m.ToUserId.HasValue ? m.ToUser.LogoUrl : "",
                },
                Title        = m.Title,
                Message      = m.Message,
                Url          = m.Url,
                CreationTime = m.CreationTime,

                ReadTime   = pu != null ? pu.ReadTime : null,
                DeleteTime = pu != null ? pu.DeleteTime : null,
            };

            if (criteria.IsReaded.HasValue)
            {
                if (criteria.IsReaded.Value)
                {
                    // 备注,读取已读,也可通过用户的 NotificationsToUser 取
                    query2 = query2.Where(m => m.ReadTime.HasValue);
                }
                else
                {
                    query2 = query2.Where(m => !m.ReadTime.HasValue);
                }
            }

            IOrderedQueryable <XM.NotificationUser> orderedQuery;

            if (criteria.PagingInfo.SortInfo != null && !criteria.PagingInfo.SortInfo.Sort.IsNullOrWhiteSpace())
            {
                orderedQuery = query2.Order(criteria.PagingInfo.SortInfo.Sort, criteria.PagingInfo.SortInfo.SortDir == SortDir.DESC);
            }
            else
            {
                // 默认排序
                orderedQuery = query2.OrderByDescending(m => m.NotificationId);
            }

            var page = await orderedQuery.GetPageAsync(criteria.PagingInfo);

            return(page);
        }