RoleDAL.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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'")
  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 = 100;
  92. string direct = " desc ";
  93. if (sortDirection == 1)
  94. direct = " asc";
  95. int start = (pageIndex - 1) * pageSize;
  96. int end = (start + 1 + pageSize);
  97. string commandText = "select * from (" +
  98. "select A.*, row_number() over" +
  99. "( order by A.Sort " + direct + " ) as rownum" +
  100. " from " + Config.TablePrefix + "Permission A" +
  101. " where A.Id in (select B.PermissionId from mem_RoleRelatePermission B" +
  102. " where B.roleid = @roleid and B.permissionId = A.Id and isdelete <> 1 )" +
  103. filter +
  104. " and A.isdelete = 0 )AAA" +
  105. " where AAA.rownum>" + start + " and AAA.rownum<" + end ;
  106. List<List<Object>> parameters1 = new List<List<Object>>();
  107. parameters1.Add(new List<Object>() { "roleid", roleId });
  108. bool result1 = DataAccess.GetValues(commandText, ref dt, DataAccess.ToParameters(parameters1).ToArray(), out result);
  109. if (result1 && dt.Rows.Count > 0)
  110. {
  111. // 把DataTable转换为IList<Role>
  112. permsList = ModelConvertHelper<Permission>.ConvertToModel(dt);
  113. }
  114. return permsList;
  115. }
  116. public static IList<Permission> GetTopPermissions()
  117. {
  118. IList<Permission> permsList = new List<Permission>(0);
  119. DataTable dt = new DataTable();
  120. string result = string.Empty;
  121. var sortDirection = 1;
  122. var pageIndex = 1;
  123. var pageSize = Config.MaxPageSize;
  124. string direct = " desc ";
  125. if (sortDirection != 1)
  126. direct = " asc";
  127. int start = (pageIndex - 1) * pageSize;
  128. int end = (start + 1 + pageSize);
  129. string commandText = "select * from (" +
  130. "select A.*, row_number() over" +
  131. "( order by A.Id " + direct + " ) as rownum" +
  132. " from " + Config.TablePrefix + "Permission A " +
  133. " where A.ParentId = @ParentId " +
  134. ") AAA" +
  135. " where AAA.rownum>" + start + " and AAA.rownum<" + end +
  136. " ";
  137. List<List<Object>> parameters1 = new List<List<Object>>();
  138. parameters1.Add(new List<Object>() { "ParentId", 0 });
  139. bool result1 = DataAccess.GetValues(commandText, ref dt, DataAccess.ToParameters(parameters1).ToArray(), out result);
  140. if (result1 && dt.Rows.Count > 0)
  141. {
  142. // 把DataTable转换为IList<Role>
  143. permsList = ModelConvertHelper<Permission>.ConvertToModel(dt);
  144. }
  145. return permsList;
  146. }
  147. }
  148. }