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 InsertableExecuteCommandAsync(this IInsertable 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 UpdateableExecuteCommandAsync(this IUpdateable 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 DeletetableExecuteCommandAsync(this IDeleteable 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> ToListExAsync(this ISugarQueryable sugarQueryable) where TAggregateRoot : AggregateRoot, IAggregateRoot, new() { try { return await sugarQueryable.ToListAsync(); } catch (Exception ex) { LogHelper.Error($"执行数据库查询(多个)过程中发生错误,{ex.Message}\n{ex.StackTrace}"); return new List(); } } public static async Task FirstExAsync(this ISugarQueryable sugarQueryable, Expression> 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 CountExAsync(this ISugarQueryable sugarQueryable, Expression> 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; } } } }