// namespace SafeCampus.System; /// public class RelationService : DbRepository, IRelationService { private readonly ILogger _logger; private readonly ISimpleCacheService _simpleCacheService; public RelationService(ILogger logger, ISimpleCacheService simpleCacheService) { _logger = logger; _simpleCacheService = simpleCacheService; } /// public async Task> GetRelationByCategory(string category) { var key = SystemConst.CACHE_SYS_RELATION + category; //先从Redis拿 var sysRelations = _simpleCacheService.Get>(key); if (sysRelations == null) { //redis没有就去数据库拿 sysRelations = await base.GetListAsync(it => it.Category == category); if (sysRelations.Count > 0) { //插入Redis _simpleCacheService.Set(key, sysRelations); } } return sysRelations; } /// public async Task> GetRelationListByObjectIdAndCategory(long objectId, string category) { var sysRelations = await GetRelationByCategory(category); var result = sysRelations.Where(it => it.ObjectId == objectId).ToList();//获取关系集合 return result; } /// public async Task GetWorkbench(long userId) { var sysRelations = await GetRelationByCategory(CateGoryConst.RELATION_SYS_USER_WORKBENCH_DATA); var result = sysRelations.Where(it => it.ObjectId == userId).FirstOrDefault();//获取个人工作台 return result; } /// public async Task> GetRelationListByObjectIdListAndCategory(List objectIds, string category) { var sysRelations = await GetRelationByCategory(category); var result = sysRelations.Where(it => objectIds.Contains(it.ObjectId)).ToList();//获取关系集合 return result; } /// public async Task> GetRelationListByTargetIdAndCategory(string targetId, string category) { var sysRelations = await GetRelationByCategory(category); var result = sysRelations.Where(it => it.TargetId == targetId).ToList();//获取关系集合 return result; } /// public async Task> GetRelationListByTargetIdListAndCategory(List targetIds, string category) { var sysRelations = await GetRelationByCategory(category); var result = sysRelations.Where(it => targetIds.Contains(it.TargetId)).ToList();//获取关系集合 return result; } /// public async Task RefreshCache(string category) { var key = SystemConst.CACHE_SYS_RELATION + category;//key _simpleCacheService.Remove(key);//删除redis await GetRelationByCategory(category);//更新缓存 } /// public async Task SaveRelationBatch(string category, long objectId, List targetIds, List extJsons, bool clear) { var sysRelations = new List();//要添加的列表 for (var i = 0; i < targetIds.Count; i++) { sysRelations.Add(new SysRelation { ObjectId = objectId, TargetId = targetIds[i], Category = category, ExtJson = extJsons == null ? null : extJsons[i] }); } //事务 var result = await Tenant.UseTranAsync(async () => { if (clear) await DeleteAsync(it => it.ObjectId == objectId && it.Category == category);//删除老的 await InsertRangeAsync(sysRelations);//添加新的 }); if (result.IsSuccess)//如果成功了 { await RefreshCache(category); } else { //写日志 _logger.LogError(result.ErrorMessage, result.ErrorException); throw Oops.Oh(ErrorCodeEnum.A0003); } } /// public async Task SaveRelation(string category, long objectId, string targetId, string extJson, bool clear, bool refreshCache = true) { var sysRelation = new SysRelation { ObjectId = objectId, TargetId = targetId, Category = category, ExtJson = extJson }; //事务 var result = await Tenant.UseTranAsync(async () => { if (clear) await DeleteAsync(it => it.ObjectId == objectId && it.Category == category);//删除老的 await InsertAsync(sysRelation);//添加新的 }); if (result.IsSuccess)//如果成功了 { if (refreshCache) await RefreshCache(category); } else { //写日志 _logger.LogError(result.ErrorMessage, result.ErrorException); throw Oops.Oh(ErrorCodeEnum.A0003); } } /// public async Task> GetUserModuleId(List roleIdList, long userId) { var moduleIds = new List(); var roleRelation = await GetRelationByCategory(CateGoryConst.RELATION_SYS_ROLE_HAS_MODULE);//获取角色模块关系集合 if (roleRelation != null && roleRelation.Count > 0) { moduleIds = roleRelation.Where(it => roleIdList.Contains(it.ObjectId)).Select(it => it.TargetId.ToLong()).ToList(); } var userRelation = await GetRelationByCategory(CateGoryConst.RELATION_SYS_USER_HAS_MODULE);//获取用户模块关系集合 if (userRelation != null && userRelation.Count > 0) { var userModuleIds = userRelation.Where(it => it.ObjectId == userId).Select(it => it.TargetId.ToLong()).ToList(); moduleIds.AddRange(userModuleIds); } return moduleIds; } }