TradeAuctionService.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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.TradeAuctionOrderRepo;
  12. import com.izouma.nineth.repo.TradeAuctionRepo;
  13. import com.izouma.nineth.utils.JpaUtils;
  14. import com.izouma.nineth.utils.ObjUtils;
  15. import lombok.AllArgsConstructor;
  16. import lombok.extern.slf4j.Slf4j;
  17. import org.apache.rocketmq.spring.core.RocketMQTemplate;
  18. import org.springframework.data.domain.Page;
  19. import org.springframework.data.redis.core.BoundValueOperations;
  20. import org.springframework.data.redis.core.RedisTemplate;
  21. import org.springframework.scheduling.annotation.Scheduled;
  22. import org.springframework.stereotype.Service;
  23. import java.math.BigDecimal;
  24. import java.math.RoundingMode;
  25. import java.security.PrivateKey;
  26. import java.time.LocalDateTime;
  27. import java.util.ArrayList;
  28. import java.util.List;
  29. import java.util.Optional;
  30. import java.util.concurrent.TimeUnit;
  31. @Service
  32. @AllArgsConstructor
  33. @Slf4j
  34. public class TradeAuctionService {
  35. private TradeAuctionRepo tradeAuctionRepo;
  36. private RedisTemplate<String, Object> redisTemplate;
  37. private RocketMQTemplate rocketMQTemplate;
  38. private GeneralProperties generalProperties;
  39. private TradeAuctionOrderRepo tradeAuctionOrderRepo;
  40. public Page<TradeAuction> all(PageQuery pageQuery) {
  41. return tradeAuctionRepo
  42. .findAll(JpaUtils.toSpecification(pageQuery, TradeAuction.class), JpaUtils.toPageRequest(pageQuery));
  43. }
  44. public TradeAuction save(TradeAuction record) {
  45. if (record.getId() != null) {
  46. TradeAuction orig = tradeAuctionRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
  47. ObjUtils.merge(orig, record);
  48. return tradeAuctionRepo.save(orig);
  49. }
  50. record.setCurrentEndTime(record.getStartTime().plusDays(1));
  51. record.setIncreasePer(6);
  52. record.setRoyalties(10);
  53. record.setCurrentStartTime(record.getStartTime());
  54. record.setCurrentPrice(record.getPrice());
  55. record.setStatus(TradeAuctionStatus.WAITING);
  56. BigDecimal result = record.getCurrentPrice().multiply(BigDecimal.valueOf(record.getIncreasePer()))
  57. .divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP);
  58. record.setNextPrice(result.add(record.getPrice()));
  59. record.setEarning(record.getNextPrice().subtract(record.getPrice()));
  60. record.setFixedPrice(record.getPrice().multiply(BigDecimal.valueOf(150))
  61. .divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP));
  62. BigDecimal serviceCharge = (BigDecimal.valueOf(0.03).multiply(record.getNextPrice()))
  63. .add(record.getCurrentPrice().subtract(record.getPrice()));
  64. // record.setEarning(result.subtract(record.getPrice()));
  65. record.setCommission(serviceCharge);
  66. record.setStock(1L);
  67. record.setSale(0L);
  68. return tradeAuctionRepo.save(record);
  69. }
  70. // public void transfer(Long id) {
  71. // TradeAuctionRecord tradeAuctionRecord = new TradeAuctionRecord();
  72. // TradeAuction tradeAuction = tradeAuctionRepo.findById(id).orElseThrow(new BusinessException("暂无"));
  73. // tradeAuction.setStatus(TradeAuctionStatus.WAITING);
  74. // increaseSale(tr)
  75. // }
  76. public synchronized Long increaseStock(Long id, int number) {
  77. BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps(RedisKeys.AUCTION_STOCK + id);
  78. if (ops.get() == null) {
  79. Boolean success = ops.setIfAbsent(Optional.ofNullable(tradeAuctionRepo.getStock(id))
  80. .orElse(0), 7, TimeUnit.DAYS);
  81. log.info("创建redis库存:{}", success);
  82. }
  83. Long stock = ops.increment(number);
  84. rocketMQTemplate.convertAndSend(generalProperties.getUpdateStockTopic(), id);
  85. return stock;
  86. }
  87. public synchronized Integer getStock(Long id) {
  88. BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps(RedisKeys.AUCTION_STOCK + id);
  89. Integer stock = (Integer) ops.get();
  90. if (stock == null) {
  91. Boolean success = ops.setIfAbsent(Optional.ofNullable(tradeAuctionRepo.getStock(id))
  92. .orElse(0), 7, TimeUnit.DAYS);
  93. log.info("创建redis库存:{}", success);
  94. return (Integer) ops.get();
  95. } else {
  96. return stock;
  97. }
  98. }
  99. public synchronized Long decreaseStock(Long id, int number) {
  100. return increaseStock(id, -number);
  101. }
  102. public synchronized Long increaseSale(Long id, int number) {
  103. BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps(RedisKeys.COLLECTION_SALE + id);
  104. if (ops.get() == null) {
  105. Boolean success = ops.setIfAbsent(Optional.ofNullable(tradeAuctionRepo.getSale(id))
  106. .orElse(0), 7, TimeUnit.DAYS);
  107. log.info("创建redis销量:{}", success);
  108. }
  109. Long sale = ops.increment(number);
  110. redisTemplate.opsForHash().increment(RedisKeys.UPDATE_SALE, id.toString(), 1);
  111. // rocketMQTemplate.convertAndSend(generalProperties.getUpdateSaleTopic(), id);
  112. return sale;
  113. }
  114. public synchronized Long decreaseSale(Long id, int number) {
  115. return increaseSale(id, -number);
  116. }
  117. // @Debounce(key = "#id", delay = 500)
  118. public void syncStock(Long id) {
  119. Integer stock = (Integer) redisTemplate.opsForValue().get(RedisKeys.COLLECTION_STOCK + id);
  120. if (stock != null) {
  121. log.info("同步库存信息{}", id);
  122. tradeAuctionRepo.updateStock(id, stock);
  123. // cacheService.clearCollection(id);
  124. }
  125. }
  126. // @Debounce(key = "#id", delay = 500)
  127. public void syncSale(Long id) {
  128. Integer sale = (Integer) redisTemplate.opsForValue().get(RedisKeys.COLLECTION_SALE + id);
  129. if (sale != null) {
  130. log.info("同步销量信息{}", id);
  131. tradeAuctionRepo.updateSale(id, sale);
  132. // cacheService.clearCollection(id);
  133. }
  134. }
  135. @Scheduled(fixedRate = 6000)
  136. public void batchStartAuction() {
  137. List<TradeAuctionStatus> tradeAuctionStatuses = new ArrayList<>();
  138. tradeAuctionStatuses.add(TradeAuctionStatus.WAITING);
  139. tradeAuctionStatuses.add(TradeAuctionStatus.NOTSTARTED);
  140. List<TradeAuction> tradeAuctions = tradeAuctionRepo
  141. .findByStatusInAndCurrentStartTimeBefore(tradeAuctionStatuses,
  142. LocalDateTime.now().plusMinutes(1));
  143. tradeAuctions.parallelStream().forEach(o -> {
  144. try {
  145. TradeAuction auction = tradeAuctionRepo.findById(o.getId())
  146. .orElseThrow(new BusinessException("订单不存在"));
  147. if (auction.getStatus() == TradeAuctionStatus.WAITING || auction
  148. .getStatus() == TradeAuctionStatus.NOTSTARTED) {
  149. start(auction);
  150. }
  151. } catch (Exception e) {
  152. log.error("取消易拍订单错误 " + o.getId(), e);
  153. }
  154. });
  155. }
  156. public void start(TradeAuction tradeAuction) {
  157. tradeAuction.setStatus(TradeAuctionStatus.ONGOING);
  158. tradeAuctionRepo.save(tradeAuction);
  159. increaseStock(tradeAuction.getId(), 1);
  160. }
  161. @Scheduled(fixedRate = 6000)
  162. public void batchPurchaseAuction() {
  163. List<TradeAuctionStatus> tradeAuctionStatuses = new ArrayList<>();
  164. tradeAuctionStatuses.add(TradeAuctionStatus.ONGOING);
  165. List<TradeAuction> tradeAuctions = tradeAuctionRepo
  166. .findByStatusInAndCurrentEndTimeBefore(tradeAuctionStatuses,
  167. LocalDateTime.now().plusMinutes(1));
  168. tradeAuctions.parallelStream().forEach(o -> {
  169. try {
  170. TradeAuction auction = tradeAuctionRepo.findById(o.getId())
  171. .orElseThrow(new BusinessException("订单不存在"));
  172. if (auction.getStatus() == TradeAuctionStatus.ONGOING) {
  173. purchase(auction);
  174. }
  175. } catch (Exception e) {
  176. log.error("取消易拍订单错误 " + o.getId(), e);
  177. }
  178. });
  179. }
  180. public void purchase(TradeAuction tradeAuction) {
  181. tradeAuction.setStatus(TradeAuctionStatus.PURCHASED);
  182. tradeAuctionRepo.save(tradeAuction);
  183. decreaseStock(tradeAuction.getId(), 1);
  184. }
  185. @Scheduled(fixedRate = 6000)
  186. public void batchPassAuction() {
  187. List<TradeAuctionStatus> tradeAuctionStatuses = new ArrayList<>();
  188. tradeAuctionStatuses.add(TradeAuctionStatus.PURCHASED);
  189. List<TradeAuction> tradeAuctions = tradeAuctionRepo
  190. .findByStatusInAndCurrentEndTimeBefore(tradeAuctionStatuses,
  191. LocalDateTime.now().plusMinutes(1));
  192. tradeAuctions.parallelStream().forEach(o -> {
  193. try {
  194. TradeAuction auction = tradeAuctionRepo.findById(o.getId())
  195. .orElseThrow(new BusinessException("订单不存在"));
  196. if (auction.getStatus() == TradeAuctionStatus.ONGOING) {
  197. pass(auction);
  198. }
  199. } catch (Exception e) {
  200. log.error("取消易拍订单错误 " + o.getId(), e);
  201. }
  202. });
  203. }
  204. public void pass(TradeAuction tradeAuction) {
  205. tradeAuction.setStatus(TradeAuctionStatus.PASS);
  206. tradeAuctionRepo.save(tradeAuction);
  207. TradeAuctionOrder tradeAuctionOrder = tradeAuctionOrderRepo.findById(tradeAuction.getCurrentOrderId())
  208. .orElseThrow(new BusinessException("暂无订单"));
  209. tradeAuctionOrder.setStatus(AuctionOrderStatus.PASS);
  210. tradeAuctionOrderRepo.save(tradeAuctionOrder);
  211. decreaseStock(tradeAuction.getId(), 1);
  212. }
  213. }