ComplianceServer/code/Hg.Complaint.Domain/CacheDomain.cs

96 lines
3.4 KiB
C#

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<DncmsbaseDbContext> _dncmsbaseRepository;
private readonly IHttpClient _httpClient;
private readonly SystemConfig _systemConfig;
private readonly IConfiguration _configuration;
public CacheDomain(IRedisManager redisManager,
IBaseRepository<DncmsbaseDbContext> dncmsbaseRepository,
IHttpClient httpClient,
IConfiguration configuration)
{
_configuration = configuration;
_redisManager = redisManager;
_dncmsbaseRepository = dncmsbaseRepository;
_httpClient = httpClient;
_systemConfig = configuration.GetSection("SystemConfig").Get<SystemConfig>();
}
public async Task<List<DeptmentDto>> GetDeptments()
{
var key = $"{CacheKeys.ZxdParameterList}";
if (!await _redisManager.ExistsAsync(key))
{
var response = await _httpClient.GetAsync<ApiResult<List<DeptmentDto>>>($"{_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<DeptmentDto>(key);
}
return new List<DeptmentDto>();
}
public async Task<List<DeptmentGroupDto>> GetDeptmentGroups()
{
var key = CacheKeys.DeptmentGroup;
if (!await _redisManager.ExistsAsync(key))
{
var query = _dncmsbaseRepository.GetRepository<DeptmentGroup>().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<Deptment> {
new Deptment {
Id = 0,
Title = "总部"
}
}
});
await _redisManager.SetAsync(key, data, TimeSpan.FromDays(1));
return data;
}
else
{
var data = await _redisManager.GetListAsync<DeptmentGroupDto>(key);
return data;
}
}
public async Task<DeptmentGroupDto?> GetDeptmentGroupByDeptid(int? deptid)
{
if (deptid == null) return null;
var list = await GetDeptmentGroups();
return list.FirstOrDefault(x => x.Deptments.Any(y => y.Id == deptid)) ?? null;
}
}
}