zoukankan      html  css  js  c++  java
  • 秒杀功能

    补充:

    实际项目中的主键不能用自增id,我们可以用snowflake算法。

    参考:http://mp.weixin.qq.com/s/bIkdcYo32yTEOGn0t8ABeg

    核心代码:

    @Controller
    @RequestMapping("/miaosha")
    public class MiaoshaController {
    
        @Autowired
        MiaoshaUserService userService;
        
        @Autowired
        RedisService redisService;
        
        @Autowired
        GoodsService goodsService;
        
        @Autowired
        OrderService orderService;
        
        @Autowired
        MiaoshaService miaoshaService;
        
        @RequestMapping("/do_miaosha")
        public String list(Model model,MiaoshaUser user,
                @RequestParam("goodsId")long goodsId) {
            model.addAttribute("user", user);
            if(user == null) {
                return "login";
            }
            //判断库存
            GoodsVo goods = goodsService.getGoodsVoByGoodsId(goodsId);
            int stock = goods.getStockCount();
            if(stock <= 0) {
                model.addAttribute("errmsg", CodeMsg.MIAO_SHA_OVER.getMsg());
                return "miaosha_fail";
            }
            //判断是否已经秒杀到了
            MiaoshaOrder order = orderService.getMiaoshaOrderByUserIdGoodsId(user.getId(), goodsId);
            if(order != null) {
                model.addAttribute("errmsg", CodeMsg.REPEATE_MIAOSHA.getMsg());
                return "miaosha_fail";
            }
            //减库存 下订单 写入秒杀订单
            OrderInfo orderInfo = miaoshaService.miaosha(user, goods);
            model.addAttribute("orderInfo", orderInfo);
            model.addAttribute("goods", goods);
            return "order_detail";
        }
    }
  • 相关阅读:
    在vue-cli中使用mock.js详解
    canvas水波纹效果
    echarts颜色渐变
    前端单词大全
    vue.js商城购买选择界面
    网站炫酷效果
    vue-cli中路由配置新写法
    Render函数
    XHTML与HTML的区别
    iview的table中点击Icon弹Poptip,render函数的写法
  • 原文地址:https://www.cnblogs.com/XJJD/p/8568105.html
Copyright © 2011-2022 走看看