AirDropService.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package com.izouma.nineth.service;
  2. import com.izouma.nineth.domain.*;
  3. import com.izouma.nineth.dto.PageQuery;
  4. import com.izouma.nineth.enums.AirDropType;
  5. import com.izouma.nineth.enums.CollectionType;
  6. import com.izouma.nineth.exception.BusinessException;
  7. import com.izouma.nineth.repo.*;
  8. import com.izouma.nineth.utils.JpaUtils;
  9. import lombok.AllArgsConstructor;
  10. import lombok.extern.slf4j.Slf4j;
  11. import org.springframework.beans.BeanUtils;
  12. import org.springframework.data.domain.Page;
  13. import org.springframework.stereotype.Service;
  14. import javax.transaction.Transactional;
  15. import java.util.List;
  16. @Service
  17. @AllArgsConstructor
  18. @Slf4j
  19. public class AirDropService {
  20. private AirDropRepo airDropRepo;
  21. private CouponRepo couponRepo;
  22. private UserCouponRepo userCouponRepo;
  23. private CollectionRepo collectionRepo;
  24. private UserRepo userRepo;
  25. private AssetService assetService;
  26. private CollectionService collectionService;
  27. public Page<AirDrop> all(PageQuery pageQuery) {
  28. return airDropRepo.findAll(JpaUtils.toSpecification(pageQuery, AirDrop.class), JpaUtils.toPageRequest(pageQuery));
  29. }
  30. @Transactional
  31. public AirDrop create(AirDrop record) {
  32. if (AirDropType.coupon == record.getType()) {
  33. Coupon coupon = couponRepo.findById(record.getCouponId()).orElseThrow(new BusinessException("兑换券不存在"));
  34. record.getUserIds().stream().parallel().forEach(userId -> {
  35. UserCoupon userCoupon = new UserCoupon();
  36. BeanUtils.copyProperties(coupon, userCoupon);
  37. userCoupon.setId(null);
  38. userCoupon.setCouponId(coupon.getId());
  39. userCoupon.setUserId(userId);
  40. userCouponRepo.save(userCoupon);
  41. });
  42. } else {
  43. Collection collection = collectionRepo.findById(record.getCollectionId())
  44. .orElseThrow(new BusinessException("藏品不存在"));
  45. if (collection.getStock() < record.getUserIds().size()) {
  46. throw new BusinessException("藏品库存不足");
  47. }
  48. List<User> users = userRepo.findByIdInAndDelFalse(record.getUserIds());
  49. for (User user : users) {
  50. try {
  51. if (collection.getType() == CollectionType.BLIND_BOX) {
  52. BlindBoxItem winItem = collectionService.draw(collection.getId());
  53. assetService.createAsset(winItem, user, null, null, "空投", collectionService.getNextNumber(winItem.getCollectionId()));
  54. } else {
  55. assetService.createAsset(collection, user, null, null, "空投", collectionService.getNextNumber(collection.getId()));
  56. }
  57. collectionRepo.increaseStock(collection.getId(), -1);
  58. collectionRepo.increaseSale(collection.getId(), 1);
  59. } catch (Exception e) {
  60. log.error("空投出错", e);
  61. }
  62. }
  63. }
  64. return airDropRepo.save(record);
  65. }
  66. }