T4FunctionInfo.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System.Collections.Generic;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Linq;
  5. using Abp.Extensions;
  6. namespace ShwasherSys.BaseSysInfo
  7. {
  8. public class T4Function
  9. {
  10. //private static readonly string ConnectionString = WebConfigurationManager.ConnectionStrings["Default"].ToString();
  11. private static readonly string ConnectionString = @"Server=localhost; Database=DBwasher2020; User Id=sa;Password=123456";
  12. //private static readonly string ConnectionString = @"Server=47.112.30.247,50681; Database=DBwasher2019; User Id=sa;Password=Iwb2017";
  13. public static T4FunctionInfo GetRootFunction()
  14. {
  15. List<T4FunctionInfo> loFuns = GetAllFunctions();
  16. T4FunctionInfo loRootFun = loFuns.FirstOrDefault(a => a.Id == 1);
  17. if (loRootFun != null)
  18. {
  19. loFuns.Remove(loRootFun);
  20. loRootFun.Children = GetChildFunctions(loRootFun.FunctionNo, loFuns);
  21. }
  22. return loRootFun;
  23. }
  24. public static T4FunctionInfo GetRootFunctionWithoutBtn()
  25. {
  26. List<T4FunctionInfo> loFuns = GetAllFunctions(false);
  27. T4FunctionInfo loRootFun = loFuns.FirstOrDefault(a => a.Id == 1);
  28. if (loRootFun != null)
  29. {
  30. loFuns.Remove(loRootFun);
  31. loRootFun.Children = GetChildFunctions(loRootFun.FunctionNo, loFuns);
  32. }
  33. return loRootFun;
  34. }
  35. private static List<T4FunctionInfo> GetChildFunctions(string pcPraentNo, List<T4FunctionInfo> poFunctions)
  36. {
  37. List<T4FunctionInfo> loFuns = new List<T4FunctionInfo>();
  38. var loParentFuns = poFunctions.Where(a => a.ParentNo == pcPraentNo).ToList();
  39. if (loParentFuns.Any())
  40. {
  41. foreach (var f in loParentFuns)
  42. {
  43. f.Children = GetChildFunctions(f.FunctionNo, poFunctions);
  44. loFuns.Add(f);
  45. }
  46. }
  47. return loFuns;
  48. }
  49. public static List<T4FunctionInfo> GetAllFunctions(bool hasBtn = true)
  50. {
  51. using (var loSqlConn = new SqlConnection(ConnectionString))
  52. {
  53. using (loSqlConn.CreateCommand())
  54. {
  55. loSqlConn.Open();
  56. string lcSql =
  57. "SELECT [Id],[FunctionNo],[ParentNo],[FunctionName],[PermissionName],[FunctionType],[FunctionPath],[Action],[Controller],[Url],[Icon],[Sort],[Depth] FROM [Sys_Functions] WHERE [IsDeleted] = 0 ";
  58. if (!hasBtn)
  59. {
  60. lcSql += " AND [FunctionType] !=2 ";
  61. }
  62. lcSql += " ORDER BY[Depth],[Sort]";
  63. SqlDataAdapter loDataAdapter = new SqlDataAdapter(lcSql, loSqlConn);
  64. DataSet loDataSet = new DataSet(); // 创建DataSet
  65. loDataAdapter.Fill(loDataSet, "Function");
  66. DataTable loTable = loDataSet.Tables["Function"];
  67. var loFuns = new List<T4FunctionInfo>();
  68. foreach (DataRow row in loTable.Rows)
  69. {
  70. string url = row["Url"] + "", controller = row["Controller"] + "", action = row["Action"] + "";
  71. if (url.IsNullOrEmpty() && !controller.IsNullOrEmpty() && !action.IsNullOrEmpty())
  72. {
  73. url = "/" + row["Controller"] + "/" + row["Action"];
  74. }
  75. loFuns.Add(new T4FunctionInfo()
  76. {
  77. Id = (int)row["Id"],
  78. FunctionNo = row["FunctionNo"] + "",
  79. ParentNo = row["ParentNo"] + "",
  80. PermissionName = row["PermissionName"] + "",
  81. FunctionName = row["FunctionName"] + "",
  82. FunctionType = row["FunctionType"] + "",
  83. FunctionPath = row["FunctionPath"] + "",
  84. Action = action,
  85. Controller = controller,
  86. Url = url,
  87. Icon = row["Icon"] + "",
  88. Sort = (int)row["Sort"],
  89. Depth = (int)row["Depth"]
  90. });
  91. }
  92. return loFuns;
  93. }
  94. }
  95. }
  96. }
  97. public class T4FunctionInfo
  98. {
  99. public int Id { get; set; }
  100. public string FunctionNo { get; set; }
  101. public string ParentNo { get; set; }
  102. public string FunctionName { get; set; }
  103. public string PermissionName { get; set; }
  104. public string FunctionType { get; set; }
  105. public string FunctionPath { get; set; }
  106. public string Action { get; set; }
  107. public string Controller { get; set; }
  108. public string Url { get; set; }
  109. public string Icon { get; set; }
  110. public int Sort { get; set; }
  111. public int Depth { get; set; }
  112. public List<T4FunctionInfo> Children { get; set; }
  113. }
  114. }