// 麻省理工学院许可证 // // 版权所有 (c) 2021-2023 zuohuaijun,大名科技(天津)有限公司 联系电话/微信:18020030720 QQ:515096995 // // 特此免费授予获得本软件的任何人以处理本软件的权利,但须遵守以下条件:在所有副本或重要部分的软件中必须包括上述版权声明和本许可声明。 // // 软件按“原样”提供,不提供任何形式的明示或暗示的保证,包括但不限于对适销性、适用性和非侵权的保证。 // 在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任负责,无论是因合同、侵权或其他方式引起的,与软件或其使用或其他交易有关。 using MapsterMapper; 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).AS().ReSetValue(x => { x.IsDelete = true; }) .IgnoreColumns(ignoreAllNullColumns: true) .EnableDiffLogEvent() // 记录差异日志 .UpdateColumns(x => new { x.IsDelete, x.UpdateTime, x.UpdateUserId }) // 允许更新的字段-AOP拦截自动设置UpdateTime、UpdateUserId .ExecuteCommand(); } /// /// 实体集合批量假删除 _rep.FakeDelete(entity) /// /// /// /// /// public static int FakeDelete(this ISugarRepository repository, List entity) where T : EntityBase, new() { return repository.Context.FakeDelete(entity); } /// /// 实体集合批量假删除 db.FakeDelete(entity) /// /// /// /// /// public static int FakeDelete(this ISqlSugarClient db, List entity) where T : EntityBase, new() { return db.Updateable(entity).AS().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).AS().ReSetValue(x => { x.IsDelete = true; }) .IgnoreColumns(ignoreAllNullColumns: true) .EnableDiffLogEvent() // 记录差异日志 .UpdateColumns(x => new { x.IsDelete, x.UpdateTime, x.UpdateUserId }) // 允许更新的字段-AOP拦截自动设置UpdateTime、UpdateUserId .ExecuteCommandAsync(); } /// /// 实体集合批量假删除异步 _rep.FakeDeleteAsync(entity) /// /// /// /// /// public static Task FakeDeleteAsync(this ISugarRepository repository, List entity) where T : EntityBase, new() { return repository.Context.FakeDeleteAsync(entity); } /// /// 实体集合批量假删除 db.FakeDelete(entity) /// /// /// /// /// public static Task FakeDeleteAsync(this ISqlSugarClient db, List entity) where T : EntityBase, new() { return db.Updateable(entity).AS().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 prefix = "", string defaultSortField = "Id", bool descSort = true) { // 约定默认每张表都有Id排序 var orderStr = string.IsNullOrWhiteSpace(defaultSortField) ? "" : $"{prefix}{defaultSortField}" + (descSort ? " Desc" : " Asc"); TypeAdapterConfig typeAdapterConfig = new(); typeAdapterConfig.ForType().IgnoreNullValues(true); Mapper mapper = new(typeAdapterConfig); // 务必将mapper设为单实例 var nowPagerInput = mapper.Map(pageInput); // 排序是否可用-排序字段和排序顺序都为非空才启用排序 if (!string.IsNullOrEmpty(nowPagerInput.Field) && !string.IsNullOrEmpty(nowPagerInput.Order)) { var col = queryable.Context.EntityMaintenance.GetEntityInfo().Columns.FirstOrDefault(u => u.PropertyName.Equals(nowPagerInput.Field, StringComparison.CurrentCultureIgnoreCase)); orderStr = col != null ? $"{prefix}{col.DbColumnName} {(nowPagerInput.Order == nowPagerInput.DescStr ? "Desc" : "Asc")}" : $"{prefix}{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).AS() .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).AS().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).AS().EnableDiffLogEvent().ExecuteCommandAsync(); } /// /// 多库查询 /// /// /// public static ISugarQueryable AS(this ISugarQueryable queryable) { var info = GetTableInfo(); return queryable.AS($"{info.Item1}.{info.Item2}"); } /// /// 多库查询 /// /// /// /// /// public static ISugarQueryable AS(this ISugarQueryable queryable) { var info = GetTableInfo(); return queryable.AS($"{info.Item1}.{info.Item2}"); } /// /// 多库更新 /// /// /// public static IUpdateable AS(this IUpdateable updateable) where T : EntityBase, new() { var info = GetTableInfo(); return updateable.AS($"{info.Item1}.{info.Item2}"); } /// /// 多库新增 /// /// /// public static IInsertable AS(this IInsertable insertable) where T : EntityBase, new() { var info = GetTableInfo(); return insertable.AS($"{info.Item1}.{info.Item2}"); } /// /// 多库删除 /// /// /// public static IDeleteable AS(this IDeleteable deleteable) where T : EntityBase, new() { var info = GetTableInfo(); return deleteable.AS($"{info.Item1}.{info.Item2}"); } /// /// 根据实体类型获取表信息 /// /// /// private static Tuple GetTableInfo() { var entityType = typeof(T); var attr = entityType.GetCustomAttribute(); var configId = attr == null ? SqlSugarConst.MainConfigId : attr.configId.ToString(); var tableName = entityType.GetCustomAttribute().TableName; return new Tuple(configId, tableName); } }