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