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 System.Linq.Expressions; 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> GetPushContentConfigsAsync(int subSystemId, int customId) { var data = await GetPushConfigAsync(subSystemId, customId); return data.ContentConfigs; } [NonAction] public async Task GetPushContentConfigAsync(int subSystemId, int customId, int trackContentId) { var data = await GetPushContentConfigsAsync(subSystemId, customId); return data.Single(x => x.TrackContentId == trackContentId); } [NonAction] public async Task 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); int nextIndex = (index + 1) % data.ContentConfigs.Count; // 计算下一个索引,处理最后一个元素的情况 return data.ContentConfigs[nextIndex]; } else return null; } else return data.ContentConfigs.FirstOrDefault(); } [NonAction] public async Task SetPushContentConfigsAsync(int? id = null, int? subSystemId = null, int? customId = null) { var data = new TrackPushConfigDto { ContentConfigs = [] }; Expression> 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>(trackPushContentList); await RedisHelper.Client.SetAsync(TrackPushContentKey(trackPushConfigEntity.SubSystemId, trackPushConfigEntity.CustomId), data); return data; } private async Task GetPushConfigAsync(int subSystemId, int customId) { var data = await RedisHelper.Client.GetAsync(TrackPushContentKey(subSystemId, customId)); data ??= await SetPushContentConfigsAsync(subSystemId: subSystemId, customId: customId); return data; } }