平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ModuleService.cs 6.0 KiB

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