同步代码

This commit is contained in:
lulz1 2024-03-01 09:19:31 +08:00
parent 1de351fa10
commit 8aeb194824
6 changed files with 41 additions and 32 deletions

View File

@ -73,15 +73,29 @@ public class OrderApplicationService {
RouteOrder order = Optional.ofNullable(routeRepository.queryById(addLegData.getRouteId())) RouteOrder order = Optional.ofNullable(routeRepository.queryById(addLegData.getRouteId()))
.orElseThrow(() -> new NotFoundException(CommonMessageConstant.NOT_FOUND)); .orElseThrow(() -> new NotFoundException(CommonMessageConstant.NOT_FOUND));
order.reloadStatus(); order.reloadStatus();
if (!order.getOrderStatus().equals(ORDER_STATUS_PREPARE)) { // if (!order.getOrderStatus().equals(ORDER_STATUS_PREPARE)) {
throw new OrderException(ORDER_STATUS_ERROR); // throw new OrderException(ORDER_STATUS_ERROR);
} // }
List<Leg> legs = processLegData(addLegData.getLegData(), order); List<Leg> legs = processLegData(addLegData.getLegData(), order);
RouteOrder routeOrder = orderDomainService.saveOrder(order); RouteOrder routeOrder = orderDomainService.saveOrder(order);
legs.forEach(leg -> Command.of(LegPrepareCommand.class).legId(leg.getLegId()).sendToQueue()); legs.forEach(leg -> Command.of(LegPrepareCommand.class).legId(leg.getLegId()).sendToQueue());
return routeOrder; // 仅在所有操作完成后保存一次 return routeOrder; // 仅在所有操作完成后保存一次
} }
@Transactional
public void changeLeg(AddLegData addLegData) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
LegData legData = addLegData.getLegData();
Leg byLegId = Optional.ofNullable(
legRepository.findByLegId(legData.getLegId())
).orElseThrow(() -> new NotFoundException(CommonMessageConstant.NOT_FOUND));
byLegId.setStartTime(LocalDateTime.parse(legData.getStartTime(), formatter));
byLegId.setEndTime(LocalDateTime.parse(legData.getEndTime(), formatter));
byLegId.setOriginId(legData.getOriginId());
byLegId.setDestinationId(legData.getDestinationId());
legRepository.save(byLegId);
}
private List<Leg> processLegData(LegData legData, RouteOrder order) { private List<Leg> processLegData(LegData legData, RouteOrder order) {
List<Leg> legs = switch (legData.legType) { List<Leg> legs = switch (legData.legType) {
case LegConstant.LEG_TYPE_TRAIN -> legFactory.createTrainLeg(legData); case LegConstant.LEG_TYPE_TRAIN -> legFactory.createTrainLeg(legData);
@ -156,15 +170,5 @@ public class OrderApplicationService {
public void updateLegToOrder(AddLegData addLegData) { public void updateLegToOrder(AddLegData addLegData) {
} }
@Transactional
public void changeLeg(AddLegData addLegData) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
LegData legData = addLegData.getLegData();
Leg byLegId = legRepository.findByLegId(legData.getLegId());
byLegId.setStartTime(LocalDateTime.parse(legData.getStartTime(), formatter));
byLegId.setEndTime(LocalDateTime.parse(legData.getEndTime(), formatter));
byLegId.setOriginId(legData.getOriginId());
byLegId.setDestinationId(legData.getDestinationId());
legRepository.save(byLegId);
}
} }

View File

@ -20,6 +20,9 @@ import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.*; import java.util.*;
import static com.chint.infrastructure.constant.LegConstant.LEG_TYPE_OTHER;
import static com.chint.infrastructure.constant.LegConstant.LEG_TYPE_TAXI;
@Slf4j @Slf4j
@Data @Data
@Table("leg") @Table("leg")
@ -82,7 +85,11 @@ public class Leg {
} }
public Leg queryEstimateAmount(EstimateAdapter estimateAdapter, String supplierName) { public Leg queryEstimateAmount(EstimateAdapter estimateAdapter, String supplierName) {
this.estimateAmount = estimateAdapter.of(supplierName).amountEstimate(this); if (this.getLegType().equals(LEG_TYPE_OTHER) || this.getLegType().equals(LEG_TYPE_TAXI)) {
} else {
this.estimateAmount = estimateAdapter.of(supplierName).amountEstimate(this);
}
return this; return this;
} }
@ -146,8 +153,8 @@ public class Leg {
case LegConstant.LEG_TYPE_TRAIN -> LegConstant.LEG_TYPE_TRAIN_NAME; case LegConstant.LEG_TYPE_TRAIN -> LegConstant.LEG_TYPE_TRAIN_NAME;
case LegConstant.LEG_TYPE_AIRPLANE -> LegConstant.LEG_TYPE_AIRPLANE_NAME; case LegConstant.LEG_TYPE_AIRPLANE -> LegConstant.LEG_TYPE_AIRPLANE_NAME;
case LegConstant.LEG_TYPE_HOTEL -> LegConstant.LEG_TYPE_HOTEL_NAME; case LegConstant.LEG_TYPE_HOTEL -> LegConstant.LEG_TYPE_HOTEL_NAME;
case LegConstant.LEG_TYPE_TAXI -> LegConstant.LEG_TYPE_TAXI_NAME; case LEG_TYPE_TAXI -> LegConstant.LEG_TYPE_TAXI_NAME;
case LegConstant.LEG_TYPE_OTHER -> LegConstant.LEG_TYPE_OTHER_NAME; case LEG_TYPE_OTHER -> LegConstant.LEG_TYPE_OTHER_NAME;
default -> "未知类型"; default -> "未知类型";
}; };
} }
@ -157,8 +164,8 @@ public class Leg {
case LegConstant.LEG_TYPE_TRAIN -> LegConstant.LEG_TYPE_TRAIN_EN_NAME; case LegConstant.LEG_TYPE_TRAIN -> LegConstant.LEG_TYPE_TRAIN_EN_NAME;
case LegConstant.LEG_TYPE_AIRPLANE -> LegConstant.LEG_TYPE_AIRPLANE_EN_NAME; case LegConstant.LEG_TYPE_AIRPLANE -> LegConstant.LEG_TYPE_AIRPLANE_EN_NAME;
case LegConstant.LEG_TYPE_HOTEL -> LegConstant.LEG_TYPE_HOTEL_EN_NAME; case LegConstant.LEG_TYPE_HOTEL -> LegConstant.LEG_TYPE_HOTEL_EN_NAME;
case LegConstant.LEG_TYPE_TAXI -> LegConstant.LEG_TYPE_TAXI_EN_NAME; case LEG_TYPE_TAXI -> LegConstant.LEG_TYPE_TAXI_EN_NAME;
case LegConstant.LEG_TYPE_OTHER -> LegConstant.LEG_TYPE_OTHER_EN_NAME; case LEG_TYPE_OTHER -> LegConstant.LEG_TYPE_OTHER_EN_NAME;
default -> "未知类型"; default -> "未知类型";
}; };
} }
@ -192,7 +199,7 @@ public class Leg {
if (!eventList.isEmpty()) { if (!eventList.isEmpty()) {
LegEvent lastEvent = eventList.get(eventList.size() - 1); LegEvent lastEvent = eventList.get(eventList.size() - 1);
int lastEventType = lastEvent.getEventType(); int lastEventType = lastEvent.getEventType();
if (newEventType != lastEventType && Math.abs(newEventType - lastEventType) > 1) { if (newEventType != lastEventType && Math.abs(newEventType - lastEventType) > 5) {
throw new LegEventException("New event must be the same as, immediately before, or immediately after the last event type."); throw new LegEventException("New event must be the same as, immediately before, or immediately after the last event type.");
} }
} }

View File

@ -20,6 +20,9 @@ public class OrderDetailFactoryImpl implements OrderDetailFactory {
orderDetail.setCurrencyType(CurrencyType.getByCode(orderLegData.getCurrencyCode())); orderDetail.setCurrencyType(CurrencyType.getByCode(orderLegData.getCurrencyCode()));
orderDetail.setDestinationId(orderLegData.getDestinationId()); orderDetail.setDestinationId(orderLegData.getDestinationId());
orderDetail.setOriginId(orderLegData.getOriginId()); orderDetail.setOriginId(orderLegData.getOriginId());
orderDetail
.price(orderLegData.getPrice())
.productType(orderLegData.getProductType());
return orderDetail; return orderDetail;
} }

View File

@ -32,7 +32,6 @@ public class CTripEstimate implements AmountEstimate {
@Override @Override
public String amountEstimate(Leg leg) { public String amountEstimate(Leg leg) {
Integer legType = leg.getLegType(); Integer legType = leg.getLegType();
return switch (legType) { return switch (legType) {
case LEG_TYPE_TRAIN -> queryTrainPrice(leg); case LEG_TYPE_TRAIN -> queryTrainPrice(leg);

View File

@ -106,13 +106,14 @@ public class LegEventHandler implements LegEventService {
public void syncLeg(LegSyncCommand command) { public void syncLeg(LegSyncCommand command) {
SyncLegData data = command.getData(); SyncLegData data = command.getData();
RouteOrder routeOrder = routeRepository.queryById(data.getRouteId()).reloadStatus(); RouteOrder routeOrder = routeRepository.queryById(data.getRouteId()).reloadStatus();
if (routeOrder.getOrderStatus() > (RouteConstant.ORDER_STATUS_APPROVAL)) { // if (routeOrder.getOrderStatus() > (RouteConstant.ORDER_STATUS_APPROVAL)) {
throw new CommandException("订单已同步"); // throw new CommandException("订单已同步");
} // }
if (routeOrder.getOrderStatus().equals(ORDER_STATUS_PREPARE)) { if (routeOrder.getOrderStatus().equals(ORDER_STATUS_PREPARE)) {
throw new CommandException("订单未提交审批"); throw new CommandException("订单未提交审批");
} }
if (routeOrder.getOrderStatus().equals(RouteConstant.ORDER_STATUS_APPROVAL)) { //这里暂时的策略是只要订单状态是大于提交审批状态 就可以同步
if (routeOrder.getOrderStatus() >= RouteConstant.ORDER_STATUS_APPROVAL) {
String supplierName = data.getSupplierName(); String supplierName = data.getSupplierName();
routeOrder.setSupplierName(supplierName); routeOrder.setSupplierName(supplierName);
//这里order所有的leg触发sync事件 //这里order所有的leg触发sync事件
@ -153,15 +154,10 @@ public class LegEventHandler implements LegEventService {
if (byOrderNo.isEmpty()) { if (byOrderNo.isEmpty()) {
//否则创建新的订单添加到routeOrder //否则创建新的订单添加到routeOrder
orderDetail = orderDetailFactory.create(data) orderDetail = orderDetailFactory.create(data)
.price(data.getPrice()).productType(data.getProductType())
.employeeNo(routeOrder.getUserId()); .employeeNo(routeOrder.getUserId());
routeOrder.addOrderDetail(orderDetail); routeOrder.addOrderDetail(orderDetail);
routeOrder = routeRepository.save(routeOrder); routeOrder = routeRepository.save(routeOrder);
// OrderDetail orderDetail = routeOrder
// .getOrderDetails()
// .stream()
// .max(Comparator.comparing(OrderDetail::getCreateTime))
// .orElseThrow(() -> new NotFoundException(CommonMessageConstant.NOT_FOUND));
//为订单添加订单已下单事件,这里需要发出额外的命令进行处理 //为订单添加订单已下单事件,这里需要发出额外的命令进行处理
orderDetail = routeOrder.matchOrderWithLeg(orderDetail); orderDetail = routeOrder.matchOrderWithLeg(orderDetail);
} else { } else {

View File

@ -5,7 +5,7 @@ public class RouteConstant {
public static final int ORDER_STATUS_PREPARE = 0; public static final int ORDER_STATUS_PREPARE = 0;
public static final String ORDER_STATUS_PREPARE_NAME = "规划中"; public static final String ORDER_STATUS_PREPARE_NAME = "规划中";
public static final int ORDER_STATUS_APPROVAL = 1; public static final int ORDER_STATUS_APPROVAL = 1;
public static final String ORDER_STATUS_APPROVAL_NAME = "提交审批"; public static final String ORDER_STATUS_APPROVAL_NAME = "未同步";
public static final int ORDER_STATUS_NOT_ORDERED = 2; public static final int ORDER_STATUS_NOT_ORDERED = 2;
public static final String ORDER_STATUS_NOT_ORDERED_NAME = "未下单"; public static final String ORDER_STATUS_NOT_ORDERED_NAME = "未下单";
public static final int ORDER_STATUS_ORDERED = 3; public static final int ORDER_STATUS_ORDERED = 3;