add-interface:增加批量创建轨迹订单的接口
This commit is contained in:
parent
54175b1e5f
commit
80e94db837
@ -15,6 +15,13 @@ public class UpsertOrderInput : UpdateOrderInput
|
||||
public int OptionUserId { get; set; }
|
||||
}
|
||||
|
||||
public class BatchUpsertOrderInput
|
||||
{
|
||||
[Required]
|
||||
[MaxLength(100)]
|
||||
public List<UpsertOrderInput> OrderInputs { get; set; } = new();
|
||||
}
|
||||
|
||||
public class OrderFlightInfo
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
@ -6,7 +6,9 @@ using SJZY.Track.Application.Contract.Services.ProvideServices;
|
||||
using SJZY.Track.Application.Services.Warning.Handlers;
|
||||
using SJZY.Track.Domain.Aggregates.OrderAggregate;
|
||||
using SJZY.Track.Domain.Aggregates.TrackDetailsAggregate;
|
||||
using SJZY.Track.Domain.Extensions;
|
||||
using SJZY.UserCenter.HttpApi.Services;
|
||||
using SqlSugar;
|
||||
|
||||
namespace SJZY.Track.Application.Services.ProvideServices;
|
||||
|
||||
@ -80,7 +82,6 @@ public partial class OrderService : ApplicationService, IOrderService
|
||||
}
|
||||
|
||||
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);
|
||||
@ -109,6 +110,104 @@ public partial class OrderService : ApplicationService, IOrderService
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量创建/新增 轨迹订单
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="BusinessException"></exception>
|
||||
[HttpPost("Order/BatchUpsert")]
|
||||
public async Task<UnifyResultDto> BatchUpsertAsync(BatchUpsertOrderInput input)
|
||||
{
|
||||
if (input.OrderInputs.Any(x => (x.EstimatedDeliveryDate.HasValue && x.DestinationTimeZone.HasValue == false) ||
|
||||
(x.EstimatedDeliveryDate.HasValue == false && x.DestinationTimeZone.HasValue)))
|
||||
{
|
||||
throw new BusinessException(message: "【预计签收时间】和【签收地时区】必须同时输入");
|
||||
}
|
||||
|
||||
var currentUser = CurrentUserRequest.CurrentUser;
|
||||
|
||||
var exp = Expressionable.Create<OrderEntity>();
|
||||
foreach (var order in input.OrderInputs)
|
||||
exp.Or(x => x.OrderType == order.OrderType && x.SubSystemId == order.SubSystemId && x.OrderNo == order.OrderNo);
|
||||
|
||||
// 获取已存在的订单
|
||||
var existsOrders = await _orderRepository.GetListAsync(exp.ToExpression());
|
||||
var updateOrders = existsOrders.Count > 0 ? new List<OrderEntity>(existsOrders.Count) : Empty<OrderEntity>.List;
|
||||
var updateRecords = existsOrders.Count > 0 ? new List<OrderOperationRecordIndex>(existsOrders.Count) : Empty<OrderOperationRecordIndex>.List;
|
||||
if (existsOrders.Count > 0)
|
||||
{
|
||||
foreach (var order in existsOrders)
|
||||
{
|
||||
var input2 = input.OrderInputs.First(x => x.OrderType == order.OrderType && x.SubSystemId == order.SubSystemId && x.OrderNo == order.OrderNo);
|
||||
var order2 = ObjectMapper.Map(input2, order);
|
||||
order2.OrderFlights = ObjectMapper.Map<List<OrderFlightInfo>, List<OrderFlightEntity>>(input2.FlightInfos);
|
||||
order2.OrderPullStatus = BuiltOrderPullStatus(input2.PullConfigInfos);
|
||||
order2.OrderPlatformNumbers = BuiltOrderPlatformNumbers(input2.PullConfigInfos);
|
||||
order2.TemplateCodes = input2.TemplateCodes == null ? null : string.Join(',', input2.TemplateCodes);
|
||||
order2.SetThirdPartyOrderNumber(order.OrderPlatformNumbers);
|
||||
updateOrders.Add(order2);
|
||||
updateRecords.Add(new OrderOperationRecordIndex
|
||||
{
|
||||
OrderId = order.Id,
|
||||
OrderNo = order.OrderNo,
|
||||
OptionType = OrderOperationType.修改订单.ToString(),
|
||||
Content = input2.ToJson(),
|
||||
CreateUserId = currentUser.UserId,
|
||||
CreateUserName = currentUser.Name,
|
||||
CreateTime = DateTime.Now
|
||||
});
|
||||
}
|
||||
|
||||
await _orderRepository.UpdateBatchOrderAsync(updateOrders);
|
||||
_ = _orderOperationRecordEsRepository.AddManyAsync(updateRecords);
|
||||
}
|
||||
|
||||
// 如果全部操作为修改
|
||||
if (existsOrders.Count == input.OrderInputs.Count)
|
||||
return new UnifyResultDto(true);
|
||||
|
||||
// 新增订单
|
||||
var existsOrderNos = existsOrders.Select(x => x.OrderNo).ToHashSet();
|
||||
var insertInputs = input.OrderInputs
|
||||
.WhereIf(existsOrderNos.Count > 0, x => !existsOrderNos.Contains(x.OrderNo))
|
||||
.ToList();
|
||||
|
||||
var insertOrders = new List<OrderEntity>(insertInputs.Count);
|
||||
var createRecords = new List<OrderOperationRecordIndex>(insertInputs.Count);
|
||||
foreach (var insertInput in insertInputs)
|
||||
{
|
||||
var order = ObjectMapper.Map<UpsertOrderInput, OrderEntity>(insertInput);
|
||||
order.OrderFlights = ObjectMapper.Map<List<OrderFlightInfo>, List<OrderFlightEntity>>(insertInput.FlightInfos);
|
||||
order.OrderPullStatus = BuiltOrderPullStatus(insertInput.PullConfigInfos);
|
||||
order.OrderPlatformNumbers = BuiltOrderPlatformNumbers(insertInput.PullConfigInfos);
|
||||
order.TemplateCodes = insertInput.TemplateCodes == null ? null : string.Join(',', insertInput.TemplateCodes);
|
||||
order.SetThirdPartyOrderNumber(order.OrderPlatformNumbers);
|
||||
insertOrders.Add(order);
|
||||
createRecords.Add(new OrderOperationRecordIndex
|
||||
{
|
||||
//OrderId = default, //稍后补充
|
||||
OrderNo = order.OrderNo,
|
||||
OptionType = OrderOperationType.创建订单.ToString(),
|
||||
Content = insertInput.ToJson(),
|
||||
CreateUserId = currentUser.UserId,
|
||||
CreateUserName = currentUser.Name,
|
||||
CreateTime = DateTime.Now
|
||||
});
|
||||
}
|
||||
|
||||
// 创建订单,创建操作记录
|
||||
var orderNoIdDict = await _orderRepository.CreateBatchOrderAsync(insertOrders);
|
||||
foreach (var record in createRecords)
|
||||
{
|
||||
var orderId = orderNoIdDict[record.OrderNo];
|
||||
record.OrderId = orderId;
|
||||
}
|
||||
|
||||
_ = _orderOperationRecordEsRepository.AddManyAsync(createRecords);
|
||||
return new UnifyResultDto(true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除订单
|
||||
/// </summary>
|
||||
|
||||
@ -34,6 +34,13 @@ public interface IOrderRepository : IBasicRepository<OrderEntity>
|
||||
/// <returns></returns>
|
||||
Task<int> CreateOrderAsync(OrderEntity order);
|
||||
|
||||
/// <summary>
|
||||
/// 批量创建订单
|
||||
/// </summary>
|
||||
/// <param name="orders"></param>
|
||||
/// <returns></returns>
|
||||
Task<Dictionary<string, int>> CreateBatchOrderAsync(List<OrderEntity> orders);
|
||||
|
||||
/// <summary>
|
||||
/// 更新订单
|
||||
/// </summary>
|
||||
@ -41,6 +48,13 @@ public interface IOrderRepository : IBasicRepository<OrderEntity>
|
||||
/// <returns></returns>
|
||||
Task UpdateOrderAsync(OrderEntity order);
|
||||
|
||||
/// <summary>
|
||||
/// 批量更新订单
|
||||
/// </summary>
|
||||
/// <param name="orders"></param>
|
||||
/// <returns></returns>
|
||||
Task UpdateBatchOrderAsync(List<OrderEntity> orders);
|
||||
|
||||
/// <summary>
|
||||
/// 删除订单
|
||||
/// </summary>
|
||||
|
||||
@ -282,6 +282,53 @@ public class OrderRepository : BasicRepository<OrderEntity>, IOrderRepository, I
|
||||
return orderId;
|
||||
}
|
||||
|
||||
public async Task<Dictionary<string, int>> CreateBatchOrderAsync(List<OrderEntity> orders)
|
||||
{
|
||||
Dictionary<string, int> orderNoIdDict;
|
||||
using var tran = TransactionHelper.GetReadCommitted();
|
||||
|
||||
var orderFlights = orders.SelectMany(x => x.OrderFlights).ToList();
|
||||
var orderPullStatus = orders.SelectMany(x => x.OrderPullStatus).ToList();
|
||||
var orderPlatformNumbers = orders.SelectMany(x => x.OrderPlatformNumbers).ToList();
|
||||
|
||||
try
|
||||
{
|
||||
// 先批量插入
|
||||
await Context.Insertable(orders).ExecuteCommandAsync();
|
||||
|
||||
var exp = Expressionable.Create<OrderEntity>();
|
||||
foreach (var order in orders)
|
||||
exp.Or(x => x.OrderType == order.OrderType && x.SubSystemId == order.SubSystemId && x.OrderNo == order.OrderNo);
|
||||
|
||||
// 查出id
|
||||
var queryOrders = await GetListAsync(exp.ToExpression(), x => new
|
||||
{
|
||||
x.Id,
|
||||
x.OrderNo
|
||||
});
|
||||
|
||||
// 填充订单关联数据中的OrderId
|
||||
orderNoIdDict = queryOrders.ToDictionary(x => x.OrderNo, x => x.Id);
|
||||
foreach (var order in orders)
|
||||
{
|
||||
var orderId = orderNoIdDict[order.OrderNo];
|
||||
order.OrderFlights.ForEach(x => x.OrderId = orderId);
|
||||
order.OrderPullStatus.ForEach(x => x.OrderId = orderId);
|
||||
order.OrderPlatformNumbers.ForEach(x => x.OrderId = orderId);
|
||||
}
|
||||
|
||||
await Context.Insertable(orderFlights).ExecuteCommandAsync();
|
||||
await Context.Insertable(orderPullStatus).ExecuteCommandAsync();
|
||||
await Context.Insertable(orderPlatformNumbers).ExecuteCommandAsync();
|
||||
}
|
||||
finally
|
||||
{
|
||||
tran.Complete();
|
||||
}
|
||||
|
||||
return orderNoIdDict;
|
||||
}
|
||||
|
||||
public Task<int> UpdateBatchIdsAsync(List<int> ids, int newValue)
|
||||
{
|
||||
var idsString = string.Join(",", ids);
|
||||
@ -322,6 +369,48 @@ public class OrderRepository : BasicRepository<OrderEntity>, IOrderRepository, I
|
||||
tran.Complete();
|
||||
}
|
||||
|
||||
public async Task UpdateBatchOrderAsync(List<OrderEntity> orders)
|
||||
{
|
||||
using var tran = TransactionHelper.GetReadCommitted();
|
||||
|
||||
try
|
||||
{
|
||||
// 更新订单主表
|
||||
await UpdateRangeAsync(orders, x => new
|
||||
{
|
||||
x.CustomId,
|
||||
x.SubSystemId
|
||||
});
|
||||
|
||||
var orderIds = orders.Select(x => x.Id).ToHashSet();
|
||||
var orderFlights = orders.SelectMany(x => x.OrderFlights).ToList();
|
||||
var orderPullStatus = orders.SelectMany(x => x.OrderPullStatus).ToList();
|
||||
var orderPlatformNumbers = orders.SelectMany(x => x.OrderPlatformNumbers).ToList();
|
||||
foreach (var order in orders)
|
||||
{
|
||||
order.OrderPullStatus.ForEach(x => x.OrderId = order.Id);
|
||||
order.OrderPlatformNumbers.ForEach(x => x.OrderId = order.Id);
|
||||
order.OrderFlights.ForEach(x => x.OrderId = order.Id);
|
||||
}
|
||||
|
||||
// 更新订单模板
|
||||
await Context.Deleteable<OrderPullStatusEntity>().Where(x => orderIds.Contains(x.OrderId)).ExecuteCommandAsync();
|
||||
await Context.Insertable(orderPullStatus).ExecuteCommandAsync();
|
||||
|
||||
// 更新订单三方关联
|
||||
await Context.Deleteable<OrderPlatformNumberEntity>().Where(x => orderIds.Contains(x.OrderId)).ExecuteCommandAsync();
|
||||
await Context.Insertable(orderPlatformNumbers).ExecuteCommandAsync();
|
||||
|
||||
// 更新订单航班
|
||||
await Context.Deleteable<OrderFlightEntity>().Where(x => orderIds.Contains(x.OrderId)).ExecuteCommandAsync();
|
||||
await Context.Insertable(orderFlights).ExecuteCommandAsync();
|
||||
}
|
||||
finally
|
||||
{
|
||||
tran.Complete();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DeleteOrderAsync(int orderId)
|
||||
{
|
||||
using var tran = TransactionHelper.GetReadCommitted();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user