CollectionServiceTest.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package com.izouma.nineth.service;
  2. import com.izouma.nineth.ApplicationTests;
  3. import com.izouma.nineth.domain.*;
  4. import com.izouma.nineth.domain.Collection;
  5. import com.izouma.nineth.dto.CreateBlindBox;
  6. import com.izouma.nineth.enums.CollectionSource;
  7. import com.izouma.nineth.enums.CollectionType;
  8. import com.izouma.nineth.repo.CollectionRepo;
  9. import com.izouma.nineth.repo.PrivilegeOptionRepo;
  10. import com.izouma.nineth.repo.UserRepo;
  11. import com.izouma.nineth.service.storage.StorageService;
  12. import org.apache.commons.io.FileUtils;
  13. import org.apache.commons.io.FilenameUtils;
  14. import org.apache.commons.lang3.RandomStringUtils;
  15. import org.junit.jupiter.api.Test;
  16. import org.springframework.beans.BeanUtils;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import java.io.File;
  19. import java.io.FileInputStream;
  20. import java.io.IOException;
  21. import java.math.BigDecimal;
  22. import java.nio.file.Files;
  23. import java.text.SimpleDateFormat;
  24. import java.time.LocalDateTime;
  25. import java.util.*;
  26. import java.util.concurrent.atomic.AtomicInteger;
  27. import java.util.regex.Pattern;
  28. import java.util.stream.Collectors;
  29. class CollectionServiceTest extends ApplicationTests {
  30. @Autowired
  31. private CollectionService collectionService;
  32. @Autowired
  33. private CollectionRepo collectionRepo;
  34. @Autowired
  35. private StorageService storageService;
  36. @Autowired
  37. private UserRepo userRepo;
  38. @Autowired
  39. private PrivilegeOptionRepo privilegeOptionRepo;
  40. @Autowired
  41. private SysConfigService sysConfigService;
  42. @Test
  43. void toDTO() {
  44. Collection collection = collectionRepo.findById(951L).get();
  45. assert collection.getPic() != null;
  46. }
  47. @Test
  48. public void batchUpload() throws IOException {
  49. File imgDir = new File("/Users/drew/Downloads/images");
  50. AtomicInteger num = new AtomicInteger(1);
  51. User minter = userRepo.findById(53099L).get();
  52. List<PrivilegeOption> privilegeOptions = privilegeOptionRepo.findAll();
  53. List<Collection> collections = new ArrayList<>();
  54. List<BlindBoxItem> items = new ArrayList<>();
  55. Arrays.stream(imgDir.list((dir, name) -> Pattern.matches(".*\\.png", name))).parallel().forEach(file -> {
  56. try {
  57. File img = new File(imgDir, file);
  58. Collection collection = Collection.builder()
  59. .name(String.format("OASIS尼尔斯 #%03d", num.getAndIncrement()))
  60. .pic(Collections.singletonList(uploadFile(img)))
  61. .minter(minter.getNickname())
  62. .minterAvatar(minter.getAvatar())
  63. .minterId(minter.getId())
  64. .detail(FileUtils.readFileToString(new File("/Users/drew/Desktop/detail.txt"), "UTF-8"))
  65. .type(CollectionType.DEFAULT)
  66. .properties(new ArrayList<>())
  67. .privileges(privilegeOptions.stream().filter(p -> p.getName().equals("悄悄话")).map(p -> {
  68. Privilege p1 = new Privilege();
  69. BeanUtils.copyProperties(p, p1);
  70. p1.setDetail("绿洲元宇宙在现实世界入口位置是东经121.911305,北纬30.919856");
  71. return p1;
  72. }).collect(Collectors.toList()))
  73. .canResale(true)
  74. .royalties(10)
  75. .serviceCharge(5)
  76. .category("收藏品")
  77. .source(CollectionSource.OFFICIAL)
  78. .sale(0)
  79. .stock(1)
  80. .total(1)
  81. .likes(0)
  82. .onShelf(false)
  83. .salable(false)
  84. .price(new BigDecimal("59.9"))
  85. .originalPrice(new BigDecimal("59.9"))
  86. .build();
  87. collectionRepo.save(collection);
  88. collections.add(collection);
  89. items.add(BlindBoxItem
  90. .builder()
  91. .collectionId(collection.getId())
  92. .total(1)
  93. .stock(1)
  94. .rare(file.startsWith("qie_"))
  95. .build());
  96. } catch (Exception e) {
  97. }
  98. });
  99. Collection blindBox = Collection.builder()
  100. .name("1M001:绿洲海洋尼尔斯限定盲盒")
  101. .pic(Collections.singletonList(uploadFile(new File("/Users/drew/Downloads/images/BGbule1_BGTX1_T1_L1_YF7_MZ2_YJ0.png"))))
  102. .minter(minter.getNickname())
  103. .minterAvatar(minter.getAvatar())
  104. .minterId(minter.getId())
  105. .detail(FileUtils.readFileToString(new File("/Users/drew/Desktop/detail.txt"), "UTF-8"))
  106. .type(CollectionType.BLIND_BOX)
  107. .properties(new ArrayList<>())
  108. .privileges(privilegeOptions.stream().filter(p -> p.getName().equals("悄悄话")).map(p -> {
  109. Privilege p1 = new Privilege();
  110. BeanUtils.copyProperties(p, p1);
  111. p1.setDetail("绿洲元宇宙在现实世界入口位置是东经121.911305,北纬30.919856");
  112. return p1;
  113. }).collect(Collectors.toList()))
  114. .canResale(true)
  115. .royalties(10)
  116. .serviceCharge(5)
  117. .category("收藏品")
  118. .source(CollectionSource.OFFICIAL)
  119. .sale(0)
  120. .stock(1)
  121. .total(1)
  122. .likes(0)
  123. .onShelf(true)
  124. .salable(false)
  125. .price(new BigDecimal("59.9"))
  126. .originalPrice(new BigDecimal("59.9"))
  127. .scheduleSale(true)
  128. .startTime(LocalDateTime.of(2021, 12, 23, 20, 0, 0))
  129. .build();
  130. collectionService.createBlindBox(new CreateBlindBox(blindBox, items));
  131. }
  132. public FileObject uploadFile(File file) throws IOException {
  133. String ext = Optional.of(FilenameUtils.getExtension(file.getName())).orElse("")
  134. .toLowerCase().replace("jpeg", "jpg");
  135. String basePath = Optional.ofNullable(Files.probeContentType(file.toPath())).orElse("application")
  136. .split("/")[0];
  137. String path = basePath + "/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
  138. + RandomStringUtils.randomAlphabetic(8)
  139. + "." + ext;
  140. return new FileObject("", storageService.uploadFromInputStream(new FileInputStream(file), path), null, ext);
  141. }
  142. }