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

175 lines
6.0 KiB

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