namespace Admin.NET.Core; /// /// 仓储拓展 /// public static class RepositoryExtension { /// /// 实体假删除 _rep.FakeDelete(entity) /// /// /// /// /// public static int FakeDelete(this ISugarRepository repository, T entity) where T : EntityBase, new() { return repository.Context.FakeDelete(entity); } /// /// 实体假删除 db.FakeDelete(entity) /// /// /// /// /// public static int FakeDelete(this ISqlSugarClient db, T entity) where T : EntityBase, new() { return db.Updateable(entity).ReSetValue(x => { x.IsDelete = true; }) .IgnoreColumns(ignoreAllNullColumns: true) .EnableDiffLogEvent() // 记录差异日志 .UpdateColumns(x => new { x.IsDelete, x.UpdateTime, x.UpdateUserId }) // 允许更新的字段-AOP拦截自动设置UpdateTime、UpdateUserId .ExecuteCommand(); } /// /// 实体假删除异步 _rep.FakeDeleteAsync(entity) /// /// /// /// /// public static Task FakeDeleteAsync(this ISugarRepository repository, T entity) where T : EntityBase, new() { return repository.Context.FakeDeleteAsync(entity); } /// /// 实体假删除 db.FakeDelete(entity) /// /// /// /// /// public static Task FakeDeleteAsync(this ISqlSugarClient db, T entity) where T : EntityBase, new() { return db.Updateable(entity).ReSetValue(x => { x.IsDelete = true; }) .IgnoreColumns(ignoreAllNullColumns: true) .EnableDiffLogEvent() // 记录差异日志 .UpdateColumns(x => new { x.IsDelete, x.UpdateTime, x.UpdateUserId }) // 允许更新的字段-AOP拦截自动设置UpdateTime、UpdateUserId .ExecuteCommandAsync(); } /// /// 排序方式(默认降序) /// /// /// /// 默认排序字段 /// 是否降序 /// public static ISugarQueryable OrderBuilder(this ISugarQueryable queryable, BasePageInput pageInput, string defualtSortField = "Id", bool descSort = true) { var orderStr = ""; // 约定默认每张表都有Id排序 if (!string.IsNullOrWhiteSpace(defualtSortField)) { orderStr = descSort ? defualtSortField + " Desc" : defualtSortField + " Asc"; } TypeAdapterConfig config = new(); config.ForType().IgnoreNullValues(true); Mapper mapper = new(config); // 务必将mapper设为单实例 var nowPagerInput = mapper.Map(pageInput); // 排序是否可用-排序字段和排序顺序都为非空才启用排序 if (!string.IsNullOrEmpty(nowPagerInput.Field) && !string.IsNullOrEmpty(nowPagerInput.Order)) { orderStr = $"{nowPagerInput.Field} {(nowPagerInput.Order == nowPagerInput.DescStr ? "Desc" : "Asc")}"; } return queryable.OrderByIF(!string.IsNullOrWhiteSpace(orderStr), orderStr); } /// /// 更新实体并记录差异日志 _rep.UpdateWithDiffLog(entity) /// /// /// /// /// /// public static int UpdateWithDiffLog(this ISugarRepository repository, T entity, bool ignoreAllNullColumns = true) where T : EntityBase, new() { return repository.Context.UpdateWithDiffLog(entity, ignoreAllNullColumns); } /// /// 更新实体并记录差异日志 _rep.UpdateWithDiffLog(entity) /// /// /// /// /// /// public static int UpdateWithDiffLog(this ISqlSugarClient db, T entity, bool ignoreAllNullColumns = true) where T : EntityBase, new() { return db.Updateable(entity) .IgnoreColumns(ignoreAllNullColumns: ignoreAllNullColumns) .EnableDiffLogEvent() .ExecuteCommand(); } /// /// 更新实体并记录差异日志 _rep.UpdateWithDiffLogAsync(entity) /// /// /// /// /// /// public static Task UpdateWithDiffLogAsync(this ISugarRepository repository, T entity, bool ignoreAllNullColumns = true) where T : EntityBase, new() { return repository.Context.UpdateWithDiffLogAsync(entity, ignoreAllNullColumns); } /// /// 更新实体并记录差异日志 _rep.UpdateWithDiffLogAsync(entity) /// /// /// /// /// /// public static Task UpdateWithDiffLogAsync(this ISqlSugarClient db, T entity, bool ignoreAllNullColumns = true) where T : EntityBase, new() { return db.Updateable(entity) .IgnoreColumns(ignoreAllNullColumns: ignoreAllNullColumns) .EnableDiffLogEvent() .ExecuteCommandAsync(); } /// /// 新增实体并记录差异日志 _rep.InsertWithDiffLog(entity) /// /// /// /// /// public static int InsertWithDiffLog(this ISugarRepository repository, T entity) where T : EntityBase, new() { return repository.Context.InsertWithDiffLog(entity); } /// /// 新增实体并记录差异日志 _rep.InsertWithDiffLog(entity) /// /// /// /// /// public static int InsertWithDiffLog(this ISqlSugarClient db, T entity) where T : EntityBase, new() { return db.Insertable(entity).EnableDiffLogEvent().ExecuteCommand(); } /// /// 新增实体并记录差异日志 _rep.InsertWithDiffLogAsync(entity) /// /// /// /// /// public static Task InsertWithDiffLogAsync(this ISugarRepository repository, T entity) where T : EntityBase, new() { return repository.Context.InsertWithDiffLogAsync(entity); } /// /// 新增实体并记录差异日志 _rep.InsertWithDiffLog(entity) /// /// /// /// /// public static Task InsertWithDiffLogAsync(this ISqlSugarClient db, T entity) where T : EntityBase, new() { return db.Insertable(entity).EnableDiffLogEvent().ExecuteCommandAsync(); } /// /// 多库查询 /// /// /// public static ISugarQueryable AS(this ISugarQueryable queryable) { return queryable.AS(GetTableName(queryable.Context.Ado)); } /// /// 多库查询 /// /// /// /// /// public static ISugarQueryable AS(this ISugarQueryable queryable) { return queryable.AS(GetTableName(queryable.Context.Ado)); } /// /// 根据实体类型获取表信息 /// /// /// private static string GetTableName(IAdo ado) { var entityType = typeof(T); var attr = entityType.GetCustomAttribute(); var configId = attr == null ? SqlSugarConst.ConfigId : attr.configId.ToString(); var tableName = entityType.GetCustomAttribute().TableName; //根据实际的数据库类型 修改此处 如果固定使用一个数据库,可用直接写死 var wholeTableName = $"{configId}.dbo.{tableName}"; if (ado is MySqlProvider) { wholeTableName = $"{configId}.{tableName}"; } else if (ado is SqlServerProvider) { wholeTableName = $"{configId}.dbo.{tableName}"; } return wholeTableName; } }