AirDropService.java 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. collection.setStock(collection.getStock() - 1);
  58. collection.setSale(collection.getSale() + 1);
  59. collectionRepo.save(collection);
  60. } catch (Exception e) {
  61. log.error("空投出错", e);
  62. }
  63. }
  64. }
  65. return airDropRepo.save(record);
  66. }
  67. }