FunctionsAppService.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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.Domain.Repositories;
  9. using Abp.Runtime.Caching;
  10. using WeApp.Authorization;
  11. using WeApp.BaseInfo;
  12. using WeApp.BaseSystem.Functions.Dto;
  13. using WeApp.Configuration;
  14. using IwbZero.AppServiceBase;
  15. using IwbZero.Auditing;
  16. using IwbZero.Authorization.Base.Users;
  17. using IwbZero.Runtime.Session;
  18. namespace WeApp.BaseSystem.Functions
  19. {
  20. [AbpAuthorize, AuditLog("系统功能菜单", "菜单")]
  21. public class FunctionsAppService : IwbAsyncCrudAppService<SysFunction, FunctionDto, int, IwbPagedRequestDto, FunctionCreateDto, FunctionUpdateDto>, IFunctionsAppService
  22. {
  23. public FunctionsAppService(ICacheManager cacheManager, IRepository<SysFunction, int> repository) : base(repository, "FunctionNo")
  24. {
  25. CacheManager = cacheManager;
  26. }
  27. protected override bool KeyIsAuto { get; set; } = false;
  28. protected override string KeyExistMessage => string.Format(L(IwbLanguageMessage.KeyExistMessageFormatter), L("function"));
  29. protected override string KeyNotExistMessage => string.Format(L(IwbLanguageMessage.KeyNotExistMessageFormatter), L("function"));
  30. #region SelectList
  31. /// <summary>
  32. /// 获取 Function 下拉框选项
  33. /// </summary>
  34. /// <returns></returns>
  35. [DisableAuditing]
  36. public override async Task<List<SelectListItem>> GetSelectList()
  37. {
  38. var list = await Repository.GetAllListAsync();
  39. var sList = new List<SelectListItem>();
  40. foreach (var l in list)
  41. {
  42. sList.Add(new SelectListItem() { Value = l.FunctionNo, Text = l.FunctionName });
  43. }
  44. return sList;
  45. }
  46. /// <summary>
  47. /// 获取 Function 下拉框选项
  48. /// </summary>
  49. /// <returns></returns>
  50. [DisableAuditing]
  51. public override async Task<string> GetSelectStr()
  52. {
  53. var list = await Repository.GetAllListAsync();
  54. string options = "";
  55. foreach (var f in list)
  56. {
  57. options += "<option value=\"" + f.FunctionNo + "\">" + f.FunctionName + "</option>";
  58. }
  59. return options;
  60. }
  61. #endregion
  62. #region CURD
  63. [DisableAuditing]
  64. [AbpAuthorize(PermissionNames.PagesSystemMgFunctionMgQuery)]
  65. public override async Task<PagedResultDto<FunctionDto>> GetAll(IwbPagedRequestDto input)
  66. {
  67. var query = CreateFilteredQuery(input).Where(a => a.FunctionType != 0);
  68. query = ApplyFilter(query, input);
  69. var totalCount = await AsyncQueryableExecuter.CountAsync(query);
  70. query = query.OrderBy(a => a.FunctionType).ThenBy(a => a.Sort);
  71. //query = ApplyPaging(query, input);
  72. var entities = await AsyncQueryableExecuter.ToListAsync(query);
  73. return new PagedResultDto<FunctionDto>(totalCount, entities.Select(MapToEntityDto).ToList());
  74. }
  75. [AbpAuthorize(PermissionNames.PagesSystemMgFunctionMgCreate)]
  76. public override async Task Create(FunctionCreateDto input)
  77. {
  78. if (AbpSession.UserType != UsersAndRolesTypeDefinition.Supper)
  79. {
  80. ThrowError(IwbLanguageMessage.NoPermissionOperation);
  81. }
  82. input.ParentNo = string.IsNullOrEmpty(input.ParentNo) ? "0" : input.ParentNo;
  83. var entity = await Repository.FirstOrDefaultAsync(a => a.FunctionNo == input.ParentNo);
  84. if (entity == null)
  85. {
  86. ThrowError("FunctionHasNoParent");
  87. return;
  88. }
  89. input.Depth = entity.Depth + 1;
  90. //input.FunctionPath = entity.FunctionPath + "," + input.FunctionNo;
  91. input.PermissionName = entity.PermissionName + "." + input.FunctionNo;
  92. input.NeedAuth = true;
  93. await CreateEntity(input);
  94. }
  95. [AbpAuthorize(PermissionNames.PagesSystemMgFunctionMgUpdate)]
  96. public override async Task Update(FunctionUpdateDto input)
  97. {
  98. input.ParentNo = string.IsNullOrEmpty(input.ParentNo) ? "0" : input.ParentNo;
  99. input.NeedAuth = true;
  100. var entity = await Repository.FirstOrDefaultAsync(a => a.Id == input.Id);
  101. if (entity == null)
  102. {
  103. CheckErrors(NotExistMessage);
  104. return;
  105. }
  106. if (AbpSession.GetClaimValue(IwbClaimTypes.UserName) != UserBase.AdminUserName)
  107. {
  108. input.FunctionType = entity.FunctionType;
  109. input.PermissionName = entity.PermissionName;
  110. input.Action = entity.Action;
  111. input.Controller = entity.Controller;
  112. //input.FunctionPath = entity.FunctionPath;
  113. input.Script = entity.Script;
  114. input.Class = entity.Class;
  115. input.Url = entity.Url;
  116. }
  117. MapToEntity(input, entity);
  118. await Repository.UpdateAsync(entity);
  119. await CacheManager.GetCache(IwbCacheNames.FunctionCache).RemoveAsync(entity.FunctionNo);
  120. await CacheManager.GetCache(IwbCacheNames.NavFunctionCache).RemoveAsync(entity.FunctionNo);
  121. }
  122. [AbpAuthorize(PermissionNames.PagesSystemMgFunctionMgDelete)]
  123. public override async Task Delete(EntityDto<int> input)
  124. {
  125. var entity = await Repository.FirstOrDefaultAsync(a => a.Id == input.Id);
  126. if ((await Repository.GetAllListAsync(a => a.ParentNo == entity.FunctionNo)).Any())
  127. {
  128. ThrowError("FunctionHasChildren");
  129. }
  130. await Repository.DeleteAsync(entity);
  131. await CurrentUnitOfWork.SaveChangesAsync();
  132. await CacheManager.GetCache(IwbCacheNames.FunctionCache).RemoveAsync(entity.FunctionNo);
  133. await CacheManager.GetCache(IwbCacheNames.NavFunctionCache).RemoveAsync(entity.FunctionNo);
  134. }
  135. /// <summary>
  136. /// 上移
  137. /// </summary>
  138. /// <param name="input"></param>
  139. /// <returns></returns>
  140. [AbpAuthorize(PermissionNames.PagesSystemMgFunctionMgMoveUp), AuditLog("上移菜单")]
  141. public async Task MoveUp(MoveUpFunctionDto input)
  142. {
  143. var fun = await GetEntityById(input.Id);
  144. int sort = fun.Sort;
  145. var prevFun = await Repository.GetAsync(input.PrevId);
  146. int prevSort = prevFun.Sort;
  147. fun.Sort = prevSort;
  148. prevFun.Sort = sort;
  149. await CurrentUnitOfWork.SaveChangesAsync();
  150. }
  151. /// <summary>
  152. /// 下移
  153. /// </summary>
  154. /// <param name="input"></param>
  155. /// <returns></returns>
  156. [AbpAuthorize(PermissionNames.PagesSystemMgFunctionMgMoveDown), AuditLog("下移菜单")]
  157. public async Task MoveDown(MoveDownFunctionDto input)
  158. {
  159. var fun = await GetEntityById(input.Id);
  160. int sort = fun.Sort;
  161. var nextFun = await Repository.GetAsync(input.NextId);
  162. int nextSort = nextFun.Sort;
  163. fun.Sort = nextSort;
  164. nextFun.Sort = sort;
  165. await CurrentUnitOfWork.SaveChangesAsync();
  166. }
  167. /// <summary>
  168. /// 强制刷新
  169. /// </summary>
  170. /// <returns></returns>
  171. [AbpAuthorize(PermissionNames.PagesSystemMgFunctionMgRefresh), AuditLog("强制刷新")]
  172. public async Task Refresh()
  173. {
  174. await CacheManager.GetCache(IwbCacheNames.FunctionCache).ClearAsync();
  175. await CacheManager.GetCache(IwbCacheNames.NavFunctionCache).ClearAsync();
  176. }
  177. #endregion
  178. /// <summary>
  179. /// 重写排序方法
  180. /// </summary>
  181. /// <param name="query"></param>
  182. /// <param name="input"></param>
  183. /// <returns></returns>
  184. protected override IQueryable<SysFunction> ApplySorting(IQueryable<SysFunction> query, IwbPagedRequestDto input)
  185. {
  186. return query.OrderBy(a => a.FunctionType).ThenBy(a => a.Sort);
  187. }
  188. protected override SysFunction MapToEntity(FunctionCreateDto input)
  189. {
  190. return base.MapToEntity(input);
  191. }
  192. }
  193. }