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 -> {