1.点击按钮,表单提交到MiaoshaController
1.判断用户是否登录 没登录就跳转到login
2.判断库存 根据goodsId进行库存查询
3.如果已经秒杀到了,进入订单OrderService是否秒杀到了商品
package com.ryan.miaosha.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.ryan.miaosha.dao.MiaoshaUser;
import com.ryan.miaosha.domain.CodeMsg;
import com.ryan.miaosha.domain.MiaoshaOrder;
import com.ryan.miaosha.domain.OrderInfo;
import com.ryan.miaosha.service.GoodsService;
import com.ryan.miaosha.service.MiaoshaGoodsService;
import com.ryan.miaosha.service.OrderService;
import com.ryan.miaosha.vo.GoodsVo;
@Controller
@RequestMapping("/miaosha")
public class MiaoshaController {
@Autowired
GoodsService goodsService;
@Autowired
OrderService orderService;
@Autowired
MiaoshaGoodsService miaoshaGoodsService;
@RequestMapping("/do_miaosha")
String doMiaosha(Model model,MiaoshaUser user,@RequestParam("goodsId")long goodsId) {
model.addAttribute("user", user);
if(user==null)
{return "login";}
//判断库存
GoodsVo goodsVoByGoodsId = goodsService.getGoodsVoByGoodsId(goodsId);
if(goodsVoByGoodsId.getStockCount()<=0) {
model.addAttribute("errmsg", CodeMsg.MIAOSHA_OVER.getMsg());
return "miaosha_failure";
}
//判断是否同一个账号多次秒杀
MiaoshaOrder miaoshaOrder=orderService.getMiaoshaOrderByUserIdGoodsId(user.getId(),goodsId);
if(miaoshaOrder!=null) {
model.addAttribute("errmsg", CodeMsg.REPEATE_MIAOSHA.getMsg());
return "miaosha_failure";
}
//减库存、下订单、写入秒杀订单
OrderInfo orderInfo=miaoshaGoodsService.miaosha(user,goodsVoByGoodsId);
model.addAttribute("orderInfo", orderInfo);
model.addAttribute("goods", goodsVoByGoodsId);
return "order_detail";
}
}
2.MiaoshaGoodsService
package com.ryan.miaosha.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ryan.miaosha.dao.MiaoshaUser;
import com.ryan.miaosha.domain.OrderInfo;
import com.ryan.miaosha.vo.GoodsVo;
@Service
public class MiaoshaGoodsService {
@Autowired
GoodsService goodsService;
@Autowired
OrderService orderService;
@Transactional
public OrderInfo miaosha(MiaoshaUser user, GoodsVo goodsVo) {
//减库存 下订单 写入秒杀订单
goodsService.reduceStock(goodsVo);
//order_info miaosha_order
return orderService.createOrder(user,goodsVo);
}
}
3. GoodsService
package com.ryan.miaosha.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ryan.miaosha.dao.GoodsDao;
import com.ryan.miaosha.dao.User;
import com.ryan.miaosha.dao.UserDao;
import com.ryan.miaosha.domain.Goods;
import com.ryan.miaosha.domain.MiaoshaGoods;
import com.ryan.miaosha.vo.GoodsVo;
@Service
public class GoodsService {
@Autowired
GoodsDao goodsDao;
public List<GoodsVo> listGoodsVo() {
return goodsDao.listGoodsVo();
}
public GoodsVo getGoodsVoByGoodsId(long goodsId) {
return goodsDao.getGoodsVoByGoodsId(goodsId);
}
public void reduceStock(GoodsVo goodsVo) {
MiaoshaGoods g=new MiaoshaGoods();
g.setGoodsId(goodsVo.getId());
goodsDao.reduceStock(g);
}
}
4.GoodsDao
package com.ryan.miaosha.dao;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.ryan.miaosha.domain.Goods;
import com.ryan.miaosha.domain.MiaoshaGoods;
import com.ryan.miaosha.vo.GoodsVo;
@Mapper
public interface GoodsDao {
@Select("select g.*,mg.stock_count,mg.start_date,mg.end_date,mg.miaosha_price from miaosha_goods mg left join goods g on mg.goods_id=g.id")
public List<GoodsVo> listGoodsVo();
@Select("select g.*,mg.stock_count,mg.start_date,mg.end_date,mg.miaosha_price from miaosha_goods mg left join goods g on mg.goods_id=g.id where goods_id =#{goodsId}")
public GoodsVo getGoodsVoByGoodsId(@Param("goodsId")long goodsId);
@Update("update miaosha_goods set stock_count=stock_count-1 where goods_id=#{goodsId}")
public int reduceStock(MiaoshaGoods g);
}
2.3.OrderService
package com.ryan.miaosha.service;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ryan.miaosha.dao.MiaoshaUser;
import com.ryan.miaosha.dao.OrderDao;
import com.ryan.miaosha.domain.MiaoshaOrder;
import com.ryan.miaosha.domain.OrderInfo;
import com.ryan.miaosha.vo.GoodsVo;
@Service
public class OrderService {
@Autowired
OrderDao orderDao;
public MiaoshaOrder getMiaoshaOrderByUserIdGoodsId(long userId, long goodsId) {
return orderDao.getMiaoshaOrderByUserIdGoodsId(userId,goodsId);
}
@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.setStatus(0);
orderInfo.setUserId(user.getId());
long orderId=orderDao.insert(orderInfo);
MiaoshaOrder miaoshaOrder=new MiaoshaOrder();
miaoshaOrder.setGoodsId(goodsVo.getId());
miaoshaOrder.setOrderId(orderId);
miaoshaOrder.setUserId(user.getId());
orderDao.insertMiaoshaOrder(miaoshaOrder);
return orderInfo;
}
}
2.4.OrderDao
package com.ryan.miaosha.dao;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import com.ryan.miaosha.domain.MiaoshaOrder;
import com.ryan.miaosha.domain.OrderInfo;
@Mapper
public interface OrderDao {
@Select("select* from miaosha_order where user_id=#{userId} and goods_id=#{goodsId}")
public MiaoshaOrder getMiaoshaOrderByUserIdGoodsId(@Param("userId")long userId,@Param("goodsId") long goodsId);
@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 insert(OrderInfo orderInfo);
@Insert("insert into miaosha_order (user_id,goods_id,order_id) values (#{userId},#{goodsId},#{orderId})")
public int insertMiaoshaOrder(MiaoshaOrder miaoshaOrder);
}
1.miaosha_failure.html 与 order_detail.html
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" >
<head>
<title>秒杀失败</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<!-- jquery -->
<script type="text/javascript" th:src="@{/js/jquery.min.js}"></script>
<!-- bootstrap -->
<link rel="stylesheet" type="text/css" th:href="@{/bootstrap/css/bootstrap.min.css}" />
<script type="text/javascript" th:src="@{/bootstrap/js/bootstrap.min.js}"></script>
<!-- jquery-validator -->
<script type="text/javascript" th:src="@{/jquery-validation/jquery.validate.min.js}"></script>
<script type="text/javascript" th:src="@{/jquery-validation/localization/messages_zh.min.js}"></script>
<!-- layer -->
<script type="text/javascript" th:src="@{/layer/layer.js}"></script>
<!-- md5.js -->
<script type="text/javascript" th:src="@{/js/md5.min.js}"></script>
<!-- common.js -->
<script type="text/javascript" th:src="@{/js/common.js}"></script>
</head>
<body>
<div class="panel panel-success">
<div class="panel-heading">
</div>
<div class="panel-body">
<span id="name">秒杀失败: <span th:text="${errmsg}"></span></span>
<button class="btn btn-success" type="button" id="button">倒计时</button>
</div>
<div class="panel-footer">
</div>
</div>
</body>
</html>
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" >
<head>
<title>订单详情</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<!-- jquery -->
<script type="text/javascript" th:src="@{/js/jquery.min.js}"></script>
<!-- bootstrap -->
<link rel="stylesheet" type="text/css" th:href="@{/bootstrap/css/bootstrap.min.css}" />
<script type="text/javascript" th:src="@{/bootstrap/js/bootstrap.min.js}"></script>
<!-- jquery-validator -->
<script type="text/javascript" th:src="@{/jquery-validation/jquery.validate.min.js}"></script>
<script type="text/javascript" th:src="@{/jquery-validation/localization/messages_zh.min.js}"></script>
<!-- layer -->
<script type="text/javascript" th:src="@{/layer/layer.js}"></script>
<!-- md5.js -->
<script type="text/javascript" th:src="@{/js/md5.min.js}"></script>
<!-- common.js -->
<script type="text/javascript" th:src="@{/js/common.js}"></script>
</head>
<body>
<div class="panel panel-success">
<div class="panel-heading">
</div>
<div class="panel-body">
<span id="name">订单ID: <span th:text="${orderInfo.id}"></span></span>
<button class="btn btn-success" type="button" id="button">倒计时</button>
</div>
<div class="panel-footer">
</div>
</div>
</body>
</html>