using Furion.FriendlyException;
using Mapster;
using MapsterMapper;
namespace YBEE.EQM.Core
{
///
/// 列表通用排序
///
public static class PageInputOrder
{
///
/// 排序方式(默认降序)
///
///
/// 默认排序字段
/// 是否降序
///
public static string OrderBuilder(T pageInput, string defualtSortField = "Id", bool descSort = true)
{
string orderStr;
// 约定默认每张表都有Id排序
if (!string.IsNullOrWhiteSpace(defualtSortField))
orderStr = descSort ? defualtSortField + " Desc" : defualtSortField + " Asc";
else
throw Oops.Oh("排序字段不能为空!");
TypeAdapterConfig config = new();
config.ForType().IgnoreNullValues(true);
Mapper mapper = new(config); // 务必将mapper设为单实例
PageInputBase nowPagerInput = mapper.Map(pageInput);
// 排序是否可用-排序字段为非空才启用排序
if (!string.IsNullOrEmpty(nowPagerInput.SortField))
{
orderStr = $"{nowPagerInput.SortField} {(nowPagerInput.SortOrder == nowPagerInput.DescStr ? "Desc" : "Asc")}";
}
return orderStr;
}
}
}