diff --git a/src/main/java/com/chint/application/commands/RouteAddOrderCommand.java b/src/main/java/com/chint/application/commands/RouteUpdateOrderCommand.java similarity index 77% rename from src/main/java/com/chint/application/commands/RouteAddOrderCommand.java rename to src/main/java/com/chint/application/commands/RouteUpdateOrderCommand.java index 32591f89..4cc69774 100644 --- a/src/main/java/com/chint/application/commands/RouteAddOrderCommand.java +++ b/src/main/java/com/chint/application/commands/RouteUpdateOrderCommand.java @@ -7,10 +7,10 @@ import lombok.Data; import static com.chint.infrastructure.constant.Constant.LEG_EVENT_ORDERED; @Data -public class RouteAddOrderCommand extends Command { +public class RouteUpdateOrderCommand extends Command { private Integer LegEventType = LEG_EVENT_ORDERED; private OrderLegData data; - public RouteAddOrderCommand data(OrderLegData data) { + public RouteUpdateOrderCommand data(OrderLegData data) { this.data = data; return this; } diff --git a/src/main/java/com/chint/application/services/OrderApplicationService.java b/src/main/java/com/chint/application/services/OrderApplicationService.java index 5a51ba0b..8449330a 100644 --- a/src/main/java/com/chint/application/services/OrderApplicationService.java +++ b/src/main/java/com/chint/application/services/OrderApplicationService.java @@ -87,7 +87,7 @@ public class OrderApplicationService { @Transactional public void ordered(OrderLegData data) { - Command.of(RouteAddOrderCommand.class).data(data).sendToQueue(); + Command.of(RouteUpdateOrderCommand.class).data(data).sendToQueue(); } @Transactional diff --git a/src/main/java/com/chint/domain/aggregates/order/CarOrderDetail.java b/src/main/java/com/chint/domain/aggregates/order/CarOrderDetail.java new file mode 100644 index 00000000..edce6e8e --- /dev/null +++ b/src/main/java/com/chint/domain/aggregates/order/CarOrderDetail.java @@ -0,0 +1,12 @@ +package com.chint.domain.aggregates.order; + +import lombok.Data; +import org.springframework.data.annotation.Id; +import org.springframework.data.relational.core.mapping.Table; + +@Data +@Table("car_order_detail") +public class CarOrderDetail { + @Id + private Long id; +} diff --git a/src/main/java/com/chint/domain/aggregates/order/FlightOrderDetail.java b/src/main/java/com/chint/domain/aggregates/order/FlightOrderDetail.java new file mode 100644 index 00000000..66659d7e --- /dev/null +++ b/src/main/java/com/chint/domain/aggregates/order/FlightOrderDetail.java @@ -0,0 +1,12 @@ +package com.chint.domain.aggregates.order; + +import lombok.Data; +import org.springframework.data.annotation.Id; +import org.springframework.data.relational.core.mapping.Table; + +@Data +@Table("flight_order_detail") +public class FlightOrderDetail { + @Id + private Long id; +} diff --git a/src/main/java/com/chint/domain/aggregates/order/HotelOrderDetail.java b/src/main/java/com/chint/domain/aggregates/order/HotelOrderDetail.java new file mode 100644 index 00000000..b725f9c6 --- /dev/null +++ b/src/main/java/com/chint/domain/aggregates/order/HotelOrderDetail.java @@ -0,0 +1,13 @@ +package com.chint.domain.aggregates.order; + + +import lombok.Data; +import org.springframework.data.annotation.Id; +import org.springframework.data.relational.core.mapping.Table; + +@Data +@Table("hotel_order_detail") +public class HotelOrderDetail { + @Id + private Long id; +} diff --git a/src/main/java/com/chint/domain/aggregates/order/OrderDetail.java b/src/main/java/com/chint/domain/aggregates/order/OrderDetail.java index 4847a32a..58fed40c 100644 --- a/src/main/java/com/chint/domain/aggregates/order/OrderDetail.java +++ b/src/main/java/com/chint/domain/aggregates/order/OrderDetail.java @@ -38,6 +38,21 @@ public class OrderDetail { @MappedCollection(idColumn = "order_id", keyColumn = "order_key") private List orderEventList; + @MappedCollection(idColumn = "order_id") + private CarOrderDetail carOrderDetail; + + @MappedCollection(idColumn = "order_id") + private FlightOrderDetail flightOrderDetail; + + @MappedCollection(idColumn = "order_id") + private HotelOrderDetail hotelOrderDetail; + + @MappedCollection(idColumn = "order_id") + private TrainOrderDetail trainOrderDetail; + + @MappedCollection(idColumn = "order_id") + private OtherOrderDetail otherOrderDetail; + @Transient private String orderStatus; diff --git a/src/main/java/com/chint/domain/aggregates/order/OrderEvent.java b/src/main/java/com/chint/domain/aggregates/order/OrderEvent.java index 6cd4fa9a..968dcd6e 100644 --- a/src/main/java/com/chint/domain/aggregates/order/OrderEvent.java +++ b/src/main/java/com/chint/domain/aggregates/order/OrderEvent.java @@ -9,7 +9,6 @@ import org.springframework.data.relational.core.mapping.Table; import java.time.LocalDateTime; import static com.chint.infrastructure.constant.Constant.*; -import static com.chint.infrastructure.constant.Constant.LEG_EVENT_REJECT_NAME; @Data @Table("order_event") @@ -32,6 +31,7 @@ public class OrderEvent { case ORDER_EVENT_CHANGE -> ORDER_EVENT_CHANGE_NAME; case ORDER_EVENT_CANCEL -> ORDER_EVENT_CANCEL_NAME; case ORDER_EVENT_ORDERED -> ORDER_EVENT_ORDERED_NAME; + case ORDER_EVENT_REFUND -> ORDER_EVENT_REFUND_NAME; default -> "未知事件"; }; } @@ -40,4 +40,10 @@ public class OrderEvent { this.setEventName(translateOrderEvent(this.eventType)); return this; } + + public OrderEvent of(Integer eventType) { + OrderEvent orderEvent = new OrderEvent(); + orderEvent.setEventType(eventType); + return orderEvent; + } } diff --git a/src/main/java/com/chint/domain/aggregates/order/OtherOrderDetail.java b/src/main/java/com/chint/domain/aggregates/order/OtherOrderDetail.java new file mode 100644 index 00000000..7f1bb4fc --- /dev/null +++ b/src/main/java/com/chint/domain/aggregates/order/OtherOrderDetail.java @@ -0,0 +1,12 @@ +package com.chint.domain.aggregates.order; + +import lombok.Data; +import org.springframework.data.annotation.Id; +import org.springframework.data.relational.core.mapping.Table; + +@Data +@Table("other_order_detail") +public class OtherOrderDetail { + @Id + private Long id; +} diff --git a/src/main/java/com/chint/domain/aggregates/order/TrainOrderDetail.java b/src/main/java/com/chint/domain/aggregates/order/TrainOrderDetail.java new file mode 100644 index 00000000..bc94e5fc --- /dev/null +++ b/src/main/java/com/chint/domain/aggregates/order/TrainOrderDetail.java @@ -0,0 +1,13 @@ +package com.chint.domain.aggregates.order; + + +import lombok.Data; +import org.springframework.data.annotation.Id; +import org.springframework.data.relational.core.mapping.Table; + +@Data +@Table("train_order_detail") +public class TrainOrderDetail { + @Id + private Long id; +} diff --git a/src/main/java/com/chint/domain/factoriy/order_detail/OrderDetailFactory.java b/src/main/java/com/chint/domain/factoriy/order_detail/OrderDetailFactory.java index a0d14530..66d5a682 100644 --- a/src/main/java/com/chint/domain/factoriy/order_detail/OrderDetailFactory.java +++ b/src/main/java/com/chint/domain/factoriy/order_detail/OrderDetailFactory.java @@ -1,8 +1,11 @@ package com.chint.domain.factoriy.order_detail; import com.chint.domain.aggregates.order.OrderDetail; +import com.chint.domain.aggregates.order.OrderEvent; import com.chint.domain.value_object.OrderLegData; public interface OrderDetailFactory { OrderDetail create(OrderLegData orderLegData); + + OrderEvent createEvent(Integer eventType); } diff --git a/src/main/java/com/chint/domain/factoriy/order_detail/OrderDetailFactoryImpl.java b/src/main/java/com/chint/domain/factoriy/order_detail/OrderDetailFactoryImpl.java index ee72a57b..0109278a 100644 --- a/src/main/java/com/chint/domain/factoriy/order_detail/OrderDetailFactoryImpl.java +++ b/src/main/java/com/chint/domain/factoriy/order_detail/OrderDetailFactoryImpl.java @@ -1,6 +1,7 @@ package com.chint.domain.factoriy.order_detail; import com.chint.domain.aggregates.order.OrderDetail; +import com.chint.domain.aggregates.order.OrderEvent; import com.chint.domain.value_object.OrderLegData; import org.springframework.stereotype.Component; @@ -10,4 +11,9 @@ public class OrderDetailFactoryImpl implements OrderDetailFactory { public OrderDetail create(OrderLegData orderLegData) { return OrderDetail.of(orderLegData.getOrderNo(), orderLegData.getSupplierName()); } + + @Override + public OrderEvent createEvent(Integer eventType) { + return null; + } } diff --git a/src/main/java/com/chint/domain/repository/OrderDetailRepository.java b/src/main/java/com/chint/domain/repository/OrderDetailRepository.java index 956eabf5..2bf134a1 100644 --- a/src/main/java/com/chint/domain/repository/OrderDetailRepository.java +++ b/src/main/java/com/chint/domain/repository/OrderDetailRepository.java @@ -2,6 +2,10 @@ package com.chint.domain.repository; import com.chint.domain.aggregates.order.OrderDetail; +import java.util.Optional; + public interface OrderDetailRepository { OrderDetail findById(Long orderDetailId); + + Optional findByOrderNo(String orderNo); } diff --git a/src/main/java/com/chint/domain/service/leg_event/LegEventHandler.java b/src/main/java/com/chint/domain/service/leg_event/LegEventHandler.java index dc71f316..d6566459 100644 --- a/src/main/java/com/chint/domain/service/leg_event/LegEventHandler.java +++ b/src/main/java/com/chint/domain/service/leg_event/LegEventHandler.java @@ -4,6 +4,7 @@ import com.chint.application.commands.*; import com.chint.domain.aggregates.order.*; import com.chint.domain.exceptions.CommandException; import com.chint.domain.factoriy.leg_event.LegEventFactory; +import com.chint.domain.factoriy.order.RouteOrderFactory; import com.chint.domain.factoriy.order_detail.OrderDetailFactory; import com.chint.domain.repository.LegRepository; import com.chint.domain.repository.OrderDetailRepository; @@ -17,8 +18,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import static com.chint.infrastructure.constant.Constant.ORDER_STATUS_APPROVAL; -import static com.chint.infrastructure.constant.Constant.ORDER_STATUS_PREPARE; +import java.util.Optional; + +import static com.chint.infrastructure.constant.Constant.*; @Service public class LegEventHandler implements LegEventService { @@ -38,6 +40,9 @@ public class LegEventHandler implements LegEventService { @Autowired private OrderDetailFactory orderDetailFactory; + @Autowired + private RouteOrderFactory routeOrderFactory; + @Autowired private OrderDetailRepository orderDetailRepository; @@ -99,28 +104,43 @@ public class LegEventHandler implements LegEventService { @Transactional @Override - public void routeAddOrder(RouteAddOrderCommand command) { + public void routeUpdateOrder(RouteUpdateOrderCommand command) { OrderLegData data = command.getData(); - //首先查询到 行程规划订单 , 为形成规划订单添加 外部订单信息 - OrderDetail orderDetail = orderDetailFactory.create(data) - .price(data.getPrice()).productType(data.getProductType()); String actualOrderNo = data.getActualOrderNo(); RouteOrder routeOrder = routeRepository.findByActualOrderNo(actualOrderNo); - routeOrder.addOrderDetail(orderDetail); - //结合外部订单信息,筛选最合适的leg节点 ,触发下单事件 ; - routeOrder.getLegItems() + //首先查询到行程规划订单 , 从routOrder当中查询是否以及存在该订单 + Optional byOrderNo = routeOrder.getOrderDetails() .stream() - .filter(leg -> leg.getLegType().equals(orderDetail.getProductType()) - && leg.getDestinationId().equals(orderDetail.getDestinationId()) - && leg.getOriginId().equals(orderDetail.getOriginId()) - && leg.getStartTime().isBefore(orderDetail.getStartTime()) - && leg.getEndTime().isAfter(orderDetail.getEndTime())) - .findFirst() - .ifPresent(leg -> { - LegEvent legEvent = legEventFactory.creatLegEvent(command.getLegEventType()); - legEvent.addOrderDetail(orderDetail); - leg.addEvent(legEvent); - }); + .filter(orderDetail -> orderDetail.getOrderNo().equals(data.getOrderNo())) + .findFirst(); + if (byOrderNo.isPresent()) { + //如果订单以及存在了,对订单的状态进行更新, + OrderEvent event = orderDetailFactory.createEvent(data.getOrderStatus()); + OrderDetail orderDetail = byOrderNo.get(); + orderDetail.addOrderEvent(event); + } else { + //否则创建新的订单添加到routeOrder + OrderDetail orderDetail = orderDetailFactory.create(data) + .price(data.getPrice()).productType(data.getProductType()); + //为订单添加订单已下单事件 + OrderEvent orderEvent = orderDetailFactory.createEvent(ORDER_EVENT_PREPARE); + orderDetail.addOrderEvent(orderEvent); + routeOrder.addOrderDetail(orderDetail); + //结合外部订单信息,筛选最合适的leg节点 ,触发下单事件 ; + routeOrder.getLegItems() + .stream() + .filter(leg -> leg.getLegType().equals(orderDetail.getProductType()) + && leg.getDestinationId().equals(orderDetail.getDestinationId()) + && leg.getOriginId().equals(orderDetail.getOriginId()) + && leg.getStartTime().isBefore(orderDetail.getStartTime()) + && leg.getEndTime().isAfter(orderDetail.getEndTime())) + .findFirst() + .ifPresent(leg -> { + LegEvent legEvent = legEventFactory.creatLegEvent(command.getLegEventType()); + legEvent.addOrderDetail(orderDetail); + leg.addEvent(legEvent); + }); + } routeRepository.save(routeOrder); } diff --git a/src/main/java/com/chint/domain/service/leg_event/LegEventService.java b/src/main/java/com/chint/domain/service/leg_event/LegEventService.java index 0b91d8dd..a1bfd78c 100644 --- a/src/main/java/com/chint/domain/service/leg_event/LegEventService.java +++ b/src/main/java/com/chint/domain/service/leg_event/LegEventService.java @@ -14,7 +14,7 @@ public interface LegEventService { //下单事件 void orderLeg(LegOrderedCommand command); - void routeAddOrder(RouteAddOrderCommand command); + void routeUpdateOrder(RouteUpdateOrderCommand command); //付款 void payForLeg(LegPayedCommand command); diff --git a/src/main/java/com/chint/domain/service/leg_event/LegEventServiceImpl.java b/src/main/java/com/chint/domain/service/leg_event/LegEventServiceImpl.java index 7e3d1297..dc4c0594 100644 --- a/src/main/java/com/chint/domain/service/leg_event/LegEventServiceImpl.java +++ b/src/main/java/com/chint/domain/service/leg_event/LegEventServiceImpl.java @@ -1,24 +1,10 @@ package com.chint.domain.service.leg_event; import com.chint.application.commands.*; -import com.chint.domain.aggregates.order.*; -import com.chint.domain.exceptions.CommandException; -import com.chint.domain.factoriy.leg_event.LegEventFactory; -import com.chint.domain.repository.LegRepository; -import com.chint.domain.repository.RouteRepository; -import com.chint.domain.service.order_sync.SyncAdapter; -import com.chint.domain.value_object.ApproveLegData; -import com.chint.domain.value_object.OrderLegData; -import com.chint.domain.value_object.PayLegData; -import com.chint.domain.value_object.SyncLegData; import com.chint.infrastructure.echo_framework.annotation.ListenTo; import com.chint.infrastructure.echo_framework.annotation.TransitionTo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -import static com.chint.infrastructure.constant.Constant.ORDER_STATUS_APPROVAL; -import static com.chint.infrastructure.constant.Constant.ORDER_STATUS_PREPARE; @Service public class LegEventServiceImpl implements LegEventService { @@ -57,10 +43,10 @@ public class LegEventServiceImpl implements LegEventService { legEventHandler.orderLeg(command); } - @ListenTo(command = "RouteAddOrderCommand", order = 0) + @ListenTo(command = "RouteUpdateOrderCommand", order = 0) @Override - public void routeAddOrder(RouteAddOrderCommand command) { - legEventHandler.routeAddOrder(command); + public void routeUpdateOrder(RouteUpdateOrderCommand command) { + legEventHandler.routeUpdateOrder(command); } @Override diff --git a/src/main/java/com/chint/domain/service/supplier/CTripOrderDataAdapter.java b/src/main/java/com/chint/domain/service/supplier/CTripOrderDataAdapter.java index 144f4a9c..fceb2f86 100644 --- a/src/main/java/com/chint/domain/service/supplier/CTripOrderDataAdapter.java +++ b/src/main/java/com/chint/domain/service/supplier/CTripOrderDataAdapter.java @@ -110,4 +110,31 @@ public class CTripOrderDataAdapter implements OrderDataAdapter { default -> throw new NotFoundException(NOT_FOUND); }; } + + private Integer translateCarOrderStatus(String carOrderStatus){ + return switch (carOrderStatus) { + case "CreateOrder" -> ORDER_EVENT_PREPARE; //已下单 + case "WaitReply" -> ORDER_EVENT_ORDERED; //等待应答 + case "WaitService" -> ORDER_EVENT_ORDERED; //等待接驾 + case "DriverArrived" -> ORDER_EVENT_ORDERED; //司机就位 + case "InService" -> ORDER_EVENT_ORDERED; //正在服务 + case "EndService" -> ORDER_EVENT_PAYED; //行程结束 + case "Canceling" -> ORDER_EVENT_CANCEL; //取消中 + case "Canceled" -> ORDER_EVENT_CANCEL; //已取消 + case "Successful" -> ORDER_EVENT_CANCEL; //已成交 + default -> throw new NotFoundException(NOT_FOUND); + }; + } + + private Integer translateFlightOrderStatus(String flightOrderStatus){ + return switch (flightOrderStatus) { + case "W" -> ORDER_EVENT_PREPARE; //未处理 + case "P" -> ORDER_EVENT_ORDERED; //处理中 + case "C" -> ORDER_EVENT_CANCEL; //已取消 + case "R" -> ORDER_EVENT_REFUND; //全部退票 + case "T" -> ORDER_EVENT_REFUND; //部分退票 + case "S" -> ORDER_EVENT_ORDERED; //已成交 + default -> throw new NotFoundException(NOT_FOUND); + }; + } } diff --git a/src/main/java/com/chint/domain/service/supplier/SupplierServiceImpl.java b/src/main/java/com/chint/domain/service/supplier/SupplierServiceImpl.java index 34359d34..17b57ebe 100644 --- a/src/main/java/com/chint/domain/service/supplier/SupplierServiceImpl.java +++ b/src/main/java/com/chint/domain/service/supplier/SupplierServiceImpl.java @@ -1,6 +1,6 @@ package com.chint.domain.service.supplier; -import com.chint.application.commands.RouteAddOrderCommand; +import com.chint.application.commands.RouteUpdateOrderCommand; import com.chint.domain.value_object.SupplierCallbackData; import com.chint.infrastructure.echo_framework.command.Command; import org.springframework.beans.factory.annotation.Autowired; @@ -18,6 +18,6 @@ public class SupplierServiceImpl implements SupplierService { .of(callbackData.getSupplierName()) .adapt(callbackData) .ifPresent(data -> - Command.of(RouteAddOrderCommand.class).data(data).sendToQueue()); + Command.of(RouteUpdateOrderCommand.class).data(data).sendToQueue()); } } diff --git a/src/main/java/com/chint/domain/value_object/OrderLegData.java b/src/main/java/com/chint/domain/value_object/OrderLegData.java index f006b301..9e35ae05 100644 --- a/src/main/java/com/chint/domain/value_object/OrderLegData.java +++ b/src/main/java/com/chint/domain/value_object/OrderLegData.java @@ -14,6 +14,7 @@ public class OrderLegData { private Integer productType; private String price; private Integer orderStatus; + private String originOrderStatus; private OrderLegData(Builder builder) { this.actualOrderNo = builder.actualOrderNo; @@ -35,6 +36,7 @@ public class OrderLegData { private Integer productType; private String price; private Integer orderStatus; + private String originOrderStatus; public Builder() { } @@ -70,6 +72,11 @@ public class OrderLegData { return this; } + public Builder originOrderStatus(String originOrderStatus) { + this.originOrderStatus = originOrderStatus; + return this; + } + public OrderLegData build() { return new OrderLegData(this); } diff --git a/src/main/java/com/chint/infrastructure/constant/Constant.java b/src/main/java/com/chint/infrastructure/constant/Constant.java index 6ee177e0..4ffa0614 100644 --- a/src/main/java/com/chint/infrastructure/constant/Constant.java +++ b/src/main/java/com/chint/infrastructure/constant/Constant.java @@ -110,6 +110,8 @@ public class Constant { public static final String ORDER_EVENT_PAYED_NAME = "已预定"; public static final int ORDER_EVENT_CHANGE = 3; public static final String ORDER_EVENT_CHANGE_NAME = "改签"; + public static final int ORDER_EVENT_REFUND = 4; + public static final String ORDER_EVENT_REFUND_NAME = "退票"; public static final int ORDER_EVENT_CANCEL = -1; public static final String ORDER_EVENT_CANCEL_NAME = "取消"; diff --git a/src/main/java/com/chint/infrastructure/repository/OrderDetailRepositoryImpl.java b/src/main/java/com/chint/infrastructure/repository/OrderDetailRepositoryImpl.java index 7052f7d8..bcbc24d2 100644 --- a/src/main/java/com/chint/infrastructure/repository/OrderDetailRepositoryImpl.java +++ b/src/main/java/com/chint/infrastructure/repository/OrderDetailRepositoryImpl.java @@ -7,6 +7,8 @@ import com.chint.infrastructure.repository.jdbc.JdbcOrderDetailRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; +import java.util.Optional; + import static com.chint.infrastructure.constant.Constant.NOT_FOUND; @Repository @@ -18,4 +20,9 @@ public class OrderDetailRepositoryImpl implements OrderDetailRepository { public OrderDetail findById(Long orderDetailId) { return orderDetailRepository.findById(orderDetailId).orElseThrow(() -> new NotFoundException(NOT_FOUND)); } + + @Override + public Optional findByOrderNo(String orderNo) { + return Optional.ofNullable(orderDetailRepository.findByOrderNo(orderNo)); + } } diff --git a/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcOrderDetailRepository.java b/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcOrderDetailRepository.java index 3d858309..6b39f2e5 100644 --- a/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcOrderDetailRepository.java +++ b/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcOrderDetailRepository.java @@ -6,4 +6,6 @@ import org.springframework.stereotype.Repository; @Repository public interface JdbcOrderDetailRepository extends CrudRepository { + + OrderDetail findByOrderNo(String orderNo); }