完成逻辑删除功能

This commit is contained in:
橙子
2023-01-21 16:10:46 +08:00
parent fceefac0ee
commit 11bfefcd04
5 changed files with 96 additions and 4 deletions

View File

@@ -8,13 +8,15 @@ using System.Threading.Tasks;
using Yi.Framework.Core.Attributes;
using Yi.Framework.Core.Enums;
using Yi.Framework.Core.Helper;
using Yi.Framework.Data.Entities;
using Yi.Framework.Ddd.Dtos;
using Yi.Framework.Ddd.Entities;
using Yi.Framework.Ddd.Repositories;
namespace Yi.Framework.Core.Sqlsugar.Repositories
{
[AppService(ServiceType = typeof(IRepository<>))]
public class SqlsugarRepository<T> : SimpleClient<T>, IRepository<T> where T : class, new()
public class SqlsugarRepository<T> : SimpleClient<T>, IRepository<T> where T : class, IEntity, new()
{
public SqlsugarRepository(ISqlSugarClient context) : base(context)
{
@@ -35,7 +37,82 @@ namespace Yi.Framework.Core.Sqlsugar.Repositories
public async Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, IPagedAndSortedResultRequestDto page, string? orderBy, OrderByEnum orderByType = OrderByEnum.Asc)
{
return await _DbQueryable.Where(whereExpression).OrderByIF(orderBy is not null, orderBy + " "+orderByType.ToString().ToLower()).ToPageListAsync(page.PageIndex, page.PageSize);
return await _DbQueryable.Where(whereExpression).OrderByIF(orderBy is not null, orderBy + " " + orderByType.ToString().ToLower()).ToPageListAsync(page.PageIndex, page.PageSize);
}
public override async Task<bool> DeleteAsync(T deleteObj)
{
//逻辑删除
if (deleteObj is ISoftDelete)
{
//反射赋值
ReflexHelper.SetModelValue(nameof(ISoftDelete.IsDeleted), true, deleteObj);
return await UpdateAsync(deleteObj);
}
else
{
return await base.DeleteAsync(deleteObj);
}
}
public override async Task<bool> DeleteAsync(List<T> deleteObjs)
{
if (typeof(T) is ISoftDelete)
{
//反射赋值
deleteObjs.ForEach(e => ReflexHelper.SetModelValue(nameof(ISoftDelete.IsDeleted), true, e));
return await UpdateRangeAsync(deleteObjs);
}
else
{
return await base.DeleteAsync(deleteObjs);
}
}
public override async Task<bool> DeleteAsync(Expression<Func<T, bool>> whereExpression)
{
if (typeof(T) is ISoftDelete)
{
var entities = await GetListAsync(whereExpression);
//反射赋值
entities.ForEach(e => ReflexHelper.SetModelValue(nameof(ISoftDelete.IsDeleted), true, e));
return await UpdateRangeAsync(entities);
}
else
{
return await base.DeleteAsync(whereExpression);
}
}
public override async Task<bool> DeleteByIdAsync(dynamic id)
{
if (typeof(T) is ISoftDelete)
{
var entity = await GetByIdAsync(id);
//反射赋值
ReflexHelper.SetModelValue(nameof(ISoftDelete.IsDeleted), true, entity);
return await UpdateAsync(entity);
}
else
{
return await base.DeleteByIdAsync((object)id);
}
}
public override async Task<bool> DeleteByIdsAsync(dynamic[] ids)
{
if (typeof(T) is ISoftDelete)
{
var entities = await _DbQueryable.In(ids).ToListAsync();
//反射赋值
entities.ForEach(e => ReflexHelper.SetModelValue(nameof(ISoftDelete.IsDeleted), true, e));
return await UpdateRangeAsync(entities);
}
else
{
return await base.DeleteByIdsAsync(ids);
}
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Data.Entities
{
public interface IState
{
public bool State { get; set; }
}
}

View File

@@ -6,10 +6,11 @@ using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Core.Enums;
using Yi.Framework.Ddd.Dtos;
using Yi.Framework.Ddd.Entities;
namespace Yi.Framework.Ddd.Repositories
{
public interface IRepository<T>
public interface IRepository<T> where T : class, IEntity, new()
{
//单查
Task<T> GetByIdAsync(dynamic id);

View File

@@ -69,7 +69,7 @@ namespace Yi.Framework.Ddd.Services
{
//使用反射暂时先使用sqlsuga的雪花id提供
//ps: linshi
ReflexHelper.SetModelValue("Id", SnowflakeHelper.NextId, entity);
ReflexHelper.SetModelValue(nameof(IEntity<long>.Id), SnowflakeHelper.NextId, entity);
}
}

View File

@@ -74,3 +74,4 @@
2023:01:20-18:50:46本次运行启动时间为6513毫秒
2023:01:20-18:53:16本次运行启动时间为5186毫秒
2023:01:20-19:01:36本次运行启动时间为5194毫秒
2023:01:21-15:04:00本次运行启动时间为7763毫秒