|
@@ -1,6 +1,8 @@
|
|
|
package com.izouma.nineth.service;
|
|
package com.izouma.nineth.service;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
+import com.izouma.nineth.config.GeneralProperties;
|
|
|
|
|
+import com.izouma.nineth.config.RedisKeys;
|
|
|
import com.izouma.nineth.domain.Collection;
|
|
import com.izouma.nineth.domain.Collection;
|
|
|
import com.izouma.nineth.domain.*;
|
|
import com.izouma.nineth.domain.*;
|
|
|
import com.izouma.nineth.dto.CollectionDTO;
|
|
import com.izouma.nineth.dto.CollectionDTO;
|
|
@@ -13,16 +15,20 @@ import com.izouma.nineth.repo.*;
|
|
|
import com.izouma.nineth.utils.JpaUtils;
|
|
import com.izouma.nineth.utils.JpaUtils;
|
|
|
import com.izouma.nineth.utils.SecurityUtils;
|
|
import com.izouma.nineth.utils.SecurityUtils;
|
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.AllArgsConstructor;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.collections.MapUtils;
|
|
import org.apache.commons.collections.MapUtils;
|
|
|
import org.apache.commons.lang3.RandomUtils;
|
|
import org.apache.commons.lang3.RandomUtils;
|
|
|
import org.apache.commons.lang3.Range;
|
|
import org.apache.commons.lang3.Range;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
+import org.apache.rocketmq.spring.core.RocketMQTemplate;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.data.domain.Page;
|
|
import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.domain.PageImpl;
|
|
import org.springframework.data.domain.PageImpl;
|
|
|
import org.springframework.data.domain.PageRequest;
|
|
import org.springframework.data.domain.PageRequest;
|
|
|
import org.springframework.data.domain.Sort;
|
|
import org.springframework.data.domain.Sort;
|
|
|
import org.springframework.data.jpa.domain.Specification;
|
|
import org.springframework.data.jpa.domain.Specification;
|
|
|
|
|
+import org.springframework.data.redis.core.BoundValueOperations;
|
|
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.scheduling.TaskScheduler;
|
|
import org.springframework.scheduling.TaskScheduler;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
@@ -33,19 +39,25 @@ import java.time.LocalDateTime;
|
|
|
import java.time.ZoneId;
|
|
import java.time.ZoneId;
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
import java.util.concurrent.ScheduledFuture;
|
|
import java.util.concurrent.ScheduledFuture;
|
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
+@Slf4j
|
|
|
@Service
|
|
@Service
|
|
|
@AllArgsConstructor
|
|
@AllArgsConstructor
|
|
|
public class CollectionService {
|
|
public class CollectionService {
|
|
|
|
|
|
|
|
- private CollectionRepo collectionRepo;
|
|
|
|
|
- private LikeRepo likeRepo;
|
|
|
|
|
- private BlindBoxItemRepo blindBoxItemRepo;
|
|
|
|
|
- private AppointmentRepo appointmentRepo;
|
|
|
|
|
- private UserRepo userRepo;
|
|
|
|
|
- private TaskScheduler taskScheduler;
|
|
|
|
|
- private CacheService cacheService;
|
|
|
|
|
|
|
+ private CollectionRepo collectionRepo;
|
|
|
|
|
+ private LikeRepo likeRepo;
|
|
|
|
|
+ private BlindBoxItemRepo blindBoxItemRepo;
|
|
|
|
|
+ private AppointmentRepo appointmentRepo;
|
|
|
|
|
+ private UserRepo userRepo;
|
|
|
|
|
+ private TaskScheduler taskScheduler;
|
|
|
|
|
+ private CacheService cacheService;
|
|
|
|
|
+ private RedisTemplate<String, Object> redisTemplate;
|
|
|
|
|
+ private RocketMQTemplate rocketMQTemplate;
|
|
|
|
|
+ private GeneralProperties generalProperties;
|
|
|
|
|
+
|
|
|
|
|
|
|
|
private final Map<Long, ScheduledFuture<?>> tasks = new HashMap<>();
|
|
private final Map<Long, ScheduledFuture<?>> tasks = new HashMap<>();
|
|
|
|
|
|
|
@@ -335,4 +347,20 @@ public class CollectionService {
|
|
|
collection.setStock(collection.getStock() + number);
|
|
collection.setStock(collection.getStock() + number);
|
|
|
collection.setTotal(collection.getTotal() + number);
|
|
collection.setTotal(collection.getTotal() + number);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ public synchronized Long increaseStock(Long id, int number) {
|
|
|
|
|
+ BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps(RedisKeys.COLLECTION_STOCK + id);
|
|
|
|
|
+ if (ops.get() == null) {
|
|
|
|
|
+ Boolean success = ops.setIfAbsent(Optional.ofNullable(collectionRepo.getStock(id))
|
|
|
|
|
+ .orElse(0), 7, TimeUnit.DAYS);
|
|
|
|
|
+ log.info("创建redis库存:{}", success);
|
|
|
|
|
+ }
|
|
|
|
|
+ Long stock = ops.increment(number);
|
|
|
|
|
+ rocketMQTemplate.convertAndSend(generalProperties.getUpdateStockTopic(), id);
|
|
|
|
|
+ return stock;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public synchronized Long decreaseStock(Long id, int number) {
|
|
|
|
|
+ return increaseStock(id, -number);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|