-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSeKillController.java
281 lines (256 loc) · 9.95 KB
/
SeKillController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package com.example.seckilldemo.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.seckilldemo.config.AccessLimit;
import com.example.seckilldemo.entity.TOrder;
import com.example.seckilldemo.entity.TSeckillOrder;
import com.example.seckilldemo.entity.TUser;
import com.example.seckilldemo.exception.GlobalException;
import com.example.seckilldemo.rabbitmq.MQSender;
import com.example.seckilldemo.service.ITGoodsService;
import com.example.seckilldemo.service.ITOrderService;
import com.example.seckilldemo.service.ITSeckillOrderService;
import com.example.seckilldemo.utils.JsonUtil;
import com.example.seckilldemo.vo.GoodsVo;
import com.example.seckilldemo.vo.RespBean;
import com.example.seckilldemo.vo.RespBeanEnum;
import com.example.seckilldemo.vo.SeckillMessage;
import com.wf.captcha.ArithmeticCaptcha;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* 秒杀
*
* @author: LC
* @date 2022/3/4 11:34 上午
* @ClassName: SeKillController
*/
@Slf4j
@Controller
@RequestMapping("/seckill")
@Api(value = "秒杀", tags = "秒杀")
public class SeKillController implements InitializingBean {
@Autowired
private ITGoodsService itGoodsService;
@Autowired
private ITSeckillOrderService itSeckillOrderService;
@Autowired
private ITOrderService orderService;
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private MQSender mqSender;
@Autowired
private RedisScript<Long> redisScript;
private Map<Long, Boolean> EmptyStockMap = new HashMap<>();
@ApiOperation("获取验证码")
@GetMapping(value = "/captcha")
public void verifyCode(TUser tUser, Long goodsId, HttpServletResponse response) {
if (tUser == null || goodsId < 0) {
throw new GlobalException(RespBeanEnum.REQUEST_ILLEGAL);
}
//设置请求头为输出图片的类型
response.setContentType("image/jpg");
response.setHeader("Pargam", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
//生成验证码
ArithmeticCaptcha captcha = new ArithmeticCaptcha(130, 32, 3);
redisTemplate.opsForValue().set("captcha:" + tUser.getId() + ":" + goodsId, captcha.text(), 300, TimeUnit.SECONDS);
try {
captcha.out(response.getOutputStream());
} catch (IOException e) {
log.error("验证码生成失败", e.getMessage());
}
}
/**
* 获取秒杀地址
*
* @param tuser
* @param goodsId
* @param captcha
* @return com.example.seckilldemo.vo.RespBean
* @author LiChao
* @operation add
* @date 4:04 下午 2022/3/9
**/
@ApiOperation("获取秒杀地址")
@AccessLimit(second = 5, maxCount = 5, needLogin = true)
@GetMapping(value = "/path")
@ResponseBody
public RespBean getPath(TUser tuser, Long goodsId, String captcha, HttpServletRequest request) {
if (tuser == null) {
return RespBean.error(RespBeanEnum.SESSION_ERROR);
}
// ValueOperations valueOperations = redisTemplate.opsForValue();
//限制访问次数,5秒内访问5次
// String uri = request.getRequestURI();
// captcha = "0";
// Integer count = (Integer) valueOperations.get(uri + ":" + tuser.getId());
// if (count == null) {
// valueOperations.set(uri + ":" + tuser.getId(), 1, 5, TimeUnit.SECONDS);
// } else if (count < 5) {
// valueOperations.increment(uri + ":" + tuser.getId());
// } else {
// return RespBean.error(RespBeanEnum.ACCESS_LIMIT_REACHED);
// }
boolean check = orderService.checkCaptcha(tuser, goodsId, captcha);
if (!check) {
return RespBean.error(RespBeanEnum.ERROR_CAPTCHA);
}
String str = orderService.createPath(tuser, goodsId);
return RespBean.success(str);
}
/**
* 获取秒杀结果
*
* @param tUser
* @param goodsId
* @return orderId 成功 ;-1 秒杀失败 ;0 排队中
* @author LiChao
* @operation add
* @date 7:04 下午 2022/3/8
**/
@ApiOperation("获取秒杀结果")
@GetMapping("getResult")
@ResponseBody
public RespBean getResult(TUser tUser, Long goodsId) {
if (tUser == null) {
return RespBean.error(RespBeanEnum.SESSION_ERROR);
}
Long orderId = itSeckillOrderService.getResult(tUser, goodsId);
return RespBean.success(orderId);
}
/**
* 秒杀功能
*
* @param user
* @param goodsId
* @return java.lang.String
* @author LC
* @operation add
* @date 11:36 上午 2022/3/4
**/
@ApiOperation("秒杀功能")
@RequestMapping(value = "/{path}/doSeckill", method = RequestMethod.POST)
@ResponseBody
public RespBean doSecKill(@PathVariable String path, TUser user, Long goodsId) {
if (user == null) {
return RespBean.error(RespBeanEnum.SESSION_ERROR);
}
//优化后代码
ValueOperations valueOperations = redisTemplate.opsForValue();
boolean check = orderService.checkPath(user, goodsId, path);
if (!check) {
return RespBean.error(RespBeanEnum.REQUEST_ILLEGAL);
}
//判断是否重复抢购
TSeckillOrder tSeckillOrder = (TSeckillOrder) redisTemplate.opsForValue().get("order:" + user.getId() + ":" + goodsId);
if (tSeckillOrder != null) {
return RespBean.error(RespBeanEnum.REPEATE_ERROR);
}
//内存标记,减少Redis的访问
if (EmptyStockMap.get(goodsId)) {
return RespBean.error(RespBeanEnum.EMPTY_STOCK);
}
//预减库存
// Long stock = valueOperations.decrement("seckillGoods:" + goodsId);
Long stock = (Long) redisTemplate.execute(redisScript, Collections.singletonList("seckillGoods:" + goodsId), Collections.EMPTY_LIST);
if (stock < 0) {
EmptyStockMap.put(goodsId, true);
valueOperations.increment("seckillGoods:" + goodsId);
return RespBean.error(RespBeanEnum.EMPTY_STOCK);
}
SeckillMessage seckillMessag = new SeckillMessage(user, goodsId);
mqSender.sendSeckillMessage(JsonUtil.object2JsonStr(seckillMessag));
return RespBean.success(0);
/*
// model.addAttribute("user", user);
GoodsVo goodsVo = itGoodsService.findGoodsVobyGoodsId(goodsId);
if (goodsVo.getStockCount() < 1) {
// model.addAttribute("errmsg", RespBeanEnum.EMPTY_STOCK.getMessage());
return RespBean.error(RespBeanEnum.EMPTY_STOCK);
}
//判断是否重复抢购
// TSeckillOrder seckillOrder = itSeckillOrderService.getOne(new QueryWrapper<TSeckillOrder>().eq("user_id", user.getId()).eq("goods_id", goodsId));
TSeckillOrder seckillOrder = (TSeckillOrder) redisTemplate.opsForValue().get("order:" + user.getId() + ":" + goodsVo.getId());
if (seckillOrder != null) {
// model.addAttribute("errmsg", RespBeanEnum.REPEATE_ERROR.getMessage());
return RespBean.error(RespBeanEnum.REPEATE_ERROR);
}
TOrder tOrder = orderService.secKill(user, goodsVo);
// model.addAttribute("order", tOrder);
// model.addAttribute("goods", goodsVo);
return RespBean.success(tOrder);
*/
}
/**
* 秒杀功能-废弃
*
* @param model
* @param user
* @param goodsId
* @return java.lang.String
* @author LC
* @operation add
* @date 11:36 上午 2022/3/4
**/
@ApiOperation("秒杀功能-废弃")
@RequestMapping(value = "/doSeckill2", method = RequestMethod.POST)
public String doSecKill2(Model model, TUser user, Long goodsId) {
model.addAttribute("user", user);
GoodsVo goodsVo = itGoodsService.findGoodsVobyGoodsId(goodsId);
if (goodsVo.getStockCount() < 1) {
model.addAttribute("errmsg", RespBeanEnum.EMPTY_STOCK.getMessage());
return "secKillFail";
}
//判断是否重复抢购
TSeckillOrder seckillOrder = itSeckillOrderService.getOne(new QueryWrapper<TSeckillOrder>().eq("user_id", user.getId()).eq("goods_id", goodsId));
if (seckillOrder != null) {
model.addAttribute("errmsg", RespBeanEnum.REPEATE_ERROR.getMessage());
return "secKillFail";
}
TOrder tOrder = orderService.secKill(user, goodsVo);
model.addAttribute("order", tOrder);
model.addAttribute("goods", goodsVo);
return "orderDetail";
}
/**
* 系统初始化,把商品库存数量加载到Redis
*
* @param
* @return void
* @author LiChao
* @operation add
* @date 6:29 下午 2022/3/8
**/
@Override
public void afterPropertiesSet() throws Exception {
List<GoodsVo> list = itGoodsService.findGoodsVo();
if (CollectionUtils.isEmpty(list)) {
return;
}
list.forEach(goodsVo -> {
redisTemplate.opsForValue().set("seckillGoods:" + goodsVo.getId(), goodsVo.getStockCount());
EmptyStockMap.put(goodsVo.getId(), false);
});
}
}