CM_PermissionController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. using JCSoft.WX.Framework.Api;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.AspNetCore.Http;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using Common.Wechat;
  9. using Common;
  10. using System.Data;
  11. using Common.Model;
  12. using ZcPeng.PublicLibrary;
  13. using CoreEntity.Entity;
  14. using CoreEntity.DAL;
  15. using System.Collections.Concurrent;
  16. using Newtonsoft.Json.Converters;
  17. using Newtonsoft.Json;
  18. using System.Data.SqlClient;
  19. using Microsoft.Extensions.Primitives;
  20. using Microsoft.Extensions.Caching.Memory;
  21. using Jwt;
  22. using PublicLibrary.Model;
  23. using Newtonsoft.Json.Linq;
  24. using SupplierWeb.Codes.mvc;
  25. using SupplierWeb.Codes.Auth;
  26. using Common.Config;
  27. namespace SupplierWeb.Controllers
  28. {
  29. [Route("web/CM_Permission")]
  30. public class CM_PermissionController : BaseController
  31. {
  32. public CM_PermissionController(IMemoryCache cache, IApiClient client) : base(cache, client)
  33. {
  34. }
  35. private ConcurrentDictionary<String, CM_Permission> PermissionMap = new ConcurrentDictionary<String, CM_Permission>();
  36. /// <summary>
  37. /// 角色列表
  38. /// </summary>
  39. /// <param name="filters"></param>
  40. /// <param name="pageIndex"></param>
  41. /// <param name="pageSize"></param>
  42. /// <param name="sortField"></param>
  43. /// <param name="sortDirection"></param>
  44. /// <param name="sumFields"></param>
  45. /// <param name="data"></param>
  46. /// <returns></returns>
  47. [AuthPermission]
  48. [HttpPost, Route("index")]
  49. public JsonResult Indexp(QueryFilter[] filters, Int32 pageIndex, Int32 pageSize,
  50. string sortField, Int32 sortDirection, string[] sumFields, [FromBody]dynamic data)
  51. {
  52. //取出参数
  53. if (data != null)
  54. {
  55. filters = data.filters.ToObject<QueryFilter[]>();
  56. pageIndex = data.pageIndex;
  57. pageSize = data.pageSize;
  58. sortField = data.sortField;
  59. sortDirection = data.sortDirection;
  60. sumFields = data.sumFields.ToObject<string[]>();
  61. }
  62. #region 获取角色列表
  63. DataTable dt = new DataTable();
  64. string result;
  65. IList<CM_Permission> permss = new List<CM_Permission>(0);
  66. List<SqlParameter> parameters = new List<SqlParameter>();
  67. string filterstr = QueryFilter.getFilterSqlParam(filters, out parameters, new CM_Permission(), "A.");
  68. string direct = " desc ";
  69. if (sortDirection != 1)
  70. {
  71. direct = " asc";
  72. }
  73. int start = (pageIndex - 1) * pageSize;
  74. int end = (start + 1 + pageSize);
  75. string commandText0 = "select * from ";
  76. string commandText1 = "(" +
  77. "select A.*,row_number() over" +
  78. "( order by A.ModuleId " + direct + " ) as rownum from " +
  79. "CM_Permission as A " +
  80. " where 1=1 " +
  81. //" and A.CreationPerson = '" + userids + "'" +
  82. filterstr +
  83. ")AAA ";
  84. string commandText2 = " where AAA.rownum>" + start + " and AAA.rownum<" + end;
  85. string commandText3 = commandText0 + commandText1 + commandText2;
  86. bool success = DataAccess.GetValues(commandText3, ref dt, parameters.ToArray(), out result);
  87. #endregion
  88. if (dt != null && dt.Rows.Count > 0)
  89. {
  90. // 把DataTable转换为IList<Permission>
  91. permss = ModelConvertHelper<CM_Permission>.ConvertToModel(dt);
  92. #region 获取权限的角色列表,角色列表
  93. //角色列表
  94. IList<CM_Permission> Permissions = GetPermissions();
  95. // 把DataTable转换为IList<Permission>
  96. if (PermissionMap.Count < Permissions.Count)
  97. {
  98. foreach (CM_Permission Permission in Permissions)
  99. {
  100. PermissionMap.TryAdd(Permission.ModuleId, Permission);
  101. }
  102. }
  103. #region
  104. foreach (CM_Permission perms in permss)
  105. {
  106. string direct1 = " desc ";
  107. dt = DataAccess.GetDataTable("CM_Permission", "ModuleId", "*", "ModuleId=" + perms.ModuleId, "", "ModuleId" + direct1, 1, 100, out var msg);
  108. // 把DataTable转换为IList<PermissionRelatePermission>
  109. if (dt != null && dt.Rows.Count > 0)
  110. {
  111. IList<CM_Permission> permRelates = ModelConvertHelper<CM_Permission>.ConvertToModel(dt);
  112. List<CM_Permission> listPermissions = new List<CM_Permission>();
  113. List<int> superior_list = new List<int>(0);
  114. foreach (CM_Permission permsr in permRelates)
  115. {
  116. CM_Permission Permission;
  117. if (PermissionMap.TryGetValue(permsr.ModuleId, out Permission))
  118. {
  119. listPermissions.Add(Permission);
  120. }
  121. superior_list.Add(Convert.ToInt32(permsr.ModuleId));
  122. }
  123. //perms.PermissionList = listPermissions;
  124. perms.ModuleId = string.Join(",", superior_list.ToArray());
  125. }
  126. }
  127. #endregion
  128. #endregion
  129. }
  130. string result1;
  131. long totalcount = DataAccess.GetRowCountDefine("select count(ModuleId) from " + commandText1, parameters.ToArray(), out result1);
  132. IsoDateTimeConverter timejson = new IsoDateTimeConverter
  133. {
  134. DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss"
  135. };
  136. //IList<Menu> menus = Permission.Convert(permss);
  137. var jsonData = JsonConvert.SerializeObject(permss, timejson);
  138. return Json(new
  139. {
  140. items = JsonConvert.DeserializeObject(jsonData),
  141. sum = new { },
  142. totalCount = totalcount
  143. });
  144. }
  145. /// <summary>
  146. /// 新增角色
  147. /// </summary>
  148. /// <param name="ModuleId"></param>
  149. /// <param name="PermissionCode"></param>
  150. /// <param name="ModuleCode"></param>
  151. /// <param name="data"></param>
  152. /// <returns></returns>
  153. [AuthPermission]
  154. [HttpPost, Route("add")]
  155. public JsonResult Addp(
  156. string ModuleId, string ParentModuleId,
  157. string ModuleCode, [FromBody]dynamic data
  158. )
  159. {
  160. if (data != null)
  161. {
  162. ModuleId = data.ModuleId;
  163. ParentModuleId = data.ParentModuleId;
  164. ModuleCode = data.ModuleCode;
  165. }
  166. #region 添加功能
  167. String uuid = System.Guid.NewGuid().ToString("N");
  168. string commandText = "INSERT INTO CM_Permission (ModuleId," +
  169. "ParentModuleId,ModuleCode)" +
  170. " VALUES ('" + uuid + "',@ParentModuleId,@ModuleCode)";
  171. string result;
  172. //准备参数
  173. List<List<Object>> parameters = new List<List<Object>>();
  174. parameters.Add(new List<Object>() { "ModuleId", uuid });
  175. parameters.Add(new List<Object>() { "ParentModuleId", ParentModuleId });
  176. parameters.Add(new List<Object>() { "ModuleCode", ModuleCode });
  177. List<SqlParameter> parameters1 = DataAccess.ToParameters(parameters);
  178. int success = DataAccess.ExecuteCommand(commandText, parameters1, out result);
  179. #endregion
  180. //#region 添加角色
  181. //int id = BaseDAL.GetId(Config.TablePrefix + "Permission");
  182. //PermissionDAL.SavePermissionsRelatePermission(id, ModuleIdis);
  183. //#endregion
  184. return Json(new
  185. {
  186. success = success
  187. });
  188. }
  189. /// <summary>
  190. /// 修改角色
  191. /// </summary>
  192. /// <param name="ModuleId"></param>
  193. /// <param name="PermissionCode"></param>
  194. /// <param name="ModuleCode"></param>
  195. /// <param name="data"></param>
  196. /// <returns></returns>
  197. [AuthPermission]
  198. [HttpPost, Route("edit")]
  199. public JsonResult editp(String ModuleId, String ParentModuleId,
  200. string ModuleCode, [FromBody]dynamic data)
  201. {
  202. if (data != null)
  203. {
  204. ModuleId = data.ModuleId;
  205. ParentModuleId = data.ParentModuleId;
  206. ModuleCode = data.ModuleCode;
  207. }
  208. #region 编辑用户
  209. string commandText = "UPDATE CM_Permission " +
  210. " SET ParentModuleId = @ParentModuleId,ModuleCode = @ModuleCode WHERE ModuleId= @ModuleId";
  211. string result;
  212. //准备参数
  213. List<List<Object>> parameters = new List<List<Object>>();
  214. parameters.Add(new List<Object>() { "ModuleId", ModuleId });
  215. parameters.Add(new List<Object>() { "ParentModuleId", ParentModuleId });
  216. parameters.Add(new List<Object>() { "ModuleCode", ModuleCode });
  217. List<SqlParameter> parameters1 = DataAccess.ToParameters(parameters);
  218. int success = DataAccess.ExecuteCommand(commandText, parameters1, out result);
  219. #endregion
  220. return Json(new
  221. {
  222. success = success
  223. });
  224. }
  225. /// <summary>
  226. /// 删除角色
  227. /// </summary>
  228. /// <param name="id"></param>
  229. /// <returns></returns>
  230. [AuthPermission]
  231. [HttpPost, Route("delete")]
  232. public ActionResult Delete(string id)
  233. {
  234. //准备SQL语句
  235. string commandText = "DELETE FROM CM_Permission WHERE ModuleId = @ModuleId";
  236. //准备参数
  237. List<List<Object>> parameters = new List<List<Object>>();
  238. parameters.Add(new List<Object>() { "ModuleId", id });
  239. string result = "";
  240. //转换参数
  241. List<SqlParameter> parameters1 = DataAccess.ToParameters(parameters);
  242. //执行并返回结果
  243. int success = DataAccess.ExecuteCommand(commandText, parameters1, out result);
  244. return Content("{success:" + success + "}");
  245. }
  246. public IList<CM_Permission> GetTopPermissions()
  247. {
  248. IList<CM_Permission> permsList = new List<CM_Permission>(0);
  249. DataTable dt = new DataTable();
  250. string result = string.Empty;
  251. var sortDirection = 1;
  252. var pageIndex = 1;
  253. var pageSize = Config.MaxPageSize;
  254. string direct = " desc ";
  255. if (sortDirection != 1)
  256. direct = " asc";
  257. int start = (pageIndex - 1) * pageSize;
  258. int end = (start + 1 + pageSize);
  259. string commandText = "select * from (" +
  260. "select A.*, row_number() over" +
  261. "( order by A.ModuleId " + direct + " ) as rownum" +
  262. " from CM_Permission A ) AAA" +
  263. " where AAA.rownum>" + start + " and AAA.rownum<" + end +
  264. " ";
  265. List<List<Object>> parameters1 = new List<List<Object>>();
  266. parameters1.Add(new List<Object>() { "ModuleId", 0 });
  267. bool result1 = DataAccess.GetValues(commandText, ref dt, DataAccess.ToParameters(parameters1).ToArray(), out result);
  268. if (result1 && dt.Rows.Count > 0)
  269. {
  270. // 把DataTable转换为IList<Permission>
  271. permsList = ModelConvertHelper<CM_Permission>.ConvertToModel(dt);
  272. }
  273. return permsList;
  274. }
  275. public static IList<CM_Permission> GetPermissions()
  276. {
  277. IList<CM_Permission> PermissionList = new List<CM_Permission>(0);
  278. DataTable result;
  279. string direct = " desc ";
  280. result = DataAccess.GetDataTable("CM_Permission", "Id", "ModuleId,ModuleCode,PermissionCode", "", "", "Id" + direct, 1, 100, out var msg);
  281. if (result != null && result.Rows.Count > 0)
  282. {
  283. // 把DataTable转换为IList<Permission>
  284. PermissionList = ModelConvertHelper<CM_Permission>.ConvertToModel(result);
  285. }
  286. return PermissionList;
  287. }
  288. }
  289. }