From 80e94db8374cfe4e1d56b8e9df070768ad47d8be Mon Sep 17 00:00:00 2001 From: hardstylewyl <827778732@qq.com> Date: Tue, 24 Jun 2025 16:45:02 +0800 Subject: [PATCH 1/2] =?UTF-8?q?add-interface:=E5=A2=9E=E5=8A=A0=E6=89=B9?= =?UTF-8?q?=E9=87=8F=E5=88=9B=E5=BB=BA=E8=BD=A8=E8=BF=B9=E8=AE=A2=E5=8D=95?= =?UTF-8?q?=E7=9A=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Dtos/ProvideServices/UpsertOrderInput.cs | 7 ++ .../Services/ProvideServices/OrderService.cs | 101 +++++++++++++++++- .../OrderAggregate/IOrderRepository.cs | 14 +++ .../Repositories/OrderRepository.cs | 89 +++++++++++++++ 4 files changed, 210 insertions(+), 1 deletion(-) diff --git a/src/SJZY.Track.Application.Contract/Dtos/ProvideServices/UpsertOrderInput.cs b/src/SJZY.Track.Application.Contract/Dtos/ProvideServices/UpsertOrderInput.cs index 628a6155..a32b72d8 100644 --- a/src/SJZY.Track.Application.Contract/Dtos/ProvideServices/UpsertOrderInput.cs +++ b/src/SJZY.Track.Application.Contract/Dtos/ProvideServices/UpsertOrderInput.cs @@ -15,6 +15,13 @@ public class UpsertOrderInput : UpdateOrderInput public int OptionUserId { get; set; } } +public class BatchUpsertOrderInput +{ + [Required] + [MaxLength(100)] + public List OrderInputs { get; set; } = new(); +} + public class OrderFlightInfo { /// diff --git a/src/SJZY.Track.Application/Services/ProvideServices/OrderService.cs b/src/SJZY.Track.Application/Services/ProvideServices/OrderService.cs index b4917999..8c110fdc 100644 --- a/src/SJZY.Track.Application/Services/ProvideServices/OrderService.cs +++ b/src/SJZY.Track.Application/Services/ProvideServices/OrderService.cs @@ -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(input); - order.SubSystemId = input.SubSystemId; order.OrderFlights = ObjectMapper.Map, List>(input.FlightInfos); order.OrderPullStatus = BuiltOrderPullStatus(input.PullConfigInfos); order.OrderPlatformNumbers = BuiltOrderPlatformNumbers(input.PullConfigInfos); @@ -109,6 +110,104 @@ public partial class OrderService : ApplicationService, IOrderService }); } + /// + /// 批量创建/新增 轨迹订单 + /// + /// + /// + /// + [HttpPost("Order/BatchUpsert")] + public async Task 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(); + 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(existsOrders.Count) : Empty.List; + var updateRecords = existsOrders.Count > 0 ? new List(existsOrders.Count) : Empty.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>(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(insertInputs.Count); + var createRecords = new List(insertInputs.Count); + foreach (var insertInput in insertInputs) + { + var order = ObjectMapper.Map(insertInput); + order.OrderFlights = ObjectMapper.Map, List>(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); + } + /// /// 删除订单 /// diff --git a/src/SJZY.Track.Domain/Aggregates/OrderAggregate/IOrderRepository.cs b/src/SJZY.Track.Domain/Aggregates/OrderAggregate/IOrderRepository.cs index 75ce70f3..fc8834f9 100644 --- a/src/SJZY.Track.Domain/Aggregates/OrderAggregate/IOrderRepository.cs +++ b/src/SJZY.Track.Domain/Aggregates/OrderAggregate/IOrderRepository.cs @@ -34,6 +34,13 @@ public interface IOrderRepository : IBasicRepository /// Task CreateOrderAsync(OrderEntity order); + /// + /// 批量创建订单 + /// + /// + /// + Task> CreateBatchOrderAsync(List orders); + /// /// 更新订单 /// @@ -41,6 +48,13 @@ public interface IOrderRepository : IBasicRepository /// Task UpdateOrderAsync(OrderEntity order); + /// + /// 批量更新订单 + /// + /// + /// + Task UpdateBatchOrderAsync(List orders); + /// /// 删除订单 /// diff --git a/src/SJZY.Track.Repository/Repositories/OrderRepository.cs b/src/SJZY.Track.Repository/Repositories/OrderRepository.cs index e2ef66ee..55690b31 100644 --- a/src/SJZY.Track.Repository/Repositories/OrderRepository.cs +++ b/src/SJZY.Track.Repository/Repositories/OrderRepository.cs @@ -282,6 +282,53 @@ public class OrderRepository : BasicRepository, IOrderRepository, I return orderId; } + public async Task> CreateBatchOrderAsync(List orders) + { + Dictionary 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(); + 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 UpdateBatchIdsAsync(List ids, int newValue) { var idsString = string.Join(",", ids); @@ -322,6 +369,48 @@ public class OrderRepository : BasicRepository, IOrderRepository, I tran.Complete(); } + public async Task UpdateBatchOrderAsync(List 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().Where(x => orderIds.Contains(x.OrderId)).ExecuteCommandAsync(); + await Context.Insertable(orderPullStatus).ExecuteCommandAsync(); + + // 更新订单三方关联 + await Context.Deleteable().Where(x => orderIds.Contains(x.OrderId)).ExecuteCommandAsync(); + await Context.Insertable(orderPlatformNumbers).ExecuteCommandAsync(); + + // 更新订单航班 + await Context.Deleteable().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(); From 7ba774b0773b42025c014e525a6b7123e1364554 Mon Sep 17 00:00:00 2001 From: hardstylewyl <827778732@qq.com> Date: Tue, 24 Jun 2025 16:45:27 +0800 Subject: [PATCH 2/2] update-package:7.0.23->7.0.24 --- .../SJZY.Track.Application.Contract.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SJZY.Track.Application.Contract/SJZY.Track.Application.Contract.csproj b/src/SJZY.Track.Application.Contract/SJZY.Track.Application.Contract.csproj index 57603afe..fecf6b17 100644 --- a/src/SJZY.Track.Application.Contract/SJZY.Track.Application.Contract.csproj +++ b/src/SJZY.Track.Application.Contract/SJZY.Track.Application.Contract.csproj @@ -5,7 +5,7 @@ net7.0 True - 7.0.23 + 7.0.24 True