From a27a9eacae4914074ef7f9479d94291c2b24e909 Mon Sep 17 00:00:00 2001 From: lulz1 Date: Wed, 19 Jun 2024 14:50:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=8F=90=E4=BA=A4=E5=AE=A1=E6=89=B9?= =?UTF-8?q?=E4=B9=8B=E5=90=8E=E8=87=AA=E5=8A=A8=E5=90=8C=E6=AD=A5=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/RouteRequestDomainService.java | 49 +++++++++++-------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/chint/domain/service/RouteRequestDomainService.java b/src/main/java/com/chint/domain/service/RouteRequestDomainService.java index 4c722052..cae28c9a 100644 --- a/src/main/java/com/chint/domain/service/RouteRequestDomainService.java +++ b/src/main/java/com/chint/domain/service/RouteRequestDomainService.java @@ -18,9 +18,11 @@ import com.chint.infrastructure.echo_framework.annotation.ListenTo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; import static com.chint.infrastructure.constant.LegConstant.LEG_EVENT_APPROVAL; @@ -77,18 +79,22 @@ public class RouteRequestDomainService { @ListenTo(command = "RouteAutoSyncCommand", order = 0) - public RouteRequest syncRouteRequestToSupplierAuto(RouteAutoSyncCommand command) { + public void syncRouteRequestToSupplierAuto(RouteAutoSyncCommand command) { RouteOrder routeOrder = command.getRouteOrder(); legDomainService.checkLegIfStartApproval(routeOrder); - + List> futures = new ArrayList<>(); supplierRepository.findAll() - .forEach(it -> { - List legs = legDomainService.queryLegsCanSync(routeOrder, it); - List legIdList = legs.stream().map(leg -> leg.getLegId()).toList(); - RouteRequest routeRequest = getRouteRequest(routeOrder, legIdList, it.getName()); - - }); - return null; + .forEach(it -> futures.add( + CompletableFuture.runAsync(() -> { + List legs = legDomainService.queryLegsCanSync(routeOrder, it); + List legIdList = legs.stream().map(Leg::getLegId).collect(Collectors.toList()); + RouteRequest routeRequest = createRouteRequest(routeOrder, legIdList, it.getName()); + syncRouteRequest(routeRequest, it); + }) + )); + CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])) + .thenRun(() -> routeRepository.save(routeOrder)) //保存聚合根 + .join(); } public RouteRequest syncRouteRequestToSupplier(SyncLegData syncLegData) { @@ -96,10 +102,20 @@ public class RouteRequestDomainService { legDomainService.checkLegIfStartApproval(routeOrder); List legIds = syncLegData.getLegIds(); String supplierName = syncLegData.getSupplierName(); - return createAndSyncRouteRequest(routeOrder, legIds, supplierName); + RouteRequest routeRequest = createRouteRequest(routeOrder, legIds, supplierName); + supplierRepository.findBySupplierName(supplierName) + .ifPresentOrElse( + supplier -> syncRouteRequest(routeRequest, supplier), + () -> { + throw new CommandException("未找到供应商,无法同步差旅申请单,联系管理员"); + } + ); + //保存聚合根 + routeRepository.save(routeOrder); + return routeRequest; } - private RouteRequest createAndSyncRouteRequest(RouteOrder routeOrder, List legIds, String supplierName) { + private RouteRequest createRouteRequest(RouteOrder routeOrder, List legIds, String supplierName) { RouteRequest routeRequest = getRouteRequest(routeOrder, legIds, supplierName); if (routeRequest.getStatus().equals(ROUTE_REQUEST_STATUS_PREPARE) && routeRequest.getSupplier() == null) { //如果是刚生成的订单需要加入行程规划单当中 @@ -114,22 +130,13 @@ public class RouteRequestDomainService { .stream() .map(RouteRequestLeg::getLeg) .forEach(it -> it.addEvent(legEventFactory.creatLegEvent(LEG_EVENT_NOT_ORDERED))); - supplierRepository.findBySupplierName(supplierName) - .ifPresentOrElse( - supplier -> syncRouteRequest(routeRequest, supplier, routeOrder), - () -> { - throw new CommandException("未找到供应商,无法同步差旅申请单,联系管理员"); - } - ); return routeRequest; } // 提取同步逻辑到一个单独的方法 - private void syncRouteRequest(RouteRequest routeRequest, Supplier supplier, RouteOrder routeOrder) { + private void syncRouteRequest(RouteRequest routeRequest, Supplier supplier) { if (supplierAdapterSelector.of(supplier.getName(), SupplierOrderSync.class).syncRouteRequest(routeRequest, supplier)) { routeRequest.addEvent(RouteRequestEvent.sync(supplier.getName())); - // 通过保存聚合根来保存差旅申请单 - routeRepository.save(routeOrder); } else { throw new CommandException("同步差旅申请单失败,联系管理员"); }