TradeAuctionService.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package com.izouma.nineth.service;
  2. import com.izouma.nineth.config.GeneralProperties;
  3. import com.izouma.nineth.config.RedisKeys;
  4. import com.izouma.nineth.domain.TradeAuction;
  5. import com.izouma.nineth.domain.TradeAuctionOrder;
  6. import com.izouma.nineth.domain.TradeAuctionRecord;
  7. import com.izouma.nineth.dto.PageQuery;
  8. import com.izouma.nineth.enums.AuctionOrderStatus;
  9. import com.izouma.nineth.enums.TradeAuctionStatus;
  10. import com.izouma.nineth.exception.BusinessException;
  11. import com.izouma.nineth.repo.TradeAuctionRepo;
  12. import com.izouma.nineth.utils.JpaUtils;
  13. import com.izouma.nineth.utils.ObjUtils;
  14. import lombok.AllArgsConstructor;
  15. import lombok.extern.slf4j.Slf4j;
  16. import org.apache.rocketmq.spring.core.RocketMQTemplate;
  17. import org.springframework.data.domain.Page;
  18. import org.springframework.data.redis.core.BoundValueOperations;
  19. import org.springframework.data.redis.core.RedisTemplate;
  20. import org.springframework.scheduling.annotation.Scheduled;
  21. import org.springframework.stereotype.Service;
  22. import java.security.PrivateKey;
  23. import java.time.LocalDateTime;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.Optional;
  27. import java.util.concurrent.TimeUnit;
  28. @Service
  29. @AllArgsConstructor
  30. @Slf4j
  31. public class TradeAuctionService {
  32. private TradeAuctionRepo tradeAuctionRepo;
  33. private RedisTemplate<String, Object> redisTemplate;
  34. private RocketMQTemplate rocketMQTemplate;
  35. private GeneralProperties generalProperties;
  36. public Page<TradeAuction> all(PageQuery pageQuery) {
  37. return tradeAuctionRepo
  38. .findAll(JpaUtils.toSpecification(pageQuery, TradeAuction.class), JpaUtils.toPageRequest(pageQuery));
  39. }
  40. public TradeAuction save(TradeAuction record) {
  41. if (record.getId() != null) {
  42. TradeAuction orig = tradeAuctionRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
  43. ObjUtils.merge(orig, record);
  44. return tradeAuctionRepo.save(orig);
  45. }
  46. record.setCurrentEndTime(record.getStartTime());
  47. record.setCurrentPrice(record.getPrice());
  48. record.setStatus(TradeAuctionStatus.WAITING);
  49. return tradeAuctionRepo.save(record);
  50. }
  51. // public void transfer(Long id) {
  52. // TradeAuctionRecord tradeAuctionRecord = new TradeAuctionRecord();
  53. // TradeAuction tradeAuction = tradeAuctionRepo.findById(id).orElseThrow(new BusinessException("暂无"));
  54. // tradeAuction.setStatus(TradeAuctionStatus.WAITING);
  55. // increaseSale(tr)
  56. // }
  57. public synchronized Long increaseStock(Long id, int number) {
  58. BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps(RedisKeys.AUCTION_STOCK + id);
  59. if (ops.get() == null) {
  60. Boolean success = ops.setIfAbsent(Optional.ofNullable(tradeAuctionRepo.getStock(id))
  61. .orElse(0), 7, TimeUnit.DAYS);
  62. log.info("创建redis库存:{}", success);
  63. }
  64. Long stock = ops.increment(number);
  65. rocketMQTemplate.convertAndSend(generalProperties.getUpdateStockTopic(), id);
  66. return stock;
  67. }
  68. public synchronized Integer getStock(Long id) {
  69. BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps(RedisKeys.AUCTION_STOCK + id);
  70. Integer stock = (Integer) ops.get();
  71. if (stock == null) {
  72. Boolean success = ops.setIfAbsent(Optional.ofNullable(tradeAuctionRepo.getStock(id))
  73. .orElse(0), 7, TimeUnit.DAYS);
  74. log.info("创建redis库存:{}", success);
  75. return (Integer) ops.get();
  76. } else {
  77. return stock;
  78. }
  79. }
  80. public synchronized Long decreaseStock(Long id, int number) {
  81. return increaseStock(id, -number);
  82. }
  83. public synchronized Long increaseSale(Long id, int number) {
  84. BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps(RedisKeys.COLLECTION_SALE + id);
  85. if (ops.get() == null) {
  86. Boolean success = ops.setIfAbsent(Optional.ofNullable(tradeAuctionRepo.getSale(id))
  87. .orElse(0), 7, TimeUnit.DAYS);
  88. log.info("创建redis销量:{}", success);
  89. }
  90. Long sale = ops.increment(number);
  91. redisTemplate.opsForHash().increment(RedisKeys.UPDATE_SALE, id.toString(), 1);
  92. // rocketMQTemplate.convertAndSend(generalProperties.getUpdateSaleTopic(), id);
  93. return sale;
  94. }
  95. public synchronized Long decreaseSale(Long id, int number) {
  96. return increaseSale(id, -number);
  97. }
  98. // @Debounce(key = "#id", delay = 500)
  99. public void syncStock(Long id) {
  100. Integer stock = (Integer) redisTemplate.opsForValue().get(RedisKeys.COLLECTION_STOCK + id);
  101. if (stock != null) {
  102. log.info("同步库存信息{}", id);
  103. tradeAuctionRepo.updateStock(id, stock);
  104. // cacheService.clearCollection(id);
  105. }
  106. }
  107. // @Debounce(key = "#id", delay = 500)
  108. public void syncSale(Long id) {
  109. Integer sale = (Integer) redisTemplate.opsForValue().get(RedisKeys.COLLECTION_SALE + id);
  110. if (sale != null) {
  111. log.info("同步销量信息{}", id);
  112. tradeAuctionRepo.updateSale(id, sale);
  113. // cacheService.clearCollection(id);
  114. }
  115. }
  116. @Scheduled(fixedRate = 6000)
  117. public void batchStartAuction() {
  118. List<TradeAuctionStatus> tradeAuctionStatuses = new ArrayList<>();
  119. tradeAuctionStatuses.add(TradeAuctionStatus.WAITING);
  120. tradeAuctionStatuses.add(TradeAuctionStatus.NOTSTARTED);
  121. List<TradeAuction> tradeAuctions = tradeAuctionRepo
  122. .findByStatusInAndCurrentEndTimeAfter(tradeAuctionStatuses,
  123. LocalDateTime.now().plusMinutes(1));
  124. tradeAuctions.parallelStream().forEach(o -> {
  125. try {
  126. TradeAuction auction = tradeAuctionRepo.findById(o.getId())
  127. .orElseThrow(new BusinessException("订单不存在"));
  128. if (auction.getStatus() == TradeAuctionStatus.WAITING || auction
  129. .getStatus() == TradeAuctionStatus.NOTSTARTED) {
  130. start(auction);
  131. }
  132. } catch (Exception e) {
  133. log.error("取消易拍订单错误 " + o.getId(), e);
  134. }
  135. });
  136. }
  137. public void start(TradeAuction tradeAuction) {
  138. tradeAuction.setStatus(TradeAuctionStatus.ONGOING);
  139. tradeAuctionRepo.save(tradeAuction);
  140. increaseStock(tradeAuction.getId(), 1);
  141. }
  142. }