ActivityOrderService.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.AssetStatus;
  5. import com.izouma.nineth.enums.OrderStatus;
  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.apache.commons.collections.CollectionUtils;
  12. import org.springframework.data.domain.Page;
  13. import org.springframework.stereotype.Service;
  14. import java.util.List;
  15. import java.util.Optional;
  16. import java.util.stream.Collectors;
  17. @Slf4j
  18. @Service
  19. @AllArgsConstructor
  20. public class ActivityOrderService {
  21. private ActivityOrderRepo activityOrderRepo;
  22. private ActivityCollectionRepo activityCollectionRepo;
  23. private ActivityCollectionService activityCollectionService;
  24. private AssetRepo assetRepo;
  25. private OrderRepo orderRepo;
  26. private CollectionRepo collectionRepo;
  27. private ActivityMaterialRepo activityMaterialRepo;
  28. private CollectionService collectionService;
  29. private AssetService assetService;
  30. private UserRepo userRepo;
  31. public Page<ActivityOrder> all(PageQuery pageQuery) {
  32. return activityOrderRepo.findAll(JpaUtils.toSpecification(pageQuery, ActivityOrder.class), JpaUtils.toPageRequest(pageQuery));
  33. }
  34. public Asset create(User user, Long mintActivityId) {
  35. try {
  36. ActivityCollection activity = activityCollectionRepo.findById(mintActivityId)
  37. .orElseThrow(new BusinessException("活动不存在"));
  38. //库存
  39. int stock = Optional.ofNullable(activityCollectionService.increaseStock(mintActivityId, -1))
  40. .map(Math::toIntExact)
  41. .orElseThrow(new BusinessException("很遗憾,活动已无库存"));
  42. if (stock < 0) {
  43. throw new BusinessException("活动已无库存");
  44. }
  45. //数量
  46. List<Asset> assets = assetRepo.findAllByCollectionIdAndUserIdAndStatus(activity.getCollectionId(), user.getId(),
  47. AssetStatus.NORMAL);
  48. if (assets.size() < activity.getNum()) {
  49. throw new BusinessException("数量不足,无法领取");
  50. }
  51. //兑换后的
  52. Collection award = collectionRepo.findById(activity.getAwardCollectionId())
  53. .orElseThrow(new BusinessException("无藏品"));
  54. int awardStock = Optional.ofNullable(collectionService.decreaseStock(activity.getAwardCollectionId(), 1))
  55. .map(Math::toIntExact)
  56. .orElseThrow(new BusinessException("兑换藏品无库存"));
  57. if (awardStock < 0) {
  58. throw new BusinessException("兑换藏品无库存");
  59. }
  60. //指定账户
  61. User newOwner = userRepo.findByIdAndDelFalse(1590945L).orElseThrow(new BusinessException("无法兑换"));
  62. List<Asset> consumption = assets.stream().limit(activity.getNum()).collect(Collectors.toList());
  63. //消耗资产
  64. consumption.forEach(asset -> {
  65. if (asset.isPublicShow()) {
  66. if (asset.getPublicCollectionId() != null) {
  67. List<Order> orders = orderRepo.findByCollectionId(asset.getPublicCollectionId());
  68. if (orders.stream().anyMatch(o -> o.getStatus() != OrderStatus.CANCELLED)) {
  69. throw new BusinessException("已有订单不可兑换");
  70. }
  71. Collection collection = collectionRepo.findById(asset.getPublicCollectionId())
  72. .orElseThrow(new BusinessException("无展示记录"));
  73. List<Long> collectionIds = collectionRepo.findAllByAssetId(collection.getAssetId());
  74. if (CollectionUtils.isNotEmpty(collectionIds)) {
  75. log.info("删除collection {}", collectionIds);
  76. collectionRepo.deleteAllByIdIn(collectionIds);
  77. } else {
  78. log.info("删除collection {}", collection.getId());
  79. collectionRepo.delete(collection);
  80. }
  81. }
  82. }
  83. });
  84. //收集记录
  85. ActivityOrder order = activityOrderRepo.save(ActivityOrder.builder()
  86. .userId(user.getId())
  87. .phone(user.getPhone())
  88. .activityCollection(activity.getCollectionName())
  89. .activityCollectionId(mintActivityId)
  90. .build());
  91. //兑换详情
  92. consumption.forEach(asset -> {
  93. activityMaterialRepo.save(
  94. ActivityMaterial.builder()
  95. .orderId(order.getId())
  96. .assetId(asset.getId())
  97. .category(asset.getCategory())
  98. .collectionId(asset.getCollectionId())
  99. .name(asset.getName())
  100. .number(asset.getNumber())
  101. .pic(asset.getPic())
  102. .build());
  103. // 转赠
  104. assetService.transfer(asset, asset.getPrice(), newOwner, "转赠", null);
  105. });
  106. //发放新的
  107. return assetService.createAsset(award, user, order.getId(), null, "兑换",
  108. award.getTotal() > 1 ? collectionService.getNextNumber(award.getId()) : null);
  109. } catch (Exception e) {
  110. // 错了加库存
  111. activityCollectionService.increaseStock(mintActivityId, 1);
  112. throw e;
  113. }
  114. }
  115. }