TsysPrivRepository.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using Ropin.Inspection.Model;
  2. using Ropin.Inspection.Model.Entities;
  3. using Ropin.Inspection.Model.ViewModel;
  4. using Ropin.Inspection.Model.ViewModel.SYS;
  5. using Ropin.Inspection.Repository.SYS.Interface;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace Ropin.Inspection.Repository.SYS
  12. {
  13. public class TsysPrivRepository: RepositoryBase<TSYS_Priv, string>, ITsysPrivRepository
  14. {
  15. public TsysPrivRepository(InspectionDbContext dbContext) : base(dbContext)
  16. {
  17. }
  18. public TSYS_Priv GetPrivEntity(string code)
  19. {
  20. TSYS_Priv priv = new TSYS_Priv();
  21. priv= DbContext.Set<TSYS_Priv>().Where(i => i.C_Code==code).FirstOrDefault();
  22. return priv;
  23. }
  24. public List<TsysPrivTreeModel> GetAllPrivList(string ParentCode,string name,string status)
  25. {
  26. string sql = string.Empty;
  27. MySqlConnector.MySqlParameter[] parameters = new[] {
  28. new MySqlConnector.MySqlParameter("ParentCode", ParentCode) ,
  29. new MySqlConnector.MySqlParameter("name", "%" +name+"%"),
  30. new MySqlConnector.MySqlParameter("status", status)
  31. };
  32. sql = @" select P.*,M.C_Name as C_ModuleName,T.C_Name as C_TypeName from TSYS_Priv P
  33. LEFT JOIN TBDM_CodeDetail M on (P.C_Module=M.C_Code)
  34. LEFT JOIN TBDM_CodeDetail T on (P.C_Type=T.C_Code) WHERE 1=1";
  35. if (!string.IsNullOrEmpty(ParentCode))
  36. {
  37. if (ParentCode == "null")
  38. {
  39. sql += " and (P.C_ParentCode is null or P.C_ParentCode='') ";
  40. }
  41. else
  42. {
  43. sql += " and P.C_ParentCode=@ParentCode ";
  44. }
  45. }
  46. if (!string.IsNullOrEmpty(name))
  47. {
  48. sql += " and P.C_Name like @name ";
  49. }
  50. if (!string.IsNullOrEmpty(status))
  51. {
  52. sql += " and P.C_Status =@status ";
  53. }
  54. List<TsysPrivTreeModel> result = EntityFrameworkCoreExtensions.GetList<TsysPrivTreeModel>(DbContext.Database, sql, parameters).ToList();
  55. return result;
  56. }
  57. public async Task<List<TsysPrivTreeModel>> GetPrivTree(TsysPrivSearch model)
  58. {
  59. List< TsysPrivTreeModel > treeList = new List< TsysPrivTreeModel >();
  60. if (model==null)
  61. {
  62. model = new TsysPrivSearch();
  63. }
  64. //treeModels = GetAllPrivList("null","",model.C_Status);
  65. List<TsysPrivTreeModel> treeModels = GetAllPrivList("", "", model.C_Status);
  66. if (treeModels.First()==null)
  67. {
  68. treeModels = null;
  69. }
  70. if (treeModels!=null&& treeModels.Count>0)
  71. {
  72. foreach (var item in treeModels.Where(m => m.C_ParentCode == null).OrderBy(m => m.I_Sort))
  73. {
  74. //获得子级
  75. var children = GetChildLevel(treeModels, new List<TsysPrivTreeModel>(), item.C_Code);
  76. if (children != null && children.Count > 0)
  77. {
  78. item.Open = true;
  79. item.Children = children;
  80. }
  81. else
  82. {
  83. item.Open = false;
  84. item.Children = null;
  85. }
  86. treeList.Add(item);
  87. }
  88. //treeModels = GetChildren(model, treeModels);
  89. }
  90. if (treeList.Count>0)
  91. {
  92. return await Task.Run(() => treeList);
  93. }
  94. else
  95. {
  96. return await Task.Run(() => treeModels);
  97. }
  98. }
  99. List<TsysPrivTreeModel> GetChildLevel(List<TsysPrivTreeModel> model, List<TsysPrivTreeModel> list,string code)
  100. {
  101. //var data = model.Where(m => m.C_ParentCode.Equals(code)).OrderBy(m => m.I_Sort).ToList();
  102. foreach (var row in model.Where(m => m.C_ParentCode == code).OrderBy(m => m.I_Sort))
  103. {
  104. var res = GetChildLevel(model, new List<TsysPrivTreeModel>(), row.C_Code);
  105. if (res != null && res.Count > 0)
  106. {
  107. row.Open = true;
  108. row.Children = res;
  109. }
  110. else
  111. {
  112. row.Open = false;
  113. row.Children = null;
  114. }
  115. list.Add(row);
  116. }
  117. return list;
  118. }
  119. public List<TsysPrivTreeModel> GetChildren(TsysPrivSearch model, List<TsysPrivTreeModel> list)
  120. {
  121. foreach (TsysPrivTreeModel item in list)
  122. {
  123. List<TsysPrivTreeModel> ChildrenList = GetAllPrivList(item.C_Code,"",model.C_Status);
  124. //item.Children = ChildrenList;
  125. if (ChildrenList != null&& ChildrenList.Count>0&& ChildrenList.FirstOrDefault()!=null)
  126. {
  127. item.Children = ChildrenList;
  128. GetChildren(model, ChildrenList);
  129. }
  130. }
  131. return list;
  132. }
  133. }
  134. }