From ed5ea9ae4c2e1a9f114e2302a9b3ca20168535c8 Mon Sep 17 00:00:00 2001 From: sunpengfei <i@angelzzz.com> Date: 星期二, 12 八月 2025 09:55:56 +0800 Subject: [PATCH] feat:开发 --- FlexJobApi.Core/Utils/DbUtils/DbUtils.cs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 51 insertions(+), 2 deletions(-) diff --git a/FlexJobApi.Core/Utils/DbUtils/DbUtils.cs b/FlexJobApi.Core/Utils/DbUtils/DbUtils.cs index 2e0f85f..2e1639d 100644 --- a/FlexJobApi.Core/Utils/DbUtils/DbUtils.cs +++ b/FlexJobApi.Core/Utils/DbUtils/DbUtils.cs @@ -148,7 +148,8 @@ 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); @@ -176,7 +177,8 @@ where TItem : class, new() where TResult : PagedListQueryResult<TItem>, new() { - q = q.CustomOrderBy(page.OrderInput); + 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>(); @@ -250,6 +252,32 @@ 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> /// 璁剧疆鏄惁绂佺敤 /// </summary> @@ -302,6 +330,27 @@ : 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> -- Gitblit v1.9.1