using DG.Redis; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Zxd.Core.Shared.Dto; namespace Hg.Complaint.Domain { internal class CacheDomain : ICacheDomain { private readonly IRedisManager _redisManager; private readonly IBaseRepository _dncmsbaseRepository; private readonly IHttpClient _httpClient; private readonly SystemConfig _systemConfig; private readonly IConfiguration _configuration; public CacheDomain(IRedisManager redisManager, IBaseRepository dncmsbaseRepository, IHttpClient httpClient, IConfiguration configuration) { _configuration = configuration; _redisManager = redisManager; _dncmsbaseRepository = dncmsbaseRepository; _httpClient = httpClient; _systemConfig = configuration.GetSection("SystemConfig").Get(); } public async Task> GetDeptments() { var key = $"{CacheKeys.ZxdParameterList}"; if (!await _redisManager.ExistsAsync(key)) { var response = await _httpClient.GetAsync>>($"{_systemConfig.ZxdCoreUrl}/Api/Deptment/Depts"); if (response.Code == 0) { await _redisManager.SetAsync(key, response.Data, TimeSpan.FromDays(1)); return response.Data; } } else { return await _redisManager.GetListAsync(key); } return new List(); } public async Task> GetDeptmentGroups() { var key = CacheKeys.DeptmentGroup; if (!await _redisManager.ExistsAsync(key)) { var query = _dncmsbaseRepository.GetRepository().Query() .Include(x => x.Deptments) .Select(x => new DeptmentGroupDto { Id = x.Id, CrmAppid = x.CrmAppid, GroupName = x.GroupName, Deptments = x.Deptments, }); var data = await query.ToListAsync(); data.Add(new DeptmentGroupDto { Id = 0, CrmAppid = "", GroupName = "总部", Deptments = new List { new Deptment { Id = 0, Title = "总部" } } }); await _redisManager.SetAsync(key, data, TimeSpan.FromDays(1)); return data; } else { var data = await _redisManager.GetListAsync(key); return data; } } public async Task GetDeptmentGroupByDeptid(int? deptid) { if (deptid == null) return null; var list = await GetDeptmentGroups(); return list.FirstOrDefault(x => x.Deptments.Any(y => y.Id == deptid)) ?? null; } } }