package com.yyk.controller;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yyk.entity.Goods;
import com.yyk.entity.User;
@Controller
public class GoodsController {
@Resource
private RedisTemplate redisTemplate;
//去到登录页面
@RequestMapping("/tologin")
public String toLogin() {
return "login";
}
//去到登录页面
@RequestMapping("/login")
public String login(User user) {
//判断用户和密码是否正确
//获取所有用户数据
ListOperations<String,User> opsForList = redisTemplate.opsForList();
List<User> list = opsForList.range("user_list", 0, -1);
//遍历
for (User u : list) {
//判断
if(u.getUsername().equals(user.getUsername()) && u.getPassword().equals(user.getPassword())) {
return "redirect:list";
}
}
return "redirect:tologin";
}
@RequestMapping("/list")
public String list(Model model) {
//获取list操作对象
ListOperations<String,Goods> opsForList = redisTemplate.opsForList();
//获取所有的商品数据
List<Goods> list = opsForList.range("goodsList", 0, -1);
model.addAttribute("list", list);
return "list";
}
/**
* 列表返回json
* @param model
* @return
*/
@RequestMapping("getNum")
@ResponseBody
public List<Goods> getNum() {
//获取list操作对象
ListOperations<String,Goods> opsForList = redisTemplate.opsForList();
//获取所有的商品数据
List<Goods> list = opsForList.range("goodsList", 0, -1);
return list;
}
/**
* 抢购
* @param id
* @param model
* @return
*/
@RequestMapping("buy")
public String buy(Integer id,Model model) {
//获取list操作对象
ListOperations<String,Goods> opsForList = redisTemplate.opsForList();
//获取所有的商品数据
List<Goods> list = opsForList.range("goodsList", 0, -1);
//单个商品修改
for (int i=0;i<list.size();i++) {
//遍历所有的商品数据
Goods goods = list.get(i);
//根据商品id,判断其是否是同一个商品
if(goods.getId().equals(id)) {
//如果找到要抢购的商品
if(goods.getNum() > 0) {
//将商品数量-1
goods.setNum(goods.getNum() - 1);
//再存入list中
list.set(i, goods);
model.addAttribute("msg", "秒杀成功");
}else {
model.addAttribute("msg", "秒杀失败");
}
}
}
//删除原来的数据
redisTemplate.delete("goodsList");
//重新存入redis中
opsForList.leftPushAll("goodsList", list);
return "result";
}
}
Goods
private Integer id;
private String name;
private String date;
private Integer num;
User
private Integer id;
private String username;
private String password;
list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css"
href="/resource/css/bootstrap.min.css" />
<script type="text/javascript" src="/resource/js/jquery-3.2.1.js"></script>
<script type="text/javascript" src="/resource/js/bootstrap.min.js"></script>
<script type="text/javascript">
var to = new Date(2020,4,26,21,55,00)
var timer = setInterval(function(){
var now = new Date();
var countTime = to - now;//毫秒值
var s = Math.floor(countTime/1000);//秒值
$.post(
"getNum",
{},
function(data){
$("#tab").html();//清空原来的数据
var str =
' <tr>' +
' <th>商品id</th>' +
' <th>商品名称</th>' +
' <th>预售时间</th>' +
' <th>商品库存</th>' +
' <th>倒计时</th>' +
' <th>操作</th>' +
' </tr>' ;
$.each(data,function(index,good){
str += '<tr>' +
'<td>' + good.id + '</td>' +
'<td>' + good.name + '</td>' +
'<td>' + good.date + '</td>' +
'<td>' + good.num + '</td>' ;
if(s > 0){
str +='<td>' + s + '秒 </td>' +
'<td><input type="button" value="秒杀" disabled></td> </tr>';
}else{
str +='<td>秒杀开始! </td>' +
'<td><input type="button" value="秒杀" οnclick="buy('+ good.id +')"></td> </tr>';
}
})
$("#tab").html(str);
},
"json"
);
},1000);
function buy(id){
location="buy?id="+id;
}
</script>
</head>
<body>
<table id="tab">
<tr>
<td>商品id</td>
<td>商品名称</td>
<td>抢购时间</td>
<td>库存数量</td>
<td>倒计时</td>
<td>操作</td>
</tr>
<c:forEach items="${list }" var="g">
<tr>
<td>${g.id }</td>
<td>${g.name }</td>
<td>${g.date }</td>
<td>${g.num }</td>
<td></td>
<td></td>
</tr>
</c:forEach>
</table>
</body>
</html>