public OrderDTO create(OrderDTO orderDTO) { String orderId = KeyUtil.genUniqueKey(); BigDecimal orderAmount = new BigDecimal(BigInteger.ZERO); //1.查询商品(数量,价格) for (OrderDetail orderDetail : orderDTO.getOrderDetailList()) { ProductInfo productInfo = productService.findById(orderDetail.getProductId()); if (null == productInfo) { throw new SellerException(ResultEnum.PRODUCT_NOT_EXIST); } //2.计算总价 orderAmount = productInfo.getProductPrice().multiply(new BigDecimal(orderDetail.getProductCount())).add(orderAmount); //3.订单详情入库 BeanUtils.copyProperties(productInfo, orderDetail); orderDetail.setDetailId(KeyUtil.genUniqueKey()); orderDetail.setOrderId(orderId); OrderDetail orderDetail1 = orderDetailRepository.save(orderDetail); orderDetailRepository.save(orderDetail); } //3.写入订单数据库 OrderMaster orderMaster = new OrderMaster(); BeanUtils.copyProperties(orderDTO, orderMaster); orderMaster.setOrderId(orderId); orderMaster.setOrderAmount(orderAmount); orderMaster.setOrderStatus(OrderStatusEnum.NEW.getCode()); orderMaster.setPayStatus(PayStatusEnum.WAIT.getCode()); orderMasterRepository.save(orderMaster); //todo 4.扣库存 List<CartDTO> cartDTOList = orderDTO.getOrderDetailList() .stream() .map(e -> new CartDTO(e.getProductId(), e.getProductCount())) .collect(Collectors.toList()); productService.decreaseStock(cartDTOList); return null; }
如果涉及到高并发,需要使用redis的锁机制,来防止出现超卖的现象
原文地址:https://www.cnblogs.com/qingmuchuanqi48/p/12000307.html
时间: 2024-10-10 16:27:22