临时提交

This commit is contained in:
李鹏军 2024-03-16 10:29:04 +08:00
parent c30542024c
commit d819dfe6d8
7 changed files with 91 additions and 46 deletions

View File

@ -10,11 +10,16 @@ public class TrackPushConfigDto
/// <summary> /// <summary>
/// 轨迹推送内容配置 /// 轨迹推送内容配置
/// </summary> /// </summary>
public List<TrackPushContentConfigDto> ContentConfigs { get; set; } public List<TrackPushContentConfigDto> ContentConfigs { get; set; } = new();
} }
public class TrackPushContentConfigDto public class TrackPushContentConfigDto
{ {
/// <summary>
/// 服务商code
/// </summary>
public string ServiceProviderCode { get; set; } = null!;
/// <summary> /// <summary>
/// 扩展字段参数值 /// 扩展字段参数值
/// </summary> /// </summary>

View File

@ -20,7 +20,7 @@ public class TrackUpsertEvent : UpsertTrackInput
public int SubSystemId { get; set; } public int SubSystemId { get; set; }
/// <summary> /// <summary>
/// 最新推送代码(异步维护) /// 最新推送轨迹内容id(异步维护)
/// </summary> /// </summary>
public int? LatestPushTrackContentId { get; set; } public int? LatestPushTrackContentId { get; set; }

View File

@ -2,7 +2,7 @@
using SJZY.Track.Application.Contract.Dtos.ProvideServices; using SJZY.Track.Application.Contract.Dtos.ProvideServices;
using SJZY.Track.Application.Contract.Services; using SJZY.Track.Application.Contract.Services;
using SJZY.Track.Domain.Aggregates.OrderAggregate; using SJZY.Track.Domain.Aggregates.OrderAggregate;
using SJZY.Track.Domain.Aggregates.TrackContentAggregate; using SJZY.Track.Domain.Aggregates.TrackDetailsAggregate;
using Volo.Abp.Http.Client.DynamicProxying; using Volo.Abp.Http.Client.DynamicProxying;
namespace SJZY.Track.Application.Services.ProvideServices; namespace SJZY.Track.Application.Services.ProvideServices;
@ -14,23 +14,23 @@ namespace SJZY.Track.Application.Services.ProvideServices;
[Route("/ProviderService")] [Route("/ProviderService")]
public class TrackPushService : ApplicationService public class TrackPushService : ApplicationService
{ {
private readonly ITrackDetailsRepository _trackDetailsRepository;
private readonly IOrderRepository _orderRepository; private readonly IOrderRepository _orderRepository;
private readonly ITrackContentRepository _trackContentRepository;
private readonly ITrackPushCacheService _pushCacheService; private readonly ITrackPushCacheService _pushCacheService;
private readonly IHttpClientProxy<IInterfaceForwardServiceV2> _httpClientProxy; private readonly IHttpClientProxy<IInterfaceForwardServiceV2> _httpClientProxy;
public TrackPushService(IOrderRepository orderRepository, public TrackPushService(
IHttpClientProxy<IInterfaceForwardServiceV2> httpClientProxy, ITrackPushCacheService pushCacheService, IHttpClientProxy<IInterfaceForwardServiceV2> httpClientProxy, ITrackPushCacheService pushCacheService,
ITrackContentRepository trackContentRepository) ITrackDetailsRepository trackDetailsRepository, IOrderRepository orderRepository)
{ {
_orderRepository = orderRepository;
_httpClientProxy = httpClientProxy; _httpClientProxy = httpClientProxy;
_pushCacheService = pushCacheService; _pushCacheService = pushCacheService;
_trackContentRepository = trackContentRepository; _trackDetailsRepository = trackDetailsRepository;
_orderRepository = orderRepository;
} }
/// <summary> /// <summary>
/// 推送轨迹 /// 手工推送轨迹
/// </summary> /// </summary>
/// <param name="input"></param> /// <param name="input"></param>
/// <returns></returns> /// <returns></returns>
@ -38,10 +38,7 @@ public class TrackPushService : ApplicationService
public async Task<bool> PushTrackAsync(PushTrackInput input) public async Task<bool> PushTrackAsync(PushTrackInput input)
{ {
input.OrderNos = input.OrderNos.Where(x => !x.IsNullOrWhiteSpace()).Distinct().ToList(); // 单号去重、去空 input.OrderNos = input.OrderNos.Where(x => !x.IsNullOrWhiteSpace()).Distinct().ToList(); // 单号去重、去空
if (input.OrderNos.Count == 0)
{
throw new BusinessException(message: "订单一个都不存在");
}
var orders = await _orderRepository.GetListAsync(x => var orders = await _orderRepository.GetListAsync(x =>
x.OrderType == input.OrderType && input.OrderNos.Contains(x.OrderNo), x => new x.OrderType == input.OrderType && input.OrderNos.Contains(x.OrderNo), x => new
@ -49,27 +46,46 @@ public class TrackPushService : ApplicationService
x.Id, x.OrderNo, x.SubSystemId, x.CustomId x.Id, x.OrderNo, x.SubSystemId, x.CustomId
}); });
var trackContents = await _trackContentRepository.GetListAsync(x => x.Code == input.TrackCode, x => new if (input.OrderNos.Count == 0)
{ {
x.SubSystemId, x.Id throw new BusinessException(message: "一个推送配置都不存在");
}); }
foreach (var group in orders.GroupBy(x => var tracks = await _trackDetailsRepository.GetListAsync(input.OrderType, input.OrderNos, input.TrackCode);
new { x.SubSystemId, x.CustomId }))
foreach (var order in orders)
{ {
var trackContent = trackContents.FirstOrDefault(x => x.SubSystemId == group.Key.SubSystemId); // 如果没有轨迹,就不需要推
if (trackContent == null) continue; var track = tracks.FirstOrDefault(x => x.OrderId == order.Id);
var config = await _pushCacheService.GetPushContentConfigAsync(group.Key.SubSystemId, if (track == null) continue;
group.Key.CustomId, trackContent.Id);
var data = group.Select(x => new // 手工推送不推断,直接无脑推
var config =
await _pushCacheService.GetPushContentConfigAsync(order.SubSystemId, order.CustomId,
track.TrackContentId);
var data = new
{ {
x.OrderNo order.OrderNo,
}).ToList(); data = new[]
{
new
{
// 推真实的发生地和发生时间
location = track.TrackPlace,
date = track.TrackTime.AddHours(config.TrackTimeZone ??
track.TrackTimeZone - track.TrackTimeZone),
timeZone = config.TrackTimeZone ?? track.TrackTimeZone,
description = config.TrackEnContentValue,
description_cn = config.TrackContentValue,
code = config.TrackContentCodeValue,
status = config.TrackStatusValue,
extra = config.ExpandFieldsValue
}
}
};
// 批量获取轨迹 await _httpClientProxy.Service.ForwardWithSubscribeAsync("A024", config.ServiceProviderCode, data);
// await _httpClientProxy.Service.BatchForwardThenPushToQueueAsync(
// "A024", group.Key.ServiceProviderCode, data,
// GlobalConst.TrackRefreshSubscribe, JsonConvert.SerializeObject(group.Key));
} }
return true; return true;

View File

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

View File

@ -10,6 +10,8 @@ public interface ITrackDetailsRepository
Task<List<TrackDetailsIndex>> GetListAsync(int orderType, List<string> orderNos); Task<List<TrackDetailsIndex>> GetListAsync(int orderType, List<string> orderNos);
Task<List<TrackDetailsIndex>> GetListAsync(int orderType, List<string> orderNos, string trackCode);
Task<(List<TrackDetailsIndex> list, long total)> GetListAsync(TrackDetailsQueryInput input); Task<(List<TrackDetailsIndex> list, long total)> GetListAsync(TrackDetailsQueryInput input);
Task<List<TrackDetailsIndex>> GetListAsync(int orderId); Task<List<TrackDetailsIndex>> GetListAsync(int orderId);

View File

@ -11,6 +11,11 @@ public class TrackDetailsIndex : IdBaseIndex
/// </summary> /// </summary>
public int OrderId { get; set; } public int OrderId { get; set; }
/// <summary>
/// 服务商code
/// </summary>
public string? ServiceProviderCode { get; set; }
/// <summary> /// <summary>
/// 订单号 /// 订单号
/// </summary> /// </summary>

View File

@ -87,6 +87,11 @@ internal class TrackDetailsRepository : ElasticSearchContextAbstract<TrackDetail
return data.Documents.ToList(); return data.Documents.ToList();
} }
public Task<List<TrackDetailsIndex>> GetListAsync(int orderType, List<string> orderNos, string trackCode)
{
throw new NotImplementedException();
}
public async Task<List<TrackDetailsIndex>> GetListAsync(int orderId) public async Task<List<TrackDetailsIndex>> GetListAsync(int orderId)
{ {
var data = await Context.SearchAsync<TrackDetailsIndex>(x => x var data = await Context.SearchAsync<TrackDetailsIndex>(x => x
@ -134,11 +139,12 @@ internal class TrackDetailsRepository : ElasticSearchContextAbstract<TrackDetail
public async Task<bool> DeleteTrackAsync(int orderId, List<string> trackCodes) public async Task<bool> DeleteTrackAsync(int orderId, List<string> trackCodes)
{ {
var r = await Context.DeleteByQueryAsync<TrackDetailsIndex>(x => x var r = await Context.DeleteByQueryAsync<TrackDetailsIndex>(x => x
.Query( .Query(
q => q =>
q.Term(t => t.OrderId, orderId) q.Term(t => t.OrderId, orderId)
&& q.Term(t => t.IsCanDelete, true) && q.Term(t => t.IsCanDelete, true)
&& q.Terms(t => t.Field(f => f.TrackCode.Suffix("keyword")).Terms(trackCodes))) && q.Terms(t => t.Field(f => f.TrackCode.Suffix("keyword")).Terms(trackCodes)))
.Refresh() // 可选:立即刷新索引使更改立即可见
); );
if (!r.IsValid) if (!r.IsValid)