| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- package com.izouma.nineth.service;
- import com.izouma.nineth.ApplicationTests;
- import com.izouma.nineth.domain.*;
- import com.izouma.nineth.domain.Collection;
- import com.izouma.nineth.dto.CreateBlindBox;
- import com.izouma.nineth.enums.CollectionSource;
- import com.izouma.nineth.enums.CollectionType;
- import com.izouma.nineth.repo.CollectionRepo;
- import com.izouma.nineth.repo.PrivilegeOptionRepo;
- import com.izouma.nineth.repo.UserRepo;
- import com.izouma.nineth.service.storage.StorageService;
- import org.apache.commons.io.FileUtils;
- import org.apache.commons.io.FilenameUtils;
- import org.apache.commons.lang3.RandomStringUtils;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.BeanUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.math.BigDecimal;
- import java.nio.file.Files;
- import java.text.SimpleDateFormat;
- import java.time.LocalDateTime;
- import java.util.*;
- import java.util.concurrent.atomic.AtomicInteger;
- import java.util.regex.Pattern;
- import java.util.stream.Collectors;
- class CollectionServiceTest extends ApplicationTests {
- @Autowired
- private CollectionService collectionService;
- @Autowired
- private CollectionRepo collectionRepo;
- @Autowired
- private StorageService storageService;
- @Autowired
- private UserRepo userRepo;
- @Autowired
- private PrivilegeOptionRepo privilegeOptionRepo;
- @Autowired
- private SysConfigService sysConfigService;
- @Test
- void toDTO() {
- Collection collection = collectionRepo.findById(951L).get();
- assert collection.getPic() != null;
- }
- @Test
- public void batchUpload() throws IOException {
- File imgDir = new File("/Users/drew/Downloads/images");
- AtomicInteger num = new AtomicInteger(1);
- User minter = userRepo.findById(53099L).get();
- List<PrivilegeOption> privilegeOptions = privilegeOptionRepo.findAll();
- List<Collection> collections = new ArrayList<>();
- List<BlindBoxItem> items = new ArrayList<>();
- Arrays.stream(imgDir.list((dir, name) -> Pattern.matches(".*\\.png", name))).parallel().forEach(file -> {
- try {
- File img = new File(imgDir, file);
- Collection collection = Collection.builder()
- .name(String.format("OASIS尼尔斯 #%03d", num.getAndIncrement()))
- .pic(Collections.singletonList(uploadFile(img)))
- .minter(minter.getNickname())
- .minterAvatar(minter.getAvatar())
- .minterId(minter.getId())
- .detail(FileUtils.readFileToString(new File("/Users/drew/Desktop/detail.txt"), "UTF-8"))
- .type(CollectionType.DEFAULT)
- .properties(new ArrayList<>())
- .privileges(privilegeOptions.stream().filter(p -> p.getName().equals("悄悄话")).map(p -> {
- Privilege p1 = new Privilege();
- BeanUtils.copyProperties(p, p1);
- p1.setDetail("绿洲元宇宙在现实世界入口位置是东经121.911305,北纬30.919856");
- return p1;
- }).collect(Collectors.toList()))
- .canResale(true)
- .royalties(10)
- .serviceCharge(5)
- .category("收藏品")
- .source(CollectionSource.OFFICIAL)
- .sale(0)
- .stock(1)
- .total(1)
- .likes(0)
- .onShelf(false)
- .salable(false)
- .price(new BigDecimal("59.9"))
- .originalPrice(new BigDecimal("59.9"))
- .build();
- collectionRepo.save(collection);
- collections.add(collection);
- items.add(BlindBoxItem
- .builder()
- .collectionId(collection.getId())
- .total(1)
- .stock(1)
- .rare(file.startsWith("qie_"))
- .build());
- } catch (Exception e) {
- }
- });
- Collection blindBox = Collection.builder()
- .name("1M001:绿洲海洋尼尔斯限定盲盒")
- .pic(Collections.singletonList(uploadFile(new File("/Users/drew/Downloads/images/BGbule1_BGTX1_T1_L1_YF7_MZ2_YJ0.png"))))
- .minter(minter.getNickname())
- .minterAvatar(minter.getAvatar())
- .minterId(minter.getId())
- .detail(FileUtils.readFileToString(new File("/Users/drew/Desktop/detail.txt"), "UTF-8"))
- .type(CollectionType.BLIND_BOX)
- .properties(new ArrayList<>())
- .privileges(privilegeOptions.stream().filter(p -> p.getName().equals("悄悄话")).map(p -> {
- Privilege p1 = new Privilege();
- BeanUtils.copyProperties(p, p1);
- p1.setDetail("绿洲元宇宙在现实世界入口位置是东经121.911305,北纬30.919856");
- return p1;
- }).collect(Collectors.toList()))
- .canResale(true)
- .royalties(10)
- .serviceCharge(5)
- .category("收藏品")
- .source(CollectionSource.OFFICIAL)
- .sale(0)
- .stock(1)
- .total(1)
- .likes(0)
- .onShelf(true)
- .salable(false)
- .price(new BigDecimal("59.9"))
- .originalPrice(new BigDecimal("59.9"))
- .scheduleSale(true)
- .startTime(LocalDateTime.of(2021, 12, 23, 20, 0, 0))
- .build();
- collectionService.createBlindBox(new CreateBlindBox(blindBox, items));
- }
- public FileObject uploadFile(File file) throws IOException {
- String ext = Optional.of(FilenameUtils.getExtension(file.getName())).orElse("")
- .toLowerCase().replace("jpeg", "jpg");
- String basePath = Optional.ofNullable(Files.probeContentType(file.toPath())).orElse("application")
- .split("/")[0];
- String path = basePath + "/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
- + RandomStringUtils.randomAlphabetic(8)
- + "." + ext;
- return new FileObject("", storageService.uploadFromInputStream(new FileInputStream(file), path), null, ext);
- }
- }
|