From 4fe1eb411b45ee507c2cc79dd9018ac42f0f80b9 Mon Sep 17 00:00:00 2001 From: lulz1 Date: Tue, 6 Feb 2024 16:16:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20=E9=83=A8=E5=88=86?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E7=BC=BA=E5=B0=91=E4=BA=8B=E5=8A=A1=E6=B3=A8?= =?UTF-8?q?=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/leg_event/LegEventHandler.java | 126 ++++++++++++++++++ .../leg_event/LegEventServiceImpl.java | 79 ++--------- .../repository/CityRepositoryImpl.java | 3 +- .../repository/jdbc/JdbcCityRepository.java | 1 + 4 files changed, 136 insertions(+), 73 deletions(-) create mode 100644 src/main/java/com/chint/domain/service/leg_event/LegEventHandler.java 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 new file mode 100644 index 00000000..b7225851 --- /dev/null +++ b/src/main/java/com/chint/domain/service/leg_event/LegEventHandler.java @@ -0,0 +1,126 @@ +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 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 LegEventHandler implements LegEventService{ + + @Autowired + private SyncAdapter syncAdapter; + + @Autowired + private RouteRepository routeRepository; + + @Autowired + private LegRepository legRepository; + + @Autowired + private LegEventFactory legEventFactory; + + @Transactional + @Override + public void prepareLeg(LegPrepareCommand command) { + Leg leg = legRepository.findByLegId(Leg.of(command.getLegId())); + if (leg.getEventList().isEmpty()) { + LegEvent legEvent = legEventFactory.creatLegEvent(command.getLegEventType()); + leg.addEvent(legEvent); + legRepository.save(leg); + } + } + @Transactional + @Override + public void approveLeg(LegApprovalCommand command) { + ApproveLegData data = command.getData(); + RouteOrder routeOrder = routeRepository.findByFakeOrderNo(data.getFakeOrderNo()); + + if (routeOrder.getOrderStatus().equals(ORDER_STATUS_PREPARE)) { + ApproveOrderNo approveOrderNo = routeOrder.getApproveOrderNo(); + approveOrderNo.setActualOrderNo(data.getActualOrderNo()); + approveOrderNo.setAccountCompany(data.getAccountCompany()); + + //这里order所有的leg触发approve事件 + routeOrder.getLegItems().forEach(leg -> leg.getEventList().add( + legEventFactory.creatLegEvent(command.getLegEventType()) + )); + + //保存routeOrder的状态 + routeRepository.save(routeOrder); + } else { + throw new CommandException("订单未初始化"); + } + } + @Transactional + @Override + public void syncLeg(LegSyncCommand command) { + SyncLegData data = command.getData(); + RouteOrder routeOrder = routeRepository.queryById(data.getRouteId()).reloadStatus(); + if (routeOrder.getOrderStatus().equals(ORDER_STATUS_APPROVAL)) { + String supplierName = data.getSupplierName(); + routeOrder.setSupplierName(supplierName); + //这里order所有的leg触发approve事件 + routeOrder.getLegItems().forEach(leg -> leg.getEventList().add( + legEventFactory.creatLegEvent(command.getLegEventType()) + )); + //保存routeOrder的状态 + routeRepository.save(routeOrder); + syncAdapter.of(supplierName).syncSupplierOrder(routeOrder.reloadStatus()); + } else { + throw new CommandException("订单未提交审批"); + } + } + @Transactional + @Override + public void orderLeg(LegOrderedCommand command) { + OrderLegData data = command.getData(); + Leg leg = legRepository.findByLegId(Leg.of(command.getLegId())); + LegEvent legEvent = legEventFactory.creatLegEvent(command.getLegEventType()); + OrderDetail orderDetail = new OrderDetail(); + orderDetail.setAmount(data.getOrderNo()); + orderDetail.setAmount(data.getAmount()); + legEvent.setOrderDetail(orderDetail); + leg.addEvent(legEvent); + legRepository.save(leg); + } + @Transactional + @Override + public void payForLeg(LegPayedCommand command) { + PayLegData data = command.getData(); + Leg leg = legRepository.findByLegId(Leg.of(command.getLegId())); + LegEvent legEvent = legEventFactory.creatLegEvent(command.getLegEventType()); + OrderDetail orderDetail = new OrderDetail(); + orderDetail.setAmount(data.getOrderNo()); + orderDetail.setAmount(data.getAmount()); + legEvent.setOrderDetail(orderDetail); + leg.addEvent(legEvent); + legRepository.save(leg); + } + @Transactional + @Override + public void finishLeg(LegFinishedCommand command) { + Leg leg = legRepository.findByLegId(Leg.of(command.getLegId())); + LegEvent legEvent = legEventFactory.creatLegEvent(command.getLegEventType()); + leg.addEvent(legEvent); + legRepository.save(leg); + } + @Transactional + @Override + public void rejectLeg(LegRejectCommand 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 09fb67b5..67d2edd7 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 @@ -15,6 +15,7 @@ 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; @@ -23,27 +24,14 @@ import static com.chint.infrastructure.constant.Constant.ORDER_STATUS_PREPARE; public class LegEventServiceImpl implements LegEventService { @Autowired - private SyncAdapter syncAdapter; + private LegEventHandler legEventHandler; - @Autowired - private RouteRepository routeRepository; - - @Autowired - private LegRepository legRepository; - - @Autowired - private LegEventFactory legEventFactory; @TransitionTo(command = "LegPrepareCommand", order = 0) @Override public void prepareLeg(LegPrepareCommand command) { - Leg leg = legRepository.findByLegId(Leg.of(command.getLegId())); - if (leg.getEventList().isEmpty()) { - LegEvent legEvent = legEventFactory.creatLegEvent(command.getLegEventType()); - leg.addEvent(legEvent); - legRepository.save(leg); - } + legEventHandler.prepareLeg(command); } //因为审批是针对整个订单的,因此所有订单下属的行程节点触发审批事件 @@ -52,85 +40,34 @@ public class LegEventServiceImpl implements LegEventService { @ListenTo(command = "LegApprovalCommand", order = 0) @Override public void approveLeg(LegApprovalCommand command) { - ApproveLegData data = command.getData(); - RouteOrder routeOrder = routeRepository.findByFakeOrderNo(data.getFakeOrderNo()); - - if (routeOrder.getOrderStatus().equals(ORDER_STATUS_PREPARE)){ - ApproveOrderNo approveOrderNo = routeOrder.getApproveOrderNo(); - approveOrderNo.setActualOrderNo(data.getActualOrderNo()); - approveOrderNo.setAccountCompany(data.getAccountCompany()); - - //这里order所有的leg触发approve事件 - routeOrder.getLegItems().forEach(leg -> leg.getEventList().add( - legEventFactory.creatLegEvent(command.getLegEventType()) - )); - - //保存routeOrder的状态 - routeRepository.save(routeOrder); - } else { - throw new CommandException("订单未初始化"); - } + legEventHandler.approveLeg(command); } //这里需要获取同步类,价格routeOrder同步到供应商 @ListenTo(command = "LegSyncCommand", order = 0) @Override public void syncLeg(LegSyncCommand command) { - SyncLegData data = command.getData(); - RouteOrder routeOrder = routeRepository.queryById(data.getRouteId()).reloadStatus(); - if (routeOrder.getOrderStatus().equals(ORDER_STATUS_APPROVAL)) { - String supplierName = data.getSupplierName(); - routeOrder.setSupplierName(supplierName); - //这里order所有的leg触发approve事件 - routeOrder.getLegItems().forEach(leg -> leg.getEventList().add( - legEventFactory.creatLegEvent(command.getLegEventType()) - )); - //保存routeOrder的状态 - routeRepository.save(routeOrder); - syncAdapter.of(supplierName).syncSupplierOrder(routeOrder.reloadStatus()); - } else { - throw new CommandException("订单未提交审批"); - } + legEventHandler.syncLeg(command); } - //下单时间要求回传,需要付款的金额,以及生成对于的行程订单号,如果没有行程订单号根据地点和时间进行匹配 @ListenTo(command = "LegOrderedCommand", order = 0) @Override public void orderLeg(LegOrderedCommand command) { - OrderLegData data = command.getData(); - Leg leg = legRepository.findByLegId(Leg.of(command.getLegId())); - LegEvent legEvent = legEventFactory.creatLegEvent(command.getLegEventType()); - OrderDetail orderDetail = new OrderDetail(); - orderDetail.setAmount(data.getOrderNo()); - orderDetail.setAmount(data.getAmount()); - legEvent.setOrderDetail(orderDetail); - leg.addEvent(legEvent); - legRepository.save(leg); + legEventHandler.orderLeg(command); } @Override @ListenTo(command = "LegPayedCommand", order = 0) public void payForLeg(LegPayedCommand command) { - PayLegData data = command.getData(); - Leg leg = legRepository.findByLegId(Leg.of(command.getLegId())); - LegEvent legEvent = legEventFactory.creatLegEvent(command.getLegEventType()); - OrderDetail orderDetail = new OrderDetail(); - orderDetail.setAmount(data.getOrderNo()); - orderDetail.setAmount(data.getAmount()); - legEvent.setOrderDetail(orderDetail); - leg.addEvent(legEvent); - legRepository.save(leg); + legEventHandler.payForLeg(command); } @Override @ListenTo(command = "LegFinishedCommand", order = 0) public void finishLeg(LegFinishedCommand command) { - Leg leg = legRepository.findByLegId(Leg.of(command.getLegId())); - LegEvent legEvent = legEventFactory.creatLegEvent(command.getLegEventType()); - leg.addEvent(legEvent); - legRepository.save(leg); + } @Override diff --git a/src/main/java/com/chint/infrastructure/repository/CityRepositoryImpl.java b/src/main/java/com/chint/infrastructure/repository/CityRepositoryImpl.java index 50305b7d..12223a1d 100644 --- a/src/main/java/com/chint/infrastructure/repository/CityRepositoryImpl.java +++ b/src/main/java/com/chint/infrastructure/repository/CityRepositoryImpl.java @@ -3,7 +3,6 @@ package com.chint.infrastructure.repository; import com.chint.domain.aggregates.location.CityEntity; import com.chint.domain.repository.CityRepository; import com.chint.infrastructure.repository.jdbc.JdbcCityRepository; -import com.chint.interfaces.rest.ctrip.dto.location.CTripCity; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; @@ -23,7 +22,7 @@ public class CityRepositoryImpl implements CityRepository { @Override public CityEntity findByCityName(String name) { - return null; + return jdbcCityRepository.findByCityName(name); } } diff --git a/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcCityRepository.java b/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcCityRepository.java index f7670885..3e15f9e2 100644 --- a/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcCityRepository.java +++ b/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcCityRepository.java @@ -6,4 +6,5 @@ import org.springframework.stereotype.Repository; @Repository public interface JdbcCityRepository extends CrudRepository { + CityEntity findByCityName(String cityName); }