feat:修改 liteflow 节点的方法签名
This commit is contained in:
@@ -15,21 +15,21 @@ import pers.amos.mall.trade.liteflow.context.OrderContext;
|
||||
|
||||
@Component("buildResult")
|
||||
public class BuildResultNode extends NodeComponent {
|
||||
|
||||
|
||||
@Override
|
||||
public void process() throws Exception {
|
||||
public void process() {
|
||||
OrderContext context = this.getContextBean(OrderContext.class);
|
||||
Order order = context.getOrder();
|
||||
|
||||
|
||||
LogUtil.info("开始构建返回结果, orderNo={}", order.getOrderNo());
|
||||
|
||||
|
||||
// 构建返回DTO
|
||||
OrderResultDTO resultDTO = new OrderResultDTO();
|
||||
BeanUtils.copyProperties(order, resultDTO);
|
||||
resultDTO.setTickets(context.getTickets());
|
||||
|
||||
|
||||
context.setOrderResultDTO(resultDTO);
|
||||
|
||||
|
||||
LogUtil.info("返回结果构建成功, orderNo={}", order.getOrderNo());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,34 +18,34 @@ import pers.amos.mall.trade.liteflow.context.OrderContext;
|
||||
|
||||
@Component("checkInventory")
|
||||
public class CheckInventoryNode extends NodeComponent {
|
||||
|
||||
|
||||
@DubboReference
|
||||
private RouteService routeService;
|
||||
|
||||
|
||||
@Override
|
||||
public void process() throws Exception {
|
||||
public void process() {
|
||||
OrderContext context = this.getContextBean(OrderContext.class);
|
||||
OrderCreateDTO dto = context.getOrderCreateDTO();
|
||||
|
||||
|
||||
LogUtil.info("开始检查库存, orderType={}, quantity={}", dto.getOrderType(), dto.getQuantity());
|
||||
|
||||
|
||||
// 构建库存检查DTO
|
||||
InventoryCheckDTO checkDTO = new InventoryCheckDTO();
|
||||
checkDTO.setOrderType(dto.getOrderType());
|
||||
checkDTO.setScheduleCode(dto.getScheduleCode());
|
||||
checkDTO.setRollingScheduleCode(generateRollingScheduleCode(dto));
|
||||
checkDTO.setQuantity(dto.getQuantity());
|
||||
|
||||
|
||||
// Dubbo调用Route服务检查库存
|
||||
Result<Boolean> result = routeService.checkInventory(checkDTO);
|
||||
|
||||
|
||||
if (!result.isSuccess() || !Boolean.TRUE.equals(result.getData())) {
|
||||
throw new RuntimeException("库存不足:" + result.getErrorDesc());
|
||||
}
|
||||
|
||||
|
||||
LogUtil.info("库存检查通过, orderType={}, quantity={}", dto.getOrderType(), dto.getQuantity());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 生成滚动班次编号
|
||||
* 格式:线路编码 + 日期 + RS
|
||||
|
||||
@@ -21,52 +21,52 @@ import java.time.LocalDateTime;
|
||||
|
||||
@Component("createOrder")
|
||||
public class CreateOrderNode extends NodeComponent {
|
||||
|
||||
|
||||
@Autowired
|
||||
private OrderRepository orderRepository;
|
||||
|
||||
|
||||
@Override
|
||||
public void process() throws Exception {
|
||||
public void process() {
|
||||
OrderContext context = this.getContextBean(OrderContext.class);
|
||||
OrderCreateDTO dto = context.getOrderCreateDTO();
|
||||
|
||||
|
||||
// 从上下文获取订单号(临时存储在errorMessage中)
|
||||
String orderNo = context.getErrorMessage();
|
||||
|
||||
|
||||
LogUtil.info("开始创建订单, orderNo={}", orderNo);
|
||||
|
||||
|
||||
Order order = new Order();
|
||||
order.setOrderNo(orderNo);
|
||||
order.setUserNo(dto.getUserNo());
|
||||
order.setRouteCode(dto.getRouteCode());
|
||||
order.setOrderType(dto.getOrderType());
|
||||
order.setScheduleCode(dto.getScheduleCode());
|
||||
|
||||
|
||||
// 如果是滚动发车,设置滚动班次编号
|
||||
if (OrderTypeEnum.ROLLING.getCode().equals(dto.getOrderType())) {
|
||||
String rollingScheduleCode = dto.getRouteCode() + "-" +
|
||||
String rollingScheduleCode = dto.getRouteCode() + "-" +
|
||||
dto.getScheduleDate().replace("-", "") + "-RS";
|
||||
order.setRollingScheduleCode(rollingScheduleCode);
|
||||
}
|
||||
|
||||
|
||||
order.setTicketType(dto.getTicketType());
|
||||
order.setQuantity(dto.getQuantity());
|
||||
|
||||
|
||||
// TODO: 根据线路和票种查询价格,这里暂时写死
|
||||
BigDecimal unitPrice = new BigDecimal("50.00");
|
||||
order.setUnitPrice(unitPrice);
|
||||
order.setTotalAmount(unitPrice.multiply(new BigDecimal(dto.getQuantity())));
|
||||
|
||||
|
||||
order.setOrderStatus(OrderStatusEnum.UNPAID.getCode());
|
||||
order.setCreateTime(LocalDateTime.now());
|
||||
order.setExpireTime(LocalDateTime.now().plusMinutes(15)); // 15分钟支付超时
|
||||
order.setUpdateTime(LocalDateTime.now());
|
||||
|
||||
|
||||
orderRepository.save(order);
|
||||
|
||||
|
||||
// 将订单保存到上下文
|
||||
context.setOrder(order);
|
||||
|
||||
|
||||
LogUtil.info("订单创建成功, orderNo={}, totalAmount={}", orderNo, order.getTotalAmount());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ public class GenerateTicketsNode extends NodeComponent {
|
||||
private TicketService ticketService;
|
||||
|
||||
@Override
|
||||
public void process() throws Exception {
|
||||
public void process() {
|
||||
OrderContext context = this.getContextBean(OrderContext.class);
|
||||
Order order = context.getOrder();
|
||||
|
||||
|
||||
@@ -22,21 +22,21 @@ import java.time.format.DateTimeFormatter;
|
||||
|
||||
@Component("lockInventory")
|
||||
public class LockInventoryNode extends NodeComponent {
|
||||
|
||||
|
||||
@DubboReference
|
||||
private RouteService routeService;
|
||||
|
||||
|
||||
@Override
|
||||
public void process() throws Exception {
|
||||
public void process() {
|
||||
OrderContext context = this.getContextBean(OrderContext.class);
|
||||
OrderCreateDTO dto = context.getOrderCreateDTO();
|
||||
|
||||
|
||||
// 生成订单号
|
||||
String orderNo = generateOrderNo();
|
||||
|
||||
LogUtil.info("开始锁定库存, orderNo={}, orderType={}, quantity={}",
|
||||
|
||||
LogUtil.info("开始锁定库存, orderNo={}, orderType={}, quantity={}",
|
||||
orderNo, dto.getOrderType(), dto.getQuantity());
|
||||
|
||||
|
||||
// 构建库存操作DTO
|
||||
InventoryOperationDTO operationDTO = new InventoryOperationDTO();
|
||||
operationDTO.setOrderNo(orderNo);
|
||||
@@ -44,27 +44,27 @@ public class LockInventoryNode extends NodeComponent {
|
||||
operationDTO.setScheduleCode(dto.getScheduleCode());
|
||||
operationDTO.setRollingScheduleCode(generateRollingScheduleCode(dto));
|
||||
operationDTO.setQuantity(dto.getQuantity());
|
||||
|
||||
|
||||
// Dubbo调用Route服务锁定库存
|
||||
Result<Boolean> result = routeService.lockInventory(operationDTO);
|
||||
|
||||
|
||||
if (!result.isSuccess() || !Boolean.TRUE.equals(result.getData())) {
|
||||
throw new RuntimeException("库存锁定失败:" + result.getErrorDesc());
|
||||
}
|
||||
|
||||
|
||||
// 将订单号和滚动班次编号保存到上下文
|
||||
dto.setScheduleCode(dto.getScheduleCode());
|
||||
if (OrderTypeEnum.ROLLING.getCode().equals(dto.getOrderType())) {
|
||||
// 将生成的滚动班次编号保存到DTO,供后续节点使用
|
||||
context.setErrorMessage(operationDTO.getRollingScheduleCode()); // 临时存储
|
||||
}
|
||||
|
||||
|
||||
LogUtil.info("库存锁定成功, orderNo={}", orderNo);
|
||||
|
||||
|
||||
// 将订单号保存到上下文
|
||||
context.setErrorMessage(orderNo); // 临时存储订单号
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 生成订单号
|
||||
* 格式:ORD + yyyyMMddHHmmssSSS + 6位随机数
|
||||
@@ -74,7 +74,7 @@ public class LockInventoryNode extends NodeComponent {
|
||||
String random = RandomUtil.randomNumbers(6);
|
||||
return "ORD" + timestamp + random;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 生成滚动班次编号
|
||||
*/
|
||||
|
||||
@@ -15,31 +15,31 @@ import pers.amos.mall.trade.liteflow.context.OrderContext;
|
||||
|
||||
@Component("validateParams")
|
||||
public class ValidateParamsNode extends NodeComponent {
|
||||
|
||||
|
||||
@Override
|
||||
public void process() throws Exception {
|
||||
public void process() {
|
||||
OrderContext context = this.getContextBean(OrderContext.class);
|
||||
OrderCreateDTO dto = context.getOrderCreateDTO();
|
||||
|
||||
|
||||
LogUtil.info("开始参数校验, userNo={}", dto.getUserNo());
|
||||
|
||||
|
||||
// 校验基本参数
|
||||
if (!StringUtils.hasText(dto.getUserNo())) {
|
||||
throw new IllegalArgumentException("用户编号不能为空");
|
||||
}
|
||||
|
||||
|
||||
if (!StringUtils.hasText(dto.getRouteCode())) {
|
||||
throw new IllegalArgumentException("线路编码不能为空");
|
||||
}
|
||||
|
||||
|
||||
if (!StringUtils.hasText(dto.getOrderType())) {
|
||||
throw new IllegalArgumentException("订单类型不能为空");
|
||||
}
|
||||
|
||||
|
||||
if (dto.getQuantity() == null || dto.getQuantity() <= 0) {
|
||||
throw new IllegalArgumentException("购票数量必须大于0");
|
||||
}
|
||||
|
||||
|
||||
// 根据订单类型校验特定参数
|
||||
if (OrderTypeEnum.FIXED.getCode().equals(dto.getOrderType())) {
|
||||
if (!StringUtils.hasText(dto.getScheduleCode())) {
|
||||
@@ -52,7 +52,7 @@ public class ValidateParamsNode extends NodeComponent {
|
||||
} else {
|
||||
throw new IllegalArgumentException("订单类型无效");
|
||||
}
|
||||
|
||||
|
||||
LogUtil.info("参数校验通过, userNo={}", dto.getUserNo());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user