平安校园
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.
 
 
 
 
 
 

191 line
7.4 KiB

  1. //
  2. namespace SafeCampus.System;
  3. /// <summary>
  4. /// <inheritdoc cref="IButtonService"/>
  5. /// </summary>
  6. public class ButtonService : DbRepository<SysResource>, IButtonService
  7. {
  8. private readonly ILogger<ButtonService> _logger;
  9. private readonly IResourceService _resourceService;
  10. private readonly IRelationService _relationService;
  11. public ButtonService(ILogger<ButtonService> logger, IResourceService resourceService, IRelationService relationService)
  12. {
  13. _logger = logger;
  14. _resourceService = resourceService;
  15. _relationService = relationService;
  16. }
  17. /// <inheritdoc/>
  18. public async Task<SqlSugarPagedList<SysResource>> Page(ButtonPageInput input)
  19. {
  20. var query = Context.Queryable<SysResource>().Where(it => it.ParentId == input.ParentId && it.Category == CateGoryConst.RESOURCE_BUTTON)
  21. .WhereIF(!string.IsNullOrEmpty(input.SearchKey), it => it.Title.Contains(input.SearchKey) || it.Path.Contains(input.SearchKey))//根据关键字查询
  22. .OrderByIF(!string.IsNullOrEmpty(input.SortField), $"{input.SortField} {input.SortOrder}").OrderBy(it => it.SortCode);//排序
  23. var pageInfo = await query.ToPagedListAsync(input.PageNum, input.PageSize);//分页
  24. return pageInfo;
  25. }
  26. /// <inheritdoc />
  27. public async Task Add(ButtonAddInput input)
  28. {
  29. await CheckInput(input);//检查参数
  30. var sysResource = input.Adapt<SysResource>();//实体转换
  31. if (await InsertAsync(sysResource))//插入数据
  32. await _resourceService.RefreshCache(CateGoryConst.RESOURCE_BUTTON);//刷新缓存
  33. }
  34. /// <inheritdoc />
  35. public async Task<List<long>> AddBatch(ButtonAddInput input)
  36. {
  37. var sysResources = new List<SysResource>();//按钮列表
  38. var codeList = new List<string> { "Add", "Edit", "Delete", "BatchDelete", "Import", "Export", "BatchEdit" };//code后缀
  39. var titleList = new List<string> { "新增", "编辑", "删除", "批量删除", "导入", "导出", "批量编辑" };//title前缀
  40. var idList = new List<long>();//Id列表
  41. for (var i = 0; i < codeList.Count; i++)
  42. {
  43. var id = CommonUtils.GetSingleId();
  44. sysResources.Add(new SysResource
  45. {
  46. Id = id,
  47. Title = titleList[i] + input.Title,//标题等于前缀输入的值
  48. Code = input.Code + codeList[i],//code等于输入的值加后缀
  49. ParentId = input.ParentId,
  50. SortCode = i + 1
  51. });
  52. idList.Add(id);
  53. }
  54. //遍历列表
  55. foreach (var sysResource in sysResources)
  56. {
  57. await CheckInput(sysResource);//检查按钮参数
  58. }
  59. //添加到数据库
  60. if (await InsertRangeAsync(sysResources))//插入数据
  61. {
  62. await _resourceService.RefreshCache(CateGoryConst.RESOURCE_BUTTON);//刷新缓存
  63. return sysResources.Select(it => it.Id).ToList();
  64. }
  65. return new List<long>();
  66. }
  67. /// <inheritdoc />
  68. public async Task Edit(ButtonEditInput input)
  69. {
  70. await CheckInput(input);//检查参数
  71. var sysResource = input.Adapt<SysResource>();//实体转换
  72. //事务
  73. var result = await Tenant.UseTranAsync(async () =>
  74. {
  75. await UpdateAsync(sysResource);//更新按钮
  76. });
  77. if (result.IsSuccess)//如果成功了
  78. {
  79. await _resourceService.RefreshCache(CateGoryConst.RESOURCE_BUTTON);//资源表按钮刷新缓存
  80. }
  81. else
  82. {
  83. //写日志
  84. _logger.LogError(result.ErrorMessage, result.ErrorException);
  85. throw Oops.Oh(ErrorCodeEnum.A0002);
  86. }
  87. }
  88. /// <inheritdoc />
  89. public async Task Delete(BaseIdListInput input)
  90. {
  91. //获取所有ID
  92. var ids = input.Ids;
  93. //获取所有按钮集合
  94. var buttonList = await _resourceService.GetListByCategory(CateGoryConst.RESOURCE_BUTTON);
  95. #region 处理关系表角色资源信息
  96. //获取所有菜单集合
  97. var menuList = await _resourceService.GetListByCategory(CateGoryConst.RESOURCE_MENU);
  98. //获取按钮的父菜单id集合
  99. var parentIds = buttonList.Where(it => ids.Contains(it.Id)).Select(it => it.ParentId.Value.ToString()).ToList();
  100. //获取关系表分类为SYS_ROLE_HAS_RESOURCE数据
  101. var roleResources = await _relationService.GetRelationByCategory(CateGoryConst.RELATION_SYS_ROLE_HAS_RESOURCE);
  102. //获取相关关系表数据
  103. var relationList = roleResources.Where(it => parentIds.Contains(it.TargetId))//目标ID是父ID中
  104. .Where(it => it.ExtJson != null).ToList();//扩展信息不为空
  105. //遍历关系表
  106. relationList.ForEach(it =>
  107. {
  108. var relationRoleResuorce = it.ExtJson.ToJsonEntity<RelationRoleResource>();//拓展信息转实体
  109. var buttonInfo = relationRoleResuorce.ButtonInfo;//获取按钮信息
  110. if (buttonInfo.Count > 0)
  111. {
  112. // 使用 LINQ 查询找出交集的补集(即不同元素)
  113. var diffArr = buttonInfo.Except(ids).Union(ids.Except(buttonInfo)).ToList();
  114. relationRoleResuorce.ButtonInfo = diffArr;//重新赋值按钮信息
  115. it.ExtJson = relationRoleResuorce.ToJson();//重新赋值拓展信息
  116. }
  117. });
  118. #endregion 处理关系表角色资源信息
  119. //事务
  120. var result = await Tenant.UseTranAsync(async () =>
  121. {
  122. await DeleteByIdsAsync(ids.Cast<object>().ToArray());//删除按钮
  123. if (relationList.Count > 0)
  124. {
  125. await Context.Updateable(relationList).UpdateColumns(it => it.ExtJson).ExecuteCommandAsync();//修改拓展信息
  126. }
  127. });
  128. if (result.IsSuccess)//如果成功了
  129. {
  130. await _resourceService.RefreshCache(CateGoryConst.RESOURCE_BUTTON);//资源表按钮刷新缓存
  131. await _relationService.RefreshCache(CateGoryConst.RELATION_SYS_ROLE_HAS_RESOURCE);//关系表刷新角色资源缓存
  132. }
  133. else
  134. {
  135. //写日志
  136. _logger.LogError(result.ErrorMessage, result.ErrorException);
  137. throw Oops.Oh(ErrorCodeEnum.A0002);
  138. }
  139. }
  140. /// <inheritdoc />
  141. public async Task<SysResource> Detail(BaseIdInput input)
  142. {
  143. var sysResources = await _resourceService.GetListByCategory(CateGoryConst.RESOURCE_BUTTON);
  144. var resource = sysResources.Where(it => it.Id == input.Id).FirstOrDefault();
  145. return resource;
  146. }
  147. #region 方法
  148. /// <summary>
  149. /// 检查输入参数
  150. /// </summary>
  151. /// <param name="sysResource"></param>
  152. private async Task CheckInput(SysResource sysResource)
  153. {
  154. //获取所有按钮和菜单
  155. var buttonList = await _resourceService.GetListAsync(new List<string> { CateGoryConst.RESOURCE_BUTTON, CateGoryConst.RESOURCE_MENU });
  156. //判断code是否重复
  157. if (buttonList.Any(it => it.Code == sysResource.Code && it.Id != sysResource.Id))
  158. throw Oops.Bah($"存在重复的按钮编码:{sysResource.Code}");
  159. //判断菜单是否存在
  160. if (!buttonList.Any(it => it.Id == sysResource.ParentId))
  161. throw Oops.Bah($"不存在的父级菜单:{sysResource.ParentId}");
  162. sysResource.Category = CateGoryConst.RESOURCE_BUTTON;//设置分类为按钮
  163. }
  164. #endregion 方法
  165. }