TradeAuctionService.java 6.8 KB

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