AirDropService.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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.scheduling.annotation.Async;
  16. import org.springframework.stereotype.Service;
  17. import java.time.LocalDateTime;
  18. import java.util.List;
  19. import java.util.stream.Collectors;
  20. @Service
  21. @AllArgsConstructor
  22. @Slf4j
  23. public class AirDropService {
  24. private AirDropRepo airDropRepo;
  25. private CouponRepo couponRepo;
  26. private UserCouponRepo userCouponRepo;
  27. private CollectionRepo collectionRepo;
  28. private UserRepo userRepo;
  29. private AssetService assetService;
  30. private CollectionService collectionService;
  31. private ShowroomService showroomService;
  32. private TokenHistoryRepo tokenHistoryRepo;
  33. private AssetRepo assetRepo;
  34. private CollectionPrivilegeRepo collectionPrivilegeRepo;
  35. public Page<AirDrop> all(PageQuery pageQuery) {
  36. return airDropRepo
  37. .findAll(JpaUtils.toSpecification(pageQuery, AirDrop.class), JpaUtils.toPageRequest(pageQuery));
  38. }
  39. public AirDrop create(AirDrop record) {
  40. if (record.getTargets().isEmpty()) throw new BusinessException("空投对象不能为空");
  41. if (record.getTargets().stream().mapToInt(DropTarget::getNum).sum() > 300)
  42. throw new BusinessException("空投数量不能超过300");
  43. if (AirDropType.coupon == record.getType()) {
  44. Coupon coupon = couponRepo.findById(record.getCouponId()).orElseThrow(new BusinessException("兑换券不存在"));
  45. for (DropTarget target : record.getTargets()) {
  46. for (int i = 0; i < target.getNum(); i++) {
  47. UserCoupon userCoupon = new UserCoupon();
  48. BeanUtils.copyProperties(coupon, userCoupon);
  49. userCoupon.setId(null);
  50. userCoupon.setCouponId(coupon.getId());
  51. userCoupon.setUserId(target.getUserId());
  52. userCouponRepo.save(userCoupon);
  53. }
  54. }
  55. } else {
  56. Collection collection = collectionRepo.findById(record.getCollectionId())
  57. .orElseThrow(new BusinessException("藏品不存在"));
  58. if (collection.isSalable()) {
  59. throw new BusinessException("请先设置藏品为不可购买");
  60. }
  61. if (!record.isIgnoreStockCheck() && collection.getStock() < record.getUserIds().size()) {
  62. throw new BusinessException("藏品库存不足");
  63. }
  64. List<User> users = userRepo.findByIdInAndDelFalse(record.getTargets().stream()
  65. .map(DropTarget::getUserId).collect(Collectors.toList()));
  66. for (DropTarget target : record.getTargets()) {
  67. User user = users.stream().filter(u -> u.getId().equals(target.getUserId()))
  68. .findFirst().orElse(null);
  69. if (user == null) continue;
  70. try {
  71. for (int i = 0; i < target.getNum(); i++) {
  72. if (collection.getType() == CollectionType.BLIND_BOX) {
  73. BlindBoxItem winItem = collectionService.draw(collection.getId());
  74. if (record.isSimulateOrder()) {
  75. assetService.createAsset(winItem, user, 0L, collection.getPrice(), "出售",
  76. winItem.getTotal() > 1 ?
  77. collectionService.getNextNumber(winItem.getCollectionId()) : null,
  78. collection.getHoldDays());
  79. } else {
  80. //查看有无vip权限
  81. CollectionPrivilege collectionPrivilege = collectionPrivilegeRepo
  82. .findByCollectionId(record.getCollectionId());
  83. if (ObjectUtils.isNotEmpty(collectionPrivilege)) {
  84. if (collectionPrivilege.isVip()) {
  85. //更新vip信息
  86. userRepo.updateVipPurchase(user.getId(), 1);
  87. }
  88. }
  89. assetService.createAsset(winItem, user, null, null, "空投",
  90. winItem.getTotal() > 1 ?
  91. collectionService.getNextNumber(winItem.getCollectionId()) : null,
  92. collection.getHoldDays());
  93. }
  94. } else {
  95. if (record.isSimulateOrder()) {
  96. assetService.createAsset(collection, user, 0L, collection.getPrice(),
  97. "出售", collection.getTotal() > 1 ?
  98. collectionService.getNextNumber(collection.getId()) : null);
  99. } else {
  100. //查看有无vip权限
  101. CollectionPrivilege collectionPrivilege = collectionPrivilegeRepo
  102. .findByCollectionId(record.getCollectionId());
  103. if (ObjectUtils.isNotEmpty(collectionPrivilege)) {
  104. if (collectionPrivilege.isVip()) {
  105. //更新vip信息
  106. userRepo.updateVipPurchase(user.getId(), 1);
  107. }
  108. }
  109. Asset asset = assetService.createAsset(collection, user, null, null,
  110. "空投", collection.getTotal() > 1 ?
  111. collectionService.getNextNumber(collection.getId()) : null);
  112. //创建展厅
  113. if (collection.getType() == CollectionType.SHOWROOM) {
  114. asset.setOasisId(record.getOasisId());
  115. showroomService.save(asset);
  116. }
  117. }
  118. // Asset asset = assetService.createAsset(collection, user, null, null, "空投", collectionService.getNextNumber(collection.getId()));
  119. }
  120. collectionService.decreaseStock(collection.getId(), 1);
  121. collectionService.increaseSale(collection.getId(), 1);
  122. }
  123. } catch (Exception e) {
  124. log.error("空投出错", e);
  125. }
  126. }
  127. }
  128. return airDropRepo.save(record);
  129. }
  130. @Async
  131. public void asyncDrop(Long collectionId, Long userId, int num, LocalDateTime time) {
  132. drop(collectionId, userId, num, time, null);
  133. }
  134. public void drop(Long collectionId, Long userId, int num, LocalDateTime time, Long oasisId) {
  135. Collection collection = collectionRepo.findById(collectionId)
  136. .orElseThrow(new BusinessException("藏品不存在"));
  137. User user = userRepo.findById(userId).orElseThrow(new BusinessException("用户不存在"));
  138. try {
  139. for (int i = 0; i < num; i++) {
  140. Asset asset;
  141. if (collection.getType() == CollectionType.BLIND_BOX) {
  142. BlindBoxItem winItem = collectionService.draw(collection.getId());
  143. asset = assetService.createAsset(winItem, user, 0L, collection.getPrice(), "出售",
  144. winItem.getTotal() > 1 ? collectionService.getNextNumber(winItem.getCollectionId()) : null,
  145. collection.getHoldDays());
  146. } else {
  147. asset = assetService.createAsset(collection, user, 0L, collection.getPrice(), "出售",
  148. collection.getTotal() > 1 ? collectionService.getNextNumber(collection.getId()) : null);
  149. }
  150. assetRepo.flush();
  151. tokenHistoryRepo.flush();
  152. asset.setCreatedAt(time.plusSeconds((long) (Math.random() * 120)));
  153. if (oasisId != null) {
  154. asset.setOasisId(oasisId);
  155. }
  156. assetRepo.save(asset);
  157. for (TokenHistory tokenHistory : tokenHistoryRepo
  158. .findByTokenIdOrderByCreatedAtDesc(asset.getTokenId())) {
  159. tokenHistory.setCreatedAt(asset.getCreatedAt());
  160. tokenHistoryRepo.save(tokenHistory);
  161. }
  162. log.info("空投成功{}/{} collectionId={}, userId={}", i + 1, num, collectionId, userId);
  163. }
  164. } catch (Exception e) {
  165. log.error("空投出错", e);
  166. }
  167. }
  168. public void drop(List<Collection> collections, String phone, LocalDateTime time) {
  169. // List<Collection> collections = collectionRepo.findAllById(collectionId);
  170. User user = userRepo.findByPhoneAndDelFalse(phone).orElseThrow(new BusinessException("用户不存在"));
  171. try {
  172. for (Collection collection : collections) {
  173. Asset asset;
  174. if (collection.getType() == CollectionType.BLIND_BOX) {
  175. BlindBoxItem winItem = collectionService.draw(collection.getId());
  176. asset = assetService.createAsset(winItem, user, 0L, collection.getPrice(), "出售",
  177. winItem.getTotal() > 1 ? collectionService.getNextNumber(winItem.getCollectionId()) : null,
  178. collection.getHoldDays());
  179. } else {
  180. asset = assetService.createAsset(collection, user, 0L, collection.getPrice(), "出售",
  181. collection.getTotal() > 1 ? collectionService.getNextNumber(collection.getId()) : null);
  182. }
  183. assetRepo.flush();
  184. tokenHistoryRepo.flush();
  185. asset.setCreatedAt(time.plusSeconds((long) (Math.random() * 120)));
  186. assetRepo.save(asset);
  187. for (TokenHistory tokenHistory : tokenHistoryRepo
  188. .findByTokenIdOrderByCreatedAtDesc(asset.getTokenId())) {
  189. tokenHistory.setCreatedAt(asset.getCreatedAt());
  190. tokenHistoryRepo.save(tokenHistory);
  191. }
  192. }
  193. } catch (Exception e) {
  194. log.error("空投出错", e);
  195. }
  196. }
  197. }