秒杀(防止超卖)

库存递减Dao

    /***
     * 库存递减
     * 递减数量
     * 商品ID
     * ------->控制超卖 and seckill_num>=#{count} 秒杀的数量不能大于剩余库存数量
     */
    @Update("update tb_sku set seckill_num=seckill_num-#{count} where id=#{id} and seckill_num>=#{count} and islock=1")
    int dcount(@Param("id")String id,@Param("count")Integer count);

库存递减serviceImpl

    /***
     * 库存递减
     * @param id
     * @param count
     * @return
     */
    @Override
    public int dcount(String id, Integer count) {
        //1.调用Dao实现递减
        int dcount = skuMapper.dcount(id, count);
        //2.递减失败
        if(dcount==0){
            //查询
            Sku sku = skuMapper.selectByPrimaryKey(id);
            //2.1递减失败原因->库存不足->405
            if(sku.getSeckillNum()<count){
                return StatusCode.DECOUNT_NUM;
            }else if (sku.getIslock()==2){
                //2.2递减失败原因->变成热点->205
                return StatusCode.DECOUNT_HOT;
            }
            return 0;
        }
        return StatusCode.DECOUNT_OK;
    }

库存递减Controller

    /****
     * 库存递减
     */
    @PutMapping(value = "/dcount/{id}/{count}")
    public Result<Sku> dcount(@PathVariable(value = "id")String id,@PathVariable(value = "count")Integer count){
        //1.调用业务层实现递减
        int code = skuService.dcount(id, count);
        String message="";
        Sku sku = null;
        switch (code){
            case StatusCode.DECOUNT_OK:
                sku = skuService.findById(id);
                message="库存递减成功!";
                break;
            case StatusCode.DECOUNT_NUM:
                message="库存不足!";
                break;
            case StatusCode.DECOUNT_HOT:
                message="商品是热点商品!";
                break;
                default:
        }
        //3.根据状态码,响应不同的提示信息
        return  new Result<Sku>(true,code,message,sku);
    }

新增订单serviceImpl

   /***
     * 添加订单
     * @param order
     * @return
     */
    @GlobalTransactional // 这里需要私用seata控制全局事务
    @Override
    public int add(Order order) {
        String userKey="USER"+order.getUsername()+"ID"+order.getSkuId();
        //1.递减库存
        Result<Sku> dcount = skuFeign.dcount(order.getSkuId(), order.getTotalNum());
        //2.递减成功->下单->记录当前用户抢单的时间点,24小时内不能抢购该商品
        if(dcount.getCode()== StatusCode.DECOUNT_OK){
            //int q=10/0;
            Sku sku = dcount.getData();
            //下单
            //order.setId("No"+idWorker.nextId());
            order.setOrderStatus("0");
            order.setPayStatus("0");
            order.setConsignStatus("0");
            order.setSkuId(sku.getId());
            order.setName(sku.getName());
            order.setPrice(sku.getSeckillPrice()*order.getTotalNum());
            orderMapper.insertSelective(order);
            //记录当前用户抢单的时间点,24小时内不能抢购该商品
            redisTemplate.boundValueOps(userKey).set("");
            redisTemplate.boundValueOps(userKey).expire(1, TimeUnit.MINUTES);
            return StatusCode.ORDER_OK;
        }else{
            //3.递减失败
            //405库存不足
            if(dcount.getCode()==StatusCode.DECOUNT_NUM){
                return StatusCode.DECOUNT_NUM;
            }else if(dcount.getCode()==StatusCode.DECOUNT_HOT){
                //205商品热点
                String key = "SKU_"+order.getSkuId();
                Long increment = redisTemplate.boundHashOps(key).increment(userKey, 1);
                if(increment==1){
                    //执行排队
                    Map<String,String> queueMap = new HashMap<String,String>();
                    queueMap.put("username",order.getUsername());
                    queueMap.put("id",order.getSkuId());
                    kafkaTemplate.send("neworder", JSON.toJSONString(queueMap));
                }
                return StatusCode.ORDER_QUEUE;
            }

            //0
            return dcount.getCode();
        }
    }

你可能感兴趣的:(Java精英阶级,python,前端,算法)