RoleDAL.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using Common.Model;
  2. using CoreEntity.Entity;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Text;
  7. using ZcPeng.PublicLibrary;
  8. using Common.Wechat;
  9. using System.Data.SqlClient;
  10. using PublicLibrary.Model;
  11. namespace CoreEntity.DAL
  12. {
  13. public class RoleDAL
  14. {
  15. public static IList<Role> GetRoles()
  16. {
  17. IList<Role> roleList = new List<Role>(0);
  18. DataTable result;
  19. string direct = " desc ";
  20. result = DataAccess.GetDataTable( Config.TablePrefix+"Role", "Id", "Id,RoleName,RoleRemark", "", "", "Id" + direct, 1, 100,out var msg);
  21. if (result != null && result.Rows.Count > 0)
  22. {
  23. // 把DataTable转换为IList<Role>
  24. roleList = ModelConvertHelper<Role>.ConvertToModel(result);
  25. }
  26. return roleList;
  27. }
  28. public static int SaveRoles(IList<Role> roles)
  29. {
  30. string commandText = "INSERT INTO " + Config.TablePrefix + "Role (rolename,roleremark,isdelete)"
  31. + " VALUES ";
  32. int number = 0;
  33. //准备参数
  34. List<List<Object>> parameters = new List<List<Object>>();
  35. foreach (Role role in roles)
  36. {
  37. commandText = commandText + "(@rolename" + number + ",@roleremark" + number + ",@isdelete" + number + ")";
  38. parameters.Add(new List<Object>() { "rolename" + number, role.RoleName });
  39. parameters.Add(new List<Object>() { "roleremark" + number, role.RoleRemark });
  40. parameters.Add(new List<Object>() { "isdelete" + number, role.IsDelete });
  41. number = number + 1;
  42. if (number == roles.Count)
  43. {
  44. commandText = commandText + ",";
  45. }
  46. }
  47. string result;
  48. List<SqlParameter> parameters1 = DataAccess.ToParameters(parameters);
  49. int success = DataAccess.ExecuteCommand(commandText, parameters1, out result);
  50. return success;
  51. }
  52. public static int SaveRolesRelatePermission(int permissionId,int[] rolesIds)
  53. {
  54. var param = new List<SqlParameter>();
  55. param.Add(new SqlParameter("PermissionId", permissionId));
  56. var sql = "update mem_RoleRelatePermission set IsDelete =1 where PermissionId=@PermissionId;";
  57. var index = 0;
  58. foreach (var item in rolesIds)
  59. {
  60. index++;
  61. param.Add(new SqlParameter($"{index}", item));
  62. sql += $" if (exists(select * from mem_RoleRelatePermission where PermissionId = @PermissionId and RoleId = @{index} and IsDelete = 1)) " +
  63. " begin " +
  64. $" update mem_RoleRelatePermission set IsDelete = 0 where PermissionId = @PermissionId and RoleId = @{index} and IsDelete = 1 " +
  65. " end " +
  66. " else " +
  67. " begin " +
  68. $" insert into mem_RoleRelatePermission (RoleId, PermissionId) values (@{index},@PermissionId) end ";
  69. }
  70. int success = DataAccess.ExecuteCommand(sql, param, out _);
  71. return success;
  72. }
  73. public static int DeleteRoleRelate(int PermissionId)
  74. {
  75. string commandText = "DELETE FROM mem_RoleRelatePermission WHERE PermissionId = @PermissionId";
  76. //准备参数
  77. List<List<Object>> parameters = new List<List<Object>>();
  78. parameters.Add(new List<Object>() { "PermissionId", PermissionId });
  79. string result = "";
  80. List<SqlParameter> parameters1 = DataAccess.ToParameters(parameters);
  81. int success = DataAccess.ExecuteCommand(commandText, parameters1, out result);
  82. return success;
  83. }
  84. public static IList<Permission> GetPermissions(Int64 roleId,string filter = " and A.AuthType ='U'",string ParentId="")
  85. {
  86. IList<Permission> permsList = new List<Permission>(0);
  87. DataTable dt = new DataTable();
  88. string result = string.Empty;
  89. var sortDirection = 1;
  90. var pageIndex = 1;
  91. var pageSize = 100000;
  92. string direct = " desc ";
  93. if (sortDirection == 1)
  94. direct = " asc";
  95. int start = (pageIndex - 1) * pageSize;
  96. int end = (start + 1 + pageSize);
  97. var filterstr = ParentId!=null && ParentId != "" ? " and A.PermissionCode like '"+ParentId+ "00_%'" : filter;
  98. string commandText = "select * from (" +
  99. "select A.*, row_number() over" +
  100. "( order by A.Sort " + direct + " ) as rownum " +
  101. " from " + Config.TablePrefix + "Permission A " +
  102. " where A.Id in (select B.PermissionId from mem_RoleRelatePermission B" +
  103. " where B.roleid = @roleid and B.permissionId = A.Id and isdelete <> 1 )" +
  104. filterstr +
  105. " and A.isdelete = 0 )AAA" +
  106. " where AAA.rownum>" + start + " and AAA.rownum<" + end ;
  107. List<List<Object>> parameters1 = new List<List<Object>>();
  108. parameters1.Add(new List<Object>() { "roleid", roleId });
  109. bool result1 = DataAccess.GetValues(commandText, ref dt, DataAccess.ToParameters(parameters1).ToArray(), out result);
  110. if (result1 && dt.Rows.Count > 0)
  111. {
  112. // 把DataTable转换为IList<Role>
  113. permsList = ModelConvertHelper<Permission>.ConvertToModel(dt);
  114. }
  115. return permsList;
  116. }
  117. public static IList<Permission> GetTopPermissions()
  118. {
  119. IList<Permission> permsList = new List<Permission>(0);
  120. DataTable dt = new DataTable();
  121. string result = string.Empty;
  122. var sortDirection = 1;
  123. var pageIndex = 1;
  124. var pageSize = Config.MaxPageSize;
  125. string direct = " desc ";
  126. if (sortDirection != 1)
  127. direct = " asc";
  128. int start = (pageIndex - 1) * pageSize;
  129. int end = (start + 1 + pageSize);
  130. string commandText = "select * from (" +
  131. "select A.*, row_number() over" +
  132. "( order by A.Id " + direct + " ) as rownum" +
  133. " from " + Config.TablePrefix + "Permission A " +
  134. " where A.ParentId = @ParentId " +
  135. " or exists (select B.Id from "+Config.TablePrefix+ "Permission B" +
  136. " where B.ParentId=0 " +
  137. " and B.Id = A.ParentId) " +
  138. ") AAA" +
  139. " where AAA.rownum>" + start + " and AAA.rownum<" + end +
  140. " ";
  141. List<List<Object>> parameters1 = new List<List<Object>>();
  142. parameters1.Add(new List<Object>() { "ParentId", 0 });
  143. bool result1 = DataAccess.GetValues(commandText, ref dt, DataAccess.ToParameters(parameters1).ToArray(), out result);
  144. if (result1 && dt.Rows.Count > 0)
  145. {
  146. // 把DataTable转换为IList<Role>
  147. permsList = ModelConvertHelper<Permission>.ConvertToModel(dt);
  148. }
  149. return permsList;
  150. }
  151. }
  152. }