|
@@ -1,20 +1,71 @@
|
|
|
package com.izouma.nineth.service;
|
|
package com.izouma.nineth.service;
|
|
|
|
|
|
|
|
|
|
+import com.izouma.nineth.annotations.Debounce;
|
|
|
import com.izouma.nineth.domain.MintActivity;
|
|
import com.izouma.nineth.domain.MintActivity;
|
|
|
import com.izouma.nineth.dto.PageQuery;
|
|
import com.izouma.nineth.dto.PageQuery;
|
|
|
import com.izouma.nineth.repo.MintActivityRepo;
|
|
import com.izouma.nineth.repo.MintActivityRepo;
|
|
|
import com.izouma.nineth.utils.JpaUtils;
|
|
import com.izouma.nineth.utils.JpaUtils;
|
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.AllArgsConstructor;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.data.domain.Page;
|
|
import org.springframework.data.domain.Page;
|
|
|
|
|
+import org.springframework.data.redis.core.BoundValueOperations;
|
|
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
+import java.util.Optional;
|
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
+
|
|
|
|
|
+@Slf4j
|
|
|
@Service
|
|
@Service
|
|
|
@AllArgsConstructor
|
|
@AllArgsConstructor
|
|
|
public class MintActivityService {
|
|
public class MintActivityService {
|
|
|
|
|
|
|
|
- private MintActivityRepo mintActivityRepo;
|
|
|
|
|
|
|
+ private MintActivityRepo mintActivityRepo;
|
|
|
|
|
+ private RedisTemplate<String, Object> redisTemplate;
|
|
|
|
|
+ private CacheService cacheService;
|
|
|
|
|
|
|
|
public Page<MintActivity> all(PageQuery pageQuery) {
|
|
public Page<MintActivity> all(PageQuery pageQuery) {
|
|
|
return mintActivityRepo.findAll(JpaUtils.toSpecification(pageQuery, MintActivity.class), JpaUtils.toPageRequest(pageQuery));
|
|
return mintActivityRepo.findAll(JpaUtils.toSpecification(pageQuery, MintActivity.class), JpaUtils.toPageRequest(pageQuery));
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ public synchronized Long increaseStock(Long id, int number) {
|
|
|
|
|
+ BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps("mintActivityStock::" + id);
|
|
|
|
|
+ if (ops.get() == null) {
|
|
|
|
|
+ Boolean success = ops.setIfAbsent(Optional.ofNullable(mintActivityRepo.getStock(id))
|
|
|
|
|
+ .orElse(0), 7, TimeUnit.DAYS);
|
|
|
|
|
+ log.info("创建redis铸造活动库存:{}", success);
|
|
|
|
|
+ }
|
|
|
|
|
+ Long stock = ops.increment(number);
|
|
|
|
|
+ return stock;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public synchronized Long decreaseStock(Long id, int number) {
|
|
|
|
|
+ return increaseStock(id, -number);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public synchronized Long increaseSale(Long id, int number) {
|
|
|
|
|
+ BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps("mintActivitySale::" + id);
|
|
|
|
|
+ if (ops.get() == null) {
|
|
|
|
|
+ Boolean success = ops.setIfAbsent(Optional.ofNullable(mintActivityRepo.getStock(id))
|
|
|
|
|
+ .orElse(0), 7, TimeUnit.DAYS);
|
|
|
|
|
+ log.info("创建redis铸造活动销量:{}", success);
|
|
|
|
|
+ }
|
|
|
|
|
+ Long sale = ops.increment(number);
|
|
|
|
|
+ return sale;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public synchronized Long decreaseSale(Long id, int number) {
|
|
|
|
|
+ return increaseStock(id, -number);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Debounce(key = "#id", delay = 500)
|
|
|
|
|
+ public void syncStock(Long id) {
|
|
|
|
|
+ Integer stock = (Integer) redisTemplate.opsForValue().get("mintActivityStock::" + id);
|
|
|
|
|
+ if (stock != null) {
|
|
|
|
|
+ log.info("同步铸造活动库存信息{}", id);
|
|
|
|
|
+ mintActivityRepo.updateStock(id, stock);
|
|
|
|
|
+ cacheService.clearMintActivity(id);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|