| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- package com.izouma.nineth.service;
- import com.izouma.nineth.TokenHistory;
- import com.izouma.nineth.domain.*;
- import com.izouma.nineth.dto.PageQuery;
- import com.izouma.nineth.enums.AirDropType;
- import com.izouma.nineth.enums.CollectionType;
- import com.izouma.nineth.exception.BusinessException;
- import com.izouma.nineth.repo.*;
- import com.izouma.nineth.utils.JpaUtils;
- import lombok.AllArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang3.ObjectUtils;
- import org.springframework.beans.BeanUtils;
- import org.springframework.data.domain.Page;
- import org.springframework.stereotype.Service;
- import java.time.LocalDateTime;
- import java.util.List;
- @Service
- @AllArgsConstructor
- @Slf4j
- public class AirDropService {
- private AirDropRepo airDropRepo;
- private CouponRepo couponRepo;
- private UserCouponRepo userCouponRepo;
- private CollectionRepo collectionRepo;
- private UserRepo userRepo;
- private AssetService assetService;
- private CollectionService collectionService;
- private ShowroomService showroomService;
- private TokenHistoryRepo tokenHistoryRepo;
- private AssetRepo assetRepo;
- private CollectionPrivilegeRepo collectionPrivilegeRepo;
- public Page<AirDrop> all(PageQuery pageQuery) {
- return airDropRepo.findAll(JpaUtils.toSpecification(pageQuery, AirDrop.class), JpaUtils.toPageRequest(pageQuery));
- }
- public AirDrop create(AirDrop record) {
- if (AirDropType.coupon == record.getType()) {
- Coupon coupon = couponRepo.findById(record.getCouponId()).orElseThrow(new BusinessException("兑换券不存在"));
- record.getUserIds().stream().parallel().forEach(userId -> {
- UserCoupon userCoupon = new UserCoupon();
- BeanUtils.copyProperties(coupon, userCoupon);
- userCoupon.setId(null);
- userCoupon.setCouponId(coupon.getId());
- userCoupon.setUserId(userId);
- userCouponRepo.save(userCoupon);
- });
- } else {
- Collection collection = collectionRepo.findById(record.getCollectionId())
- .orElseThrow(new BusinessException("藏品不存在"));
- if (collection.isSalable()) {
- throw new BusinessException("请先设置藏品为不可购买");
- }
- if (!record.isIgnoreStockCheck() && collection.getStock() < record.getUserIds().size()) {
- throw new BusinessException("藏品库存不足");
- }
- List<User> users = userRepo.findByIdInAndDelFalse(record.getUserIds());
- for (User user : users) {
- try {
- if (collection.getType() == CollectionType.BLIND_BOX) {
- BlindBoxItem winItem = collectionService.draw(collection.getId());
- if (record.isSimulateOrder()) {
- assetService.createAsset(winItem, user, 0L, collection.getPrice(), "出售",
- winItem.getTotal() > 1 ? collectionService.getNextNumber(winItem.getCollectionId()) : null,
- collection.getHoldDays());
- } else {
- //查看有无vip权限
- CollectionPrivilege collectionPrivilege = collectionPrivilegeRepo.findByCollectionId(record.getCollectionId());
- if (ObjectUtils.isNotEmpty(collectionPrivilege)) {
- if (collectionPrivilege.isVip()) {
- //更新vip信息
- userRepo.updateVipPurchase(user.getId(), 1);
- }
- }
- assetService.createAsset(winItem, user, null, null, "空投",
- collectionService.getNextNumber(winItem.getCollectionId()), collection.getHoldDays());
- }
- } else {
- if (record.isSimulateOrder()) {
- assetService.createAsset(collection, user, 0L, collection.getPrice(), "出售",
- collection.getTotal() > 1 ? collectionService.getNextNumber(collection.getId()) : null);
- } else {
- //查看有无vip权限
- CollectionPrivilege collectionPrivilege = collectionPrivilegeRepo.findByCollectionId(record.getCollectionId());
- if (ObjectUtils.isNotEmpty(collectionPrivilege)) {
- if (collectionPrivilege.isVip()) {
- //更新vip信息
- userRepo.updateVipPurchase(user.getId(), 1);
- }
- }
- Asset asset = assetService.createAsset(collection, user, null, null, "空投", collectionService.getNextNumber(collection.getId()));
- //创建展厅
- if (collection.getType() == CollectionType.SHOWROOM) {
- showroomService.save(asset);
- }
- }
- // Asset asset = assetService.createAsset(collection, user, null, null, "空投", collectionService.getNextNumber(collection.getId()));
- }
- collectionService.decreaseStock(collection.getId(), 1);
- collectionService.increaseSale(collection.getId(), 1);
- } catch (Exception e) {
- log.error("空投出错", e);
- }
- }
- }
- return airDropRepo.save(record);
- }
- public void drop(Long collectionId, Long userId, int num, LocalDateTime time) {
- Collection collection = collectionRepo.findById(collectionId)
- .orElseThrow(new BusinessException("藏品不存在"));
- User user = userRepo.findById(userId).orElseThrow(new BusinessException("用户不存在"));
- try {
- for (int i = 0; i < num; i++) {
- Asset asset;
- if (collection.getType() == CollectionType.BLIND_BOX) {
- BlindBoxItem winItem = collectionService.draw(collection.getId());
- asset = assetService.createAsset(winItem, user, 0L, collection.getPrice(), "出售",
- winItem.getTotal() > 1 ? collectionService.getNextNumber(winItem.getCollectionId()) : null,
- collection.getHoldDays());
- } else {
- asset = assetService.createAsset(collection, user, 0L, collection.getPrice(), "出售",
- collection.getTotal() > 1 ? collectionService.getNextNumber(collection.getId()) : null);
- }
- assetRepo.flush();
- tokenHistoryRepo.flush();
- asset.setCreatedAt(time.plusSeconds((long) (Math.random() * 120)));
- assetRepo.save(asset);
- for (TokenHistory tokenHistory : tokenHistoryRepo.findByTokenIdOrderByCreatedAtDesc(asset.getTokenId())) {
- tokenHistory.setCreatedAt(asset.getCreatedAt());
- tokenHistoryRepo.save(tokenHistory);
- }
- }
- } catch (Exception e) {
- log.error("空投出错", e);
- }
- }
- public void drop(List<Collection> collections, String phone, LocalDateTime time) {
- // List<Collection> collections = collectionRepo.findAllById(collectionId);
- User user = userRepo.findByPhoneAndDelFalse(phone).orElseThrow(new BusinessException("用户不存在"));
- try {
- for (Collection collection : collections) {
- Asset asset;
- if (collection.getType() == CollectionType.BLIND_BOX) {
- BlindBoxItem winItem = collectionService.draw(collection.getId());
- asset = assetService.createAsset(winItem, user, 0L, collection.getPrice(), "出售",
- winItem.getTotal() > 1 ? collectionService.getNextNumber(winItem.getCollectionId()) : null,
- collection.getHoldDays());
- } else {
- asset = assetService.createAsset(collection, user, 0L, collection.getPrice(), "出售",
- collection.getTotal() > 1 ? collectionService.getNextNumber(collection.getId()) : null);
- }
- assetRepo.flush();
- tokenHistoryRepo.flush();
- asset.setCreatedAt(time.plusSeconds((long) (Math.random() * 120)));
- assetRepo.save(asset);
- for (TokenHistory tokenHistory : tokenHistoryRepo.findByTokenIdOrderByCreatedAtDesc(asset.getTokenId())) {
- tokenHistory.setCreatedAt(asset.getCreatedAt());
- tokenHistoryRepo.save(tokenHistory);
- }
- }
- } catch (Exception e) {
- log.error("空投出错", e);
- }
- }
- }
|