美文网首页
秒杀第十五节:秒杀模型管理--活动模型创建(与订单结合)

秒杀第十五节:秒杀模型管理--活动模型创建(与订单结合)

作者: 小石读史 | 来源:发表于2020-09-03 09:33 被阅读0次

    在前面的“秒杀第十三节:交易模型管理-交易模型创建”的订单创建中新增秒杀信息
    在OrderService的创建订单接口createOrder中增加秒杀活动id

    OrderModel createOrder(Integer userId,Integer itemId,Integer promoId,Integer amount) throws BussinessException;
    

    在OrderServiceImpl实现createOrder中也新增promoId参数增加下单查询秒杀活动

    @Override
        @Transactional
        public OrderModel createOrder(Integer userId, Integer itemId,Integer promoId, Integer amount) throws BussinessException {
            //1.校验下单状态(用户是否合法,下单商品是否存在,购买数量是否正确)
            ItemModel itemModel = itemService.getItemById(itemId);
            if(itemModel == null) {
                throw new BussinessException((EmBusinessError.PARAMETER_VALIDATION_ERROR),"商品不存在");
            }
            UserModel userModel = userService.getUserById(userId);
            if(userId == null) {
                throw new BussinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR,"用户不存在");
            }
            if(amount <= 0 || amount > 99){
                throw new BussinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR,"数量信息不正确");
            }
    
            // 校验活动信息
            if(promoId != null && itemModel.getPromoModel() != null) {
                // 1. 校验对应活动是否存在对应商品
                if(promoId.intValue() != itemModel.getPromoModel().getId()) {
                    throw new BussinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR,"活动信息不正确");
                } else if(itemModel.getPromoModel().getStatus().intValue() != 2){
                    // 2。校验活动是否在进行中
                    throw new BussinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR,"活动未开始");
                }
            }
    
            //2.落单减库存,
            boolean result = itemService.decreaseStock(itemId,amount);
            if(!result) {
                throw new BussinessException(EmBusinessError.STOCK_NOT_ENOUGH);
            }
    
            //3.订单入库
            OrderModel orderModel = new OrderModel();
            orderModel.setUserId(userId);
            orderModel.setItemId(itemId);
            orderModel.setAmount(amount);
            if(promoId != null) {
                orderModel.setItemPrice(itemModel.getPromoModel().getPromoItemPrice());
            } else {
                orderModel.setItemPrice(itemModel.getPrice());
            }
            orderModel.setPromoId(promoId);
            orderModel.setOrderPrice(orderModel.getItemPrice().multiply(BigDecimal.valueOf(amount)));
    
            //生成交易流水号,订单号
            orderModel.setId(generatorOrderNo());
            OrderDO orderDO = convertFromOrderModel(orderModel);
            orderDOMapper.insertSelective(orderDO);
            //商品销量增加
            itemService.increaseSales(itemId,amount);
            //返回前端
            return orderModel;
        }
    

    修改OrderController中创建订单方法

    //封装下单请求
        @RequestMapping(value = "/createorder", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
        @ResponseBody
        public CommonReturnType createOrder(@RequestParam(name="itemId")Integer itemId,
                                            @RequestParam(name="promoId",required = false)Integer promoId,
                                            @RequestParam(name="amount")Integer amount) throws BussinessException {
    
            //获取登录信息(Boolean)
            Boolean isLogin = (Boolean) this.httpServletRequest.getSession().getAttribute("IS_LOGIN");
    
            if(isLogin == null || !isLogin.booleanValue()){
                throw new BussinessException(EmBusinessError.USER_NOT_LOGIN,"用户还未登陆,不能下单");
            }
            UserModel userModel = (UserModel) this.httpServletRequest.getSession().getAttribute("LOGIN_USER");
            OrderModel orderModel = orderService.createOrder(userModel.getId(),itemId,promoId,amount);
            return CommonReturnType.create(null);
    
        }
    

    秒杀测试参数:http://localhost:8080/order/createorder?itemId=40&amount=2&promoId=2
    测试结果:

    image.png

    相关文章

      网友评论

          本文标题:秒杀第十五节:秒杀模型管理--活动模型创建(与订单结合)

          本文链接:https://www.haomeiwen.com/subject/mwapdktx.html