Files
ww-jst/wmsjst/JSMachine.WMS.Domain/Exstension/SqlSugarExstension.cs
T
2026-09-02 16:31:50 +08:00

104 lines
3.8 KiB
C#

using JSMachine.WMS.Domain.IRepository;
using JSMachine.WMS.Domain.Repository;
using JSMachine.WMS.Infrastructure.Helper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace JSMachine.WMS.Domain.Exstension
{
public static class SqlSugarExstension
{
public static async Task<bool> InsertableExecuteCommandAsync<TAggregateRoot>(this IInsertable<TAggregateRoot> updateable) where TAggregateRoot : AggregateRoot, IAggregateRoot, new()
{
try
{
await updateable.ExecuteCommandAsync();
return true;
}
catch (Exception ex)
{
LogHelper.Error($"执行数据库插入过程中发生错误,{ex.Message}\n{ex.StackTrace}");
return false;
}
}
public static async Task<bool> UpdateableExecuteCommandAsync<TAggregateRoot>(this IUpdateable<TAggregateRoot> updateable) where TAggregateRoot : AggregateRoot, IAggregateRoot, new()
{
try
{
await updateable.ExecuteCommandAsync();
return true;
}
catch (Exception ex)
{
LogHelper.Error($"执行数据库更改过程中发生错误,{ex.Message}\n{ex.StackTrace}");
return false;
}
}
public static async Task<bool> DeletetableExecuteCommandAsync<TAggregateRoot>(this IDeleteable<TAggregateRoot> updateable) where TAggregateRoot : AggregateRoot, IAggregateRoot, new()
{
try
{
await updateable.ExecuteCommandAsync();
return true;
}
catch (Exception ex)
{
LogHelper.Error($"执行数据库删除过程中发生错误,{ex.Message}\n{ex.StackTrace}");
return false;
}
}
public static async Task<List<TAggregateRoot>> ToListExAsync<TAggregateRoot>(this ISugarQueryable<TAggregateRoot> sugarQueryable) where TAggregateRoot : AggregateRoot, IAggregateRoot, new()
{
try
{
return await sugarQueryable.ToListAsync();
}
catch (Exception ex)
{
LogHelper.Error($"执行数据库查询(多个)过程中发生错误,{ex.Message}\n{ex.StackTrace}");
return new List<TAggregateRoot>();
}
}
public static async Task<TAggregateRoot> FirstExAsync<TAggregateRoot>(this ISugarQueryable<TAggregateRoot> sugarQueryable, Expression<Func<TAggregateRoot, bool>> expression) where TAggregateRoot : AggregateRoot, IAggregateRoot, new()
{
try
{
return await sugarQueryable.FirstAsync(expression);
}
catch (Exception ex)
{
LogHelper.Error($"执行数据库查询(单个)过程中发生错误,{ex.Message}\n{ex.StackTrace}");
return null;
}
}
public static async Task<int> CountExAsync<TAggregateRoot>(this ISugarQueryable<TAggregateRoot> sugarQueryable, Expression<Func<TAggregateRoot, bool>> expression=null) where TAggregateRoot : AggregateRoot, IAggregateRoot, new()
{
try
{
if (expression == null)
return await sugarQueryable.CountAsync();
else
return await sugarQueryable.CountAsync(expression);
}
catch (Exception ex)
{
LogHelper.Error($"执行数据库查询(计数)过程中发生错误,{ex.Message}\n{ex.StackTrace}");
return 0;
}
}
}
}