From 7fdc0f79bff69c9923c509537af92c37d6af5ea9 Mon Sep 17 00:00:00 2001 From: lulz1 Date: Fri, 12 Apr 2024 09:18:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E5=8A=9F=E8=83=BD=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=AF=B9=E5=B7=B2=E4=B8=8B=E5=8D=95=E7=9A=84=E9=A3=9E?= =?UTF-8?q?=E6=9C=BA=E4=B8=8D=E8=BF=9B=E8=A1=8C=E5=8F=96=E6=B6=88=E5=90=8C?= =?UTF-8?q?=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/aggregates/order/RouteRequest.java | 49 ++++++++++++++++--- .../service/RouteRequestDomainService.java | 32 ++++++++++-- 2 files changed, 70 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/chint/domain/aggregates/order/RouteRequest.java b/src/main/java/com/chint/domain/aggregates/order/RouteRequest.java index 20c43ebe..2a2fc6df 100644 --- a/src/main/java/com/chint/domain/aggregates/order/RouteRequest.java +++ b/src/main/java/com/chint/domain/aggregates/order/RouteRequest.java @@ -18,8 +18,9 @@ import java.io.Serializable; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; - -import static com.chint.infrastructure.constant.SupplierNameConstant.*; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; @Data @Table("route_request") @@ -79,13 +80,49 @@ public class RouteRequest implements Serializable, EventManageable { return this; } - public RouteRequest reloadGenerateRequestLegs(List legList) { - List routeRequestLegs = legList.stream().map(RouteRequestLeg::ofLeg).toList(); - this.setRouteRequestLegList(routeRequestLegs); + public RouteRequest loadLegInfoSelf(RouteOrder routeOrder) { + if (this.routeRequestLegList != null && !this.routeRequestLegList.isEmpty()) { + List legItems = routeOrder.getLegItems(); + List list = this.routeRequestLegList.stream().map(RouteRequestLeg::getLegId).toList(); + List legs = legItems.stream().filter(it -> list.contains(it.getLegId())).toList(); + return reloadGenerateRequestLegs(legs); + } return this; } - public RouteRequest reloadSupplierInfo(){ + public RouteRequest reloadGenerateRequestLegs(List legList) { + // 创建一个新的临时列表 + List tempLegList = new ArrayList<>(); + + // 如果原列表为空,则初始化为空列表 + if (this.routeRequestLegList == null) { + this.routeRequestLegList = new ArrayList<>(); + } + + // 创建一个Map从legId到RouteRequestLeg进行快速查找 + Map existingLegsMap = this.routeRequestLegList.stream() + .collect(Collectors.toMap(RouteRequestLeg::getLegId, Function.identity())); + + // 遍历传入的leg列表 + for (Leg leg : legList) { + RouteRequestLeg existingLeg = existingLegsMap.get(leg.getLegId()); + if (existingLeg != null) { + // 如果leg已存在,更新非持久化的'leg'字段 + existingLeg.setLeg(leg); + } else { + // 如果是新的leg,创建一个新的RouteRequestLeg + existingLeg = RouteRequestLeg.ofLeg(leg); + } + // 将更新或新创建的RouteRequestLeg添加到临时列表中 + tempLegList.add(existingLeg); + } + + // 使用新的临时列表替换旧的leg列表 + this.routeRequestLegList = tempLegList; + return this; + } + + public RouteRequest reloadSupplierInfo() { this.setSupplierName(SupplierConstantUtil.translateOrderSupplierName(this.supplier)); return this; } diff --git a/src/main/java/com/chint/domain/service/RouteRequestDomainService.java b/src/main/java/com/chint/domain/service/RouteRequestDomainService.java index 6bf922be..689cd4e8 100644 --- a/src/main/java/com/chint/domain/service/RouteRequestDomainService.java +++ b/src/main/java/com/chint/domain/service/RouteRequestDomainService.java @@ -13,7 +13,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; +import java.util.Map; import java.util.Optional; +import java.util.stream.Collectors; import static com.chint.infrastructure.constant.LegConstant.*; import static com.chint.infrastructure.constant.RouteRequestConstant.ROUTE_REQUEST_STATUS_PREPARE; @@ -114,11 +116,30 @@ public class RouteRequestDomainService { .findFirst(); } optionalRouteRequest.ifPresent(it -> { - it.addEvent(RouteRequestEvent.cancel(it.getSupplier())); - getLegInfoFromRouteOrder(it, routeOrder); - syncAdapter.of(it.getSupplier()).cancelRouteRequest(it); - it.getRouteRequestLegList() - .forEach(requestLeg -> requestLeg.getLeg().addEvent(legEventFactory.creatLegEvent(LEG_EVENT_APPROVAL))); + //这里的逻辑为发现 差旅申请单的机票行程节点 如果已经是 下单的状态, 就不进行取消, 因此实际上按照 该差旅申请单的 同步来进行 + List legs = it.loadLegInfoSelf(routeOrder) + .getRouteRequestLegList() + .stream() + .map(RouteRequestLeg::getLeg) + .toList(); + + Map> collect = legs + .stream() + .collect(Collectors.partitioningBy(leg -> leg.reloadStatus().getLegStatus().equals(LEG_STATUS_ORDERED))); + List orderedLegs = collect.get(true); + if (!orderedLegs.isEmpty()) { + it.reloadGenerateRequestLegs(orderedLegs); + syncAdapter.of(it.getSupplier()).syncRouteRequest(it); + it.addEvent(RouteRequestEvent.sync(it.getSupplier())); + List notOrderLegs = collect.get(false); + notOrderLegs.forEach(leg->leg.addEvent(legEventFactory.creatLegEvent(LEG_EVENT_APPROVAL))); + } else { + it.addEvent(RouteRequestEvent.cancel(it.getSupplier())); + getLegInfoFromRouteOrder(it, routeOrder); + syncAdapter.of(it.getSupplier()).cancelRouteRequest(it); + it.getRouteRequestLegList() + .forEach(requestLeg -> requestLeg.getLeg().addEvent(legEventFactory.creatLegEvent(LEG_EVENT_APPROVAL))); + } }); routeRepository.save(routeOrder); } @@ -156,6 +177,7 @@ public class RouteRequestDomainService { return routeRequest.reloadGenerateRequestLegs(legItems); } + private RouteRequest getLegInfoFromRouteOrder(RouteRequest routeRequest, RouteOrder routeOrder) { List legItems = routeOrder.getLegItems(); routeRequest.getRouteRequestLegList().forEach(it -> {