+
Skip to content

upd query #530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 53 additions & 28 deletions src/Mix.Cms.Lib/ViewModels/MixPosts/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static async Task<RepositoryResponse<PaginationModel<TView>>> SearchPosts

if (valPredicate != null)
{
return await SearchPostByValue<TView>(valPredicate, searchPostData.PagingData, searchPostData.PostType, searchPostData.Specificulture, context, transaction);
return await SearchPostByValue<TView>(valPredicate, searchPostData, context, transaction);
}
else
{
Expand Down Expand Up @@ -94,17 +94,14 @@ private static RepositoryResponse<PaginationModel<TView>> GetSortedPostByPage<TV
&& p.PageId == searchPostData.PageId
&& allPostIds.Contains(p.PostId))
.OrderBy(p => p.Priority);
var posts = allPosts.Skip(searchPostData.PagingData.PageIndex * searchPostData.PagingData.PageSize)
var posts = allPosts
.Skip(searchPostData.PagingData.PageIndex * searchPostData.PagingData.PageSize)
.Take(searchPostData.PagingData.PageSize)
.Include(m => m.MixPost)
.Select(m => m.MixPost)
.ToList();
var total = allPosts.Count();
var totalPage = total / searchPostData.PagingData.PageSize;
if (total % searchPostData.PagingData.PageSize > 0)
{
totalPage += 1;
}
var totalPage = (int)Math.Ceiling((double)total / searchPostData.PagingData.PageSize);
return new RepositoryResponse<PaginationModel<TView>>()
{
IsSucceed = true,
Expand Down Expand Up @@ -175,31 +172,28 @@ private static Expression<Func<MixDatabaseDataValue, bool>> GetCategoryPredicate

private static async Task<RepositoryResponse<PaginationModel<TView>>> SearchPostByValue<TView>(
Expression<Func<MixDatabaseDataValue, bool>> valPredicate,
PagingRequest pagingData,
string postType,
string specificulture,
SearchPostQueryModel searchPostData,
MixCmsContext context,
IDbContextTransaction transaction)
where TView : ViewModelBase<MixCmsContext, MixPost, TView>
{
var allPostIds = IQueryableHelper.GetPostIdsByValue(
valPredicate,
context,
specificulture,
postType);
var resultIds = IQueryableHelper.SortParentIds(
allPostIds.Skip(pagingData.PageIndex * pagingData.PageSize)
.Take(pagingData.PageSize),
context,
pagingData,
specificulture,
postType)
.AsEnumerable()
.Select(p => int.Parse(p))
.ToList();
searchPostData.Specificulture,
searchPostData.PostType);

var resultIds = searchPostData.PagingData.OrderBy.StartsWith("additionalData.")
? GetSortedIdsByValue(allPostIds, context, searchPostData.PagingData, searchPostData.Specificulture, searchPostData.PostType)
: searchPostData.PageId.HasValue
? GetSortedIdsByPage(allPostIds, searchPostData, context, transaction)
: allPostIds.Skip(searchPostData.PagingData.PageIndex * searchPostData.PagingData.PageSize)
.Take(searchPostData.PagingData.PageSize)
.Select(p => int.Parse(p)).ToList();


var getPosts = (await DefaultRepository<MixCmsContext, MixPost, TView>.Instance.GetModelListByAsync(
m => resultIds.Any(p => p == m.Id) && m.Specificulture == specificulture,
m => resultIds.Any(p => p == m.Id) && m.Specificulture == searchPostData.Specificulture,
context,
transaction));
var items = getPosts.Data.OrderBy(
Expand All @@ -211,15 +205,41 @@ private static async Task<RepositoryResponse<PaginationModel<TView>>> SearchPost
Data = new PaginationModel<TView>()
{
Items = items,
PageIndex = pagingData.PageIndex,
PageSize = pagingData.PageSize,
PageIndex = searchPostData.PagingData.PageIndex,
PageSize = searchPostData.PagingData.PageSize,
TotalItems = total,
TotalPage = (int)Math.Ceiling((double)total / pagingData.PageSize)
TotalPage = (int)Math.Ceiling((double)total / searchPostData.PagingData.PageSize)
}
};
return result;
}

private static List<int> GetSortedIdsByPage(
IQueryable<string> allPostIds, SearchPostQueryModel searchPostData, MixCmsContext context, IDbContextTransaction transaction)
{
return context.MixPagePost.Where(p =>
p.Specificulture == searchPostData.Specificulture
&& p.PageId == searchPostData.PageId
&& allPostIds.Contains(p.PostId.ToString()))
.OrderBy(p => p.Priority)
.Skip(searchPostData.PagingData.PageIndex * searchPostData.PagingData.PageSize)
.Take(searchPostData.PagingData.PageSize)
.Select(p=>p.PostId)
.ToList();
}

private static List<int> GetSortedIdsByValue(IQueryable<string> allPostIds, MixCmsContext context, PagingRequest pagingData, string specificulture, string postType)
{
return IQueryableHelper.SortParentIds(allPostIds,
context,
pagingData,
specificulture,
postType)
.AsEnumerable()
.Select(p => int.Parse(p))
.ToList();
}

private static Expression<Func<MixPost, bool>> BuildPostExpression(SearchPostQueryModel searchPostData)
{
Expression<Func<MixPost, bool>> predicate = model => model.Specificulture == searchPostData.Specificulture;
Expand Down Expand Up @@ -251,8 +271,13 @@ public static async Task<RepositoryResponse<PaginationModel<TView>>> GetModelist
postType ??= MixDatabaseNames.ADDITIONAL_COLUMN_POST;

var valExp = Expressions.GetMetaExpression(metaName, metaValue, culture);

return await SearchPostByValue<TView>(valExp, pagingData, postType, culture, context, transaction);
var searchPostData = new SearchPostQueryModel()
{
PagingData = pagingData,
PostType = postType,
Specificulture = culture
};
return await SearchPostByValue<TView>(valExp, searchPostData, context, transaction);
}
catch (Exception ex)
{
Expand Down
7 changes: 1 addition & 6 deletions src/Mix.Cms.Lib/ViewModels/MixPosts/IQueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static IQueryable<string> GetPostIdsByValue(
&& m.ParentType == MixDatabaseParentType.Post;

var associations = context.MixDatabaseDataAssociation.Where(relatedExp);
var parentIds = associations.Select(m => m.ParentId);
var parentIds = associations.Select(m => m.ParentId).Distinct();


return parentIds;
Expand All @@ -42,11 +42,6 @@ public static IEnumerable<string> SortParentIds(
string culture,
string postType)
{
if (!pagingData.OrderBy.StartsWith("additionalData."))
{
return parentIds;
}

string orderCol = pagingData.OrderBy.Split('.')[1];
var sortQuery = context.MixDatabaseDataValue
.Where(
Expand Down
Binary file not shown.
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载