zs
2025-05-14 800a94c4088b5ad82bdb433a9199dc2183d86959
Weben_CMSרÓôúÂëÉú³ÉÆ÷/Code/Templete/EntityFrameworkCore/EfCoreRepositoryÄ£°å.txt
@@ -1,9 +1,10 @@
using CMS.Plugin.$NameSpacePath$.Domain.$EntityName$;
using 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,105 @@
        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())
            .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​MultipleThrowException = false, CancellationToken cancellationToken = default)
    {
        if (is​MultipleThrowException)
        {
            var entitys = await (await GetDbSetAsync())
          .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())
          .WhereIf(whereConditions != null, whereConditions)
          .Where(x => !x.IsDeleted)
          .OrderByDescending(x => x.$OrderBy$)
          .FirstOrDefaultAsync(GetCancellationToken(cancellationToken));
        }
    }
}