From 9a8168790e0d6b8601b0f7f5557976358677eeb1 Mon Sep 17 00:00:00 2001
From: schangxiang@126.com <schangxiang@126.com>
Date: 周一, 19 5月 2025 08:00:50 +0800
Subject: [PATCH] 22
---
Weben_CMS专用代码生成器/Code/Templete/EntityFrameworkCore/EfCoreRepository模板.txt | 121 +++++++++++++++++++++++++++++++++++++--
1 files changed, 113 insertions(+), 8 deletions(-)
diff --git "a/Weben_CMS\344\270\223\347\224\250\344\273\243\347\240\201\347\224\237\346\210\220\345\231\250/Code/Templete/EntityFrameworkCore/EfCoreRepository\346\250\241\346\235\277.txt" "b/Weben_CMS\344\270\223\347\224\250\344\273\243\347\240\201\347\224\237\346\210\220\345\231\250/Code/Templete/EntityFrameworkCore/EfCoreRepository\346\250\241\346\235\277.txt"
index f988f2f..efb086b 100644
--- "a/Weben_CMS\344\270\223\347\224\250\344\273\243\347\240\201\347\224\237\346\210\220\345\231\250/Code/Templete/EntityFrameworkCore/EfCoreRepository\346\250\241\346\235\277.txt"
+++ "b/Weben_CMS\344\270\223\347\224\250\344\273\243\347\240\201\347\224\237\346\210\220\345\231\250/Code/Templete/EntityFrameworkCore/EfCoreRepository\346\250\241\346\235\277.txt"
@@ -1,9 +1,10 @@
-using CMS.Plugin.$NameSpacePath$.Domain.$EntityName$;
+锘縰sing CMS.Plugin.$NameSpacePath$.Domain.$EntityName$;
using CMS.Plugin.$NameSpacePath$.EntityFrameworkCore.Extensions;
using CmsQueryExtensions.Extension;
using Microsoft.EntityFrameworkCore;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
+using Volo.Abp;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
@@ -11,7 +12,7 @@
namespace CMS.Plugin.$NameSpacePath$.EntityFrameworkCore.Repositories;
/// <summary>
-/// $ChinaComment$仓储实现
+/// $ChinaComment$浠撳偍瀹炵幇
/// </summary>
public class EfCore$EntityName$Repository : EfCoreRepository<ICMSPluginDbContext, $EntityName$, Guid>, I$EntityName$Repository
{
@@ -25,7 +26,7 @@
}
/// <summary>
- /// 按照名称查找$ChinaComment$
+ /// 鎸夌収鍚嶇О鏌ユ壘$ChinaComment$
/// </summary>
/// <param name="name"></param>
/// <param name="cancellationToken"></param>
@@ -40,9 +41,9 @@
}
/// <summary>
- /// 验证名称是否存在$ChinaComment$
+ /// 楠岃瘉鍚嶇О鏄惁瀛樺湪$ChinaComment$
/// </summary>
- /// <param name="name">校验值</param>
+ /// <param name="name">鏍¢獙鍊�</param>
/// <param name="id"></param>
/// <returns></returns>
public async Task<bool> NameExistAsync(string name, Guid? id = null)
@@ -51,7 +52,7 @@
}
/// <summary>
- /// 获取最大排序$ChinaComment$
+ /// 鑾峰彇鏈�澶ф帓搴�$ChinaComment$
/// </summary>
/// <returns></returns>
public async Task<int> GetMaxSortAsync()
@@ -69,7 +70,7 @@
}
/// <summary>
- /// 获取分页列表$ChinaComment$
+ /// 鑾峰彇鍒嗛〉鍒楄〃$ChinaComment$
/// </summary>
/// <param name="whereConditions"></param>
/// <param name="sorting"></param>
@@ -90,7 +91,7 @@
}
/// <summary>
- /// 获取总数$ChinaComment$
+ /// 鑾峰彇鎬绘暟$ChinaComment$
/// </summary>
/// <param name="whereConditions"></param>
/// <param name="cancellationToken"></param>
@@ -110,4 +111,108 @@
return (await GetQueryableAsync())
.Where(x => !x.IsDeleted).IncludeDetails();
}
+
+ /// <summary>
+ /// 鐗╃悊鍒犻櫎$ChinaComment$
+ /// </summary>
+ /// <param name="id">涓婚敭ID</param>
+ /// <param name="cancellationToken"></param>
+ /// <returns></returns>
+ public virtual async Task DeletePermanentlyAsync(Guid id, CancellationToken cancellationToken = default)
+ {
+ var entity = await (await GetDbSetAsync())
+ .FirstOrDefaultAsync(x => x.Id == id && !x.IsDeleted, GetCancellationToken(cancellationToken));
+
+ if (entity == null)
+ {
+ throw new Volo.Abp.Domain.Entities.EntityNotFoundException(typeof($EntityName$), id);
+ }
+
+ // 2. 鑾峰彇 DbContext 骞舵墽琛屽垹闄�
+ var dbContext = await GetDbContextAsync();
+
+ // 鐩存帴鎵ц SQL 鍒犻櫎
+ var sql = $"DELETE FROM $TableName$ WHERE Id ='{entity.Id.ToString()}'";
+ await dbContext.Database.ExecuteSqlRawAsync(sql, cancellationToken);
+ }
+
+ /// <summary>
+ /// 鎵归噺鐗╃悊鍒犻櫎$ChinaComment$锛堢洿鎺ュ垹闄わ紝涓嶈蒋鍒犻櫎锛�
+ /// </summary>
+ /// <param name="ids">瑕佸垹闄ょ殑涓婚敭ID鍒楄〃</param>
+ /// <param name="cancellationToken"></param>
+ /// <returns></returns>
+ public virtual async Task BatchDeletePermanentlyAsync(IEnumerable<Guid> ids, CancellationToken cancellationToken = default)
+ {
+ // 1. 鏌ヨ绗﹀悎鏉′欢鐨勫疄浣擄紙鏈蒋鍒犻櫎鐨勮褰曪級
+ var entities = await (await GetDbSetAsync())
+ .Where(x => ids.Contains(x.Id) && !x.IsDeleted)
+ .ToListAsync(GetCancellationToken(cancellationToken));
+
+ if (!entities.Any())
+ {
+ // 濡傛灉娌℃湁闇�瑕佸垹闄ょ殑璁板綍锛岀洿鎺ヨ繑鍥烇紙閬垮厤涓嶅繀瑕佺殑鏁版嵁搴撴搷浣滐級
+ return;
+ }
+
+ // 2. 鑾峰彇 DbContext 骞舵墽琛屾壒閲忓垹闄�
+ var dbContext = await GetDbContextAsync();
+
+ var idsToDelete = entities.Select(e => e.Id).ToList();
+
+ // 鐩存帴鎵ц SQL 鍒犻櫎
+ var sql = $"DELETE FROM $TableName$ WHERE Id IN ({string.Join(",", idsToDelete.Select(id => $"'{id}'"))})";
+ await dbContext.Database.ExecuteSqlRawAsync(sql, cancellationToken);
+ }
+
+ /// <summary>
+ /// 鏍规嵁鏉′欢鑾峰彇$ChinaComment$鍒楄〃
+ /// </summary>
+ /// <param name="whereConditions"></param>
+ /// <param name="cancellationToken"></param>
+ /// <returns></returns>
+ public async Task<List<$EntityName$>> GetListByFilterAsync(Expression<Func<$EntityName$, bool>> whereConditions, CancellationToken cancellationToken = default)
+ {
+ return await (await GetDbSetAsync())
+ .IncludeDetails()
+ .WhereIf(whereConditions != null, whereConditions)
+ .Where(x => !x.IsDeleted)
+ .OrderByDescending(x => x.$OrderBy$)
+ .ToListAsync(GetCancellationToken(cancellationToken));
+ }
+
+ /// <summary>
+ /// 鏍规嵁鏉′欢鑾峰彇鍗曚釜$ChinaComment$
+ /// </summary>
+ /// <param name="whereConditions"></param>
+ /// <param name="isMultipleThrowException">鏄惁鏌ヨ鍑哄鏉″氨鎶ラ敊</param>
+ /// <param name="cancellationToken"></param>
+ /// <returns></returns>
+ /// <exception cref="UserFriendlyException"></exception>
+ public async Task<$EntityName$> GetSingleByFilterAsync(Expression<Func<$EntityName$, bool>> whereConditions, bool is鈥婱ultipleThrowException = false, CancellationToken cancellationToken = default)
+ {
+ if (is鈥婱ultipleThrowException)
+ {
+ var entitys = await (await GetDbSetAsync())
+ .IncludeDetails()
+ .WhereIf(whereConditions != null, whereConditions)
+ .Where(x => !x.IsDeleted)
+ .OrderByDescending(x => x.$OrderBy$)
+ .ToListAsync(GetCancellationToken(cancellationToken));
+ if (entitys?.Count > 1)
+ {
+ throw new UserFriendlyException("鏌ヨ鍒板鏉¤褰�");
+ }
+ return entitys?.FirstOrDefault();
+ }
+ else
+ {
+ return await (await GetDbSetAsync())
+ .IncludeDetails()
+ .WhereIf(whereConditions != null, whereConditions)
+ .Where(x => !x.IsDeleted)
+ .OrderByDescending(x => x.$OrderBy$)
+ .FirstOrDefaultAsync(GetCancellationToken(cancellationToken));
+ }
+ }
}
--
Gitblit v1.9.3