forked from gushen/sunway-user-system
实现了直接购买创建订单功能
This commit is contained in:
@@ -36,6 +36,15 @@ public class OrderController {
|
||||
return ResponseEntity.success("订单创建成功", result);
|
||||
}
|
||||
|
||||
@PostMapping("/{productId}")
|
||||
public ResponseEntity createOrderDirectly(@Validated @RequestBody OrderDTO orderDTO, @PathVariable Long productId ,@RequestParam Long skuId) {
|
||||
log.info("创建订单: {}", orderDTO);
|
||||
Long orderId = orderService.createOrderDirectly(orderDTO, productId ,skuId);
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("orderId", orderId);
|
||||
return ResponseEntity.success("订单创建成功", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页查询订单列表
|
||||
*/
|
||||
|
||||
@@ -141,6 +141,77 @@ public class OrderServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> im
|
||||
return orderInfo.getId();
|
||||
}
|
||||
|
||||
/*
|
||||
*直接购买
|
||||
*/
|
||||
@Override
|
||||
@Transactional
|
||||
public Long createOrderDirectly(OrderDTO orderDTO, Long productId, Long skuId) {
|
||||
// 1. 验证商品和SKU是否存在
|
||||
Product product = productMapper.selectById(productId);
|
||||
if (product == null) {
|
||||
throw new BusinessException("商品不存在: " + productId);
|
||||
}
|
||||
|
||||
ProductSku sku = productSkuMapper.selectById(skuId);
|
||||
if (sku == null) {
|
||||
throw new BusinessException("商品规格不存在: " + skuId);
|
||||
}
|
||||
|
||||
// 2. 验证SKU是否属于该商品
|
||||
if (!sku.getProductId().equals(productId)) {
|
||||
throw new BusinessException("商品与规格不匹配");
|
||||
}
|
||||
|
||||
// 3. 验证库存 (默认购买数量为1,也可从DTO中接收数量参数)
|
||||
int buyCount = 1; // 可改为从OrderDTO中获取数量
|
||||
if (sku.getStock() < buyCount) {
|
||||
throw new BusinessException("商品 " + sku.getSkuName() + " 库存不足");
|
||||
}
|
||||
|
||||
// 4. 计算总金额
|
||||
BigDecimal totalAmount = sku.getPrice().multiply(new BigDecimal(buyCount));
|
||||
BigDecimal freightAmount = new BigDecimal("0.00"); // 运费默认0,可根据业务调整
|
||||
BigDecimal payAmount = totalAmount.add(freightAmount);
|
||||
|
||||
// 5. 创建订单主表记录
|
||||
OrderInfo orderInfo = new OrderInfo();
|
||||
BeanUtils.copyProperties(orderDTO, orderInfo);
|
||||
String orderNo = generateOrderNo();
|
||||
orderInfo.setOrderNo(orderNo);
|
||||
orderInfo.setTotalAmount(totalAmount);
|
||||
orderInfo.setFreightAmount(freightAmount);
|
||||
orderInfo.setPayAmount(payAmount);
|
||||
orderInfo.setStatus(0); // 初始状态:待付款
|
||||
orderInfo.setCreateTime(LocalDateTime.now());
|
||||
orderInfo.setUpdateTime(LocalDateTime.now());
|
||||
orderInfo.setIsDeleted(0);
|
||||
orderInfoMapper.insert(orderInfo);
|
||||
|
||||
// 6. 创建订单项记录
|
||||
OrderItem orderItem = new OrderItem();
|
||||
orderItem.setOrderId(orderInfo.getId());
|
||||
orderItem.setOrderNo(orderNo);
|
||||
orderItem.setProductId(productId);
|
||||
orderItem.setSkuId(skuId);
|
||||
orderItem.setProductName(product.getName());
|
||||
orderItem.setSkuName(sku.getSkuName());
|
||||
orderItem.setProductImage(product.getMainImage());
|
||||
orderItem.setPrice(sku.getPrice());
|
||||
orderItem.setQuantity(buyCount);
|
||||
orderItem.setTotalPrice(totalAmount);
|
||||
orderItem.setCreateTime(LocalDateTime.now());
|
||||
orderItem.setUpdateTime(LocalDateTime.now());
|
||||
orderItem.setIsDeleted(0);
|
||||
orderItemMapper.insert(orderItem);
|
||||
|
||||
// 7. 扣减库存
|
||||
sku.setStock(sku.getStock() - buyCount);
|
||||
productSkuMapper.updateById(sku);
|
||||
|
||||
log.info("直接购买创建订单成功: {}", orderNo);
|
||||
return orderInfo.getId();
|
||||
}
|
||||
@Override
|
||||
public Page<OrderListVO> getOrderPage(Integer page, Integer size, Long userId, Integer status) {
|
||||
// 构造查询条件
|
||||
@@ -254,6 +325,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> im
|
||||
log.info("订单 {} 状态更新为: {}", orderInfo.getOrderNo(), newStatus);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 验证状态转换是否合法
|
||||
*/
|
||||
|
||||
@@ -41,4 +41,6 @@ public interface OrderService extends IService<OrderInfo> {
|
||||
* @param statusDTO 订单状态信息
|
||||
*/
|
||||
void updateOrderStatus(OrderStatusDTO statusDTO);
|
||||
|
||||
Long createOrderDirectly(OrderDTO orderDTO, Long productId , Long skuId);
|
||||
}
|
||||
Reference in New Issue
Block a user