|
@@ -1,6 +1,7 @@
|
|
|
package com.izouma.nineth.service;
|
|
package com.izouma.nineth.service;
|
|
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
import com.google.common.collect.Lists;
|
|
|
|
|
+import com.google.common.hash.Hashing;
|
|
|
import com.izouma.nineth.TokenHistory;
|
|
import com.izouma.nineth.TokenHistory;
|
|
|
import com.izouma.nineth.config.Constants;
|
|
import com.izouma.nineth.config.Constants;
|
|
|
import com.izouma.nineth.config.GeneralProperties;
|
|
import com.izouma.nineth.config.GeneralProperties;
|
|
@@ -35,8 +36,10 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import javax.persistence.criteria.Predicate;
|
|
import javax.persistence.criteria.Predicate;
|
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.time.Duration;
|
|
import java.time.Duration;
|
|
|
import java.time.LocalDateTime;
|
|
import java.time.LocalDateTime;
|
|
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
import java.time.temporal.ChronoUnit;
|
|
import java.time.temporal.ChronoUnit;
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
import java.util.concurrent.ExecutionException;
|
|
import java.util.concurrent.ExecutionException;
|
|
@@ -70,6 +73,7 @@ public class AssetService {
|
|
|
private AssetLockRepo assetLockRepo;
|
|
private AssetLockRepo assetLockRepo;
|
|
|
private UserBalanceService userBalanceService;
|
|
private UserBalanceService userBalanceService;
|
|
|
private PhotoAssetRepo photoAssetRepo;
|
|
private PhotoAssetRepo photoAssetRepo;
|
|
|
|
|
+ private NumberSeqRepo numberSeqRepo;
|
|
|
|
|
|
|
|
public Page<Asset> all(PageQuery pageQuery) {
|
|
public Page<Asset> all(PageQuery pageQuery) {
|
|
|
|
|
|
|
@@ -149,6 +153,9 @@ public class AssetService {
|
|
|
|
|
|
|
|
public Asset createAsset(Collection collection, User user, Long orderId, BigDecimal price, String type,
|
|
public Asset createAsset(Collection collection, User user, Long orderId, BigDecimal price, String type,
|
|
|
Integer number, boolean safeFlag) {
|
|
Integer number, boolean safeFlag) {
|
|
|
|
|
+ if (collection.isMessNumber() && number != null) {
|
|
|
|
|
+ number = getMessedNumber(collection.getId(), number, collection.getTotal());
|
|
|
|
|
+ }
|
|
|
Asset asset = Asset.create(collection, user);
|
|
Asset asset = Asset.create(collection, user);
|
|
|
asset.setTokenId(TokenUtils.genTokenId());
|
|
asset.setTokenId(TokenUtils.genTokenId());
|
|
|
asset.setNumber(number);
|
|
asset.setNumber(number);
|
|
@@ -247,12 +254,34 @@ public class AssetService {
|
|
|
return asset;
|
|
return asset;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public synchronized int getMessedNumber(Long collectionId, int number, int total) {
|
|
|
|
|
+ NumberSeq numberSeq = numberSeqRepo.findById(collectionId).orElse(null);
|
|
|
|
|
+ if (numberSeq == null || numberSeq.getTotal() != total) {
|
|
|
|
|
+ numberSeq = new NumberSeq();
|
|
|
|
|
+ numberSeq.setId(collectionId);
|
|
|
|
|
+ numberSeq.setTotal(total);
|
|
|
|
|
+ Map<Integer, Integer> map = new HashMap<>();
|
|
|
|
|
+ for (int i = 0; i < total; i++) {
|
|
|
|
|
+ map.put(i, Hashing.md5().hashString(collectionId + ":" + i, StandardCharsets.UTF_8).asInt());
|
|
|
|
|
+ }
|
|
|
|
|
+ numberSeq.setNumbers(map.entrySet().stream()
|
|
|
|
|
+ .sorted(Map.Entry.comparingByValue())
|
|
|
|
|
+ .map(Map.Entry::getKey)
|
|
|
|
|
+ .collect(Collectors.toList()));
|
|
|
|
|
+ numberSeqRepo.save(numberSeq);
|
|
|
|
|
+ }
|
|
|
|
|
+ return numberSeq.getNumbers().get(number);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public Asset createAsset(BlindBoxItem winItem, User user, Long orderId, BigDecimal price, String type,
|
|
public Asset createAsset(BlindBoxItem winItem, User user, Long orderId, BigDecimal price, String type,
|
|
|
Integer number, Integer holdDays, boolean safeFlag) {
|
|
Integer number, Integer holdDays, boolean safeFlag) {
|
|
|
Collection blindBox = collectionRepo.findDetailById(winItem.getBlindBoxId())
|
|
Collection blindBox = collectionRepo.findDetailById(winItem.getBlindBoxId())
|
|
|
.orElseThrow(new BusinessException("盲盒不存在"));
|
|
.orElseThrow(new BusinessException("盲盒不存在"));
|
|
|
Collection collection = collectionRepo.findDetailById(winItem.getCollectionId())
|
|
Collection collection = collectionRepo.findDetailById(winItem.getCollectionId())
|
|
|
.orElseThrow(new BusinessException("藏品不存在"));
|
|
.orElseThrow(new BusinessException("藏品不存在"));
|
|
|
|
|
+ if (collection.isMessNumber() && number != null) {
|
|
|
|
|
+ number = getMessedNumber(collection.getId(), number, collection.getTotal());
|
|
|
|
|
+ }
|
|
|
winItem.setCollection(collection);
|
|
winItem.setCollection(collection);
|
|
|
Asset asset = Asset.create(winItem, user, holdDays);
|
|
Asset asset = Asset.create(winItem, user, holdDays);
|
|
|
asset.setTokenId(TokenUtils.genTokenId());
|
|
asset.setTokenId(TokenUtils.genTokenId());
|
|
@@ -372,7 +401,15 @@ public class AssetService {
|
|
|
|
|
|
|
|
int holdDays;
|
|
int holdDays;
|
|
|
if (asset.getSource() == AssetSource.GIFT) {
|
|
if (asset.getSource() == AssetSource.GIFT) {
|
|
|
- holdDays = sysConfigService.getInt("gift_days");
|
|
|
|
|
|
|
+ LocalDateTime localDateTime = asset.getCreatedAt();
|
|
|
|
|
+ LocalDateTime gift_change_time = LocalDateTime
|
|
|
|
|
+ .parse(sysConfigService.getString("gift_change_time"), DateTimeFormatter
|
|
|
|
|
+ .ofPattern("yyyy-MM-dd HH:mm:ss"));
|
|
|
|
|
+ if (localDateTime.compareTo(gift_change_time) < 0) {
|
|
|
|
|
+ holdDays = 20;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ holdDays = sysConfigService.getInt("gift_days");
|
|
|
|
|
+ }
|
|
|
} else {
|
|
} else {
|
|
|
if (ObjectUtils.isEmpty(asset.getHoldDays())) {
|
|
if (ObjectUtils.isEmpty(asset.getHoldDays())) {
|
|
|
holdDays = sysConfigService.getInt("hold_days");
|
|
holdDays = sysConfigService.getInt("hold_days");
|
|
@@ -614,6 +651,10 @@ public class AssetService {
|
|
|
newAsset.setTags(new HashSet<>(asset.getTags()));
|
|
newAsset.setTags(new HashSet<>(asset.getTags()));
|
|
|
newAsset.setSafeFlag(safeFlag);
|
|
newAsset.setSafeFlag(safeFlag);
|
|
|
newAsset.setHoldDays(asset.getOldHoldDays());
|
|
newAsset.setHoldDays(asset.getOldHoldDays());
|
|
|
|
|
+ if (asset.getType().equals(CollectionType.PICTURE)) {
|
|
|
|
|
+ newAsset.setType(CollectionType.PICTURE);
|
|
|
|
|
+ newAsset.setHoldDays(0);
|
|
|
|
|
+ }
|
|
|
Long savedId = assetRepo.saveAndFlush(newAsset).getId();
|
|
Long savedId = assetRepo.saveAndFlush(newAsset).getId();
|
|
|
|
|
|
|
|
if (asset.getType().equals(CollectionType.PICTURE)) {
|
|
if (asset.getType().equals(CollectionType.PICTURE)) {
|
|
@@ -878,16 +919,16 @@ public class AssetService {
|
|
|
if (tags.isEmpty()) return new PageImpl<>(Collections.emptyList());
|
|
if (tags.isEmpty()) return new PageImpl<>(Collections.emptyList());
|
|
|
return assetRepo.findAll((Specification<Asset>) (root, query, criteriaBuilder) ->
|
|
return assetRepo.findAll((Specification<Asset>) (root, query, criteriaBuilder) ->
|
|
|
query.distinct(true).where(
|
|
query.distinct(true).where(
|
|
|
- // where userId=some id
|
|
|
|
|
- criteriaBuilder.equal(root.get("userId"), userId),
|
|
|
|
|
- // and (lockTo is null or (lockTo is not null and lockTo < now))
|
|
|
|
|
- criteriaBuilder.or(criteriaBuilder.isNull(root.get("lockTo")),
|
|
|
|
|
- criteriaBuilder.and(criteriaBuilder.isNotNull(root.get("lockTo")),
|
|
|
|
|
- criteriaBuilder.lessThan(root.get("lockTo"), LocalDateTime.now()))),
|
|
|
|
|
- // and status = 'NORMAL'
|
|
|
|
|
- criteriaBuilder.equal(root.get("status"), AssetStatus.NORMAL),
|
|
|
|
|
- // and has some tagId
|
|
|
|
|
- root.join("tags").get("id").in(tags.stream().map(Tag::getId).toArray()))
|
|
|
|
|
|
|
+ // where userId=some id
|
|
|
|
|
+ criteriaBuilder.equal(root.get("userId"), userId),
|
|
|
|
|
+ // and (lockTo is null or (lockTo is not null and lockTo < now))
|
|
|
|
|
+ criteriaBuilder.or(criteriaBuilder.isNull(root.get("lockTo")),
|
|
|
|
|
+ criteriaBuilder.and(criteriaBuilder.isNotNull(root.get("lockTo")),
|
|
|
|
|
+ criteriaBuilder.lessThan(root.get("lockTo"), LocalDateTime.now()))),
|
|
|
|
|
+ // and status = 'NORMAL'
|
|
|
|
|
+ criteriaBuilder.equal(root.get("status"), AssetStatus.NORMAL),
|
|
|
|
|
+ // and has some tagId
|
|
|
|
|
+ root.join("tags").get("id").in(tags.stream().map(Tag::getId).toArray()))
|
|
|
.getRestriction(), pageable);
|
|
.getRestriction(), pageable);
|
|
|
} else {
|
|
} else {
|
|
|
return assetRepo.findByUserIdAndStatusAndNameLike(userId, AssetStatus.NORMAL,
|
|
return assetRepo.findByUserIdAndStatusAndNameLike(userId, AssetStatus.NORMAL,
|