添加推送配置缓存
This commit is contained in:
parent
91ad7eebb8
commit
1d80a8de4e
@ -4,13 +4,6 @@ namespace SJZY.Track.Application.Contract.Services;
|
||||
|
||||
public interface ITrackPushCacheService
|
||||
{
|
||||
/// <summary>
|
||||
/// 判断客户是否需要推送
|
||||
/// </summary>
|
||||
/// <param name="customId"></param>
|
||||
/// <returns></returns>
|
||||
Task<bool> IsPushAsync(int customId);
|
||||
|
||||
/// <summary>
|
||||
/// 获取服务商拉取配置
|
||||
/// </summary>
|
||||
@ -30,9 +23,11 @@ public interface ITrackPushCacheService
|
||||
int? trackContentId);
|
||||
|
||||
/// <summary>
|
||||
/// 更新缓存
|
||||
/// 设置推送配置缓存
|
||||
/// </summary>
|
||||
/// <param name="customId"></param>
|
||||
/// <param name="isPush"></param>
|
||||
Task UpdateAsync(int customId, bool isPush);
|
||||
/// <param name="id"></param>
|
||||
/// <param name="subSystemId">子系统id</param>
|
||||
/// <param name="customId">客户id</param>
|
||||
/// <returns></returns>
|
||||
Task<List<TrackPushContentConfigDto>> SetPushContentConfigsAsync(int? id = null, int? subSystemId = null, int? customId = null);
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
using AutoMapper;
|
||||
using SJZY.Track.Application.Contract.Dtos.ProvideServices;
|
||||
using SJZY.Track.Application.Contract.Dtos.TrackPush;
|
||||
using SJZY.Track.Domain.Aggregates.TrackPullAggregate;
|
||||
using SJZY.Track.Domain.Aggregates.TrackPushAggregate;
|
||||
@ -14,5 +15,7 @@ public class TrackPushAutoMapperProfile : Profile
|
||||
CreateMap<EditTrackPushConfigInput, TrackPushPullParamsConfigEntity>(MemberList.None).Ignore(dest => dest.Id)
|
||||
.ForMember(dest => dest.IsPush, opt => opt.MapFrom(source => true));
|
||||
CreateMap<EditTrackPushContentInput, TrackPushContentConfigEntity>(MemberList.None);
|
||||
CreateMap<TrackPushContentConfigEntity, TrackPushContentConfigDto>(MemberList.None)
|
||||
.ForMember(dest => dest.OrderType, opt => opt.MapFrom(source => source.OrderNoType));
|
||||
}
|
||||
}
|
||||
@ -4,10 +4,11 @@ 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
|
||||
public class TrackPullCacheService : ApplicationService, ITrackPullCacheService, ITransientDependency
|
||||
{
|
||||
private readonly ITrackPullConfigRepository _trackPullConfigRepository;
|
||||
|
||||
@ -59,6 +60,7 @@ public class TrackPullCacheService : ApplicationService, ITrackPullCacheService
|
||||
|
||||
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);
|
||||
@ -72,7 +74,7 @@ public class TrackPullCacheService : ApplicationService, ITrackPullCacheService
|
||||
|
||||
await RedisHelper.Client.SetAsync(
|
||||
TrackPullContentKey(trackPullConfigEntity.SubSystemId, trackPullConfigEntity.ServiceProviderId), data,
|
||||
86400);
|
||||
(int)TimeSpan.FromDays(1).TotalSeconds);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
@ -212,16 +212,21 @@ public class TrackPullService : ApplicationService, ITransientDependency
|
||||
ObjectMapper.Map(input, oldTrackPullContentEntity);
|
||||
|
||||
await _trackPullConfigRepository.UpdateContentAsync(oldTrackPullContentEntity);
|
||||
await _trackPullCacheService.SetPullConfigAsync(input.TrackPullConfigId);
|
||||
await _trackPullCacheService.SetPullConfigAsync(oldTrackPullContentEntity.TrackPullConfigId);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case OptionTypeNoNull.删除:
|
||||
{
|
||||
var oldTrackPullContentEntity = await _trackPullConfigRepository.GetContentByIdAsync(input.Id!.Value) ??
|
||||
throw new BusinessException(message: _stringLocalizer["NotFindData"]);
|
||||
|
||||
await _trackPullConfigRepository.SoftDeleteContentAsync(x => x.Id == input.Id);
|
||||
await _trackPullConfigRepository.SoftDeleteContentAsync(x => x.Id == input.Id);
|
||||
await _trackPullCacheService.SetPullConfigAsync(oldTrackPullContentEntity.TrackPullConfigId);
|
||||
|
||||
break;
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
throw new BusinessException(message: _stringLocalizer[SjzyBasicLocalization.Validation_IsInEnum,
|
||||
|
||||
@ -1,41 +1,78 @@
|
||||
using SJZY.Expand.ABP.Core;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Nest;
|
||||
using SJZY.Expand.ABP.Core;
|
||||
using SJZY.Track.Application.Contract.Dtos.ProvideServices;
|
||||
using SJZY.Track.Application.Contract.Services;
|
||||
using SJZY.Track.Domain.Aggregates.TrackPullAggregate;
|
||||
using SJZY.Track.Domain.Aggregates.TrackPushAggregate;
|
||||
using SJZY.Track.Domain.Queries;
|
||||
using SJZY.UserCenter.SharedModel.Dtos.PHP.User;
|
||||
using System.Linq.Expressions;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
|
||||
namespace SJZY.Track.Application.Services.TrackPush;
|
||||
|
||||
public class TrackPushCacheService : ITrackPushCacheService, ITransientDependency
|
||||
public class TrackPushCacheService : ApplicationService, ITrackPushCacheService, ITransientDependency
|
||||
{
|
||||
private readonly IBasicQuery<TrackPushConfigEntity> _trackPushConfigQuery;
|
||||
private readonly ITrackPushConfigRepository _trackPushConfigRepository;
|
||||
|
||||
private string RedisKey(int customId) => $"{GlobalConst.TrackPushKeyPrefix}:{customId}";
|
||||
private string TrackPushContentKey(int subSystemId, int customId) => $"{GlobalConst.TrackPushKeyPrefix}:{subSystemId}:{customId}";
|
||||
|
||||
public TrackPushCacheService(IBasicQuery<TrackPushConfigEntity> trackPushConfigQuery)
|
||||
public TrackPushCacheService(ITrackPushConfigRepository trackPushConfigRepository)
|
||||
{
|
||||
_trackPushConfigQuery = trackPushConfigQuery;
|
||||
_trackPushConfigRepository = trackPushConfigRepository;
|
||||
}
|
||||
|
||||
public Task<bool> IsPushAsync(int customId)
|
||||
[NonAction]
|
||||
public async Task<List<TrackPushContentConfigDto>> GetPushContentConfigsAsync(int subSystemId, int customId)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var data = await RedisHelper.Client.GetAsync<List<TrackPushContentConfigDto>>(TrackPushContentKey(subSystemId, customId));
|
||||
|
||||
data ??= await SetPushContentConfigsAsync(subSystemId: subSystemId, customId: customId);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
public Task<List<TrackPushContentConfigDto>> GetPushContentConfigsAsync(int subSystemId, int customId)
|
||||
[NonAction]
|
||||
public async Task<TrackPushContentConfigDto?> InferNextPushContentOrNullAsync(int subSystemId, int customId, int? trackContentId)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var data = await GetPushContentConfigsAsync(subSystemId, customId);
|
||||
|
||||
if (trackContentId.HasValue)
|
||||
{
|
||||
if (data.Count > 1 && data.Any(x => x.TrackContentId == trackContentId))
|
||||
{
|
||||
int index = data.FindIndex(x => x.TrackContentId == trackContentId);
|
||||
int nextIndex = (index + 1) % data.Count; // 计算下一个索引,处理最后一个元素的情况
|
||||
|
||||
return data[nextIndex];
|
||||
}
|
||||
else return null;
|
||||
}
|
||||
else return data.FirstOrDefault();
|
||||
}
|
||||
|
||||
public Task<TrackPushContentConfigDto?> InferNextPushContentOrNullAsync(int subSystemId, int customId,
|
||||
int? trackContentId)
|
||||
[NonAction]
|
||||
public async Task<List<TrackPushContentConfigDto>> SetPushContentConfigsAsync(int? id = null, int? subSystemId = null, int? customId = null)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
var data = new List<TrackPushContentConfigDto>();
|
||||
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;
|
||||
|
||||
public async Task UpdateAsync(int customId, bool isPush)
|
||||
{
|
||||
await RedisHelper.Client.SetAsync(RedisKey(customId), isPush);
|
||||
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 = ObjectMapper.Map<List<TrackPushContentConfigEntity>, List<TrackPushContentConfigDto>>(trackPushContentList);
|
||||
|
||||
await RedisHelper.Client.SetAsync(TrackPushContentKey(trackPushConfigEntity.SubSystemId, trackPushConfigEntity.CustomId), data, (int)TimeSpan.FromDays(1).TotalSeconds);
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
@ -94,8 +94,7 @@ public class TrackPushService : ApplicationService
|
||||
if (input.Options != OptionTypeNoNull.删除)
|
||||
{
|
||||
Expression<Func<TrackPushConfigEntity, bool>> isAnyExp = x =>
|
||||
x.SubSystemId == input.SubSystemId && x.CustomId == input.CustomId &&
|
||||
x.ServiceProviderId == input.ServiceProviderId;
|
||||
x.SubSystemId == input.SubSystemId && x.CustomId == input.CustomId;
|
||||
if (input.Options == OptionTypeNoNull.修改) isAnyExp = isAnyExp.And(x => x.Id != input.Id);
|
||||
|
||||
if (await _trackPushConfigRepository.IsAnyAsync(isAnyExp))
|
||||
@ -105,68 +104,62 @@ public class TrackPushService : ApplicationService
|
||||
switch (input.Options)
|
||||
{
|
||||
case OptionTypeNoNull.新增:
|
||||
{
|
||||
var trackPushEntity = ObjectMapper.Map<EditTrackPushConfigInput, TrackPushConfigEntity>(input);
|
||||
var trackPushParamsEntity =
|
||||
ObjectMapper.Map<EditTrackPushConfigInput, TrackPushPullParamsConfigEntity>(input);
|
||||
|
||||
using (var ts = TransactionScopeHelper.Create())
|
||||
{
|
||||
int id = await _trackPushConfigRepository.InsertReturnIdentityAsync(trackPushEntity);
|
||||
var trackPushEntity = ObjectMapper.Map<EditTrackPushConfigInput, TrackPushConfigEntity>(input);
|
||||
var trackPushParamsEntity =
|
||||
ObjectMapper.Map<EditTrackPushConfigInput, TrackPushPullParamsConfigEntity>(input);
|
||||
|
||||
trackPushParamsEntity.PullOrPushConfigId = id;
|
||||
await _trackPullConfigRepository.InsertParamsAsync(trackPushParamsEntity);
|
||||
using (var ts = TransactionScopeHelper.Create())
|
||||
{
|
||||
int id = await _trackPushConfigRepository.InsertReturnIdentityAsync(trackPushEntity);
|
||||
|
||||
if (input.IsAutoPush!.Value) await _trackPushCacheService.IsPushAsync(input.CustomId!.Value);
|
||||
trackPushParamsEntity.PullOrPushConfigId = id;
|
||||
await _trackPullConfigRepository.InsertParamsAsync(trackPushParamsEntity);
|
||||
|
||||
ts.Complete();
|
||||
ts.Complete();
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case OptionTypeNoNull.修改:
|
||||
{
|
||||
var oldTrackPushEntity = await _trackPushConfigRepository.GetFirstAsync(x => x.Id == input.Id) ??
|
||||
throw new BusinessException(message: _stringLocalizer["NotFindData"]);
|
||||
|
||||
int oldCustomId = oldTrackPushEntity.CustomId;
|
||||
|
||||
var oldTrackPushParamsEntity =
|
||||
await _trackPullConfigRepository.GetParamsSingleAsync(x =>
|
||||
x.PullOrPushConfigId == input.Id && x.IsPush);
|
||||
|
||||
ObjectMapper.Map(input, oldTrackPushEntity);
|
||||
ObjectMapper.Map(input, oldTrackPushParamsEntity);
|
||||
|
||||
using (var ts = TransactionScopeHelper.Create())
|
||||
{
|
||||
await _trackPushConfigRepository.UpdateAsync(oldTrackPushEntity);
|
||||
await _trackPullConfigRepository.UpdateParamsAsync(oldTrackPushParamsEntity);
|
||||
var oldTrackPushEntity = await _trackPushConfigRepository.GetFirstAsync(x => x.Id == input.Id) ??
|
||||
throw new BusinessException(message: _stringLocalizer["NotFindData"]);
|
||||
|
||||
await _trackPushCacheService.UpdateAsync(oldCustomId, false);
|
||||
if (input.IsAutoPush!.Value) await _trackPushCacheService.IsPushAsync(input.CustomId!.Value);
|
||||
int oldCustomId = oldTrackPushEntity.CustomId;
|
||||
|
||||
ts.Complete();
|
||||
var oldTrackPushParamsEntity =
|
||||
await _trackPullConfigRepository.GetParamsSingleAsync(x =>
|
||||
x.PullOrPushConfigId == input.Id && x.IsPush);
|
||||
|
||||
ObjectMapper.Map(input, oldTrackPushEntity);
|
||||
ObjectMapper.Map(input, oldTrackPushParamsEntity);
|
||||
|
||||
using (var ts = TransactionScopeHelper.Create())
|
||||
{
|
||||
await _trackPushConfigRepository.UpdateAsync(oldTrackPushEntity);
|
||||
await _trackPullConfigRepository.UpdateParamsAsync(oldTrackPushParamsEntity);
|
||||
|
||||
ts.Complete();
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case OptionTypeNoNull.删除:
|
||||
{
|
||||
var oldTrackPushEntity = await _trackPushConfigRepository.GetFirstAsync(x => x.Id == input.Id) ??
|
||||
throw new BusinessException(message: _stringLocalizer["NotFindData"]);
|
||||
|
||||
using (var ts = TransactionScopeHelper.Create())
|
||||
{
|
||||
await _trackPushConfigRepository.SoftDeleteAsync(x => x.Id == input.Id);
|
||||
await _trackPullConfigRepository.SoftDeleteParamsAsync(x =>
|
||||
x.PullOrPushConfigId == input.Id && x.IsPush);
|
||||
await _trackPushConfigRepository.SoftDeleteContentAsync(x => x.TrackPushConfigId == input.Id);
|
||||
await _trackPushCacheService.UpdateAsync(oldTrackPushEntity.CustomId, false);
|
||||
var oldTrackPushEntity = await _trackPushConfigRepository.GetFirstAsync(x => x.Id == input.Id) ??
|
||||
throw new BusinessException(message: _stringLocalizer["NotFindData"]);
|
||||
|
||||
ts.Complete();
|
||||
using (var ts = TransactionScopeHelper.Create())
|
||||
{
|
||||
await _trackPushConfigRepository.SoftDeleteAsync(x => x.Id == input.Id);
|
||||
await _trackPullConfigRepository.SoftDeleteParamsAsync(x =>
|
||||
x.PullOrPushConfigId == input.Id && x.IsPush);
|
||||
await _trackPushConfigRepository.SoftDeleteContentAsync(x => x.TrackPushConfigId == input.Id);
|
||||
|
||||
ts.Complete();
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
@ -215,47 +208,50 @@ public class TrackPushService : ApplicationService
|
||||
switch (input.Options)
|
||||
{
|
||||
case OptionTypeNoNull.新增:
|
||||
{
|
||||
int maxSort = await _trackPushConfigRepository.ContentMaxAsync(x => x.Sort,
|
||||
x => x.TrackPushConfigId == input.TrackPushConfigId);
|
||||
|
||||
var entity = ObjectMapper.Map<EditTrackPushContentInput, TrackPushContentConfigEntity>(input);
|
||||
entity.Sort = maxSort + 1;
|
||||
|
||||
await _trackPushConfigRepository.InsertContentAsync(entity);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case OptionTypeNoNull.修改:
|
||||
{
|
||||
var entity = await _trackPushConfigRepository.GetContentByIdAsync(input.Id!.Value) ??
|
||||
throw new BusinessException(message: _stringLocalizer["NotFindData"]);
|
||||
|
||||
ObjectMapper.Map(input, entity);
|
||||
|
||||
await _trackPushConfigRepository.UpdateContentAsync(entity);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case OptionTypeNoNull.删除:
|
||||
{
|
||||
var entity = await _trackPushConfigRepository.GetContentByIdAsync(input.Id!.Value) ??
|
||||
throw new BusinessException(message: _stringLocalizer["NotFindData"]);
|
||||
|
||||
using (var ts = TransactionScopeHelper.Create())
|
||||
{
|
||||
await _trackPushConfigRepository.SoftDeleteContentAsync(x => x.Id == input.Id);
|
||||
int maxSort = await _trackPushConfigRepository.ContentMaxAsync(x => x.Sort,
|
||||
x => x.TrackPushConfigId == input.TrackPushConfigId);
|
||||
|
||||
await _trackPushConfigRepository.UpdateContentAsync(x => x.Sort == x.Sort - 1,
|
||||
x => x.TrackPushConfigId == entity.TrackPushConfigId && x.Sort > entity.Sort);
|
||||
var entity = ObjectMapper.Map<EditTrackPushContentInput, TrackPushContentConfigEntity>(input);
|
||||
entity.Sort = maxSort + 1;
|
||||
|
||||
ts.Complete();
|
||||
await _trackPushConfigRepository.InsertContentAsync(entity);
|
||||
await _trackPushCacheService.SetPushContentConfigsAsync(input.TrackPushConfigId);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case OptionTypeNoNull.修改:
|
||||
{
|
||||
var entity = await _trackPushConfigRepository.GetContentByIdAsync(input.Id!.Value) ??
|
||||
throw new BusinessException(message: _stringLocalizer["NotFindData"]);
|
||||
|
||||
ObjectMapper.Map(input, entity);
|
||||
|
||||
await _trackPushConfigRepository.UpdateContentAsync(entity);
|
||||
await _trackPushCacheService.SetPushContentConfigsAsync(entity.TrackPushConfigId);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case OptionTypeNoNull.删除:
|
||||
{
|
||||
var entity = await _trackPushConfigRepository.GetContentByIdAsync(input.Id!.Value) ??
|
||||
throw new BusinessException(message: _stringLocalizer["NotFindData"]);
|
||||
|
||||
using (var ts = TransactionScopeHelper.Create())
|
||||
{
|
||||
await _trackPushConfigRepository.SoftDeleteContentAsync(x => x.Id == input.Id);
|
||||
await _trackPushConfigRepository.UpdateContentAsync(x => x.Sort == x.Sort - 1,
|
||||
x => x.TrackPushConfigId == entity.TrackPushConfigId && x.Sort > entity.Sort);
|
||||
|
||||
ts.Complete();
|
||||
}
|
||||
|
||||
await _trackPushCacheService.SetPushContentConfigsAsync(entity.TrackPushConfigId);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
throw new BusinessException(message: _stringLocalizer[SjzyBasicLocalization.Validation_IsInEnum,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user