平安校园
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

RelationService.cs 6.0 KiB

4ヶ月前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. 
  2. //
  3. namespace SafeCampus.System;
  4. /// <inheritdoc cref="IRelationService"/>
  5. public class RelationService : DbRepository<SysRelation>, IRelationService
  6. {
  7. private readonly ILogger<RelationService> _logger;
  8. private readonly ISimpleCacheService _simpleCacheService;
  9. public RelationService(ILogger<RelationService> logger, ISimpleCacheService simpleCacheService)
  10. {
  11. _logger = logger;
  12. _simpleCacheService = simpleCacheService;
  13. }
  14. /// <inheritdoc/>
  15. public async Task<List<SysRelation>> GetRelationByCategory(string category)
  16. {
  17. var key = SystemConst.CACHE_SYS_RELATION + category;
  18. //先从Redis拿
  19. var sysRelations = _simpleCacheService.Get<List<SysRelation>>(key);
  20. if (sysRelations == null)
  21. {
  22. //redis没有就去数据库拿
  23. sysRelations = await base.GetListAsync(it => it.Category == category);
  24. if (sysRelations.Count > 0)
  25. {
  26. //插入Redis
  27. _simpleCacheService.Set(key, sysRelations);
  28. }
  29. }
  30. return sysRelations;
  31. }
  32. /// <inheritdoc/>
  33. public async Task<List<SysRelation>> GetRelationListByObjectIdAndCategory(long objectId, string category)
  34. {
  35. var sysRelations = await GetRelationByCategory(category);
  36. var result = sysRelations.Where(it => it.ObjectId == objectId).ToList();//获取关系集合
  37. return result;
  38. }
  39. /// <inheritdoc/>
  40. public async Task<SysRelation> GetWorkbench(long userId)
  41. {
  42. var sysRelations = await GetRelationByCategory(CateGoryConst.RELATION_SYS_USER_WORKBENCH_DATA);
  43. var result = sysRelations.Where(it => it.ObjectId == userId).FirstOrDefault();//获取个人工作台
  44. return result;
  45. }
  46. /// <inheritdoc/>
  47. public async Task<List<SysRelation>> GetRelationListByObjectIdListAndCategory(List<long> objectIds, string category)
  48. {
  49. var sysRelations = await GetRelationByCategory(category);
  50. var result = sysRelations.Where(it => objectIds.Contains(it.ObjectId)).ToList();//获取关系集合
  51. return result;
  52. }
  53. /// <inheritdoc/>
  54. public async Task<List<SysRelation>> GetRelationListByTargetIdAndCategory(string targetId, string category)
  55. {
  56. var sysRelations = await GetRelationByCategory(category);
  57. var result = sysRelations.Where(it => it.TargetId == targetId).ToList();//获取关系集合
  58. return result;
  59. }
  60. /// <inheritdoc/>
  61. public async Task<List<SysRelation>> GetRelationListByTargetIdListAndCategory(List<string> targetIds, string category)
  62. {
  63. var sysRelations = await GetRelationByCategory(category);
  64. var result = sysRelations.Where(it => targetIds.Contains(it.TargetId)).ToList();//获取关系集合
  65. return result;
  66. }
  67. /// <inheritdoc/>
  68. public async Task RefreshCache(string category)
  69. {
  70. var key = SystemConst.CACHE_SYS_RELATION + category;//key
  71. _simpleCacheService.Remove(key);//删除redis
  72. await GetRelationByCategory(category);//更新缓存
  73. }
  74. /// <inheritdoc/>
  75. public async Task SaveRelationBatch(string category, long objectId, List<string> targetIds,
  76. List<string> extJsons, bool clear)
  77. {
  78. var sysRelations = new List<SysRelation>();//要添加的列表
  79. for (var i = 0; i < targetIds.Count; i++)
  80. {
  81. sysRelations.Add(new SysRelation
  82. {
  83. ObjectId = objectId,
  84. TargetId = targetIds[i],
  85. Category = category,
  86. ExtJson = extJsons == null ? null : extJsons[i]
  87. });
  88. }
  89. //事务
  90. var result = await Tenant.UseTranAsync(async () =>
  91. {
  92. if (clear)
  93. await DeleteAsync(it => it.ObjectId == objectId && it.Category == category);//删除老的
  94. await InsertRangeAsync(sysRelations);//添加新的
  95. });
  96. if (result.IsSuccess)//如果成功了
  97. {
  98. await RefreshCache(category);
  99. }
  100. else
  101. {
  102. //写日志
  103. _logger.LogError(result.ErrorMessage, result.ErrorException);
  104. throw Oops.Oh(ErrorCodeEnum.A0003);
  105. }
  106. }
  107. /// <inheritdoc/>
  108. public async Task SaveRelation(string category, long objectId, string targetId,
  109. string extJson, bool clear, bool refreshCache = true)
  110. {
  111. var sysRelation = new SysRelation
  112. {
  113. ObjectId = objectId,
  114. TargetId = targetId,
  115. Category = category,
  116. ExtJson = extJson
  117. };
  118. //事务
  119. var result = await Tenant.UseTranAsync(async () =>
  120. {
  121. if (clear)
  122. await DeleteAsync(it => it.ObjectId == objectId && it.Category == category);//删除老的
  123. await InsertAsync(sysRelation);//添加新的
  124. });
  125. if (result.IsSuccess)//如果成功了
  126. {
  127. if (refreshCache)
  128. await RefreshCache(category);
  129. }
  130. else
  131. {
  132. //写日志
  133. _logger.LogError(result.ErrorMessage, result.ErrorException);
  134. throw Oops.Oh(ErrorCodeEnum.A0003);
  135. }
  136. }
  137. /// <inheritdoc/>
  138. public async Task<List<long>> GetUserModuleId(List<long> roleIdList, long userId)
  139. {
  140. var moduleIds = new List<long>();
  141. var roleRelation = await GetRelationByCategory(CateGoryConst.RELATION_SYS_ROLE_HAS_MODULE);//获取角色模块关系集合
  142. if (roleRelation != null && roleRelation.Count > 0)
  143. {
  144. moduleIds = roleRelation.Where(it => roleIdList.Contains(it.ObjectId)).Select(it => it.TargetId.ToLong()).ToList();
  145. }
  146. var userRelation = await GetRelationByCategory(CateGoryConst.RELATION_SYS_USER_HAS_MODULE);//获取用户模块关系集合
  147. if (userRelation != null && userRelation.Count > 0)
  148. {
  149. var userModuleIds = userRelation.Where(it => it.ObjectId == userId).Select(it => it.TargetId.ToLong()).ToList();
  150. moduleIds.AddRange(userModuleIds);
  151. }
  152. return moduleIds;
  153. }
  154. }