99 lines
3.7 KiB
C#
99 lines
3.7 KiB
C#
using InterfaceForward.Domain.Shared.Helpers;
|
||
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.Track2InterfaceRelay;
|
||
using TrackCenter.Domain.Consts;
|
||
|
||
namespace TrackCenter.Application.Services.ProvideServices;
|
||
|
||
/// <summary>
|
||
/// 轨迹刷新服务
|
||
/// </summary>
|
||
[ApiExplorerSettings(GroupName = "OpenApi")]
|
||
[Route("/ProviderService")]
|
||
public class TrackPullService(
|
||
ProvideQueries provideQueries,
|
||
InterfaceForwardService interfaceForwardService,
|
||
IOptionsMonitor<InterfaceRelayParamsConfigOptions> options)
|
||
: ApplicationServiceBase
|
||
{
|
||
/// <summary>
|
||
/// 手动刷新轨迹
|
||
/// </summary>
|
||
/// <param name="input"></param>
|
||
/// <returns></returns>
|
||
[HttpPost("Track/Refresh")]
|
||
public async Task<bool> RefreshTrackAsync(RefreshTrackInput input)
|
||
{
|
||
input.OrderNos = input.OrderNos.Where(x => !x.IsNullOrWhiteSpace()).Distinct().ToList();
|
||
if (input.OrderNos.Count == 0)
|
||
{
|
||
throw ProviderServiceActionResult.Exception("刷新失败", "未传入有效订单号");
|
||
}
|
||
|
||
// tips:这里没有区分订单类型,就算的不同类型的单一起刷新了也无所谓
|
||
var (orders, skipReasons) =
|
||
await provideQueries.DiagnoseManualRefreshAsync(input.AppKey, input.OrderNos);
|
||
|
||
if (orders.Count == 0)
|
||
{
|
||
var reason = skipReasons.Count > 0
|
||
? string.Join(";", skipReasons)
|
||
: "可能原因:订单不存在、已完成、无拉取状态或不在拉取时间窗口内";
|
||
throw ProviderServiceActionResult.Exception("刷新失败:没有可刷新的订单", reason);
|
||
}
|
||
|
||
var forwardErrors = new List<string>();
|
||
var submittedOrderCount = 0;
|
||
|
||
foreach (var group in orders.GroupBy(x => new
|
||
{
|
||
x.ServiceProviderCode,
|
||
x.AppKey,
|
||
x.OrderType
|
||
}))
|
||
{
|
||
var data = new List<object>(group.Select(x => new TrackPullPayload
|
||
{
|
||
OrderNo = x.OrderNo,
|
||
TrackNo = x.TrackNo,
|
||
OtherNo = x.OtherNo,
|
||
}));
|
||
var serviceProviderCode = group.Key.ServiceProviderCode;
|
||
var attach = Base64Helper.SerializeObject(new TrackRefreshAttach(group.Key.ServiceProviderCode,
|
||
group.Key.AppKey, group.Key.OrderType));
|
||
|
||
using (Track2InterfaceRelayAppContext.Use(
|
||
new Track2InterfaceRelayAppContext(group.Key.AppKey)))
|
||
{
|
||
var res = await interfaceForwardService.BatchForwardThenPushToQueueAsync(
|
||
options.CurrentValue.GetTrackInterfaceCode, serviceProviderCode, data,
|
||
GlobalConst.TrackRefreshSubscribe,
|
||
attach);
|
||
|
||
if (res.Code != GlobalConst.SuccessCode)
|
||
{
|
||
var err =
|
||
$"服务商[{serviceProviderCode}]下{group.Count()}单转发失败:{(string.IsNullOrWhiteSpace(res.Msg) ? res.ToJson() : res.Msg)}";
|
||
Log.Error($"刷新轨迹失败:{res.ToJson()}");
|
||
forwardErrors.Add(err);
|
||
continue;
|
||
}
|
||
|
||
submittedOrderCount += group.Count();
|
||
}
|
||
}
|
||
|
||
if (submittedOrderCount == 0)
|
||
{
|
||
throw ProviderServiceActionResult.Exception("刷新失败:下游转发均未成功", string.Join(";", forwardErrors));
|
||
}
|
||
|
||
return true;
|
||
}
|
||
}
|