• 秒杀功能


    补充:

    实际项目中的主键不能用自增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";
        }
    }
  • 相关阅读:
    springcloud-Ribbon之手写轮询算法
    springcloud-Ribbon负载均衡规则的替换
    git本地库和远程库的连接和断开
    本地项目第一次提交到码云或github
    python基础语法练习
    Xss-labs-level11-15
    Vulnhub-靶机-ESCALATE_LINUX: 1
    Xss-labs-level7-10
    Vulnhub-靶机-DC: 6
    Jenkins入门之执行Powershell脚本
  • 原文地址:https://www.cnblogs.com/XJJD/p/8568105.html
Copyright © 2020-2023  润新知