平安校园
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

149 рядки
6.0 KiB

  1. //
  2. namespace SafeCampus.System;
  3. /// <summary>
  4. /// <inheritdoc cref="IModuleService"/>
  5. /// </summary>
  6. public class ModuleService : DbRepository<SysResource>, IModuleService
  7. {
  8. private readonly ILogger<ModuleService> _logger;
  9. private readonly IResourceService _resourceService;
  10. private readonly IRelationService _relationService;
  11. private readonly IEventPublisher _eventPublisher;
  12. public ModuleService(ILogger<ModuleService> logger, IResourceService resourceService, IRelationService relationService,
  13. IEventPublisher eventPublisher)
  14. {
  15. _logger = logger;
  16. _resourceService = resourceService;
  17. _relationService = relationService;
  18. _eventPublisher = eventPublisher;
  19. }
  20. /// <inheritdoc/>
  21. public async Task<SqlSugarPagedList<SysResource>> Page(ModulePageInput input)
  22. {
  23. var query = Context.Queryable<SysResource>().Where(it => it.Category == CateGoryConst.RESOURCE_MODULE)//模块
  24. .WhereIF(!string.IsNullOrEmpty(input.SearchKey), it => it.Title.Contains(input.SearchKey))//根据关键字查询
  25. .OrderByIF(!string.IsNullOrEmpty(input.SortField), $"{input.SortField} {input.SortOrder}").OrderBy(it => it.SortCode);//排序
  26. var pageInfo = await query.ToPagedListAsync(input.PageNum, input.PageSize);//分页
  27. return pageInfo;
  28. }
  29. /// <inheritdoc />
  30. public async Task Add(ModuleAddInput input)
  31. {
  32. await CheckInput(input);//检查参数
  33. var sysResource = input.Adapt<SysResource>();//实体转换
  34. if (await InsertAsync(sysResource))//插入数据
  35. await _resourceService.RefreshCache(CateGoryConst.RESOURCE_MODULE);//刷新缓存
  36. }
  37. /// <inheritdoc />
  38. public async Task Edit(ModuleEditInput input)
  39. {
  40. await CheckInput(input);//检查参数
  41. var sysResource = input.Adapt<SysResource>();//实体转换
  42. if (await UpdateAsync(sysResource))//更新数据
  43. {
  44. if (sysResource.Status == CommonStatusConst.DISABLED)//如果禁用
  45. await _eventPublisher.PublishAsync(EventSubscriberConst.CLEAR_MODULE_TOKEN, sysResource.Id);//清除角色下用户缓存
  46. await _resourceService.RefreshCache(CateGoryConst.RESOURCE_MODULE);//刷新缓存
  47. }
  48. }
  49. /// <inheritdoc />
  50. public async Task<SysResource> Detail(BaseIdInput input)
  51. {
  52. var sysResources = await _resourceService.GetListByCategory(CateGoryConst.RESOURCE_MODULE);
  53. var resource = sysResources.Where(it => it.Id == input.Id).FirstOrDefault();
  54. return resource;
  55. }
  56. /// <inheritdoc />
  57. public async Task Delete(BaseIdListInput input)
  58. {
  59. //获取所有ID
  60. var ids = input.Ids;
  61. if (ids.Count > 0)
  62. {
  63. //获取所有
  64. var resourceList = await _resourceService.GetListAsync();
  65. //找到要删除的模块
  66. var sysResources = resourceList.Where(it => ids.Contains(it.Id)).ToList();
  67. //查找内置模块
  68. var system = sysResources.Where(it => it.Code == SysResourceConst.SYSTEM).FirstOrDefault();
  69. if (system != null)
  70. throw Oops.Bah($"不可删除系统内置模块:{system.Title}");
  71. if (sysResources.Any(it => it.Status == CommonStatusConst.ENABLE))//如果有启用的模块
  72. throw Oops.Bah($"请先禁用模块再删除");
  73. //获取模块下的所有菜单Id列表
  74. var menuIds = resourceList.Where(it => ids.Contains(it.Module.ToLong()) && it.ParentId.ToLong() == SafeCampusConst.ZERO)
  75. .Select(it => it.Id).ToList();
  76. //需要删除的资源ID列表
  77. var resourceIds = new List<long>();
  78. //遍历列表
  79. menuIds.ForEach(it =>
  80. {
  81. //获取菜单所有子节点
  82. var child = _resourceService.GetChildListById(resourceList, it, false);
  83. //将子节点ID添加到删除ID列表
  84. resourceIds.AddRange(child.Select(it => it.Id).ToList());
  85. resourceIds.Add(it);//添加到删除ID列表
  86. });
  87. ids.AddRange(resourceIds);
  88. //事务
  89. var result = await Tenant.UseTranAsync(async () =>
  90. {
  91. await DeleteByIdsAsync(ids.Cast<object>().ToArray());//删除菜单和按钮
  92. await Context.Deleteable<SysRelation>()//关系表删除对应SYS_ROLE_HAS_RESOURCE
  93. .Where(it => it.Category == CateGoryConst.RELATION_SYS_ROLE_HAS_RESOURCE && resourceIds.Contains(SqlFunc.ToInt64(it.TargetId)))
  94. .ExecuteCommandAsync();
  95. });
  96. if (result.IsSuccess)//如果成功了
  97. {
  98. await _resourceService.RefreshCache();//资源表刷新缓存
  99. await _relationService.RefreshCache(CateGoryConst.RELATION_SYS_ROLE_HAS_RESOURCE);//关系表刷新缓存
  100. }
  101. else
  102. {
  103. //写日志
  104. _logger.LogError(result.ErrorMessage, result.ErrorException);
  105. throw Oops.Oh(ErrorCodeEnum.A0002);
  106. }
  107. }
  108. }
  109. #region 方法
  110. /// <summary>
  111. /// 检查输入参数
  112. /// </summary>
  113. /// <param name="sysResource"></param>
  114. private async Task CheckInput(SysResource sysResource)
  115. {
  116. var sysResourceList = await _resourceService.GetListByCategory(CateGoryConst.RESOURCE_MODULE);
  117. //判断是否从存在重复模块
  118. var hasSameName = sysResourceList.Any(it => it.Title == sysResource.Title && it.Id != sysResource.Id);
  119. if (hasSameName)
  120. {
  121. throw Oops.Bah($"存在重复的模块:{sysResource.Title}");
  122. }
  123. if (sysResource.Status == CommonStatusConst.DISABLED)//如果禁用
  124. await _eventPublisher.PublishAsync(EventSubscriberConst.CLEAR_MODULE_TOKEN, sysResource.Id);//清除角色下用户缓存
  125. //设置为模块
  126. sysResource.Category = CateGoryConst.RESOURCE_MODULE;
  127. }
  128. #endregion 方法
  129. }