track/src/SJZY.Track.Application/Services/ProvideServices/TrackPushService.cs
2025-02-21 10:53:46 +08:00

197 lines
7.9 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 SJZY.Expand.ABP.Core.Common;
using SJZY.Track.Application.Contract.Dtos.ProvideServices;
using SJZY.Track.Application.Contract.Dtos.TrackOrder;
using SJZY.Track.Application.Contract.Dtos.TrackPush;
using SJZY.Track.Application.Contract.Events;
using SJZY.Track.Application.Contract.Services.ProvideServices;
using SJZY.Track.Application.Helpers;
using SJZY.Track.Domain.Aggregates.OrderAggregate;
using SJZY.Track.Domain.Aggregates.TrackDetailsAggregate;
using SJZY.Track.Domain.Aggregates.TrackPushAggregate;
using SJZY.Track.Domain.Dtos.TrackPush;
using SJZY.Track.Domain.Queries;
using SJZY.UserCenter.HttpApi.Services.User;
using Volo.Abp.Http.Client.DynamicProxying;
namespace SJZY.Track.Application.Services.ProvideServices;
/// <summary>
/// 轨迹推送服务
/// </summary>
[ApiExplorerSettings(GroupName = "提供接口")]
[Route("/ProviderService")]
public class TrackPushService : ApplicationService, ITrackPushService
{
private readonly ICapPublisher _capPublisher;
private readonly IOrderRepository _orderRepository;
private readonly IProvideQueries _provideQueries;
private readonly IHttpClientProxy<IUserBaseInfoService> _requestCurrentUserService;
private readonly ITrackPushConfigRepository _trackPushConfigRepository;
private readonly ITrackPushRecordEsRepository _trackPushRecordEsRepository;
private readonly ITrackDetailsEsRepository _trackDetailsEsRepository;
public TrackPushService(IOrderRepository orderRepository
, ICapPublisher capPublisher
, ITrackPushRecordEsRepository trackPushRecordEsRepository
, IHttpClientProxy<IUserBaseInfoService> requestCurrentUserService
, ITrackPushConfigRepository trackPushConfigRepository, IProvideQueries provideQueries,
ITrackDetailsEsRepository trackDetailsEsRepository)
{
_orderRepository = orderRepository;
_capPublisher = capPublisher;
_trackPushRecordEsRepository = trackPushRecordEsRepository;
_requestCurrentUserService = requestCurrentUserService;
_trackPushConfigRepository = trackPushConfigRepository;
_provideQueries = provideQueries;
_trackDetailsEsRepository = trackDetailsEsRepository;
}
/// <summary>
/// 手工推送轨迹
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("Track/Push")]
[CustomerResult]
public async Task<UnifyResultDto> PushTrackAsync(PushTrackInput input)
{
input.OrderNos = input.OrderNos.Where(x => !x.IsNullOrWhiteSpace()).Distinct().ToList(); // 单号去重、去空
var subSystemCode = await _provideQueries.SubSystemId2CodeAsync(input.SubSystemId);
var orders = await _orderRepository.GetArrayAsync(x =>
x.SubSystemId == input.SubSystemId && x.OrderType == input.OrderType &&
input.OrderNos.Contains(x.OrderNo),
x => new
{
x.Id,
x.CustomId
});
if (!orders.Any())
{
throw new BusinessException(message: "一个订单都不存在");
}
var userInfo = await _requestCurrentUserService.Service.GetCurrentUserBasicInfoAsync();
if (userInfo.Code != GlobalConst.SuccessCode || userInfo.Data is null)
{
throw new BusinessException(message: $"调用用户中心,获取用户信息失败:{userInfo.Msg}");
}
var name = userInfo.Data.UserName;
//查出需要推送的轨迹
var tracks = await _trackDetailsEsRepository
.GetListAsync(input.SubSystemId, input.OrderType, input.OrderNos,
new List<string>
{
input.TrackCode
});
if (tracks.Count == 0)
{
throw new BusinessException(message: "没有需要推送的轨迹");
}
//存在比较字段,需要做过滤处理
if (!string.IsNullOrWhiteSpace(input.CompareFields))
{
//根据输入值来进行过滤,比如选取code1,批次1的轨迹,可能会有多条,下面使用创建时间最新的一条
tracks = tracks.Where(x =>
{
if (string.IsNullOrWhiteSpace(x.Extra))
{
throw new BusinessException(message: $"当前轨迹不存在扩展信息,无法利用扩展字段:{input.CompareFields}");
}
var extraDict = TextJsonHelper.DeserializeToDictionary(x.Extra);
if (extraDict == null || !extraDict.TryGetValue(input.CompareFields, out var compareValue))
{
throw new BusinessException(message: $"请检查拓展字段,为什么找不到比较字段:{input.CompareFields}");
}
//补充轨迹内容
return TextJsonHelper.EqJsonElementAndObject(compareValue, input.CompareValue);
}).ToList();
}
//不同单可能会有不同的轨迹内容,先推送只推送最新的一条
foreach (var order in orders)
{
//找出每个单的某个code的最新轨迹内容
var track = tracks.Where(x => x.OrderId == order.Id)
.OrderByDescending(x => x.CreateTime)
.FirstOrDefault();
if (track == null) continue;
var trackPushEvent = new TrackPushEvent
{
OptionType = OptionType.新增,
SubSystemId = input.SubSystemId,
SubSystemCode = subSystemCode,
CustomId = order.CustomId,
OrderIds = new[] { order.Id },
TrackCode = input.TrackCode,
OperationMan = name,
SystemPushInfo = new SystemPushStruct()
{
TrackStatus = track.TrackStatus,
TrackContent = track.TrackContent,
TrackPlace = track.TrackPlace,
TrackTime = track.TrackTime,
TrackTimeZone = track.TrackTimeZone,
CompareFields = input.CompareFields,
Extra = track.Extra
}
};
await _capPublisher.PublishAsync(GlobalConst.TrackManualPushSubscribe, trackPushEvent);
}
return new UnifyResultDto(true, $"即将推送轨迹代码{input.TrackCode}给{orders.Length}个订单");
}
/// <summary>
/// 获取轨迹推送内容
/// </summary>
/// <param name="subSystemId"></param>
/// <param name="customerId"></param>
/// <returns></returns>
[HttpGet("Track/GetTrackPushContentList")]
[CustomerResult]
public async Task<UnifyResultDto<List<TrackPushContentDto>>> GetTrackPushContentListAsync(int subSystemId, int customerId)
{
var list = await _trackPushConfigRepository.GetTrackPushContentListAsync(subSystemId, customerId);
var result = ObjectMapper.Map<List<GetTrackPushContentListVo>, List<TrackPushContentDto>>(list);
return new UnifyResultDto<List<TrackPushContentDto>>(true, result, "操作成功");
}
/// <summary>
/// 获取轨迹推送记录
/// </summary>
/// <returns></returns>
/// <param name="input"></param>
[HttpGet("Track/GetTrackPushRecords")]
[CustomerResult]
public async Task<UnifyResultDto<List<TrackPushRecordOutput>>> GetTrackPushRecordsAsync([FromQuery] OrderNoInfo input)
{
var orderId = await _orderRepository.GetFirstAsync(x =>
x.SubSystemId == input.SubSystemId && x.OrderType == input.OrderType && input.OrderNo == x.OrderNo,
x => x.Id);
if (orderId == default)
{
throw new BusinessException(message: "订单不存在");
}
var records = await _trackPushRecordEsRepository.GetTrackPushRecordsAsync(orderId);
var res = ObjectMapper.Map<List<TrackPushRecordIndex>, List<TrackPushRecordOutput>>(records);
return new UnifyResultDto<List<TrackPushRecordOutput>>(true, res, "操作成功");
}
}