AssetService.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package com.izouma.nineth.service;
  2. import com.github.kevinsawicki.http.HttpRequest;
  3. import com.izouma.nineth.domain.*;
  4. import com.izouma.nineth.dto.NFT;
  5. import com.izouma.nineth.dto.NFTAccount;
  6. import com.izouma.nineth.dto.PageQuery;
  7. import com.izouma.nineth.enums.AssetStatus;
  8. import com.izouma.nineth.enums.CollectionSource;
  9. import com.izouma.nineth.enums.CollectionType;
  10. import com.izouma.nineth.event.CreateAssetEvent;
  11. import com.izouma.nineth.exception.BusinessException;
  12. import com.izouma.nineth.repo.AssetRepo;
  13. import com.izouma.nineth.repo.CollectionRepo;
  14. import com.izouma.nineth.repo.UserRepo;
  15. import com.izouma.nineth.utils.JpaUtils;
  16. import io.ipfs.api.IPFS;
  17. import io.ipfs.api.MerkleNode;
  18. import io.ipfs.api.NamedStreamable;
  19. import io.ipfs.multihash.Multihash;
  20. import lombok.AllArgsConstructor;
  21. import lombok.extern.slf4j.Slf4j;
  22. import org.apache.commons.lang3.StringUtils;
  23. import org.springframework.context.ApplicationContext;
  24. import org.springframework.data.domain.Page;
  25. import org.springframework.scheduling.annotation.Async;
  26. import org.springframework.stereotype.Service;
  27. import java.io.File;
  28. import java.math.BigDecimal;
  29. @Service
  30. @AllArgsConstructor
  31. @Slf4j
  32. public class AssetService {
  33. private AssetRepo assetRepo;
  34. private UserRepo userRepo;
  35. private NFTService nftService;
  36. private CollectionRepo collectionRepo;
  37. private ApplicationContext applicationContext;
  38. public Page<Asset> all(PageQuery pageQuery) {
  39. return assetRepo.findAll(JpaUtils.toSpecification(pageQuery, Asset.class), JpaUtils.toPageRequest(pageQuery));
  40. }
  41. @Async
  42. public void createAsset(Order order) {
  43. User user = userRepo.findById(order.getUserId()).orElseThrow(new BusinessException("用户不存在"));
  44. if (StringUtils.isEmpty(user.getPublicKey())) {
  45. NFTAccount account = nftService.createAccount(user.getUsername());
  46. user.setNftAccount(account.getAccountId());
  47. user.setKmsId(account.getAccountKmsId());
  48. user.setPublicKey(account.getPublicKey());
  49. userRepo.save(user);
  50. }
  51. try {
  52. NFT nft = nftService.createToken(user.getNftAccount());
  53. String ipfsUrl = ipfsUpload(order.getPic().get(0));
  54. if (nft != null) {
  55. Asset asset = Asset.builder()
  56. .userId(user.getId())
  57. .orderId(order.getId())
  58. .collectionId(order.getCollectionId())
  59. .minter(order.getMinter())
  60. .minterId(order.getMinterId())
  61. .minterAvatar(order.getMinterAvatar())
  62. .name(order.getName())
  63. .detail(order.getDetail())
  64. .pic(order.getPic())
  65. .properties(order.getProperties())
  66. .category(order.getCategory())
  67. .canResale(order.isCanResale())
  68. .royalties(order.getRoyalties())
  69. .serviceCharge(order.getServiceCharge())
  70. .tokenId(nft.getTokenId())
  71. .blockNumber(nft.getBlockNumber())
  72. .txHash(nft.getTxHash())
  73. .gasUsed(nft.getGasUsed())
  74. .price(order.getPrice())
  75. .status(AssetStatus.NORMAL)
  76. .ipfsUrl(ipfsUrl)
  77. .number((int) (assetRepo.countByIpfsUrlAndStatusNot(ipfsUrl, AssetStatus.TRANSFERRED) + 1))
  78. .build();
  79. assetRepo.save(asset);
  80. applicationContext.publishEvent(new CreateAssetEvent(this, true, order, asset));
  81. return;
  82. }
  83. } catch (Exception e) {
  84. log.error("创建token失败", e);
  85. }
  86. applicationContext.publishEvent(new CreateAssetEvent(this, false, order, null));
  87. }
  88. @Async
  89. public void createAsset(Order order, BlindBoxItem winItem) {
  90. User user = userRepo.findById(order.getUserId()).orElseThrow(new BusinessException("用户不存在"));
  91. if (StringUtils.isEmpty(user.getPublicKey())) {
  92. NFTAccount account = nftService.createAccount(user.getUsername());
  93. user.setNftAccount(account.getAccountId());
  94. user.setKmsId(account.getAccountKmsId());
  95. user.setPublicKey(account.getPublicKey());
  96. userRepo.save(user);
  97. }
  98. try {
  99. NFT nft = nftService.createToken(user.getNftAccount());
  100. if (nft != null) {
  101. Asset asset = Asset.builder()
  102. .userId(user.getId())
  103. .orderId(order.getId())
  104. .collectionId(order.getCollectionId())
  105. .minter(winItem.getMinter())
  106. .minterId(winItem.getMinterId())
  107. .minterAvatar(winItem.getMinterAvatar())
  108. .name(winItem.getName())
  109. .detail(winItem.getDetail())
  110. .pic(winItem.getPics())
  111. .properties(winItem.getProperties())
  112. .canResale(winItem.isCanResale())
  113. .royalties(winItem.getRoyalties())
  114. .serviceCharge(winItem.getServiceCharge())
  115. .tokenId(nft.getTokenId())
  116. .blockNumber(nft.getBlockNumber())
  117. .txHash(nft.getTxHash())
  118. .gasUsed(nft.getGasUsed())
  119. .price(order.getPrice())
  120. .status(AssetStatus.NORMAL)
  121. .ipfsUrl(ipfsUpload(winItem.getPics().get(0)))
  122. .build();
  123. assetRepo.save(asset);
  124. applicationContext.publishEvent(new CreateAssetEvent(this, true, order, asset));
  125. return;
  126. }
  127. } catch (Exception e) {
  128. log.error("创建token失败", e);
  129. }
  130. applicationContext.publishEvent(new CreateAssetEvent(this, false, order, null));
  131. }
  132. public String ipfsUpload(String url) {
  133. try {
  134. IPFS ipfs = new IPFS("121.40.132.44", 5001);
  135. HttpRequest request = HttpRequest.get(url);
  136. File file = File.createTempFile("ipfs", ".tmp");
  137. request.receive(file);
  138. NamedStreamable.FileWrapper file1 = new NamedStreamable.FileWrapper(file);
  139. MerkleNode put = ipfs.add(file1).get(0);
  140. Multihash multihash = ipfs.pin.add(put.hash).get(0);
  141. log.info("上传ipfs成功 {}", multihash.toBase58());
  142. return multihash.toBase58();
  143. } catch (Exception e) {
  144. }
  145. return null;
  146. }
  147. public void publicShow(Long id) {
  148. Asset asset = assetRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  149. if (asset.isPublicShow()) {
  150. return;
  151. }
  152. User owner = userRepo.findById(asset.getUserId()).orElseThrow(new BusinessException("用户不存在"));
  153. Collection collection = Collection.builder()
  154. .name(asset.getName())
  155. .pics(asset.getPic())
  156. .minter(asset.getMinter())
  157. .minterId(asset.getMinterId())
  158. .minterAvatar(asset.getMinterAvatar())
  159. .owner(owner.getNickname())
  160. .ownerId(owner.getId())
  161. .ownerAvatar(owner.getAvatar())
  162. .detail(asset.getDetail())
  163. .type(CollectionType.DEFAULT)
  164. .source(CollectionSource.TRANSFER)
  165. .sale(0)
  166. .stock(1)
  167. .total(1)
  168. .onShelf(true)
  169. .salable(false)
  170. .price(BigDecimal.valueOf(0))
  171. .properties(asset.getProperties())
  172. .canResale(asset.isCanResale())
  173. .royalties(asset.getRoyalties())
  174. .serviceCharge(asset.getServiceCharge())
  175. .build();
  176. collectionRepo.save(collection);
  177. asset.setPublicShow(true);
  178. asset.setPublicCollectionId(collection.getId());
  179. }
  180. public void cancelPublic(Long id) {
  181. Asset asset = assetRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  182. if (!asset.isPublicShow()) {
  183. return;
  184. }
  185. Collection collection = collectionRepo.findById(asset.getPublicCollectionId())
  186. .orElseThrow(new BusinessException("无展示记录"));
  187. }
  188. }