using System.ComponentModel;
using DS.Module.Core.Data;
using Newtonsoft.Json;
using SqlSugar;
namespace DS.Module.Core.Extensions;
public static partial class Extensions
{
///
/// 多排序方法
///
/// 要排序实体
/// 源
/// 排序条件
///
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 = 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 = await source.WhereAsync(page.PageIndex, page.PageSize, page.SortConditions);
var list = result.Item1;
var total = result.Item2;
return DataResult>.PageList(total, list, MultiLanguageConst.DataQuerySuccess);
}
///
///
///
///
///
///
///
///
///
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.OrderBy("Id ascending");
orderSource = source.OrderBy("Id");
// orderSource = source.OrderBy("GID");
}
else
{
orderSource = source.OrderBy(orderConditions);
}
source = orderSource;
return (
source.Count() != 0
? source.ToPageList(pageIndex, pageSize, ref total)
: 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.OrderBy("Id");
}
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);
}
///
/// 转换SqlSugar条件查询表达式
///
///
///
public static List ConvertSqlSugarExpression(this string ruleStr)
{
var conditions = JsonConvert.DeserializeObject(ruleStr);
var conditionalCollections = new List();
if (conditions.LogicalOperator == "and")
{
var conditionList = new List>();
foreach (var item in conditions.Conditions)
{
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>();
foreach (var group in conditions.Groups)
{
if (group.LogicalOperator == "and")
{
foreach (var item1 in group.Conditions)
{
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)
{
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>();
foreach (var item in conditions.Conditions)
{
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>();
foreach (var group in conditions.Groups)
{
if (group.LogicalOperator == "and")
{
foreach (var item1 in group.Conditions)
{
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)
{
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.GreaterThanOrEqual;
//包含
case "contains":
return ConditionalType.In;
//不包含
case "not_contain":
return ConditionalType.NotIn;
//默认
default:
return ConditionalType.Equal;
}
}
}