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;
///
/// 轨迹推送服务
///
[ApiExplorerSettings(GroupName = "提供接口")]
[Route("/ProviderService")]
public class TrackPushService : ApplicationService
{
private readonly IOrderRepository _orderRepository;
private readonly ICapPublisher _capPublisher;
private readonly ITrackPushRecordsRepository _trackPushRecordsRepository;
private readonly IHttpClientProxy _requestCurrentUserService;
public TrackPushService(IOrderRepository orderRepository
, ICapPublisher capPublisher
, ITrackPushRecordsRepository trackPushRecordsRepository,
IHttpClientProxy requestCurrentUserService)
{
_orderRepository = orderRepository;
_capPublisher = capPublisher;
_trackPushRecordsRepository = trackPushRecordsRepository;
_requestCurrentUserService = requestCurrentUserService;
}
///
/// 手工推送轨迹
///
///
///
[HttpPost("Track/Push")]
public async Task 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}个订单";
}
///
/// 定时任务推送预警(》12小时没有推送的)
///
///
[AllowAnonymous]
[HttpPost("Track/PushWarnForTask")]
public async Task PushWarnForTaskAsync()
{
// todo:订单数据上来后一次性update是否会引发长时间X独占?
var count = await _orderRepository.PushWarnOrdersAsync();
return $"本次预警扫描了:{count}个订单";
}
///
/// 获取轨迹推送记录
///
///
///
[HttpGet("Track/GetTrackPushRecords")]
public async Task> 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);
}
}