码迷,mamicode.com
首页 > 其他好文 > 详细

关于买家买入一件商品以后需要进行的逻辑

时间:2019-12-07 01:06:34      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:gen   err   products   utils   int   数据   str   库存   etc   

 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的锁机制,来防止出现超卖的现象

关于买家买入一件商品以后需要进行的逻辑

标签:gen   err   products   utils   int   数据   str   库存   etc   

原文地址:https://www.cnblogs.com/qingmuchuanqi48/p/12000307.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!