track/src/SJZY.Track.Application/Services/TrackPull/TrackPullCacheService.cs
2024-03-14 09:21:22 +08:00

57 lines
2.6 KiB
C#

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 System.Linq.Expressions;
using Volo.Abp.DependencyInjection;
namespace SJZY.Track.Application.Services.TrackPull;
public class TrackPullCacheService : ApplicationService, ITrackPullCacheService, ITransientDependency
{
private readonly ITrackPullConfigRepository _trackPullConfigRepository;
private string RedisKey(int subSystemId, int serviceProviderId) => GlobalConst.TrackPushKeyPrefix + subSystemId + ":" + serviceProviderId;
public TrackPullCacheService(ITrackPullConfigRepository trackPullConfigRepository)
{
_trackPullConfigRepository = trackPullConfigRepository;
}
[NonAction]
public async Task<TrackPullConfigDto> GetPullConfigAsync(int subSystemId, int serviceProviderId)
{
var data = await RedisHelper.Client.GetAsync<TrackPullConfigDto>(RedisKey(subSystemId, serviceProviderId));
data ??= await SetPullConfigAsync(subSystemId: subSystemId, serviceProviderId: serviceProviderId);
return data;
}
[NonAction]
public async Task<TrackPullConfigDto> SetPullConfigAsync(int? id = null, int? subSystemId = null, int? serviceProviderId = null)
{
var data = new TrackPullConfigDto();
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(RedisKey(trackPullConfigEntity.SubSystemId, trackPullConfigEntity.ServiceProviderId), data, 86400);
return data;
}
}