FunctionsAppService.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 ContractService.Authorization;
  11. using ContractService.BaseInfo;
  12. using ContractService.BaseSystem.Functions.Dto;
  13. using ContractService.Configuration;
  14. using IwbZero.AppServiceBase;
  15. using IwbZero.Auditing;
  16. using IwbZero.Authorization.Base.Users;
  17. using IwbZero.Runtime.Session;
  18. namespace ContractService.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. var totalCount = await AsyncQueryableExecuter.CountAsync(query);
  69. query = query.OrderBy(a => a.FunctionType).ThenBy(a => a.Sort);
  70. //query = ApplyPaging(query, input);
  71. var entities = await AsyncQueryableExecuter.ToListAsync(query);
  72. return new PagedResultDto<FunctionDto>(totalCount, entities.Select(MapToEntityDto).ToList());
  73. }
  74. //[AbpAuthorize(PermissionNames.PagesSystemMgFunctionMgCreate)]
  75. public override async Task Create(FunctionCreateDto input)
  76. {
  77. if (AbpSession.UserType != UsersAndRolesTypeDefinition.Supper)
  78. {
  79. ThrowError(IwbLanguageMessage.NoPermissionOperation);
  80. }
  81. input.ParentNo = string.IsNullOrEmpty(input.ParentNo) ? "0" : input.ParentNo;
  82. var entity = await Repository.FirstOrDefaultAsync(a => a.FunctionNo == input.ParentNo);
  83. if (entity == null)
  84. {
  85. ThrowError("FunctionHasNoParent");
  86. return;
  87. }
  88. input.Depth = entity.Depth + 1;
  89. //input.FunctionPath = entity.FunctionPath + "," + input.FunctionNo;
  90. input.PermissionName = entity.PermissionName + "." + input.FunctionNo;
  91. input.NeedAuth = true;
  92. await CreateEntity(input);
  93. }
  94. [AbpAuthorize(PermissionNames.PagesSystemMgFunctionMgUpdate)]
  95. public override async Task Update(FunctionUpdateDto input)
  96. {
  97. input.ParentNo = string.IsNullOrEmpty(input.ParentNo) ? "0" : input.ParentNo;
  98. input.NeedAuth = true;
  99. var entity = await Repository.FirstOrDefaultAsync(a => a.Id == input.Id);
  100. if (entity == null)
  101. {
  102. CheckErrors(NotExistMessage);
  103. return;
  104. }
  105. if (AbpSession.GetClaimValue(IwbClaimTypes.UserName) != UserBase.AdminUserName)
  106. {
  107. input.FunctionType = entity.FunctionType;
  108. input.PermissionName = entity.PermissionName;
  109. input.Action = entity.Action;
  110. input.Controller = entity.Controller;
  111. //input.FunctionPath = entity.FunctionPath;
  112. input.Script = entity.Script;
  113. input.Class = entity.Class;
  114. input.Url = entity.Url;
  115. }
  116. MapToEntity(input, entity);
  117. await Repository.UpdateAsync(entity);
  118. await CacheManager.GetCache(IwbCacheNames.FunctionCache).RemoveAsync(entity.FunctionNo);
  119. await CacheManager.GetCache(IwbCacheNames.NavFunctionCache).RemoveAsync(entity.FunctionNo);
  120. }
  121. //[AbpAuthorize(PermissionNames.PagesSystemMgFunctionMgDelete)]
  122. public override async Task Delete(EntityDto<int> input)
  123. {
  124. var entity = await Repository.FirstOrDefaultAsync(a => a.Id == input.Id);
  125. if ((await Repository.GetAllListAsync(a => a.ParentNo == entity.FunctionNo)).Any())
  126. {
  127. ThrowError("FunctionHasChildren");
  128. }
  129. await Repository.DeleteAsync(entity);
  130. await CurrentUnitOfWork.SaveChangesAsync();
  131. await CacheManager.GetCache(IwbCacheNames.FunctionCache).RemoveAsync(entity.FunctionNo);
  132. await CacheManager.GetCache(IwbCacheNames.NavFunctionCache).RemoveAsync(entity.FunctionNo);
  133. }
  134. /// <summary>
  135. /// 上移
  136. /// </summary>
  137. /// <param name="input"></param>
  138. /// <returns></returns>
  139. [AbpAuthorize(PermissionNames.PagesSystemMgFunctionMgMoveUp), AuditLog("上移菜单")]
  140. public async Task MoveUp(MoveUpFunctionDto input)
  141. {
  142. var fun = await GetEntityById(input.Id);
  143. int sort = fun.Sort;
  144. var prevFun = await Repository.GetAsync(input.PrevId);
  145. int prevSort = prevFun.Sort;
  146. fun.Sort = prevSort;
  147. prevFun.Sort = sort;
  148. await CurrentUnitOfWork.SaveChangesAsync();
  149. }
  150. /// <summary>
  151. /// 下移
  152. /// </summary>
  153. /// <param name="input"></param>
  154. /// <returns></returns>
  155. [AbpAuthorize(PermissionNames.PagesSystemMgFunctionMgMoveDown), AuditLog("下移菜单")]
  156. public async Task MoveDown(MoveDownFunctionDto input)
  157. {
  158. var fun = await GetEntityById(input.Id);
  159. int sort = fun.Sort;
  160. var nextFun = await Repository.GetAsync(input.NextId);
  161. int nextSort = nextFun.Sort;
  162. fun.Sort = nextSort;
  163. nextFun.Sort = sort;
  164. await CurrentUnitOfWork.SaveChangesAsync();
  165. }
  166. /// <summary>
  167. /// 强制刷新
  168. /// </summary>
  169. /// <returns></returns>
  170. [AbpAuthorize(PermissionNames.PagesSystemMgFunctionMgRefresh), AuditLog("强制刷新")]
  171. public async Task Refresh()
  172. {
  173. await CacheManager.GetCache(IwbCacheNames.FunctionCache).ClearAsync();
  174. await CacheManager.GetCache(IwbCacheNames.NavFunctionCache).ClearAsync();
  175. }
  176. #endregion
  177. /// <summary>
  178. /// 重写排序方法
  179. /// </summary>
  180. /// <param name="query"></param>
  181. /// <param name="input"></param>
  182. /// <returns></returns>
  183. protected override IQueryable<SysFunction> ApplySorting(IQueryable<SysFunction> query, IwbPagedRequestDto input)
  184. {
  185. return query.OrderBy(a => a.FunctionType).ThenBy(a => a.Sort);
  186. }
  187. protected override SysFunction MapToEntity(FunctionCreateDto input)
  188. {
  189. return base.MapToEntity(input);
  190. }
  191. }
  192. }