PageInputOrder.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using Furion.FriendlyException;
  2. using Mapster;
  3. using MapsterMapper;
  4. namespace YBEE.EQM.Core
  5. {
  6. /// <summary>
  7. /// 列表通用排序
  8. /// </summary>
  9. public static class PageInputOrder
  10. {
  11. /// <summary>
  12. /// 排序方式(默认降序)
  13. /// </summary>
  14. /// <param name="pageInput"> </param>
  15. /// <param name="defualtSortField"> 默认排序字段 </param>
  16. /// <param name="descSort"> 是否降序 </param>
  17. /// <returns> </returns>
  18. public static string OrderBuilder<T>(T pageInput, string defualtSortField = "Id", bool descSort = true)
  19. {
  20. string orderStr;
  21. // 约定默认每张表都有Id排序
  22. if (!string.IsNullOrWhiteSpace(defualtSortField))
  23. orderStr = descSort ? defualtSortField + " Desc" : defualtSortField + " Asc";
  24. else
  25. throw Oops.Oh("排序字段不能为空!");
  26. TypeAdapterConfig config = new();
  27. config.ForType<T, PageInputBase>().IgnoreNullValues(true);
  28. Mapper mapper = new(config); // 务必将mapper设为单实例
  29. PageInputBase nowPagerInput = mapper.Map<PageInputBase>(pageInput);
  30. // 排序是否可用-排序字段为非空才启用排序
  31. if (!string.IsNullOrEmpty(nowPagerInput.SortField))
  32. {
  33. orderStr = $"{nowPagerInput.SortField} {(nowPagerInput.SortOrder == nowPagerInput.DescStr ? "Desc" : "Asc")}";
  34. }
  35. return orderStr;
  36. }
  37. }
  38. }