track/src/SJZY.Track.Application/Services/ProvideServices/TrackPullService.cs

111 lines
4.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.Consts;
using SJZY.Track.Domain.Queries;
using Volo.Abp.Http.Client.DynamicProxying;
namespace SJZY.Track.Application.Services.ProvideServices;
/// <summary>
/// 轨迹刷新服务
/// </summary>
[ApiExplorerSettings(GroupName = "提供接口")]
[Route("/ProviderService")]
public class TrackPullService : ApplicationService
{
private readonly IProvideQueries _provideQueries;
private readonly IHttpClientProxy<IInterfaceForwardServiceV2> _httpClientProxy;
private readonly ITrackPullCacheService _trackPullCacheService;
public TrackPullService(IProvideQueries provideQueries,
IHttpClientProxy<IInterfaceForwardServiceV2> httpClientProxy,
ITrackPullCacheService trackPullCacheService)
{
_provideQueries = provideQueries;
_httpClientProxy = httpClientProxy;
_trackPullCacheService = trackPullCacheService;
}
/// <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.OrderNos);
if (orders.Count == 0)
{
// tips:可能是没有配置、或者所有单都刷完了、或者单不存在
throw new BusinessException(message: "一个能更新的都没有");
}
foreach (var group in orders.GroupBy(x =>
new TrackRefreshAttach(x.ServiceProviderId, x.ServiceProviderCode, x.SubSystemId, x.OrderType)))
{
var data = group.Select(x => new
{
x.OrderNo
}).ToList();
// 批量获取轨迹
await _httpClientProxy.Service.BatchForwardThenPushToQueueAsync(
"A012", group.Key.ServiceProviderCode, data,
GlobalConst.TrackRefreshSubscribe, JsonConvert.SerializeObject(group.Key));
}
return true;
}
/// <summary>
/// 定时任务刷新轨迹
/// </summary>
/// <returns></returns>
[AllowAnonymous]
[HttpPost("Track/RefreshForTask")]
public async Task<bool> RefreshForTaskAsync()
{
var configs = await _trackPullCacheService.GetAllPullConfigsAsync();
foreach (var config in configs)
{
var redisKey = _trackPullCacheService.TrackPullKey(config.SubSystemId, config.ServiceProviderId);
if (await RedisHelper.Client.ExistsAsync(redisKey))
{
continue;
}
var orders = await _provideQueries.FindOrderWithServiceProviderAsync(config.ServiceProviderId);
foreach (var group in orders.GroupBy(x =>
new TrackRefreshAttach(x.ServiceProviderId, x.ServiceProviderCode, x.SubSystemId,
x.OrderType)))
{
var data = group.Select(x => new
{
x.OrderNo
}).ToList();
// 批量获取轨迹
await _httpClientProxy.Service.BatchForwardThenPushToQueueAsync(
InterfaceRelayConst.GetTrackInterfaceCode, group.Key.ServiceProviderCode, data,
GlobalConst.TrackRefreshSubscribe, JsonConvert.SerializeObject(group.Key));
}
await RedisHelper.Client.SetExAsync(redisKey, config.PullInterval * 60, 1);
}
return true;
}
}