using System.Collections.Concurrent; using System.ComponentModel; using System.Reflection; using DS.Module.Core.Data; using Newtonsoft.Json; using SqlSugar; namespace DS.Module.Core.Extensions; public static partial class Extensions { static readonly ConcurrentDictionary OrderFieldCache = []; internal static List GetOrderFields(OrderByType orderByType = OrderByType.Desc) { Type type = typeof(T); if (!OrderFieldCache.TryGetValue(type, out string[]? fields)) { List list = new List(2); var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); //查找ID或创建时间,暂时只设置一个默认排序字段 var propId = Array.Find(properties, x => x.Name == "Id"); if (propId != null) { list.Add(propId.Name); } else { var propCT = Array.Find(properties, x => x.Name == "CreateTime"); if (propCT != null) list.Add(propCT.Name); } fields = [.. list]; OrderFieldCache.AddOrUpdate(type, fields, (k, v) => v = fields); } return fields.Select(x => new OrderByModel { FieldName = x, OrderByType = orderByType }).ToList(); } /// /// 多排序方法 /// /// 要排序实体 /// 源 /// 排序条件 /// public static ISugarQueryable OrderBy(this ISugarQueryable source, SortCondition[] orderConditions) { orderConditions.NotNull(nameof(orderConditions)); string orderStr = string.Empty; foreach (SortCondition orderCondition in orderConditions) { orderStr = orderStr + $"{orderCondition.SortField} {(orderCondition.ListSortDirection == ListSortDirection.Ascending ? "asc" : "desc")}, "; } orderStr = orderStr.TrimEnd(", ".ToCharArray()); return source.OrderBy(orderStr); } /// /// /// /// /// /// /// public static DataResult> ToQueryPage(this ISugarQueryable source, PageCondition page) { page.NotNull(nameof(page)); var result = page.IsExport ? source.WhereNoPage(page.SortConditions) : source.Where(page.PageIndex, page.PageSize, page.SortConditions); var list = result.data; var total = result.totalNumber; return DataResult>.PageList(total, list, MultiLanguageConst.DataQuerySuccess); } /// /// 将查询转换为分页结果 /// /// /// 数据源 /// 分页设置 /// public static async Task>> ToQueryPageAsync(this ISugarQueryable source, PageCondition? page) { page.NotNull(nameof(page)); var result = page.IsExport ? await source.WhereNoPageAsync(page.SortConditions) : await source.WhereAsync(page.PageIndex, page.PageSize, page.SortConditions); var list = result.Item1; var total = result.Item2; return DataResult>.PageList(total, list, MultiLanguageConst.DataQuerySuccess); } /// /// 过滤操作权限 /// /// /// /// /// public static ISugarQueryable WhereFilterOperationRule(this ISugarQueryable source, List conditionalModels) { source = source.Where(conditionalModels); if (source.Count() == 0) { //return new Exception("没有数据操作权限!"); Check.ExceptionEasy("NO Operation", "没有数据操作权限!"); } return source; } /// /// /// /// /// /// /// /// /// private static (List data, int totalNumber) Where(this ISugarQueryable source, int pageIndex, int pageSize, SortCondition[] orderConditions) { var total = source.Count(); ISugarQueryable orderSource; if (orderConditions == null || orderConditions.Length == 0) { orderSource = source; var orderFields = GetOrderFields(); if (orderFields?.Count > 0) orderSource = source.OrderBy(orderFields); } else { orderSource = source.OrderBy(orderConditions); } source = orderSource; return ( source.Count() != 0 ? source.ToPageList(pageIndex, pageSize, ref total) : Enumerable.Empty().ToList(), total); } private static (List data, int totalNumber) WhereNoPage(this ISugarQueryable source, SortCondition[] orderConditions) { var total = source.Count(); ISugarQueryable orderSource; if (orderConditions == null || orderConditions.Length == 0) { orderSource = source; var orderFields = GetOrderFields(); if (orderFields?.Count > 0) orderSource = source.OrderBy(orderFields); } else { orderSource = source.OrderBy(orderConditions); } source = orderSource; return ( source.Count() != 0 ? source.ToList() : Enumerable.Empty().ToList(), total); } private static async Task, int>> WhereAsync(this ISugarQueryable source, int pageIndex, int pageSize, SortCondition[] orderConditions) { ISugarQueryable orderSource; if (orderConditions == null || orderConditions.Length == 0) { orderSource = source; var orderFields = GetOrderFields(); if (orderFields?.Count > 0) orderSource = source.OrderBy(orderFields); } else { orderSource = source.OrderBy(orderConditions); } source = orderSource; var total = new RefAsync(); var list = await source.ToPageListAsync(pageIndex, pageSize, total); return new Tuple, int>(list, total.Value); } private static async Task, int>> WhereNoPageAsync(this ISugarQueryable source, SortCondition[] orderConditions) { ISugarQueryable orderSource; if (orderConditions == null || orderConditions.Length == 0) { orderSource = source; var orderFields = GetOrderFields(); if (orderFields?.Count > 0) orderSource = source.OrderBy(orderFields); } else { orderSource = source.OrderBy(orderConditions); } source = orderSource; var total = new RefAsync(); var list = await source.ToListAsync(); total = list.Count; return new Tuple, int>(list, total.Value); } /// /// 转换SqlSugar条件查询表达式 /// /// /// public static List ConvertSqlSugarExpression(this string ruleStr) { var conditions = JsonConvert.DeserializeObject(ruleStr); var conditionalCollections = new List(); if (conditions.LogicalOperator == "0") { var conditionList = new List>(); if (conditions.Conditions.IsNotNull() && conditions.Conditions.Count > 0) { foreach (var item in conditions.Conditions) { if (!string.IsNullOrEmpty(item.Field)) { conditionList.Add(new KeyValuePair (WhereType.And, new ConditionalModel { FieldName = item.Field, ConditionalType = GetConditionalType(item.Operator), FieldValue = item.Value }) ); } } } if (conditionList.Count > 0) { conditionalCollections.Add(new ConditionalCollections { ConditionalList = conditionList } ) ; } var groupList = new List>(); if (conditions.Groups.IsNotNull() && conditions.Groups.Count > 0) { foreach (var group in conditions.Groups) { if (group.LogicalOperator == "0") { foreach (var item1 in group.Conditions) { if (!string.IsNullOrEmpty(item1.Field)) { groupList.Add(new KeyValuePair (WhereType.And, new ConditionalModel { FieldName = item1.Field, ConditionalType = GetConditionalType(item1.Operator), FieldValue = item1.Value }) ); } } } else { foreach (var item1 in group.Conditions) { if (!string.IsNullOrEmpty(item1.Field)) { groupList.Add(new KeyValuePair (WhereType.Or, new ConditionalModel { FieldName = item1.Field, ConditionalType = GetConditionalType(item1.Operator), FieldValue = item1.Value }) ); } } } } } if (groupList.Count > 0) { conditionalCollections.Add(new ConditionalCollections { ConditionalList = groupList } ) ; } } else { var conditionList = new List>(); if (conditions.Conditions.IsNotNull() && conditions.Conditions.Count > 0) { foreach (var item in conditions.Conditions) { if (!string.IsNullOrEmpty(item.Field)) { conditionList.Add(new KeyValuePair (WhereType.Or, new ConditionalModel { FieldName = item.Field, ConditionalType = GetConditionalType(item.Operator), FieldValue = item.Value }) ); } } } if (conditionList.Count > 0) { conditionalCollections.Add(new ConditionalCollections { ConditionalList = conditionList } ) ; } var groupList = new List>(); if (conditions.Groups.IsNotNull() && conditions.Groups.Count > 0) { foreach (var group in conditions.Groups) { if (group.LogicalOperator == "0") { foreach (var item1 in group.Conditions) { if (!string.IsNullOrEmpty(item1.Field)) { groupList.Add(new KeyValuePair (WhereType.And, new ConditionalModel { FieldName = item1.Field, ConditionalType = GetConditionalType(item1.Operator), FieldValue = item1.Value }) ); } } } else { foreach (var item1 in group.Conditions) { if (!string.IsNullOrEmpty(item1.Field)) { groupList.Add(new KeyValuePair (WhereType.Or, new ConditionalModel { FieldName = item1.Field, ConditionalType = GetConditionalType(item1.Operator), FieldValue = item1.Value }) ); } } } } } if (groupList.Count > 0) { conditionalCollections.Add(new ConditionalCollections { ConditionalList = groupList } ) ; } } return conditionalCollections; } /// /// 转换SqlSugar 条件操作符 /// /// /// private static ConditionalType GetConditionalType(string conditionalType) { switch (conditionalType) { ////等于 //case "equal": // return ConditionalType.Equal; ////不等于 //case "not_equal": // return ConditionalType.NoEqual; ////大于 //case "GreaterThan": // return ConditionalType.GreaterThan; ////大于等于 //case "GreaterThanOrEqual": // return ConditionalType.GreaterThanOrEqual; ////小于 //case "LessThan": // return ConditionalType.LessThan; ////小于等于 //case "LessThanOrEqual": // return ConditionalType.LessThanOrEqual; ////包含 //case "contains": // return ConditionalType.In; ////不包含 //case "not_contain": // return ConditionalType.NotIn; ////默认 //default: // return ConditionalType.Equal; //等于 case "0": return ConditionalType.Equal; //不等于 case "10": return ConditionalType.NoEqual; //大于 case "2": return ConditionalType.GreaterThan; //大于等于 case "3": return ConditionalType.GreaterThanOrEqual; //小于 case "4": return ConditionalType.LessThan; //小于等于 case "5": return ConditionalType.LessThanOrEqual; //包含 case "6": return ConditionalType.In; //不包含 case "7": return ConditionalType.NotIn; //默认 default: return ConditionalType.Equal; } } }