diff --git a/src/SJZY.Track.Application.Contract/Dtos/ProvideServices/PushTrackInput.cs b/src/SJZY.Track.Application.Contract/Dtos/ProvideServices/PushTrackInput.cs new file mode 100644 index 00000000..5eaff31e --- /dev/null +++ b/src/SJZY.Track.Application.Contract/Dtos/ProvideServices/PushTrackInput.cs @@ -0,0 +1,12 @@ +using System.ComponentModel.DataAnnotations; + +namespace SJZY.Track.Application.Contract.Dtos.ProvideServices; + +public class PushTrackInput : OrderNosInfo +{ + /// + /// 轨迹code,必填 + /// + [Required] + public string TrackCode { get; set; } = null!; +} \ No newline at end of file diff --git a/src/SJZY.Track.Application.Contract/Services/ITrackPushCacheService.cs b/src/SJZY.Track.Application.Contract/Services/ITrackPushCacheService.cs index acc00b72..12db0f65 100644 --- a/src/SJZY.Track.Application.Contract/Services/ITrackPushCacheService.cs +++ b/src/SJZY.Track.Application.Contract/Services/ITrackPushCacheService.cs @@ -19,6 +19,15 @@ public interface ITrackPushCacheService /// Task> GetPushContentConfigsAsync(int subSystemId, int customId); + /// + /// 获取服务商拉取配置 + /// + /// 子系统id + /// 服务商id + /// + /// + Task GetPushContentConfigAsync(int subSystemId, int customId, int trackContentId); + /// /// 根据轨迹代码推断下一个(升序)要推送的内容,如果找不到或者已经是最后一个了,返回null /// diff --git a/src/SJZY.Track.Application/EventHandlers/OrderLinkAfterFillTrackHandler.cs b/src/SJZY.Track.Application/EventHandlers/OrderLinkAfterFillTrackHandler.cs index 80c5dda8..9335175a 100644 --- a/src/SJZY.Track.Application/EventHandlers/OrderLinkAfterFillTrackHandler.cs +++ b/src/SJZY.Track.Application/EventHandlers/OrderLinkAfterFillTrackHandler.cs @@ -31,7 +31,7 @@ public class OrderLinkAfterFillTrackHandler : ITransientDependency, ICapSubscrib var order = await _orderRepository.GetFirstAsync(x => x.Id == linkOrderId, x => new { - x.OrderType, x.OrderNo, x.CustomId, LatestPushCode = x.LatestPushTrackContentId, x.SubSystemId + x.OrderType, x.OrderNo, x.CustomId, x.LatestPushTrackContentId, x.SubSystemId }); if (order == default) continue; @@ -50,7 +50,7 @@ public class OrderLinkAfterFillTrackHandler : ITransientDependency, ICapSubscrib OrderId = linkOrderId, CustomId = order.CustomId, SubSystemId = order.SubSystemId, - LatestPushTrackContentId = order.LatestPushCode, + LatestPushTrackContentId = order.LatestPushTrackContentId, UpSubSystemId = @event.SubSystemId, }; diff --git a/src/SJZY.Track.Application/EventHandlers/TrackRefreshEventHandler.cs b/src/SJZY.Track.Application/EventHandlers/TrackRefreshEventHandler.cs index 5d74b758..e5207ee2 100644 --- a/src/SJZY.Track.Application/EventHandlers/TrackRefreshEventHandler.cs +++ b/src/SJZY.Track.Application/EventHandlers/TrackRefreshEventHandler.cs @@ -63,7 +63,7 @@ public class TrackRefreshEventHandler : ITransientDependency, ICapSubscribe var order = await _orderRepository.GetFirstAsync(x => x.OrderType == orderType && x.OrderNo == orderNo, x => new { - x.Id, x.SubSystemId, LatestPushCode = x.LatestPushTrackContentId, x.CustomId + x.Id, x.SubSystemId, x.LatestPushTrackContentId, x.CustomId }); if (order == default) return; @@ -73,7 +73,7 @@ public class TrackRefreshEventHandler : ITransientDependency, ICapSubscribe { OrderId = order.Id, SubSystemId = order.SubSystemId, - LatestPushTrackContentId = order.LatestPushCode, + LatestPushTrackContentId = order.LatestPushTrackContentId, CustomId = order.CustomId, OrderType = orderType, OrderNo = orderNo!, diff --git a/src/SJZY.Track.Application/Services/ProvideServices/TrackPushPullService.cs b/src/SJZY.Track.Application/Services/ProvideServices/TrackPullService.cs similarity index 79% rename from src/SJZY.Track.Application/Services/ProvideServices/TrackPushPullService.cs rename to src/SJZY.Track.Application/Services/ProvideServices/TrackPullService.cs index faa64a0f..e74ca894 100644 --- a/src/SJZY.Track.Application/Services/ProvideServices/TrackPushPullService.cs +++ b/src/SJZY.Track.Application/Services/ProvideServices/TrackPullService.cs @@ -1,30 +1,31 @@ -using Newtonsoft.Json; +using Microsoft.AspNetCore.Authorization; +using Newtonsoft.Json; using SJZY.Expand.ABP.Core; 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; -using SJZY.Track.Domain.Aggregates.OrderAggregate; +using SJZY.Track.Domain.Queries; using Volo.Abp.Http.Client.DynamicProxying; namespace SJZY.Track.Application.Services.ProvideServices; /// -/// 轨迹推送、拉取服务 +/// 轨迹刷新服务 /// [ApiExplorerSettings(GroupName = "提供接口")] [Route("/ProviderService")] -public class TrackPushPullService : ApplicationService +public class TrackPullService : ApplicationService { - private readonly IOrderRepository _orderRepository; + private readonly IProvideQueries _provideQueries; private readonly IHttpClientProxy _httpClientProxy; private readonly ITrackPullCacheService _trackPullCacheService; - public TrackPushPullService(IOrderRepository orderRepository, + public TrackPullService(IProvideQueries provideQueries, IHttpClientProxy httpClientProxy, ITrackPullCacheService trackPullCacheService) { - _orderRepository = orderRepository; + _provideQueries = provideQueries; _httpClientProxy = httpClientProxy; _trackPullCacheService = trackPullCacheService; } @@ -38,17 +39,16 @@ public class TrackPushPullService : ApplicationService public async Task RefreshTrackAsync(RefreshTrackInput input) { // todo:罚时? + input.OrderNos = input.OrderNos.Where(x => !x.IsNullOrWhiteSpace()).Distinct().ToList(); // 单号去重、去空 if (input.OrderNos.Count == 0) { - throw new BusinessException(message: "订单号数组不能为空"); + throw new BusinessException(message: "订单一个都不存在"); } - input.OrderNos = input.OrderNos.Distinct().ToList(); - // 这里没有区分订单类型,就算的不同类型的单一起刷新了也无所谓 - var orderTemplates = await _orderRepository.FindServiceProviderByOrderAsync(input.OrderNos); + var orders = await _provideQueries.FindOrderWithServiceProviderAsync(input.OrderNos); - foreach (var group in orderTemplates.GroupBy(x => + foreach (var group in orders.GroupBy(x => new TrackRefreshAttach(x.ServiceProviderId, x.ServiceProviderCode, x.SubSystemId, x.OrderType))) { var data = group.Select(x => new @@ -69,6 +69,7 @@ public class TrackPushPullService : ApplicationService /// 定时任务刷新轨迹 /// /// + [AllowAnonymous] [HttpPost("Track/RefreshForTask")] public async Task RefreshTrackAsync() { @@ -81,7 +82,7 @@ public class TrackPushPullService : ApplicationService continue; } - var orders = await _orderRepository.FindOrderByServiceProviderAsync(config.ServiceProviderId); + var orders = await _provideQueries.FindOrderWithServiceProviderAsync(config.ServiceProviderId); foreach (var group in orders.GroupBy(x => new TrackRefreshAttach(x.ServiceProviderId, x.ServiceProviderCode, x.SubSystemId, diff --git a/src/SJZY.Track.Application/Services/ProvideServices/TrackPushService.cs b/src/SJZY.Track.Application/Services/ProvideServices/TrackPushService.cs new file mode 100644 index 00000000..10b28796 --- /dev/null +++ b/src/SJZY.Track.Application/Services/ProvideServices/TrackPushService.cs @@ -0,0 +1,77 @@ +using SJZY.InterfaceRelay.Application.Contracts; +using SJZY.Track.Application.Contract.Dtos.ProvideServices; +using SJZY.Track.Application.Contract.Services; +using SJZY.Track.Domain.Aggregates.OrderAggregate; +using SJZY.Track.Domain.Aggregates.TrackContentAggregate; +using Volo.Abp.Http.Client.DynamicProxying; + +namespace SJZY.Track.Application.Services.ProvideServices; + +/// +/// 轨迹推送服务 +/// +[ApiExplorerSettings(GroupName = "提供接口")] +[Route("/ProviderService")] +public class TrackPushService : ApplicationService +{ + private readonly IOrderRepository _orderRepository; + private readonly ITrackContentRepository _trackContentRepository; + private readonly ITrackPushCacheService _pushCacheService; + private readonly IHttpClientProxy _httpClientProxy; + + public TrackPushService(IOrderRepository orderRepository, + IHttpClientProxy httpClientProxy, ITrackPushCacheService pushCacheService, + ITrackContentRepository trackContentRepository) + { + _orderRepository = orderRepository; + _httpClientProxy = httpClientProxy; + _pushCacheService = pushCacheService; + _trackContentRepository = trackContentRepository; + } + + /// + /// 推送轨迹 + /// + /// + /// + [HttpPost("Track/Push")] + public async Task PushTrackAsync(PushTrackInput input) + { + 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 => + x.OrderType == input.OrderType && input.OrderNos.Contains(x.OrderNo), x => new + { + x.Id, x.OrderNo, x.SubSystemId, x.CustomId + }); + + var trackContents = await _trackContentRepository.GetListAsync(x => x.Code == input.TrackCode, x => new + { + x.SubSystemId, x.Id + }); + + foreach (var group in orders.GroupBy(x => + new { x.SubSystemId, x.CustomId })) + { + var trackContent = trackContents.FirstOrDefault(x => x.SubSystemId == group.Key.SubSystemId); + if (trackContent == null) continue; + await _pushCacheService.GetPushContentConfigAsync(group.Key.SubSystemId, group.Key.CustomId, + trackContent.Id); + var data = group.Select(x => new + { + x.OrderNo + }).ToList(); + + // 批量获取轨迹 + // await _httpClientProxy.Service.BatchForwardThenPushToQueueAsync( + // "A024", group.Key.ServiceProviderCode, data, + // GlobalConst.TrackRefreshSubscribe, JsonConvert.SerializeObject(group.Key)); + } + + return true; + } +} \ No newline at end of file diff --git a/src/SJZY.Track.Application/Services/ProvideServices/TrackService.cs b/src/SJZY.Track.Application/Services/ProvideServices/TrackService.cs index b1c809d3..b9af7e6e 100644 --- a/src/SJZY.Track.Application/Services/ProvideServices/TrackService.cs +++ b/src/SJZY.Track.Application/Services/ProvideServices/TrackService.cs @@ -41,7 +41,7 @@ public partial class TrackService : ApplicationService, ITrackService var order = await _orderRepository.GetFirstAsync(x => x.OrderType == input.OrderType && x.OrderNo == input.OrderNo, x => new { - x.Id, x.SubSystemId, LatestPushCode = x.LatestPushTrackContentId, x.CustomId + x.Id, x.SubSystemId, x.LatestPushTrackContentId, x.CustomId }); if (order == default) { @@ -52,7 +52,7 @@ public partial class TrackService : ApplicationService, ITrackService var @event = ObjectMapper.Map(input); @event.OrderId = order.Id; @event.SubSystemId = order.SubSystemId; - @event.LatestPushTrackContentId = order.LatestPushCode; + @event.LatestPushTrackContentId = order.LatestPushTrackContentId; @event.CustomId = order.CustomId; await _capPublisher.PublishAsync(GlobalConst.TrackUpsertSubscribe, @event); return true; @@ -76,7 +76,7 @@ public partial class TrackService : ApplicationService, ITrackService var orders = await _orderRepository.GetListAsync(x => x.OrderType == input.OrderType && input.OrderNos.Contains(x.OrderNo), x => new { - x.Id, x.OrderNo, x.SubSystemId, LatestPushCode = x.LatestPushTrackContentId, x.CustomId + x.Id, x.OrderNo, x.SubSystemId, x.LatestPushTrackContentId, x.CustomId }); foreach (var order in orders) @@ -92,7 +92,7 @@ public partial class TrackService : ApplicationService, ITrackService OrderNo = order.OrderNo, OrderId = order.Id, SubSystemId = order.SubSystemId, - LatestPushTrackContentId = order.LatestPushCode, + LatestPushTrackContentId = order.LatestPushTrackContentId, CustomId = order.CustomId }; await _capPublisher.PublishAsync(GlobalConst.TrackUpsertSubscribe, @event); diff --git a/src/SJZY.Track.Application/Services/TrackPush/TrackPushCacheService.cs b/src/SJZY.Track.Application/Services/TrackPush/TrackPushCacheService.cs index eb11ae2c..d2bb3e2d 100644 --- a/src/SJZY.Track.Application/Services/TrackPush/TrackPushCacheService.cs +++ b/src/SJZY.Track.Application/Services/TrackPush/TrackPushCacheService.cs @@ -28,6 +28,11 @@ public class TrackPushCacheService : ITrackPushCacheService, ITransientDependenc throw new NotImplementedException(); } + public Task GetPushContentConfigAsync(int subSystemId, int customId, int trackContentId) + { + throw new NotImplementedException(); + } + public Task InferNextPushContentOrNullAsync(int subSystemId, int customId, int? trackContentId) { diff --git a/src/SJZY.Track.Domain/Aggregates/OrderAggregate/IOrderRepository.cs b/src/SJZY.Track.Domain/Aggregates/OrderAggregate/IOrderRepository.cs index 2f99ba3f..acc0d4de 100644 --- a/src/SJZY.Track.Domain/Aggregates/OrderAggregate/IOrderRepository.cs +++ b/src/SJZY.Track.Domain/Aggregates/OrderAggregate/IOrderRepository.cs @@ -50,19 +50,4 @@ public interface IOrderRepository : IBasicRepository Task UnLinkByIdsAsync(List linkIds); #endregion - - - /// - /// 根据订单模板找对应服务商 - /// - /// - /// - Task> FindServiceProviderByOrderAsync(List orderNos); - - /// - /// 获取服务商下所有单(未完成的) - /// - /// - /// - Task> FindOrderByServiceProviderAsync(int serviceProvider); } \ No newline at end of file diff --git a/src/SJZY.Track.Domain/Dtos/Order/OrderServiceProviderVO.cs b/src/SJZY.Track.Domain/Dtos/Order/OrderWithServiceProviderVO.cs similarity index 92% rename from src/SJZY.Track.Domain/Dtos/Order/OrderServiceProviderVO.cs rename to src/SJZY.Track.Domain/Dtos/Order/OrderWithServiceProviderVO.cs index 36a46aa8..df193e03 100644 --- a/src/SJZY.Track.Domain/Dtos/Order/OrderServiceProviderVO.cs +++ b/src/SJZY.Track.Domain/Dtos/Order/OrderWithServiceProviderVO.cs @@ -1,6 +1,6 @@ namespace SJZY.Track.Domain.Dtos.Order; -public class OrderServiceProviderVO +public class OrderWithServiceProviderVO { public string OrderNo { get; set; } = null!; diff --git a/src/SJZY.Track.Domain/Queries/IProvideQueries.cs b/src/SJZY.Track.Domain/Queries/IProvideQueries.cs index a3f8450c..2795df48 100644 --- a/src/SJZY.Track.Domain/Queries/IProvideQueries.cs +++ b/src/SJZY.Track.Domain/Queries/IProvideQueries.cs @@ -48,4 +48,18 @@ public interface IProvideQueries /// /// Task> MapTrackCodesAsync(int upSubSystemId, int subSystemId, params string[] trackCodes); + + /// + /// 根据订单模板找对应服务商 + /// + /// + /// + Task> FindOrderWithServiceProviderAsync(List orderNos); + + /// + /// 获取服务商下所有单(未完成的) + /// + /// + /// + Task> FindOrderWithServiceProviderAsync(int serviceProvider); } \ No newline at end of file diff --git a/src/SJZY.Track.Repository/Queries/ProvideQueries.cs b/src/SJZY.Track.Repository/Queries/ProvideQueries.cs index 04301286..8058689e 100644 --- a/src/SJZY.Track.Repository/Queries/ProvideQueries.cs +++ b/src/SJZY.Track.Repository/Queries/ProvideQueries.cs @@ -6,6 +6,7 @@ using SJZY.Track.Domain.Dtos.Order; using SJZY.Track.Domain.Dtos.TrackDetails; using SJZY.Track.Domain.Dtos.TrackTemplate; using SJZY.Track.Domain.Queries; +using SJZY.Track.Domain.VEntities; namespace SJZY.Track.Repository.Queries; @@ -105,4 +106,39 @@ public class ProvideQueries : ISugarRepository, IProvideQueries, .Select(x => x.TrackContentCode) .ToListAsync(); } + + + public async Task> FindOrderWithServiceProviderAsync(List orderNos) + { + return await Context.Queryable() + .LeftJoin((a, b) => a.Id == b.OrderId) + .LeftJoin((a, b, c) => b.ServiceProviderId == c.Id) + .Where(a => orderNos.Contains(a.OrderNo)) + .Select((a, b, c) => new OrderWithServiceProviderVO + { + OrderNo = a.OrderNo, + OrderType = a.OrderType, + ServiceProviderId = b.ServiceProviderId, + ServiceProviderCode = c.Code, + SubSystemId = a.SubSystemId + }) + .ToListAsync(); + } + + public async Task> FindOrderWithServiceProviderAsync(int serviceProvider) + { + return await Context.Queryable() + .RightJoin((a, b) => a.Id == b.OrderId) + .LeftJoin((a, b, c) => b.ServiceProviderId == c.Id) + .Where((a, b) => b.ServiceProviderId == serviceProvider && !a.IsCompleted) + .Select((a, b, c) => new OrderWithServiceProviderVO + { + OrderNo = a.OrderNo, + OrderType = a.OrderType, + ServiceProviderId = b.ServiceProviderId, + ServiceProviderCode = c.Code, + SubSystemId = a.SubSystemId + }) + .ToListAsync(); + } } \ No newline at end of file diff --git a/src/SJZY.Track.Repository/Repositories/OrderRepository.cs b/src/SJZY.Track.Repository/Repositories/OrderRepository.cs index 47c0bb60..633db8c2 100644 --- a/src/SJZY.Track.Repository/Repositories/OrderRepository.cs +++ b/src/SJZY.Track.Repository/Repositories/OrderRepository.cs @@ -1,7 +1,6 @@ using SJZY.Track.Domain; using SJZY.Track.Domain.Aggregates.OrderAggregate; using SJZY.Track.Domain.Dtos.Order; -using SJZY.Track.Domain.VEntities; namespace SJZY.Track.Repository.Repositories; @@ -84,38 +83,4 @@ public class OrderRepository : BasicRepository, IOrderRepository, I } #endregion - - public async Task> FindServiceProviderByOrderAsync(List orderNos) - { - return await Context.Queryable() - .LeftJoin((a, b) => a.Id == b.OrderId) - .LeftJoin((a, b, c) => b.ServiceProviderId == c.Id) - .Where(a => orderNos.Contains(a.OrderNo)) - .Select((a, b, c) => new OrderServiceProviderVO - { - OrderNo = a.OrderNo, - OrderType = a.OrderType, - ServiceProviderId = b.ServiceProviderId, - ServiceProviderCode = c.Code, - SubSystemId = a.SubSystemId - }) - .ToListAsync(); - } - - public async Task> FindOrderByServiceProviderAsync(int serviceProvider) - { - return await Context.Queryable() - .RightJoin((a, b) => a.Id == b.OrderId) - .LeftJoin((a, b, c) => b.ServiceProviderId == c.Id) - .Where((a, b) => b.ServiceProviderId == serviceProvider && !a.IsCompleted) - .Select((a, b, c) => new OrderServiceProviderVO - { - OrderNo = a.OrderNo, - OrderType = a.OrderType, - ServiceProviderId = b.ServiceProviderId, - ServiceProviderCode = c.Code, - SubSystemId = a.SubSystemId - }) - .ToListAsync(); - } } \ No newline at end of file