Merge branch 'abp' into card-flip

# Conflicts:
#	Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Repositories/SqlSugarRepository.cs
#	Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Caches/FileCacheItem.cs
This commit is contained in:
ccnetcore
2025-11-17 00:43:27 +08:00
2 changed files with 45 additions and 91 deletions

View File

@@ -2,13 +2,11 @@
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Text; using System.Text;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Nito.AsyncEx; using Nito.AsyncEx;
using SqlSugar; using SqlSugar;
using Volo.Abp; using Volo.Abp;
using Volo.Abp.Auditing; using Volo.Abp.Auditing;
using Volo.Abp.Data; using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories; using Volo.Abp.Domain.Repositories;
using Volo.Abp.Linq; using Volo.Abp.Linq;
@@ -17,18 +15,14 @@ using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.SqlSugarCore.Repositories namespace Yi.Framework.SqlSugarCore.Repositories
{ {
public class SqlSugarRepository<TEntity> : ISqlSugarRepository<TEntity>, IRepository<TEntity> public class SqlSugarRepository<TEntity> : ISqlSugarRepository<TEntity>, IRepository<TEntity> where TEntity : class, IEntity, new()
where TEntity : class, IEntity, new()
{ {
public ISqlSugarClient _Db => AsyncContext.Run(async () => await GetDbContextAsync()); public ISqlSugarClient _Db => AsyncContext.Run(async () => await GetDbContextAsync());
public ISugarQueryable<TEntity> _DbQueryable => _Db.Queryable<TEntity>(); public ISugarQueryable<TEntity> _DbQueryable => _Db.Queryable<TEntity>();
private readonly ISugarDbContextProvider<ISqlSugarDbContext> _dbContextProvider; private readonly ISugarDbContextProvider<ISqlSugarDbContext> _dbContextProvider;
public IAbpLazyServiceProvider LazyServiceProvider { get; set; }
protected DbConnOptions? Options => LazyServiceProvider?.LazyGetService<IOptions<DbConnOptions>>().Value;
/// <summary> /// <summary>
/// 异步查询执行器 /// 异步查询执行器
/// </summary> /// </summary>
@@ -64,26 +58,22 @@ namespace Yi.Framework.SqlSugarCore.Repositories
#region Abp模块 #region Abp模块
public virtual async Task<TEntity?> FindAsync(Expression<Func<TEntity, bool>> predicate, public virtual async Task<TEntity?> FindAsync(Expression<Func<TEntity, bool>> predicate, bool includeDetails = true, CancellationToken cancellationToken = default)
bool includeDetails = true, CancellationToken cancellationToken = default)
{ {
return await GetFirstAsync(predicate); return await GetFirstAsync(predicate);
} }
public virtual async Task<TEntity> GetAsync(Expression<Func<TEntity, bool>> predicate, public virtual async Task<TEntity> GetAsync(Expression<Func<TEntity, bool>> predicate, bool includeDetails = true, CancellationToken cancellationToken = default)
bool includeDetails = true, CancellationToken cancellationToken = default)
{ {
return await GetFirstAsync(predicate); return await GetFirstAsync(predicate);
} }
public virtual async Task DeleteAsync(Expression<Func<TEntity, bool>> predicate, bool autoSave = false, public virtual async Task DeleteAsync(Expression<Func<TEntity, bool>> predicate, bool autoSave = false, CancellationToken cancellationToken = default)
CancellationToken cancellationToken = default)
{ {
await this.DeleteAsync(predicate); await this.DeleteAsync(predicate);
} }
public virtual async Task DeleteDirectAsync(Expression<Func<TEntity, bool>> predicate, public virtual async Task DeleteDirectAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default)
CancellationToken cancellationToken = default)
{ {
await this.DeleteAsync(predicate); await this.DeleteAsync(predicate);
} }
@@ -113,71 +103,60 @@ namespace Yi.Framework.SqlSugarCore.Repositories
throw new NotImplementedException(); throw new NotImplementedException();
} }
public virtual async Task<List<TEntity>> GetListAsync(Expression<Func<TEntity, bool>> predicate, public virtual async Task<List<TEntity>> GetListAsync(Expression<Func<TEntity, bool>> predicate, bool includeDetails = false, CancellationToken cancellationToken = default)
bool includeDetails = false, CancellationToken cancellationToken = default)
{ {
return await GetListAsync(predicate); return await GetListAsync(predicate);
} }
public virtual async Task<TEntity> InsertAsync(TEntity entity, bool autoSave = false, public virtual async Task<TEntity> InsertAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
CancellationToken cancellationToken = default)
{ {
return await InsertReturnEntityAsync(entity); return await InsertReturnEntityAsync(entity);
} }
public virtual async Task InsertManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, public virtual async Task InsertManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default)
CancellationToken cancellationToken = default)
{ {
await InsertRangeAsync(entities.ToList()); await InsertRangeAsync(entities.ToList());
} }
public virtual async Task<TEntity> UpdateAsync(TEntity entity, bool autoSave = false, public virtual async Task<TEntity> UpdateAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
CancellationToken cancellationToken = default)
{ {
await UpdateAsync(entity); await UpdateAsync(entity);
return entity; return entity;
} }
public virtual async Task UpdateManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, public virtual async Task UpdateManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default)
CancellationToken cancellationToken = default)
{ {
await UpdateRangeAsync(entities.ToList()); await UpdateRangeAsync(entities.ToList());
} }
public virtual async Task DeleteAsync(TEntity entity, bool autoSave = false, public virtual async Task DeleteAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
CancellationToken cancellationToken = default)
{ {
await DeleteAsync(entity); await DeleteAsync(entity);
} }
public virtual async Task DeleteManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, public virtual async Task DeleteManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default)
CancellationToken cancellationToken = default)
{ {
await DeleteAsync(entities.ToList()); await DeleteAsync(entities.ToList());
} }
public virtual async Task<List<TEntity>> GetListAsync(bool includeDetails = false, public virtual async Task<List<TEntity>> GetListAsync(bool includeDetails = false, CancellationToken cancellationToken = default)
CancellationToken cancellationToken = default)
{ {
return await GetListAsync(); return await GetListAsync();
} }
public virtual async Task<long> GetCountAsync(CancellationToken cancellationToken = default) public virtual async Task<long> GetCountAsync(CancellationToken cancellationToken = default)
{ {
return await this.CountAsync(_ => true); return await this.CountAsync(_=>true);
} }
public virtual async Task<List<TEntity>> GetPagedListAsync(int skipCount, int maxResultCount, string sorting, public virtual async Task<List<TEntity>> GetPagedListAsync(int skipCount, int maxResultCount, string sorting, bool includeDetails = false, CancellationToken cancellationToken = default)
bool includeDetails = false, CancellationToken cancellationToken = default)
{ {
return await GetPageListAsync(_ => true, skipCount, maxResultCount); return await GetPageListAsync(_ => true, skipCount, maxResultCount);
} }
#endregion #endregion
#region DB快捷操作 #region DB快捷操作
public virtual async Task<IDeleteable<TEntity>> AsDeleteable() public virtual async Task<IDeleteable<TEntity>> AsDeleteable()
{ {
return (await GetDbSimpleClientAsync()).AsDeleteable(); return (await GetDbSimpleClientAsync()).AsDeleteable();
@@ -192,7 +171,7 @@ namespace Yi.Framework.SqlSugarCore.Repositories
{ {
return (await GetDbSimpleClientAsync()).AsInsertable(insertObj); return (await GetDbSimpleClientAsync()).AsInsertable(insertObj);
} }
public virtual async Task<IInsertable<TEntity>> AsInsertable(TEntity[] insertObjs) public virtual async Task<IInsertable<TEntity>> AsInsertable(TEntity[] insertObjs)
{ {
return (await GetDbSimpleClientAsync()).AsInsertable(insertObjs); return (await GetDbSimpleClientAsync()).AsInsertable(insertObjs);
@@ -232,11 +211,9 @@ namespace Yi.Framework.SqlSugarCore.Repositories
{ {
return (await GetDbSimpleClientAsync()).AsUpdateable(updateObjs); return (await GetDbSimpleClientAsync()).AsUpdateable(updateObjs);
} }
#endregion #endregion
#region SimpleClient模块 #region SimpleClient模块
public virtual async Task<int> CountAsync(Expression<Func<TEntity, bool>> whereExpression) public virtual async Task<int> CountAsync(Expression<Func<TEntity, bool>> whereExpression)
{ {
return await (await GetDbSimpleClientAsync()).CountAsync(whereExpression); return await (await GetDbSimpleClientAsync()).CountAsync(whereExpression);
@@ -253,6 +230,7 @@ namespace Yi.Framework.SqlSugarCore.Repositories
{ {
return await (await GetDbSimpleClientAsync()).DeleteAsync(deleteObj); return await (await GetDbSimpleClientAsync()).DeleteAsync(deleteObj);
} }
} }
public virtual async Task<bool> DeleteAsync(List<TEntity> deleteObjs) public virtual async Task<bool> DeleteAsync(List<TEntity> deleteObjs)
@@ -272,13 +250,13 @@ namespace Yi.Framework.SqlSugarCore.Repositories
{ {
if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity))) if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)))
{ {
return await (await GetDbSimpleClientAsync()).AsUpdateable() return await (await GetDbSimpleClientAsync()).AsUpdateable().SetColumns(nameof(ISoftDelete.IsDeleted), true).Where(whereExpression).ExecuteCommandAsync() > 0;
.SetColumns(nameof(ISoftDelete.IsDeleted), true).Where(whereExpression).ExecuteCommandAsync() > 0;
} }
else else
{ {
return await (await GetDbSimpleClientAsync()).DeleteAsync(whereExpression); return await (await GetDbSimpleClientAsync()).DeleteAsync(whereExpression);
} }
} }
public virtual async Task<bool> DeleteByIdAsync(dynamic id) public virtual async Task<bool> DeleteByIdAsync(dynamic id)
@@ -286,11 +264,7 @@ namespace Yi.Framework.SqlSugarCore.Repositories
if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity))) if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)))
{ {
var entity = await GetByIdAsync(id); var entity = await GetByIdAsync(id);
if (entity is null) if (entity == null) return false;
{
return false;
}
//反射赋值 //反射赋值
ReflexHelper.SetModelValue(nameof(ISoftDelete.IsDeleted), true, entity); ReflexHelper.SetModelValue(nameof(ISoftDelete.IsDeleted), true, entity);
return await UpdateAsync(entity); return await UpdateAsync(entity);
@@ -311,7 +285,6 @@ namespace Yi.Framework.SqlSugarCore.Repositories
{ {
return false; return false;
} }
//反射赋值 //反射赋值
entities.ForEach(e => ReflexHelper.SetModelValue(nameof(ISoftDelete.IsDeleted), true, e)); entities.ForEach(e => ReflexHelper.SetModelValue(nameof(ISoftDelete.IsDeleted), true, e));
return await UpdateRangeAsync(entities); return await UpdateRangeAsync(entities);
@@ -320,6 +293,7 @@ namespace Yi.Framework.SqlSugarCore.Repositories
{ {
return await (await GetDbSimpleClientAsync()).DeleteByIdAsync(ids); return await (await GetDbSimpleClientAsync()).DeleteByIdAsync(ids);
} }
} }
public virtual async Task<TEntity> GetByIdAsync(dynamic id) public virtual async Task<TEntity> GetByIdAsync(dynamic id)
@@ -328,6 +302,7 @@ namespace Yi.Framework.SqlSugarCore.Repositories
} }
public virtual async Task<TEntity> GetFirstAsync(Expression<Func<TEntity, bool>> whereExpression) public virtual async Task<TEntity> GetFirstAsync(Expression<Func<TEntity, bool>> whereExpression)
{ {
return await (await GetDbSimpleClientAsync()).GetFirstAsync(whereExpression); return await (await GetDbSimpleClientAsync()).GetFirstAsync(whereExpression);
@@ -343,19 +318,14 @@ namespace Yi.Framework.SqlSugarCore.Repositories
return await (await GetDbSimpleClientAsync()).GetListAsync(whereExpression); return await (await GetDbSimpleClientAsync()).GetListAsync(whereExpression);
} }
public virtual async Task<List<TEntity>> GetPageListAsync(Expression<Func<TEntity, bool>> whereExpression, public virtual async Task<List<TEntity>> GetPageListAsync(Expression<Func<TEntity, bool>> whereExpression, int pageNum, int pageSize)
int pageNum, int pageSize)
{ {
return await (await GetDbSimpleClientAsync()).GetPageListAsync(whereExpression, return await (await GetDbSimpleClientAsync()).GetPageListAsync(whereExpression, new PageModel() { PageIndex = pageNum, PageSize = pageSize });
new PageModel() { PageIndex = pageNum, PageSize = pageSize });
} }
public virtual async Task<List<TEntity>> GetPageListAsync(Expression<Func<TEntity, bool>> whereExpression, public virtual async Task<List<TEntity>> GetPageListAsync(Expression<Func<TEntity, bool>> whereExpression, int pageNum, int pageSize, Expression<Func<TEntity, object>>? orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
int pageNum, int pageSize, Expression<Func<TEntity, object>>? orderByExpression = null,
OrderByType orderByType = OrderByType.Asc)
{ {
return await (await GetDbSimpleClientAsync()).GetPageListAsync(whereExpression, return await (await GetDbSimpleClientAsync()).GetPageListAsync(whereExpression, new PageModel { PageIndex = pageNum, PageSize = pageSize }, orderByExpression, orderByType);
new PageModel { PageIndex = pageNum, PageSize = pageSize }, orderByExpression, orderByType);
} }
public virtual async Task<TEntity> GetSingleAsync(Expression<Func<TEntity, bool>> whereExpression) public virtual async Task<TEntity> GetSingleAsync(Expression<Func<TEntity, bool>> whereExpression)
@@ -410,40 +380,30 @@ namespace Yi.Framework.SqlSugarCore.Repositories
public virtual async Task<bool> UpdateAsync(TEntity updateObj) public virtual async Task<bool> UpdateAsync(TEntity updateObj)
{ {
if (typeof(TEntity).IsAssignableTo<IHasConcurrencyStamp>()) //带版本号乐观锁更新 if (typeof(TEntity).IsAssignableTo<IHasConcurrencyStamp>())//带版本号乐观锁更新
{ {
if (Options is not null && Options.EnabledConcurrencyException) try
{ {
try int num = await (await GetDbSimpleClientAsync())
{ .Context.Updateable(updateObj).ExecuteCommandWithOptLockAsync(true);
int num = await (await GetDbSimpleClientAsync()) return num>0;
.Context.Updateable(updateObj).ExecuteCommandWithOptLockAsync(true);
return num > 0;
}
catch (VersionExceptions ex)
{
throw new AbpDbConcurrencyException(
$"{ex.Message}[更新失败ConcurrencyStamp不是最新版本],entityInfo{updateObj}", ex);
}
} }
else catch (VersionExceptions ex)
{ {
int num = await (await GetDbSimpleClientAsync())
.Context.Updateable(updateObj).ExecuteCommandAsync(); throw new AbpDbConcurrencyException($"{ex.Message}[更新失败ConcurrencyStamp不是最新版本],entityInfo{updateObj}", ex);
return num > 0;
} }
} }
return await (await GetDbSimpleClientAsync()).UpdateAsync(updateObj); return await (await GetDbSimpleClientAsync()).UpdateAsync(updateObj);
} }
public virtual async Task<bool> UpdateAsync(Expression<Func<TEntity, TEntity>> columns, public virtual async Task<bool> UpdateAsync(Expression<Func<TEntity, TEntity>> columns, Expression<Func<TEntity, bool>> whereExpression)
Expression<Func<TEntity, bool>> whereExpression)
{ {
return await (await GetDbSimpleClientAsync()).UpdateAsync(columns, whereExpression); return await (await GetDbSimpleClientAsync()).UpdateAsync(columns, whereExpression);
} }
public virtual async Task<bool> UpdateRangeAsync(List<TEntity> updateObjs) public virtual async Task<bool> UpdateRangeAsync(List<TEntity> updateObjs)
{ {
return await (await GetDbSimpleClientAsync()).UpdateRangeAsync(updateObjs); return await (await GetDbSimpleClientAsync()).UpdateRangeAsync(updateObjs);
@@ -452,36 +412,30 @@ namespace Yi.Framework.SqlSugarCore.Repositories
#endregion #endregion
} }
public class SqlSugarRepository<TEntity, TKey> : SqlSugarRepository<TEntity>, ISqlSugarRepository<TEntity, TKey>, public class SqlSugarRepository<TEntity, TKey> : SqlSugarRepository<TEntity>, ISqlSugarRepository<TEntity, TKey>, IRepository<TEntity, TKey> where TEntity : class, IEntity<TKey>, new()
IRepository<TEntity, TKey> where TEntity : class, IEntity<TKey>, new()
{ {
public SqlSugarRepository(ISugarDbContextProvider<ISqlSugarDbContext> dbContextProvider) : base( public SqlSugarRepository(ISugarDbContextProvider<ISqlSugarDbContext> sugarDbContextProvider) : base(sugarDbContextProvider)
dbContextProvider)
{ {
} }
public virtual async Task DeleteAsync(TKey id, bool autoSave = false, public virtual async Task DeleteAsync(TKey id, bool autoSave = false, CancellationToken cancellationToken = default)
CancellationToken cancellationToken = default)
{ {
await DeleteByIdAsync(id); await DeleteByIdAsync(id);
} }
public virtual async Task DeleteManyAsync(IEnumerable<TKey> ids, bool autoSave = false, public virtual async Task DeleteManyAsync(IEnumerable<TKey> ids, bool autoSave = false, CancellationToken cancellationToken = default)
CancellationToken cancellationToken = default)
{ {
await DeleteByIdsAsync(ids.Select(x => (object)x).ToArray()); await DeleteByIdsAsync(ids.Select(x => (object)x).ToArray());
} }
public virtual async Task<TEntity?> FindAsync(TKey id, bool includeDetails = true, public virtual async Task<TEntity?> FindAsync(TKey id, bool includeDetails = true, CancellationToken cancellationToken = default)
CancellationToken cancellationToken = default)
{ {
return await GetByIdAsync(id); return await GetByIdAsync(id);
} }
public virtual async Task<TEntity> GetAsync(TKey id, bool includeDetails = true, public virtual async Task<TEntity> GetAsync(TKey id, bool includeDetails = true, CancellationToken cancellationToken = default)
CancellationToken cancellationToken = default)
{ {
return await GetByIdAsync(id); return await GetByIdAsync(id);
} }
} }
} }

View File

@@ -1,4 +1,4 @@
namespace Yi.Framework.Rbac.Domain.Shared.Caches; namespace Yi.Framework.Rbac.Domain.Shared.Caches;
public class FileCacheItem public class FileCacheItem
{ {