From 95c55328a6c2429beace30f687b5b46d453efbf1 Mon Sep 17 00:00:00 2001
From: sunpengfei <i@angelzzz.com>
Date: 星期一, 18 八月 2025 10:24:42 +0800
Subject: [PATCH] feat:开发

---
 FlexJobApi.Core/Utils/DbUtils/DbUtils.cs |   57 +++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 53 insertions(+), 4 deletions(-)

diff --git a/FlexJobApi.Core/Utils/DbUtils/DbUtils.cs b/FlexJobApi.Core/Utils/DbUtils/DbUtils.cs
index 2e0f85f..d06c426 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>
@@ -326,13 +375,13 @@
             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);

--
Gitblit v1.9.1