track/src/SJZY.Track.Application/Services/ProvideServices/TrackPushService.cs
2024-06-25 17:53:56 +08:00

115 lines
4.1 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 DotNetCore.CAP;
using Microsoft.AspNetCore.Authorization;
using SJZY.Track.Application.Contract.Dtos.ProvideServices;
using SJZY.Track.Application.Contract.Events;
using SJZY.Track.Domain.Aggregates.OrderAggregate;
using SJZY.Track.Domain.Aggregates.TrackPushAggregate;
using SJZY.Track.Domain.Consts;
using SJZY.UserCenter.HttpApi.Services;
using Volo.Abp.Http.Client.DynamicProxying;
namespace SJZY.Track.Application.Services.ProvideServices;
/// <summary>
/// 轨迹推送服务
/// </summary>
[ApiExplorerSettings(GroupName = "提供接口")]
[Route("/ProviderService")]
public class TrackPushService : ApplicationService
{
private readonly IOrderRepository _orderRepository;
private readonly ICapPublisher _capPublisher;
private readonly ITrackPushRecordsRepository _trackPushRecordsRepository;
private readonly IHttpClientProxy<IRequestCurrentUserService> _requestCurrentUserService;
public TrackPushService(IOrderRepository orderRepository
, ICapPublisher capPublisher
, ITrackPushRecordsRepository trackPushRecordsRepository,
IHttpClientProxy<IRequestCurrentUserService> requestCurrentUserService)
{
_orderRepository = orderRepository;
_capPublisher = capPublisher;
_trackPushRecordsRepository = trackPushRecordsRepository;
_requestCurrentUserService = requestCurrentUserService;
}
/// <summary>
/// 手工推送轨迹
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("Track/Push")]
public async Task<string> PushTrackAsync(PushTrackInput input)
{
input.OrderNos = input.OrderNos.Where(x => !x.IsNullOrWhiteSpace()).Distinct().ToList(); // 单号去重、去空
var orders = await _orderRepository.GetArrayAsync(x =>
x.OrderType == input.OrderType && input.OrderNos.Contains(x.OrderNo), x => new
{
x.Id, x.SubSystemId, x.CustomId
});
if (!orders.Any())
{
throw new BusinessException(message: "一个订单都不存在");
}
var userInfo = await _requestCurrentUserService.Service.GetCurrentUserBasicInfoAsync();
if (userInfo.Code != "200" || userInfo.Data is null)
{
//TODO: log || ex
throw new BusinessException(message: $"调用用户中心,获取用户信息失败:{userInfo?.Msg}");
}
var name = userInfo.Data.UserName;
foreach (var group in orders.GroupBy(x => new { x.SubSystemId, x.CustomId }))
{
var trackPushEvent = new TrackPushEvent
{
OptionType = OptionType.新增,
SubSystemId = group.Key.SubSystemId,
CustomId = group.Key.CustomId,
OrderIds = group.Select(x => x.Id).ToArray(),
TrackCode = input.TrackCode,
OperationMan = name,
};
await _capPublisher.PublishAsync(GlobalConst.TrackManualPushSubscribe, trackPushEvent);
}
return $"即将推送轨迹代码{input.TrackCode}给{orders.Length}个订单";
}
/// <summary>
/// 定时任务推送预警(》12小时没有推送的)
/// </summary>
/// <returns></returns>
[AllowAnonymous]
[HttpPost("Track/PushWarnForTask")]
public async Task<string> PushWarnForTaskAsync()
{
// todo:订单数据上来后一次性update是否会引发长时间X独占?
var count = await _orderRepository.PushWarnOrdersAsync();
return $"本次预警扫描了:{count}个订单";
}
/// <summary>
/// 获取轨迹推送记录
/// </summary>
/// <returns></returns>
/// <param name="input"></param>
[HttpGet("Track/GetTrackPushRecords")]
public async Task<List<TrackPushRecordIndex>> GetTrackPushRecordsAsync(OrderNoInfo input)
{
var orderId = await _orderRepository.GetFirstAsync(x =>
x.OrderType == input.OrderType && input.OrderNo == x.OrderNo, x => x.Id);
if (orderId == default)
{
throw new BusinessException(message: "订单不存在");
}
return await _trackPushRecordsRepository.GetTrackPushRecordsAsync(orderId);
}
}