110 lines
3.9 KiB
C#
110 lines
3.9 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
||
using Microsoft.Extensions.Options;
|
||
using TrackCenter.Application.Dtos.ProvideServices;
|
||
using TrackCenter.Application.Dtos.TrackPull;
|
||
using TrackCenter.Application.Helpers;
|
||
using TrackCenter.Application.Options;
|
||
using TrackCenter.Application.Queries;
|
||
using TrackCenter.Application.Rabbit;
|
||
using TrackCenter.Application.Track2InterfaceRelay;
|
||
using TrackCenter.Domain.Extensions;
|
||
|
||
namespace TrackCenter.Application.Services.ProvideServices;
|
||
|
||
/// <summary>
|
||
/// 轨迹刷新服务
|
||
/// </summary>
|
||
[ApiExplorerSettings(GroupName = "提供接口")]
|
||
[Route("/ProviderService")]
|
||
public class TrackPullService : ApplicationServiceBase
|
||
{
|
||
private readonly IHttpClientProxy<IInterfaceForwardServiceV2> _httpClientProxy;
|
||
private readonly IOptionsMonitor<InterfaceRelayParamsConfigOptions> _options;
|
||
private readonly ProvideQueries _provideQueries;
|
||
|
||
public TrackPullService(ProvideQueries provideQueries,
|
||
IHttpClientProxy<IInterfaceForwardServiceV2> httpClientProxy,
|
||
IOptionsMonitor<InterfaceRelayParamsConfigOptions> options)
|
||
{
|
||
_provideQueries = provideQueries;
|
||
_httpClientProxy = httpClientProxy;
|
||
_options = options;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 手动刷新轨迹
|
||
/// </summary>
|
||
/// <param name="input"></param>
|
||
/// <returns></returns>
|
||
[HttpPost("Track/Refresh")]
|
||
public async Task<bool> RefreshTrackAsync(RefreshTrackInput input)
|
||
{
|
||
// todo:罚时?
|
||
input.OrderNos = input.OrderNos.Where(x => !x.IsNullOrWhiteSpace()).Distinct().ToList(); // 单号去重、去空
|
||
|
||
// tips:这里没有区分订单类型,就算的不同类型的单一起刷新了也无所谓
|
||
var orders = await _provideQueries.FindOrderWithServiceProviderAsync(input.SubSystemId, input.OrderNos);
|
||
|
||
if (orders.Count == 0)
|
||
{
|
||
// tips:可能是没有配置、或者所有单都刷完了、或者单不存在
|
||
throw new BusinessException(message: "一个能更新的都没有");
|
||
}
|
||
|
||
foreach (var group in orders.GroupBy(x =>
|
||
new
|
||
{
|
||
x.ServiceProviderCode,
|
||
x.SubSystemId,
|
||
x.SubSystemCode,
|
||
x.OrderType
|
||
}))
|
||
{
|
||
var data = new List<object>(group.Select(x => new TrackPullPayload
|
||
{
|
||
OrderNo = x.OrderNo
|
||
}));
|
||
var serviceProviderCode = group.FirstOrDefault()!.ServiceProviderCode;
|
||
|
||
var attach = Base64Helper.SerializeObject(new TrackRefreshAttach(group.Key.ServiceProviderCode,
|
||
group.Key.SubSystemId, group.Key.OrderType));
|
||
|
||
using (Track2InterfaceRelaySubSystemContext.Use(
|
||
new Track2InterfaceRelaySubSystemContext(group.Key.SubSystemCode)))
|
||
{
|
||
// 批量获取轨迹
|
||
var res = await _httpClientProxy.Service.BatchForwardThenPushToQueueAsync(
|
||
_options.CurrentValue.GetTrackInterfaceCode, serviceProviderCode, data,
|
||
GlobalConst.TrackRefreshSubscribe,
|
||
attach);
|
||
|
||
if (res.Code != GlobalConst.SuccessCode)
|
||
{
|
||
LogHelper.Error($"刷新轨迹失败:{res.ToJson()}");
|
||
}
|
||
}
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 轨迹刷新测试
|
||
/// </summary>
|
||
/// <param name="trackRefreshEventHandler"></param>
|
||
/// <param name="input"></param>
|
||
/// <returns></returns>
|
||
[HttpPost("Track/TrackRefreshEventTest")]
|
||
#if DEBUG
|
||
[AllowAnonymous]
|
||
#endif
|
||
public async Task<bool> TrackRefreshEventTest([FromServices] TrackRefreshEventHandler trackRefreshEventHandler,
|
||
InterfaceRelayForwardReplayEvent input)
|
||
{
|
||
await trackRefreshEventHandler.ConsumerAsync(input);
|
||
|
||
return true;
|
||
}
|
||
}
|