|
@@ -1,5 +1,6 @@
|
|
|
package com.sf.service;
|
|
|
|
|
|
+import com.sf.utils.DateUtils;
|
|
|
import com.sf.utils.RedisUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -11,6 +12,8 @@ import org.springframework.data.redis.core.script.DefaultRedisScript;
|
|
|
import org.springframework.scripting.support.ResourceScriptSource;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.time.LocalDateTime;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
|
|
@@ -26,17 +29,41 @@ public class RedisServiceImpl implements RedisService {
|
|
|
|
|
|
|
|
|
|
|
|
+ String keyStart = "1_startTime";
|
|
|
+ String keyEnd = "1_endTime";
|
|
|
+ String startStr = (String) redisUtils.get(keyStart);
|
|
|
+ String endStr = (String) redisUtils.get(keyEnd);
|
|
|
+
|
|
|
+ LocalDateTime startTime = DateUtils.getDateTime(startStr);
|
|
|
+ LocalDateTime endTime = DateUtils.getDateTime(endStr);
|
|
|
+ LocalDateTime nowTime = LocalDateTime.now();
|
|
|
+
|
|
|
+ if (nowTime.isBefore(startTime)) {
|
|
|
+ log.info("还未到秒杀开始时间");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (nowTime.isAfter(endTime)) {
|
|
|
+ log.info("秒杀已结束");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ log.info("正在进行秒杀");
|
|
|
+
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ String key = goodsId + "_stock_num";
|
|
|
+ Integer num = (Integer) redisUtils.get(key);
|
|
|
+
|
|
|
+
|
|
|
+ if (num == 0) {
|
|
|
+ log.info("已经秒杀光了");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
handleByLua(userId, goodsId);
|
|
|
|
|
|
|
|
@@ -107,6 +134,19 @@ public class RedisServiceImpl implements RedisService {
|
|
|
|
|
|
@Override
|
|
|
public void init() {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ String startTime = DateUtils.getDateTimeStr(2024, 2, 22);
|
|
|
+ String endTime = DateUtils.getDateTimeStr(2024, 2, 24);
|
|
|
+
|
|
|
+ String keyStart = "1_startTime";
|
|
|
+ String keyEnd = "1_endTime";
|
|
|
+
|
|
|
+ redisUtils.set(keyStart, startTime);
|
|
|
+ redisUtils.set(keyEnd, endTime);
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
@@ -124,9 +164,5 @@ public class RedisServiceImpl implements RedisService {
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
}
|