track/src/SJZY.Track.Application/Services/TrackPush/TrackPushCacheService.cs
2024-03-16 10:29:04 +08:00

101 lines
3.9 KiB
C#

using System.Linq.Expressions;
using SJZY.Expand.ABP.Core;
using SJZY.Track.Application.Contract.Dtos.ProvideServices;
using SJZY.Track.Application.Contract.Services;
using SJZY.Track.Domain.Aggregates.TrackPushAggregate;
using Volo.Abp.DependencyInjection;
namespace SJZY.Track.Application.Services.TrackPush;
public class TrackPushCacheService : ApplicationService, ITrackPushCacheService, ITransientDependency
{
private readonly ITrackPushConfigRepository _trackPushConfigRepository;
private string TrackPushContentKey(int subSystemId, int customId) =>
$"{GlobalConst.TrackPushKeyPrefix}:{subSystemId}:{customId}";
public TrackPushCacheService(ITrackPushConfigRepository trackPushConfigRepository)
{
_trackPushConfigRepository = trackPushConfigRepository;
}
[NonAction]
public async Task<List<TrackPushContentConfigDto>> GetPushContentConfigsAsync(int subSystemId, int customId)
{
var data = await GetPushConfigAsync(subSystemId, customId);
return data.ContentConfigs;
}
[NonAction]
public async Task<TrackPushContentConfigDto> GetPushContentConfigAsync(int subSystemId, int customId,
int trackContentId)
{
var data = await GetPushContentConfigsAsync(subSystemId, customId);
return data.Single(x => x.TrackContentId == trackContentId);
}
[NonAction]
public async Task<TrackPushContentConfigDto?> InferNextPushContentOrNullAsync(int subSystemId, int customId,
int? trackContentId)
{
var data = await GetPushConfigAsync(subSystemId, customId);
if (!data.IsAutoPush) return null;
if (trackContentId.HasValue)
{
if (data.ContentConfigs.Count > 1 && data.ContentConfigs.Any(x => x.TrackContentId == trackContentId))
{
int index = data.ContentConfigs.FindIndex(x => x.TrackContentId == trackContentId);
if (index == data.ContentConfigs.Count - 1) return null;
int nextIndex = (index + 1) % data.ContentConfigs.Count; // 计算下一个索引,处理最后一个元素的情况
return data.ContentConfigs[nextIndex];
}
return null;
}
return data.ContentConfigs.FirstOrDefault();
}
[NonAction]
public async Task<TrackPushConfigDto> SetPushContentConfigsAsync(int? id = null, int? subSystemId = null,
int? customId = null)
{
var data = new TrackPushConfigDto { ContentConfigs = new() };
Expression<Func<TrackPushConfigEntity, bool>> exp = x => true;
if (id.HasValue) exp = x => x.Id == id;
else if (subSystemId.HasValue && customId.HasValue)
exp = x => x.SubSystemId == subSystemId && x.CustomId == customId;
var trackPushConfigEntity = await _trackPushConfigRepository.GetFirstAsync(exp);
if (trackPushConfigEntity == null) return data;
var trackPushContentList =
await _trackPushConfigRepository.GetContentListAsync(x => x.TrackPushConfigId == trackPushConfigEntity.Id);
if (trackPushContentList.Count == 0) return data;
trackPushContentList = trackPushContentList.OrderBy(x => x.Sort).ToList();
data.IsAutoPush = trackPushConfigEntity.IsAutoPush;
data.ContentConfigs =
ObjectMapper.Map<List<TrackPushContentConfigEntity>, List<TrackPushContentConfigDto>>(trackPushContentList);
await RedisHelper.Client.SetAsync(
TrackPushContentKey(trackPushConfigEntity.SubSystemId, trackPushConfigEntity.CustomId), data);
return data;
}
private async Task<TrackPushConfigDto> GetPushConfigAsync(int subSystemId, int customId)
{
var data = await RedisHelper.Client.GetAsync<TrackPushConfigDto>(TrackPushContentKey(subSystemId, customId));
data ??= await SetPushContentConfigsAsync(subSystemId: subSystemId, customId: customId);
return data;
}
}