AirDropService.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package com.izouma.nineth.service;
  2. import com.izouma.nineth.TokenHistory;
  3. import com.izouma.nineth.domain.*;
  4. import com.izouma.nineth.dto.PageQuery;
  5. import com.izouma.nineth.enums.AirDropType;
  6. import com.izouma.nineth.enums.CollectionType;
  7. import com.izouma.nineth.exception.BusinessException;
  8. import com.izouma.nineth.repo.*;
  9. import com.izouma.nineth.utils.JpaUtils;
  10. import lombok.AllArgsConstructor;
  11. import lombok.extern.slf4j.Slf4j;
  12. import org.apache.commons.lang3.ObjectUtils;
  13. import org.springframework.beans.BeanUtils;
  14. import org.springframework.data.domain.Page;
  15. import org.springframework.stereotype.Service;
  16. import java.time.LocalDateTime;
  17. import java.util.List;
  18. @Service
  19. @AllArgsConstructor
  20. @Slf4j
  21. public class AirDropService {
  22. private AirDropRepo airDropRepo;
  23. private CouponRepo couponRepo;
  24. private UserCouponRepo userCouponRepo;
  25. private CollectionRepo collectionRepo;
  26. private UserRepo userRepo;
  27. private AssetService assetService;
  28. private CollectionService collectionService;
  29. private ShowroomService showroomService;
  30. private TokenHistoryRepo tokenHistoryRepo;
  31. private AssetRepo assetRepo;
  32. private CollectionPrivilegeRepo collectionPrivilegeRepo;
  33. public Page<AirDrop> all(PageQuery pageQuery) {
  34. return airDropRepo.findAll(JpaUtils.toSpecification(pageQuery, AirDrop.class), JpaUtils.toPageRequest(pageQuery));
  35. }
  36. public AirDrop create(AirDrop record) {
  37. if (AirDropType.coupon == record.getType()) {
  38. Coupon coupon = couponRepo.findById(record.getCouponId()).orElseThrow(new BusinessException("兑换券不存在"));
  39. record.getUserIds().stream().parallel().forEach(userId -> {
  40. UserCoupon userCoupon = new UserCoupon();
  41. BeanUtils.copyProperties(coupon, userCoupon);
  42. userCoupon.setId(null);
  43. userCoupon.setCouponId(coupon.getId());
  44. userCoupon.setUserId(userId);
  45. userCouponRepo.save(userCoupon);
  46. });
  47. } else {
  48. Collection collection = collectionRepo.findById(record.getCollectionId())
  49. .orElseThrow(new BusinessException("藏品不存在"));
  50. if (collection.isSalable()) {
  51. throw new BusinessException("请先设置藏品为不可购买");
  52. }
  53. if (!record.isIgnoreStockCheck() && collection.getStock() < record.getUserIds().size()) {
  54. throw new BusinessException("藏品库存不足");
  55. }
  56. List<User> users = userRepo.findByIdInAndDelFalse(record.getUserIds());
  57. for (User user : users) {
  58. try {
  59. if (collection.getType() == CollectionType.BLIND_BOX) {
  60. BlindBoxItem winItem = collectionService.draw(collection.getId());
  61. if (record.isSimulateOrder()) {
  62. assetService.createAsset(winItem, user, 0L, collection.getPrice(), "出售",
  63. winItem.getTotal() > 1 ? collectionService.getNextNumber(winItem.getCollectionId()) : null,
  64. collection.getHoldDays());
  65. } else {
  66. //查看有无vip权限
  67. CollectionPrivilege collectionPrivilege = collectionPrivilegeRepo.findByCollectionId(record.getCollectionId());
  68. if (ObjectUtils.isNotEmpty(collectionPrivilege)) {
  69. if (collectionPrivilege.isVip()) {
  70. //更新vip信息
  71. userRepo.updateVipPurchase(user.getId(), 1);
  72. }
  73. }
  74. assetService.createAsset(winItem, user, null, null, "空投",
  75. collectionService.getNextNumber(winItem.getCollectionId()), collection.getHoldDays());
  76. }
  77. } else {
  78. if (record.isSimulateOrder()) {
  79. assetService.createAsset(collection, user, 0L, collection.getPrice(), "出售",
  80. collection.getTotal() > 1 ? collectionService.getNextNumber(collection.getId()) : null);
  81. } else {
  82. //查看有无vip权限
  83. CollectionPrivilege collectionPrivilege = collectionPrivilegeRepo.findByCollectionId(record.getCollectionId());
  84. if (ObjectUtils.isNotEmpty(collectionPrivilege)) {
  85. if (collectionPrivilege.isVip()) {
  86. //更新vip信息
  87. userRepo.updateVipPurchase(user.getId(), 1);
  88. }
  89. }
  90. Asset asset = assetService.createAsset(collection, user, null, null, "空投", collectionService.getNextNumber(collection.getId()));
  91. //创建展厅
  92. if (collection.getType() == CollectionType.SHOWROOM) {
  93. showroomService.save(asset);
  94. }
  95. }
  96. // Asset asset = assetService.createAsset(collection, user, null, null, "空投", collectionService.getNextNumber(collection.getId()));
  97. }
  98. collectionService.decreaseStock(collection.getId(), 1);
  99. collectionService.increaseSale(collection.getId(), 1);
  100. } catch (Exception e) {
  101. log.error("空投出错", e);
  102. }
  103. }
  104. }
  105. return airDropRepo.save(record);
  106. }
  107. public void drop(Long collectionId, Long userId, int num, LocalDateTime time) {
  108. Collection collection = collectionRepo.findById(collectionId)
  109. .orElseThrow(new BusinessException("藏品不存在"));
  110. User user = userRepo.findById(userId).orElseThrow(new BusinessException("用户不存在"));
  111. try {
  112. for (int i = 0; i < num; i++) {
  113. Asset asset;
  114. if (collection.getType() == CollectionType.BLIND_BOX) {
  115. BlindBoxItem winItem = collectionService.draw(collection.getId());
  116. asset = assetService.createAsset(winItem, user, 0L, collection.getPrice(), "出售",
  117. winItem.getTotal() > 1 ? collectionService.getNextNumber(winItem.getCollectionId()) : null,
  118. collection.getHoldDays());
  119. } else {
  120. asset = assetService.createAsset(collection, user, 0L, collection.getPrice(), "出售",
  121. collection.getTotal() > 1 ? collectionService.getNextNumber(collection.getId()) : null);
  122. }
  123. assetRepo.flush();
  124. tokenHistoryRepo.flush();
  125. asset.setCreatedAt(time.plusSeconds((long) (Math.random() * 120)));
  126. assetRepo.save(asset);
  127. for (TokenHistory tokenHistory : tokenHistoryRepo.findByTokenIdOrderByCreatedAtDesc(asset.getTokenId())) {
  128. tokenHistory.setCreatedAt(asset.getCreatedAt());
  129. tokenHistoryRepo.save(tokenHistory);
  130. }
  131. }
  132. } catch (Exception e) {
  133. log.error("空投出错", e);
  134. }
  135. }
  136. public void drop(List<Collection> collections, String phone, LocalDateTime time) {
  137. // List<Collection> collections = collectionRepo.findAllById(collectionId);
  138. User user = userRepo.findByPhoneAndDelFalse(phone).orElseThrow(new BusinessException("用户不存在"));
  139. try {
  140. for (Collection collection : collections) {
  141. Asset asset;
  142. if (collection.getType() == CollectionType.BLIND_BOX) {
  143. BlindBoxItem winItem = collectionService.draw(collection.getId());
  144. asset = assetService.createAsset(winItem, user, 0L, collection.getPrice(), "出售",
  145. winItem.getTotal() > 1 ? collectionService.getNextNumber(winItem.getCollectionId()) : null,
  146. collection.getHoldDays());
  147. } else {
  148. asset = assetService.createAsset(collection, user, 0L, collection.getPrice(), "出售",
  149. collection.getTotal() > 1 ? collectionService.getNextNumber(collection.getId()) : null);
  150. }
  151. assetRepo.flush();
  152. tokenHistoryRepo.flush();
  153. asset.setCreatedAt(time.plusSeconds((long) (Math.random() * 120)));
  154. assetRepo.save(asset);
  155. for (TokenHistory tokenHistory : tokenHistoryRepo.findByTokenIdOrderByCreatedAtDesc(asset.getTokenId())) {
  156. tokenHistory.setCreatedAt(asset.getCreatedAt());
  157. tokenHistoryRepo.save(tokenHistory);
  158. }
  159. }
  160. } catch (Exception e) {
  161. log.error("空投出错", e);
  162. }
  163. }
  164. }