平安校园
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

ButtonService.cs 7.4 KiB

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