|
|
@@ -2,8 +2,8 @@ package com.izouma.nineth.service;
|
|
|
|
|
|
import com.izouma.nineth.TokenHistory;
|
|
|
import com.izouma.nineth.config.GeneralProperties;
|
|
|
-import com.izouma.nineth.domain.*;
|
|
|
import com.izouma.nineth.domain.Collection;
|
|
|
+import com.izouma.nineth.domain.*;
|
|
|
import com.izouma.nineth.dto.PageQuery;
|
|
|
import com.izouma.nineth.dto.UserHistory;
|
|
|
import com.izouma.nineth.enums.AssetStatus;
|
|
|
@@ -20,8 +20,6 @@ import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
-import org.apache.rocketmq.client.producer.DefaultMQProducer;
|
|
|
-import org.apache.rocketmq.common.message.Message;
|
|
|
import org.apache.rocketmq.spring.core.RocketMQTemplate;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
@@ -30,12 +28,11 @@ import org.springframework.data.domain.Pageable;
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import javax.persistence.criteria.Predicate;
|
|
|
import java.math.BigDecimal;
|
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.temporal.ChronoUnit;
|
|
|
import java.util.*;
|
|
|
-import java.util.function.Predicate;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
|
@@ -379,6 +376,45 @@ public class AssetService {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ public Page<UserHistory> userHistory(Long userId, PageQuery pageQuery) {
|
|
|
+ Page<TokenHistory> page = tokenHistoryRepo.findAll(((root, criteriaQuery, criteriaBuilder) -> {
|
|
|
+ List<Predicate> and = JpaUtils.toPredicates(pageQuery, TokenHistory.class, root, criteriaQuery, criteriaBuilder);
|
|
|
+ Map<String, Object> query = pageQuery.getQuery();
|
|
|
+ if (ObjectUtils.isEmpty(query.get("toUserId"))) {
|
|
|
+ and.add(criteriaBuilder.and(criteriaBuilder.equal(root.get("toUserId"), userId)));
|
|
|
+ }
|
|
|
+ if (ObjectUtils.isEmpty(query.get("fromUserId"))) {
|
|
|
+ and.add(criteriaBuilder.and(criteriaBuilder.equal(root.get("fromUserId"), userId)));
|
|
|
+ }
|
|
|
+ return criteriaBuilder.and(and.toArray(new Predicate[0]));
|
|
|
+ }),JpaUtils.toPageRequest(pageQuery));
|
|
|
+
|
|
|
+ Set<String> tokenIds = page.stream().map(TokenHistory::getTokenId).collect(Collectors.toSet());
|
|
|
+ List<Asset> assets = tokenIds.isEmpty() ? new ArrayList<>() : assetRepo.findByTokenIdIn(tokenIds);
|
|
|
+
|
|
|
+ return page.map(tokenHistory -> {
|
|
|
+ UserHistory userHistory = new UserHistory();
|
|
|
+ BeanUtils.copyProperties(tokenHistory, userHistory);
|
|
|
+ Optional<Asset> asset = assets.stream().filter(a -> a.getTokenId().equals(tokenHistory.getTokenId()))
|
|
|
+ .findAny();
|
|
|
+ userHistory.setAssetName(asset.map(Asset::getName).orElse(null));
|
|
|
+ userHistory.setPic(asset.map(Asset::getPic).orElse(new ArrayList<>()));
|
|
|
+ switch (tokenHistory.getOperation()) {
|
|
|
+ case "出售":
|
|
|
+ case "转让":
|
|
|
+ userHistory.setDescription(tokenHistory.getToUserId().equals(userId) ? "作品交易——买入" : "作品交易——售出");
|
|
|
+ break;
|
|
|
+ case "空投":
|
|
|
+ userHistory.setDescription("空投");
|
|
|
+ break;
|
|
|
+ case "转赠":
|
|
|
+ userHistory.setDescription(tokenHistory.getToUserId().equals(userId) ? "他人赠送" : "作品赠送");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return userHistory;
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
public String mint() {
|
|
|
for (Asset asset : assetRepo.findByTxHashIsNullAndTokenIdNotNullAndCreatedAtBefore(LocalDateTime.now())) {
|
|
|
rocketMQTemplate.syncSend(generalProperties.getMintTopic(), asset.getId());
|