FunctionsAppService.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using System.Web.Mvc;
  5. using Abp.Application.Services.Dto;
  6. using Abp.Auditing;
  7. using Abp.Authorization;
  8. using Abp.Dependency;
  9. using Abp.Domain.Repositories;
  10. using Abp.Runtime.Caching;
  11. using Castle.Core.Internal;
  12. using ShwasherSys.Authorization.Permissions;
  13. using ShwasherSys.BaseSysInfo.Functions.Dto;
  14. using ShwasherSys.BaseSysInfo.States;
  15. using ShwasherSys.Lambda;
  16. using IwbZero;
  17. using IwbZero.AppServiceBase;
  18. using IwbZero.Auditing;
  19. using IwbZero.Authorization.Permissions;
  20. using IwbZero.IdentityFramework;
  21. using ShwasherSys.Authorization.Users;
  22. namespace ShwasherSys.BaseSysInfo.Functions
  23. {
  24. [AbpAuthorize, AuditLog("系统功能菜单", "菜单")]
  25. public class FunctionsAppService : ShwasherAsyncCrudAppService<SysFunction, FunctionDto, int, PagedRequestDto, FunctionCreateDto, FunctionUpdateDto>, IFunctionsAppService
  26. {
  27. private readonly IStatesAppService _statesAppService;
  28. public FunctionsAppService(IStatesAppService statesAppService, ICacheManager cacheManager, IRepository<SysFunction, int> repository)
  29. : base(repository, "FunctionNo")
  30. {
  31. CacheManager = cacheManager;
  32. _statesAppService = statesAppService;
  33. KeyIsAuto = false;
  34. }
  35. protected override string ExistMessage { get; set; } = "功能菜单已存在,请检查后重试!";
  36. protected override string NotExistMessage { get; set; } = "功能菜单不存在,请检查后重试!";
  37. protected override string GetPermissionName { get; set; } = PermissionNames.PagesSystemSysFunction;
  38. protected override string GetAllPermissionName { get; set; } = PermissionNames.PagesSystemSysFunction;
  39. protected override string CreatePermissionName { get; set; } = PermissionNames.PagesSystemSysFunctionCreate;
  40. protected override string UpdatePermissionName { get; set; } = PermissionNames.PagesSystemSysFunctionUpdate;
  41. protected override string DeletePermissionName { get; set; } = PermissionNames.PagesSystemSysFunctionDelete;
  42. #region SelectList
  43. /// <summary>
  44. /// 获取 Function 下拉框选项
  45. /// </summary>
  46. /// <returns></returns>
  47. [DisableAuditing]
  48. public async Task<List<SelectListItem>> GetFunctionSelect()
  49. {
  50. var list = await Repository.GetAllListAsync();
  51. var sList = new List<SelectListItem>();
  52. foreach (var l in list)
  53. {
  54. sList.Add(new SelectListItem() { Value = l.FunctionNo, Text = l.FunctionName });
  55. }
  56. return sList;
  57. }
  58. /// <summary>
  59. /// 获取 Function 下拉框选项
  60. /// </summary>
  61. /// <returns></returns>
  62. [DisableAuditing]
  63. public async Task<string> GetFunctionSelectStr()
  64. {
  65. var list = await Repository.GetAllListAsync();
  66. string options = "";
  67. foreach (var f in list)
  68. {
  69. options += "<option value=\"" + f.FunctionNo + "\">" + f.FunctionName + "</option>";
  70. }
  71. return options;
  72. }
  73. ///// <summary>
  74. ///// 获取 Function 下拉框选项
  75. ///// </summary>
  76. ///// <returns></returns>
  77. //public async Task<string> GetLogSel()
  78. //{
  79. // var list = (await Repository.GetAllListAsync()).Where(a => a.FunctionType == 1 && !a.Controller.IsNullOrEmpty());
  80. // string lcRetval = "";
  81. // foreach (var f in list)
  82. // {
  83. // lcRetval += "<option value=\"" + f.Controller + "\">" + f.FunctionName + "</option>";
  84. // }
  85. // return lcRetval;
  86. //}
  87. #endregion
  88. #region CURD
  89. [DisableAuditing]
  90. public async Task<SysFunction> GetFunByPermissionName(string name)
  91. {
  92. var fun = await Repository.FirstOrDefaultAsync(a => a.PermissionName == name);
  93. return fun;
  94. }
  95. [DisableAuditing]
  96. public override async Task<PagedResultDto<FunctionDto>> GetAll(PagedRequestDto input)
  97. {
  98. CheckGetAllPermission();
  99. var query = CreateFilteredQuery(input).Where(a => a.FunctionType != 0);
  100. if (input.SearchList != null && input.SearchList.Count > 0)
  101. {
  102. List<LambdaObject> objList = new List<LambdaObject>();
  103. foreach (var o in input.SearchList)
  104. {
  105. if (o.KeyWords.IsNullOrEmpty())
  106. continue;
  107. object keyWords = o.KeyWords;
  108. objList.Add(new LambdaObject
  109. {
  110. FieldType = (LambdaFieldType)o.FieldType,
  111. FieldName = o.KeyField,
  112. FieldValue = keyWords,
  113. ExpType = (LambdaExpType)o.ExpType
  114. });
  115. }
  116. var exp = objList.GetExp<SysFunction>();
  117. query = query.Where(exp);
  118. }
  119. var totalCount = await AsyncQueryableExecuter.CountAsync(query);
  120. query = ApplySorting(query, input);
  121. query = ApplyPaging(query, input);
  122. var entities = await AsyncQueryableExecuter.ToListAsync(query);
  123. var dtos = new PagedResultDto<FunctionDto>(
  124. totalCount,
  125. entities.Select(a => new FunctionDto()
  126. {
  127. Id = a.Id,
  128. FunctionNo = a.FunctionNo,
  129. ParentNo = a.ParentNo,
  130. FunctionName = a.FunctionName,
  131. PermissionName = a.PermissionName,
  132. FunctionType = a.FunctionType,
  133. FunctionPath = a.FunctionPath,
  134. Action = a.Action,
  135. Controller = a.Controller,
  136. Url = a.Url,
  137. Icon = a.Icon,
  138. Class = a.Class,
  139. Script = a.Script,
  140. Sort = a.Sort,
  141. Depth = a.Depth,
  142. FunctionTypeName = _statesAppService.GetDisplayValue("SysFunction", "FunctionType", a.FunctionType.ToString())
  143. }).ToList()
  144. );
  145. return dtos;
  146. }
  147. public override async Task<FunctionDto> Create(FunctionCreateDto input)
  148. {
  149. input.ParentNo = input.ParentNo.IsNullOrEmpty() ? "0" : input.ParentNo;
  150. input.FunctionPath = input.FunctionPath + "," + input.FunctionNo;
  151. input.PermissionName = input.PermissionName + "." + input.FunctionNo;
  152. var dto = await CreateEntity1(input);
  153. await Refresh();
  154. return dto;
  155. }
  156. public override async Task<FunctionDto> Update(FunctionUpdateDto input)
  157. {
  158. input.ParentNo = input.ParentNo.IsNullOrEmpty() ? "0" : input.ParentNo;
  159. var entity = await GetEntityByIdAsync(input.Id);
  160. string sysUser = AbpSession.UserName.ToUpper();
  161. if (sysUser != "ADMIN" && sysUser != "SYSTEM")
  162. {
  163. input.FunctionType = entity.FunctionType;
  164. input.PermissionName = entity.PermissionName;
  165. input.Action = entity.Action;
  166. input.Controller = entity.Controller;
  167. input.FunctionPath = entity.FunctionPath;
  168. input.Script = entity.Script;
  169. input.Class = entity.Class;
  170. input.Url = entity.Url;
  171. }
  172. MapToEntity(input, entity);
  173. var dto = await UpdateEntity1(input);
  174. await CacheManager.GetCache(IwbZeroConsts.SysFunctionItemCache).RemoveAsync(dto.FunctionNo);
  175. return dto;
  176. }
  177. public override async Task Delete(EntityDto<int> input)
  178. {
  179. CheckDeletePermission();
  180. var entity = await Repository.FirstOrDefaultAsync(a => a.Id == input.Id);
  181. if ((await Repository.GetAllListAsync(a => a.ParentNo == entity.FunctionNo)).Any())
  182. {
  183. CheckErrors(IwbIdentityResult.Failed("此菜单下还有子菜单,不能删除"));
  184. }
  185. await Repository.DeleteAsync(entity);
  186. await CurrentUnitOfWork.SaveChangesAsync();
  187. await CacheManager.GetCache(IwbZeroConsts.SysFunctionItemCache).RemoveAsync(entity.FunctionNo);
  188. }
  189. /// <summary>
  190. /// 上移
  191. /// </summary>
  192. /// <param name="input"></param>
  193. /// <returns></returns>
  194. [AbpAuthorize(PermissionNames.PagesSystemSysFunctionMoveUp), AuditLog("上移菜单")]
  195. public async Task MoveUp(MoveUpFunctionDto input)
  196. {
  197. var fun = await Repository.GetAsync(input.Id);
  198. int sort = fun.Sort;
  199. var prevFun = await Repository.GetAsync(input.PrevId);
  200. int prevSort = prevFun.Sort;
  201. fun.Sort = prevSort;
  202. prevFun.Sort = sort;
  203. await CurrentUnitOfWork.SaveChangesAsync();
  204. }
  205. /// <summary>
  206. /// 下移
  207. /// </summary>
  208. /// <param name="input"></param>
  209. /// <returns></returns>
  210. [AbpAuthorize(PermissionNames.PagesSystemSysFunctionMoveDown), AuditLog("下移菜单")]
  211. public async Task MoveDown(MoveDownFunctionDto input)
  212. {
  213. var fun = await Repository.GetAsync(input.Id);
  214. int sort = fun.Sort;
  215. var nextFun = await Repository.GetAsync(input.NextId);
  216. int nextSort = nextFun.Sort;
  217. fun.Sort = nextSort;
  218. nextFun.Sort = sort;
  219. await CurrentUnitOfWork.SaveChangesAsync();
  220. }
  221. /// <summary>
  222. /// 强制刷新
  223. /// </summary>
  224. /// <returns></returns>
  225. [AbpAuthorize(PermissionNames.PagesSystemSysFunctionRefresh), AuditLog("强制刷新")]
  226. public async Task Refresh()
  227. {
  228. await CacheManager.GetCache(IwbZeroConsts.SysFunctionCache).ClearAsync();
  229. await CacheManager.GetCache(IwbZeroConsts.SysFunctionItemCache).ClearAsync();
  230. }
  231. /// <summary>
  232. /// 重写排序方法
  233. /// </summary>
  234. /// <param name="query"></param>
  235. /// <param name="input"></param>
  236. /// <returns></returns>
  237. protected override IQueryable<SysFunction> ApplySorting(IQueryable<SysFunction> query, PagedRequestDto input)
  238. {
  239. return query.OrderBy(a => a.FunctionType).ThenBy(a => a.Sort);
  240. }
  241. #endregion
  242. }
  243. }