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("同步差旅申请单失败,联系管理员"); }