3-4 订单详情页

首先MiaoshaController类,因为生成订单之前要进行秒杀。其中,@RequestParam是获取表单提交的name属性中value

①判断秒杀商品的库存。

MiaoshaController:

GoodsVo goodsVo = goodsService.getOneGoodsVoById(goodsId);
int stockCount = goodsVo.getStockCount();
if(stockCount <= 0){
model.addAttribute("errmsg", CodeMsg.MIAO_SHA_OVER.getMsg());
return "miaosha_fail";
}

GoodsService:

public GoodsVo getOneGoodsVoById(long goodsId) {
return goodsDao.getOneGoodsVoById(goodsId);

}

GoodsDao:

@Select("select g.*, mg.stock_count, mg.miaosha_price, mg.start_date, mg.end_date from miaosha_goods mg left join goods g on mg.goods_id = g.id where mg.goods_id = #{id}")

public GoodsVo getOneGoodsVoById(@Param("id")long goodsId);

②防止重复秒杀

MiaoshaController:

//防止一个人秒杀多个商品,判断是否已经秒杀到了  ==> 到OrderService中去查找(查找生成的秒杀类型的订单,这种订单必须是一个用户和一个特价商品关联产生的,且每个用户只能产生一个)

MiaoshaOrder miaoshaOrder = orderService.getMiaoshaOrderByUserIdGoodsId(user.getId(),goodsId);
if(miaoshaOrder != null){
model.addAttribute("errmsg", CodeMsg.REPEAT_MIAOSHA.getMsg());
return "miaosha_fail";

}

OrderService:

public MiaoshaOrder getMiaoshaOrderByUserIdGoodsId(Long userId, long goodsId) {
return orderDao.getMiaoshaOrderByUserIdGoodsId(userId, goodsId);

}

OrderDao:

@Select("select * from miaosha_order where user_id= #{id} and goods_id=#{goodsId}")

public MiaoshaOrder getMiaoshaOrderByUserIdGoodsId(@Param("id")Long id, @Param("goodsId")long goodsId);


③//减库存,(需要在一个事务当中去做,搞一个service,做一个MiaoshaService,然后进行一个Transaction操作)

OrderInfo orderInfo = miaoshaService.miaosha(user, goodsVo);//为什么成功了以后要返回订单呢?因为我们想秒杀成功之后直接进入到订单详情页


@Transactional
public OrderInfo miaosha(MiaoshaUser user, GoodsVo goodsVo) {
//减库存(减miaosha_goods的库存), 写入秒杀订单
goodsService.reduceStock(goodsVo);
//下订单, 包含两步:一、order_info  二、miaosha_order  实际上是两步操作
return orderService.createOrder(user, goodsVo);

}

GoodsService:

public void reduceStock(GoodsVo goodsVo) {
// MiaoshaGoods mg = new MiaoshaGoods();
// mg.setGoodsId(goodsVo.getId());
goodsDao.reduceStock(goodsVo.getId());

}

GoodsDao:

@Update("update miaosha_goods set stock_count=stock_count-1 where goods_id = #{goods_id}")

public void reduceStock(@Param("goods_id")long goods_id);

④下(写)订单,写入秒杀订单

OrderService:

@Transactional
public OrderInfo createOrder(MiaoshaUser user, GoodsVo goodsVo) {
OrderInfo orderInfo = new OrderInfo();
orderInfo.setCreateDate(new Date());
orderInfo.setDeliveryAddrId(0L);
orderInfo.setGoodsCount(1);//本订单购买商品的数量
orderInfo.setGoodsId(goodsVo.getId());
orderInfo.setGoodsName(goodsVo.getGoodsName());
orderInfo.setGoodsPrice(goodsVo.getMiaoshaPrice());
orderInfo.setOrderChannel(1);
orderInfo.setPayDate(new Date());
orderInfo.setStatus(0);
orderInfo.setUserId(user.getId());
long orderId = orderDao.insertOrderInfo(orderInfo);
MiaoshaOrder miaoshaOrder = new MiaoshaOrder();
miaoshaOrder.setGoodsId(goodsVo.getId());
miaoshaOrder.setOrderId(orderId);
miaoshaOrder.setUserId(user.getId());
//miaoshaOrder.setId(id);//这里对id不需要进行设置
orderDao.insertMiaoshaOrder(miaoshaOrder);
return orderInfo;//最终返回的是orderInfo

}

OrderDao:   从形参传递进来的javaBean,在使用的时候直接${...}即可取出。

@Insert("insert into order_info(user_id, goods_id, goods_name, goods_count, goods_price, order_channel, status, create_date) values(#{userId},"
+ " #{goodsId}, #{goodsName}, #{goodsCount}, #{goodsPrice}, #{orderChannel}, #{status}, #{createDate})")
@SelectKey(keyColumn="id", keyProperty="id", resultType=long.class, before=false, statement="select last_insert_id()")
public long insertOrderInfo(OrderInfo orderInfo);

@Insert("insert into miaosha_order(user_id, order_id, goods_id) values(#{userId},#{orderId},#{goodsId})")
public int insertMiaoshaOrder(MiaoshaOrder miaoshaOrder);

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页