track/src/SJZY.Track.Repository/BasicRepository.cs
2024-02-28 11:40:15 +08:00

56 lines
2.0 KiB
C#

using System.Linq.Expressions;
using SJZY.Track.Domain;
using SJZY.Track.Domain.SeedWork;
using Volo.Abp;
namespace SJZY.Track.Repository;
/// <summary>
/// 仓储基类实现
/// </summary>
/// <typeparam name="T"></typeparam>
public class BasicRepository<T> : BasicQuery<T>, IBasicRepository<T> where T : class, IAggregateRoot, new()
{
public async Task<bool> UpdateAsync(T entity, Expression<Func<T, object>> ignoreColumns)
{
return await Context.Updateable(entity)
.IgnoreColumnsIF(ignoreColumns != null, ignoreColumns)
.ExecuteCommandHasChangeAsync();
}
public async Task<bool> UpdateColumnAsync(T entity, Expression<Func<T, object>> updateColumns)
{
return await Context.Updateable(entity)
.UpdateColumnsIF(updateColumns != null, updateColumns)
.ExecuteCommandHasChangeAsync();
}
public async Task<bool> UpdateAsync(T entity, bool ignoreAllNullColumns = false, bool ignoreAllDefaultValue = false)
{
return await Context.Updateable(entity)
.IgnoreColumns(ignoreAllNullColumns: ignoreAllNullColumns, ignoreAllDefaultValue: ignoreAllDefaultValue)
.ExecuteCommandHasChangeAsync();
}
public async Task<bool> UpdateRangeAsync(List<T> entities, Expression<Func<T, object>> columns = null)
{
return await Context.Updateable(entities)
.UpdateColumnsIF(columns != null, columns)
.ExecuteCommandHasChangeAsync();
}
public async Task<bool> SoftDeleteAsync(Expression<Func<T, bool>> where)
{
if (!typeof(T).IsAssignableTo<ISoftDeleteBasicEntity>())
throw new BusinessException(
message: $"实体没有实现{nameof(ISoftDeleteBasicEntity)}不能调用{nameof(SoftDeleteAsync)}");
// tips: required where
if (where == default) return false;
return await Context.Updateable<T>()
.Where(where)
.SetColumns(nameof(ISoftDeleteBasicEntity.IsDeleted), 1)
.ExecuteCommandHasChangeAsync();
}
}