Java秒杀系统-3.9-秒杀流程实现

  1.前端 :前端点击goods_detail 的立即秒杀,提交action=“/miaosha/do_miaosha” 的表单。提交的参数为商品的goods.id。

 2. controller:

  2.1秒杀用户添加到model,如果用户为null,则跳转到login 页面。通过GoodsService判断s商品GoodsVo库存stock是否大于0,如果否,则返回errmsg ,CondeMsg为秒杀结束。返回到页面“miaosha_fail”

 2.2 判断是否秒杀结束了,通过MiaoshaOrder  order=getMiaoshaOrderByUserIdGoodsId,传入用户id,和商品id查找是否有秒杀订单。如果返回order为空则返回重复秒杀了。跳转到秒杀失败页面“miaosha_fail”.

2.3 调用 miaoshaServie 的方法

OrderInfo miaosha(MiaoshaUser user, GoodsVo goods),将OrderiInfo 

2.4 将 OrderInfo 和Goods 保存到Model中映射。

3.MiaoshaService 

3.1调用GoodsService的reduceStock扣减库存,传入goodsId

3.2调用OrderService 的createOrder创建订单。传入参数userId,goodsId

/**
 * @ClassName HelloController
 * @Description TODO
 * @Author zengjx
 * @Company zengjx
 * @Date 2020/1/30  22:00
 * @Version V1.0
 */

@Controller
@RequestMapping("/miaosha")
public class MiaoshaController {
    private   static Logger logger= LoggerFactory.getLogger(MiaoshaController.class) ;

    @Autowired
  private GoodsService  goodsService;  
  @Autowired
  private MiaoshaService  miaoshaService;
  @Autowired
  private   OrderService     orderService;
    @Autowired
    private RedisService redisService;

  @RequestMapping("/do_miaosha")
   public   String do_miaosha(HttpServletRequest   servletRequest,Model  model, @RequestParam (value = "goodsId") Long    goodsId
          ){
      Cookie[] cookies = servletRequest.getCookies();
      String token=null;
      for (int i = 0; i 

  

@Service

public class MiaoshaServiceImpl   implements MiaoshaService {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private OrderService orderService;

//    @Override
//    public boolean miaosha(Long goodsId) {
//
//       //扣减库存
//        GoodsVo  goodsVo =new GoodsVo();
//
//        return goodsService.reduceStock(goodsVo);
//    }
     @Transactional
    @Override
    public OrderInfo miaosha(MiaoshaUser user, GoodsVo goodsVo) {
     //扣减库存
        boolean bRet = goodsService.reduceStock(goodsVo);
        //生成订单
        OrderInfo   orderInfo =new OrderInfo();
        orderInfo.setGoodsId(goodsVo.getId());
        orderInfo.setStatus(0);
        orderInfo.setCreateDate(new Date());
        orderInfo.setGoodsCount(goodsVo.getStockCount()-1);
        orderInfo.setGoodsName(goodsVo.getGoodsName());
        orderInfo.setUserId(user.getId());
        long orderId = orderService.insertOrder(orderInfo);

        MiaoshaOrder miaoshaOrder = new MiaoshaOrder();
        miaoshaOrder.setGoodsId(goodsVo.getId());
        miaoshaOrder.setOrderId(orderId);
        miaoshaOrder.setUserId(user.getId());
        orderService.insertMiaoshaOrder(miaoshaOrder);

        return orderInfo;
    }
@Service

public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsDao   goodsDao;

    @Override
    public List listGoods() {
        List goodsVos = goodsDao.listGoodsVo();
        return goodsVos;
    }

    @Override
    public GoodsVo getGoodsVoByGoodsId(Long goodsId) {

        GoodsVo goodsVo = goodsDao.getGoodsVoByGoodsId(goodsId);

        return goodsVo;
    }
    /***
     *   扣减库存
     * @param goods
     * @return
     */
    public boolean reduceStock(GoodsVo goods) {
        MiaoshaGoods  miaoshaGoods= new MiaoshaGoods();
        miaoshaGoods.setId(goods.getId());
        miaoshaGoods.setGoodsId(goods.getId());
        miaoshaGoods.setStockCount(goods.getStockCount());
        int stock = goodsDao.reduceStock(miaoshaGoods);

         return  stock>0;
    }

    /**
     * 恢复库存
     * @param goodsList
     */
    public void resetStock(List goodsList) {

        for (GoodsVo goodsVo : goodsList) {
            MiaoshaGoods  goods =new MiaoshaGoods();
            goods.setStockCount(goodsVo.getStockCount());
            goodsDao.resetStock(goods);
        }

    }
}
@Service

public class OrderServiceImpl  implements OrderService {
    private   static Logger logger= LoggerFactory.getLogger(OrderServiceImpl.class) ;

    @Autowired
    private OrderDao    orderDao;

  public  long   insertOrder(OrderInfo   orderInfo){
      logger.info("OrderServiceImpl insertOrder");
     return  orderDao.insert(orderInfo);
  }

    @Override
    public MiaoshaOrder getMiaoshaOrderByUserIdGoodsId(Long id, Long goodsId) {

        logger.info("OrderServiceImpl getMiaoshaOrderByUserIdGoodsId   id ="+id +" goodsId ="+goodsId);

        return  orderDao.getMiaoshaOrderByUserIdGoodsId(id,goodsId);
    }
    public int insertMiaoshaOrder(MiaoshaOrder miaoshaOrder){
      return   orderDao.insertMiaoshaOrder(miaoshaOrder);
    }

}

  Java秒杀系统-3.9-秒杀流程实现_第1张图片

 

你可能感兴趣的:(秒杀项目)