track/src/SJZY.Track.Application/Services/ProvideServices/OrderService.cs
云龙 王 e09b8ac482 add-entity:
增加外部单号关联表实体OrderPlatformNumberEntity,一个主单关联对个外部单

update-entity:
OrderEntity,增加OrderPlatformNumbers导航属性用于SqlSugar的Include关联加载
OrderPullStatusEntity,增加Order(第三方单号)字段,当作拉取轨迹的首选项

入参模型更新:
UpdateOrderInput:轨迹订单创建/更新入参,增加【预计到达时间】【目的地时区】用于初始化

反参模型更新:
TrackOrderDto:用于轨迹订单查询的反参,增加【预计到达时间】【目的地时区】【第三方单号(格式化拼接后的字符串)】

feat:
- 实现轨迹订单查询支持三方单号查询,并将多个关联三方单号按(平台名称-三方单号)格式使用(、)拼接返回
- 创建/修改订单增加校验,规则为:【预计到达时间】【目的地时区】入参字段只允许全有值或全无值。
2024-11-14 16:12:29 +08:00

177 lines
6.5 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.PublicMethords;
using SJZY.Track.Application.Contract.Dtos.ProvideServices;
using SJZY.Track.Application.Contract.Services.ProvideServices;
using SJZY.Track.Domain.Aggregates.OrderAggregate;
using SJZY.Track.Domain.Aggregates.TrackDetailsAggregate;
using SJZY.UserCenter.HttpApi.Services;
namespace SJZY.Track.Application.Services.ProvideServices;
/// <summary>
/// 订单服务
/// </summary>
[ApiExplorerSettings(GroupName = "提供接口")]
[Route("/ProviderService")]
public partial class OrderService : ApplicationService, IOrderService
{
private readonly IOrderOperationRecordEsRepository _orderOperationRecordEsRepository;
private readonly IOrderRepository _orderRepository;
public OrderService(IOrderRepository orderRepository,
ICapPublisher capPublisher, ITrackDetailsEsRepository trackDetailsEsRepository,
IOrderOperationRecordEsRepository orderOperationRecordEsRepository)
{
_orderRepository = orderRepository;
_orderOperationRecordEsRepository = orderOperationRecordEsRepository;
_trackDetailsEsRepository = trackDetailsEsRepository;
_capPublisher = capPublisher;
}
/// <summary>
/// 新增、更新订单(存在就更新)
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("Order/Upsert")]
public async Task<UnifyResultDto<IdBaseDto>> UpsertAsync(UpsertOrderInput input)
{
// 校验预计到达时间是否不是同时存在或同时不存在
if ((input.EstimatedDeliveryDate.HasValue && input.DestinationTimeZone.HasValue == false) ||
(input.EstimatedDeliveryDate.HasValue == false && input.DestinationTimeZone.HasValue))
{
throw new BusinessException(message: "【目的地时区】和【预计到达时间】必须同时输入");
}
// 校验订单是否已存在,存在则更新
var order = await _orderRepository.GetFirstAsync(x =>
x.SubSystemId == input.SubSystemId && x.OrderType == input.OrderType && x.OrderNo == input.OrderNo);
if (order != default)
{
// 订单已存在则视为更新
await UpdateOrderAsync(input, order);
var index = new OrderOperationRecordIndex
{
OrderId = order.Id,
OrderNo = input.OrderNo,
OptionType = OptionType.修改.ToString(),
Content = input.ToJson(),
};
_ = _orderOperationRecordEsRepository.AddAsync(index);
return new UnifyResultDto<IdBaseDto>(true, new IdBaseDto
{
Id = order.Id
});
}
order = ObjectMapper.Map<UpsertOrderInput, OrderEntity>(input);
order.SubSystemId = input.SubSystemId;
order.OrderFlights = ObjectMapper.Map<List<OrderFlightInfo>, List<OrderFlightEntity>>(input.FlightInfos);
order.OrderPullStatus = BuiltOrderPullStatus(input.PullConfigInfos);
order.OrderPlatformNumbers = BuiltOrderPlatformNumbers(input.PullConfigInfos);
var orderId = await _orderRepository.CreateOrderAsync(order);
// 写入操作日志
var indexForAdd = new OrderOperationRecordIndex
{
OrderId = orderId,
OrderNo = input.OrderNo,
OptionType = OptionType.新增.ToString(),
Content = input.ToJson(),
CreateUserId = CurrentUserRequest.CurrentUser.UserId,
CreateTime = DateTime.Now
};
_ = _orderOperationRecordEsRepository.AddAsync(indexForAdd);
return new UnifyResultDto<IdBaseDto>(true, new IdBaseDto
{
Id = orderId
});
}
/// <summary>
/// 删除订单
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="BusinessException"></exception>
[HttpPost("Order/Delete")]
public async Task<UnifyResultDto<IdBaseDto>> DeleteOrderAsync(DeleteOrderInput input)
{
var orderId = await _orderRepository.GetFirstAsync(x =>
x.SubSystemId == input.SubSystemId && x.OrderType == input.OrderType && x.OrderNo == input.OrderNo,
x => x.Id);
if (orderId == default)
{
throw new BusinessException(message: $"订单:{input.OrderNo}不存在");
}
await _orderRepository.DeleteOrderAsync(orderId);
return new UnifyResultDto<IdBaseDto>(true, new IdBaseDto
{
Id = orderId
});
}
private async Task UpdateOrderAsync(UpdateOrderInput input, OrderEntity order)
{
order = ObjectMapper.Map(input, order);
order.OrderFlights = ObjectMapper.Map<List<OrderFlightInfo>, List<OrderFlightEntity>>(input.FlightInfos);
await _orderRepository.UpdateOrderAsync(order);
}
private List<OrderPullStatusEntity> BuiltOrderPullStatus(List<PullConfigInfo>? pullConfigInfos)
{
if (pullConfigInfos == null) return new List<OrderPullStatusEntity>();
return pullConfigInfos.Select(x =>
{
var status = new OrderPullStatusEntity
{
ServiceProviderCode = x.ServiceCode,
OrderNo = x.OrderNo,
PullStatus = TrackPullStatus.Init,
StartTrackCode = x.StartCode ?? GlobalConst.EmptyString,
EndTrackCode = x.EndCode ?? GlobalConst.EmptyString,
SilenceBeforeStart = x.SilenceTime,
SilenceAfterEnd = x.StopTime,
StartTime = GlobalConst.MaxDateTime,
EndTime = GlobalConst.MaxDateTime,
};
// 没有开始轨迹则立即进入拉取前静默期
if (x.StartCode == null)
{
status.PullStatus = TrackPullStatus.SilenceBeforeStart; // 会被定时任务扫描到并启动
status.StartTime = DateTime.Now.AddHours(x.SilenceTime);
}
return status;
}).ToList();
}
private List<OrderPlatformNumberEntity> BuiltOrderPlatformNumbers(List<PullConfigInfo>? pullConfigInfos)
{
if (pullConfigInfos == null) return new List<OrderPlatformNumberEntity>();
return pullConfigInfos.Select(x =>
{
var status = new OrderPlatformNumberEntity
{
//三方单号
OrderNumber = x.OrderNo,
//三方平台名称,目前认为是服务商code
Platform = x.ServiceCode,
};
return status;
}).ToList();
}
}