| | |
| | | var q = rep.AsQueryable().AsNoTracking(); |
| | | if (query != null) q = query(q); |
| | | else q = q.OrderBy(it => it.Sort).ThenBy(it => it.CreatedTime); |
| | | q = q.CustomOrderBy(page.OrderInput); |
| | | if (page.OrderInput.IsNotNull()) |
| | | q = q.CustomOrderBy(page.OrderInput); |
| | | var s = selector == null |
| | | ? q.ProjectToType<TItem>() |
| | | : q.Select(selector); |
| | | var pagedList = await s.ToPagedListAsync(page.Page, page.Rows, cancellationToken); |
| | | var result = new PagedListQueryResult<TItem>(); |
| | | result.PageModel = page.Adapt<PagedListQueryResultPageModel>(); |
| | | result.PageModel.TotalCount = pagedList.TotalCount; |
| | | result.PageModel.TotalPage = pagedList.TotalPages; |
| | | result.Data = pagedList.Items.ToList(); |
| | | return result; |
| | | } |
| | | /// <summary> |
| | | /// 查询分页列表数据 |
| | | /// </summary> |
| | | /// <typeparam name="TResult"></typeparam> |
| | | /// <typeparam name="TItem"></typeparam> |
| | | /// <param name="page"></param> |
| | | /// <param name="q"></param> |
| | | /// <param name="cancellationToken"></param> |
| | | /// <returns></returns> |
| | | public static async Task<TResult> GetPagedListAsync<TResult, TItem>( |
| | | this PagedListQueryPageModel page, |
| | | IQueryable<TItem> q, |
| | | CancellationToken cancellationToken = default) |
| | | where TItem : class, new() |
| | | where TResult : PagedListQueryResult<TItem>, new() |
| | | { |
| | | if (page.OrderInput.IsNotNull()) |
| | | q = q.CustomOrderBy(page.OrderInput); |
| | | var pagedList = await q.ToPagedListAsync(page.Page, page.Rows, cancellationToken); |
| | | var result = new TResult(); |
| | | result.PageModel = page.Adapt<PagedListQueryResultPageModel>(); |
| | | result.PageModel.TotalCount = pagedList.TotalCount; |
| | | result.PageModel.TotalPage = pagedList.TotalPages; |
| | |
| | | /// <param name="q"></param> |
| | | /// <param name="cancellationToken"></param> |
| | | /// <returns></returns> |
| | | public static async Task<PagedListQueryResult<TItem>> GetPagedListAsync<TItem>( |
| | | public static Task<PagedListQueryResult<TItem>> GetPagedListAsync<TItem>( |
| | | this PagedListQueryPageModel page, |
| | | IQueryable<TItem> q, |
| | | CancellationToken cancellationToken = default) |
| | | where TItem : class, new() |
| | | { |
| | | //q = q.CustomOrderBy(page.OrderInput); |
| | | var pagedList = await q.ToPagedListAsync(page.Page, page.Rows, cancellationToken); |
| | | var result = new PagedListQueryResult<TItem>(); |
| | | result.PageModel = page.Adapt<PagedListQueryResultPageModel>(); |
| | | result.PageModel.TotalCount = pagedList.TotalCount; |
| | | result.PageModel.TotalPage = pagedList.TotalPages; |
| | | result.Data = pagedList.Items.ToList(); |
| | | return result; |
| | | return GetPagedListAsync<PagedListQueryResult<TItem>, TItem>(page, q, cancellationToken); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 排序 |
| | | /// </summary> |
| | | /// <typeparam name="T"></typeparam> |
| | | /// <param name="q"></param> |
| | | /// <param name="orders"></param> |
| | | /// <returns></returns> |
| | | public static IQueryable<T> CustomOrderBy<T>( |
| | | this IQueryable<T> q, |
| | | List<PagedListQueryPageModelOrderInput> orders) |
| | | public static IOrderedQueryable<T> CustomOrderBy<T>(this IQueryable<T> q, List<PagedListQueryPageModelOrderInput> orders) |
| | | { |
| | | if (orders.IsNull()) return q; |
| | | |
| | | var entityType = typeof(T); |
| | | int index = 0; |
| | | |
| | | var props = entityType.GetProperties(); |
| | | foreach (var order in orders) |
| | | ParameterExpression parameter = Expression.Parameter(typeof(T), "p"); |
| | | PagedListQueryPageModelOrderInput orderInput = orders[0]; |
| | | IOrderedQueryable<T> orderedQueryable = (orderInput.Order == EnumPagedListOrder.Asc) |
| | | ? OrderBy(q, orderInput.Property, parameter) |
| | | : OrderByDescending(q, orderInput.Property, parameter); |
| | | for (int i = 1; i < orders.Count; i++) |
| | | { |
| | | if (string.IsNullOrEmpty(order.Property)) continue; |
| | | |
| | | // 获取排序字段的属性信息 |
| | | var propertyInfo = props.FirstOrDefault(it => it.Name.Equals(order.Property, StringComparison.OrdinalIgnoreCase)); |
| | | if (propertyInfo == null) throw Oops.Oh(EnumErrorCodeType.s404, $"该排序字段{order.Property}"); |
| | | |
| | | // 创建表达式树 |
| | | var parameter = Expression.Parameter(entityType, "x"); |
| | | var propertyAccess = Expression.Property(parameter, propertyInfo); |
| | | var lambda = Expression.Lambda(propertyAccess, parameter); |
| | | |
| | | string methodName; |
| | | if (index == 0) |
| | | { |
| | | // 首次排序 |
| | | methodName = order.Order == EnumPagedListOrder.Asc |
| | | ? "OrderBy" |
| | | : "OrderByDescending"; |
| | | } |
| | | else |
| | | { |
| | | // 二次及以后排序 |
| | | methodName = order.Order == EnumPagedListOrder.Asc |
| | | ? "ThenBy" |
| | | : "ThenByDescending"; |
| | | } |
| | | |
| | | // 调用相应的排序方法 |
| | | var resultExpression = Expression.Call( |
| | | typeof(Queryable), |
| | | methodName, |
| | | [entityType, propertyInfo.PropertyType], |
| | | q.Expression, |
| | | Expression.Quote(lambda) |
| | | ); |
| | | |
| | | q = q.Provider.CreateQuery<T>(resultExpression); |
| | | index++; |
| | | PagedListQueryPageModelOrderInput orderInput2 = orders[i]; |
| | | orderedQueryable = (orderInput2.Order == EnumPagedListOrder.Asc) |
| | | ? ThenBy(orderedQueryable, orderInput2.Property, parameter) |
| | | : ThenByDescending(orderedQueryable, orderInput2.Property, parameter); |
| | | } |
| | | |
| | | return q; |
| | | return orderedQueryable; |
| | | } |
| | | |
| | | private static IOrderedQueryable<T> Ordering<T>(IQueryable<T> source, ParameterExpression parameter, string propertyName, string methodName) |
| | | { |
| | | Type typeFromHandle = typeof(T); |
| | | MemberExpression memberExpression = Expression.PropertyOrField(parameter, propertyName); |
| | | LambdaExpression expression = Expression.Lambda(memberExpression, parameter); |
| | | MethodCallExpression expression2 = Expression.Call(typeof(Queryable), methodName, [typeFromHandle, memberExpression.Type], source.Expression, Expression.Quote(expression)); |
| | | return (IOrderedQueryable<T>)source.Provider.CreateQuery<T>(expression2); |
| | | } |
| | | |
| | | public static IOrderedQueryable<T> OrderBy<T>(IQueryable<T> source, string propertyName, ParameterExpression parameter) |
| | | { |
| | | return Ordering(source, parameter, propertyName, "OrderBy"); |
| | | } |
| | | |
| | | public static IOrderedQueryable<T> OrderByDescending<T>(IQueryable<T> source, string propertyName, ParameterExpression parameter) |
| | | { |
| | | return Ordering(source, parameter, propertyName, "OrderByDescending"); |
| | | } |
| | | |
| | | public static IOrderedQueryable<T> ThenBy<T>(IOrderedQueryable<T> source, string propertyName, ParameterExpression parameter) |
| | | { |
| | | return Ordering(source, parameter, propertyName, "ThenBy"); |
| | | } |
| | | |
| | | public static IOrderedQueryable<T> ThenByDescending<T>(IOrderedQueryable<T> source, string propertyName, ParameterExpression parameter) |
| | | { |
| | | return Ordering(source, parameter, propertyName, "ThenByDescending"); |
| | | } |
| | | |
| | | public static async Task<TResult> GetDetail<TEntity, TResult>( |
| | | this Guid id, |
| | | CancellationToken cancellationToken = default) |
| | | where TEntity : CommonEntity, new() |
| | | { |
| | | var rep = Db.GetRepository<TEntity>(); |
| | | return await rep.AsQueryable().AsNoTracking() |
| | | .Where(it => it.Id == id) |
| | | .GetDetail<TEntity, TResult>(cancellationToken); |
| | | } |
| | | |
| | | public static async Task<TResult> GetDetail<TEntity, TResult>( |
| | | this IQueryable<TEntity> q, |
| | | CancellationToken cancellationToken = default) |
| | | { |
| | | var model = await q |
| | | .ProjectToType<TResult>() |
| | | .FirstOrDefaultAsync(cancellationToken); |
| | | if (model == null) |
| | | { |
| | | var summary = await typeof(TEntity).GetSummary(); |
| | | throw Oops.Oh(EnumErrorCodeType.s404, $"{summary ?? "信息"}"); |
| | | } |
| | | return model; |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | : 0; |
| | | } |
| | | |
| | | public static async Task<Guid> UpdateData<TEntity, TRequest>( |
| | | this IQueryable<TEntity> q, |
| | | TRequest request, |
| | | Action<TEntity> update = null, |
| | | CancellationToken cancellationToken = default) |
| | | where TEntity : CommonEntity, new() |
| | | { |
| | | var rep = Db.GetRepository<TEntity>(); |
| | | var entity = await q.FirstOrDefaultAsync(); |
| | | if (entity == null) |
| | | { |
| | | var summary = await typeof(TEntity).GetSummary(); |
| | | throw Oops.Oh(EnumErrorCodeType.s404, $"{summary ?? "信息"}"); |
| | | } |
| | | |
| | | if (update != null) update(entity); |
| | | else request.Adapt(entity); |
| | | await rep.UpdateAsync(entity); |
| | | return entity.Id; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 保存数据 |
| | | /// </summary> |
| | |
| | | var summary = await typeof(TEntity).GetSummary(xmlDoc); |
| | | var rep = Db.GetRepository<TEntity>(); |
| | | if (checkExist != null && await rep.AsQueryable().AsNoTracking().AnyAsync(checkExist)) |
| | | throw Oops.Oh(EnumErrorCodeType.s405, $"该{summary ?? "信息"}"); |
| | | throw Oops.Oh(EnumErrorCodeType.s405, $"{summary ?? "信息"}"); |
| | | if (request.Id.HasValue) |
| | | { |
| | | var q = rep.AsQueryable(); |
| | | if (query != null) q = query(q); |
| | | var entity = await q.FirstOrDefaultAsync(it => it.Id == request.Id, cancellationToken); |
| | | if (entity == null) throw Oops.Oh(EnumErrorCodeType.s404, $"该{summary ?? "信息"}"); |
| | | if (entity == null) throw Oops.Oh(EnumErrorCodeType.s404, $"{summary ?? "信息"}"); |
| | | if (update != null) update(entity); |
| | | else request.Adapt(entity); |
| | | await rep.UpdateAsync(entity); |