feat: 提交审批之后自动同步功能

This commit is contained in:
lulz1 2024-06-19 14:50:08 +08:00
parent 78c4bb7812
commit a27a9eacae
1 changed files with 28 additions and 21 deletions

View File

@ -18,9 +18,11 @@ import com.chint.infrastructure.echo_framework.annotation.ListenTo;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static com.chint.infrastructure.constant.LegConstant.LEG_EVENT_APPROVAL; import static com.chint.infrastructure.constant.LegConstant.LEG_EVENT_APPROVAL;
@ -77,18 +79,22 @@ public class RouteRequestDomainService {
@ListenTo(command = "RouteAutoSyncCommand", order = 0) @ListenTo(command = "RouteAutoSyncCommand", order = 0)
public RouteRequest syncRouteRequestToSupplierAuto(RouteAutoSyncCommand command) { public void syncRouteRequestToSupplierAuto(RouteAutoSyncCommand command) {
RouteOrder routeOrder = command.getRouteOrder(); RouteOrder routeOrder = command.getRouteOrder();
legDomainService.checkLegIfStartApproval(routeOrder); legDomainService.checkLegIfStartApproval(routeOrder);
List<CompletableFuture<Void>> futures = new ArrayList<>();
supplierRepository.findAll() supplierRepository.findAll()
.forEach(it -> { .forEach(it -> futures.add(
List<Leg> legs = legDomainService.queryLegsCanSync(routeOrder, it); CompletableFuture.runAsync(() -> {
List<Long> legIdList = legs.stream().map(leg -> leg.getLegId()).toList(); List<Leg> legs = legDomainService.queryLegsCanSync(routeOrder, it);
RouteRequest routeRequest = getRouteRequest(routeOrder, legIdList, it.getName()); List<Long> legIdList = legs.stream().map(Leg::getLegId).collect(Collectors.toList());
RouteRequest routeRequest = createRouteRequest(routeOrder, legIdList, it.getName());
}); syncRouteRequest(routeRequest, it);
return null; })
));
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
.thenRun(() -> routeRepository.save(routeOrder)) //保存聚合根
.join();
} }
public RouteRequest syncRouteRequestToSupplier(SyncLegData syncLegData) { public RouteRequest syncRouteRequestToSupplier(SyncLegData syncLegData) {
@ -96,10 +102,20 @@ public class RouteRequestDomainService {
legDomainService.checkLegIfStartApproval(routeOrder); legDomainService.checkLegIfStartApproval(routeOrder);
List<Long> legIds = syncLegData.getLegIds(); List<Long> legIds = syncLegData.getLegIds();
String supplierName = syncLegData.getSupplierName(); 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<Long> legIds, String supplierName) { private RouteRequest createRouteRequest(RouteOrder routeOrder, List<Long> legIds, String supplierName) {
RouteRequest routeRequest = getRouteRequest(routeOrder, legIds, supplierName); RouteRequest routeRequest = getRouteRequest(routeOrder, legIds, supplierName);
if (routeRequest.getStatus().equals(ROUTE_REQUEST_STATUS_PREPARE) && routeRequest.getSupplier() == null) { if (routeRequest.getStatus().equals(ROUTE_REQUEST_STATUS_PREPARE) && routeRequest.getSupplier() == null) {
//如果是刚生成的订单需要加入行程规划单当中 //如果是刚生成的订单需要加入行程规划单当中
@ -114,22 +130,13 @@ public class RouteRequestDomainService {
.stream() .stream()
.map(RouteRequestLeg::getLeg) .map(RouteRequestLeg::getLeg)
.forEach(it -> it.addEvent(legEventFactory.creatLegEvent(LEG_EVENT_NOT_ORDERED))); .forEach(it -> it.addEvent(legEventFactory.creatLegEvent(LEG_EVENT_NOT_ORDERED)));
supplierRepository.findBySupplierName(supplierName)
.ifPresentOrElse(
supplier -> syncRouteRequest(routeRequest, supplier, routeOrder),
() -> {
throw new CommandException("未找到供应商,无法同步差旅申请单,联系管理员");
}
);
return routeRequest; 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)) { if (supplierAdapterSelector.of(supplier.getName(), SupplierOrderSync.class).syncRouteRequest(routeRequest, supplier)) {
routeRequest.addEvent(RouteRequestEvent.sync(supplier.getName())); routeRequest.addEvent(RouteRequestEvent.sync(supplier.getName()));
// 通过保存聚合根来保存差旅申请单
routeRepository.save(routeOrder);
} else { } else {
throw new CommandException("同步差旅申请单失败,联系管理员"); throw new CommandException("同步差旅申请单失败,联系管理员");
} }