OrgExtensions.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using XF.Domain.Exceptions;
  8. namespace XF.Domain.Extensions
  9. {
  10. public static class OrgExtensions
  11. {
  12. public static List<string> GetHigherOrgCodes(this string orgCode, bool withSelf = false)
  13. {
  14. if (string.IsNullOrEmpty(orgCode))
  15. return new();
  16. var length = orgCode.Length;
  17. if (length % 3 != 0)
  18. throw new UserFriendlyException($"非法OrgCode, code: {orgCode}");
  19. var rsp = new List<string>();
  20. if (withSelf)
  21. rsp.Add(orgCode);
  22. var code = orgCode.Substring(0, length - 3);
  23. while (code.Length > 0)
  24. {
  25. rsp.Add(code);
  26. code = code.Substring(0, code.Length - 3);
  27. }
  28. return rsp;
  29. }
  30. /// <summary>
  31. /// 部门等级转为中文(支持1~10,如有需要自行扩展)
  32. /// </summary>
  33. /// <param name="orgLevel"></param>
  34. /// <returns></returns>
  35. public static string ToChinese(this int orgLevel) =>
  36. orgLevel switch
  37. {
  38. 1 => "一",
  39. 2 => "二",
  40. 3 => "三",
  41. 4 => "四",
  42. 5 => "五",
  43. 6 => "六",
  44. 7 => "七",
  45. 8 => "八",
  46. 9 => "九",
  47. 10 => "十",
  48. _ => throw new ArgumentOutOfRangeException(nameof(orgLevel), orgLevel, "该部门等级暂不支持中文表示")
  49. };
  50. }
  51. }