track/src/SJZY.Track.Application/Services/TrackPull/TrackPullCacheService.cs
2024-03-15 16:09:27 +08:00

81 lines
3.3 KiB
C#

using System.Linq.Expressions;
using SJZY.Expand.ABP.Core;
using SJZY.Track.Application.Contract.Dtos.TrackPull;
using SJZY.Track.Application.Contract.Services;
using SJZY.Track.Domain.Aggregates.TrackPullAggregate;
using SJZY.Track.Domain.Dtos.TrackPull;
using Volo.Abp.DependencyInjection;
namespace SJZY.Track.Application.Services.TrackPull;
public class TrackPullCacheService : ApplicationService, ITrackPullCacheService, ITransientDependency
{
private readonly ITrackPullConfigRepository _trackPullConfigRepository;
public string TrackPullKey(int subSystemId, int serviceProviderId) =>
$"{GlobalConst.TrackPullKeyPrefix}:{subSystemId}:{serviceProviderId}";
public string TrackPullContentKey(int subSystemId, int serviceProviderId) =>
$"{GlobalConst.TrackPullContentKeyPrefix}:{subSystemId}:{serviceProviderId}";
public TrackPullCacheService(ITrackPullConfigRepository trackPullConfigRepository)
{
_trackPullConfigRepository = trackPullConfigRepository;
}
[NonAction]
public async Task<List<TrackPullConfigDto>> GetAllPullConfigsAsync()
{
return await _trackPullConfigRepository.GetListAsync(x => true, x => new TrackPullConfigDto
{
SubSystemId = x.SubSystemId,
ServiceProviderId = x.ServiceProviderId,
PullInterval = x.PullInterval
});
}
[NonAction]
public async Task<TrackPullParamWithContentConfigDto> GetPullParamWithContentConfigAsync(int subSystemId,
int serviceProviderId)
{
var data = await RedisHelper.Client.GetAsync<TrackPullParamWithContentConfigDto>(TrackPullContentKey(
subSystemId,
serviceProviderId));
data ??= await SetPullConfigAsync(subSystemId: subSystemId, serviceProviderId: serviceProviderId);
return data;
}
[NonAction]
public async Task<TrackPullParamWithContentConfigDto> SetPullConfigAsync(int? id = null, int? subSystemId = null,
int? serviceProviderId = null)
{
var data = new TrackPullParamWithContentConfigDto();
Expression<Func<TrackPullConfigEntity, bool>> exp = x => true;
if (id.HasValue) exp = x => x.Id == id;
else if (subSystemId.HasValue && serviceProviderId.HasValue)
exp = x => x.SubSystemId == subSystemId && x.ServiceProviderId == serviceProviderId;
var trackPullConfigEntity = await _trackPullConfigRepository.GetFirstAsync(exp);
if (trackPullConfigEntity == null) return data;
var trackPullConfigDetail = await _trackPullConfigRepository.GetDetailAsync(trackPullConfigEntity.Id);
if (trackPullConfigDetail is null) return data;
var trackPullContentList = await _trackPullConfigRepository.GetContentListCacheAsync(trackPullConfigEntity.Id);
if (trackPullContentList.Count == 0) return data;
data.ParamConfig =
ObjectMapper.Map<GetTrackPullConfigDetailOutputVO, TrackPullParamConfigDto>(trackPullConfigDetail);
data.ContentConfigs =
ObjectMapper.Map<List<GetTrackPullContentListCacheVO>, List<TrackPullContentConfigDto>>(
trackPullContentList);
await RedisHelper.Client.SetAsync(
TrackPullContentKey(trackPullConfigEntity.SubSystemId, trackPullConfigEntity.ServiceProviderId), data,
(int)TimeSpan.FromDays(1).TotalSeconds);
return data;
}
}