using Microsoft.AspNetCore.Authorization; using Microsoft.Extensions.Options; using SJZY.Expand.ABP.Core.Logger; using SJZY.Expand.ABP.Core.PublicMethords; using SJZY.InterfaceRelay.Application.Contracts; using SJZY.Track.Application.Contract.Dtos.ProvideServices; using SJZY.Track.Application.Contract.Dtos.TrackPull; using SJZY.Track.Application.Contract.Services.ProvideServices; using SJZY.Track.Application.Helpers; using SJZY.Track.Application.Options; using SJZY.Track.Application.Rabbit; using SJZY.Track.Application.Track2InterfaceRelay; using SJZY.Track.Domain.Queries; using Volo.Abp.Http.Client.DynamicProxying; namespace SJZY.Track.Application.Services.ProvideServices; /// /// 轨迹刷新服务 /// [ApiExplorerSettings(GroupName = "提供接口")] [Route("/ProviderService")] public class TrackPullService : ApplicationService, ITrackPullService { private readonly IHttpClientProxy _httpClientProxy; private readonly IOptionsMonitor _options; private readonly IProvideQueries _provideQueries; public TrackPullService(IProvideQueries provideQueries, IHttpClientProxy httpClientProxy, IOptionsMonitor options) { _provideQueries = provideQueries; _httpClientProxy = httpClientProxy; _options = options; } /// /// 手动刷新轨迹 /// /// /// [HttpPost("Track/Refresh")] public async Task 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(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; } /// /// 轨迹刷新测试 /// /// /// /// [HttpPost("Track/TrackRefreshEventTest")] #if DEBUG [AllowAnonymous] #endif public async Task TrackRefreshEventTest([FromServices] TrackRefreshEventHandler trackRefreshEventHandler, InterfaceRelayForwardReplayEvent input) { await trackRefreshEventHandler.ConsumerAsync(input); return true; } }