xiongzhu 4 năm trước cách đây
mục cha
commit
27bc5b8dd8

+ 7 - 0
src/main/java/com/izouma/nineth/domain/Asset.java

@@ -1,6 +1,7 @@
 package com.izouma.nineth.domain;
 
 import com.izouma.nineth.annotations.Searchable;
+import com.izouma.nineth.converter.FileObjectConverter;
 import com.izouma.nineth.converter.FileObjectListConverter;
 import com.izouma.nineth.converter.PrivilegeListConverter;
 import com.izouma.nineth.converter.PropertyListConverter;
@@ -96,6 +97,10 @@ public class Asset extends BaseEntity {
     @Column(columnDefinition = "TEXT")
     private List<FileObject> pic;
 
+    @Column(columnDefinition = "TEXT")
+    @Convert(converter = FileObjectConverter.class)
+    private FileObject model3d;
+
     @ApiModelProperty("tokenId")
     private String tokenId;
 
@@ -154,6 +159,7 @@ public class Asset extends BaseEntity {
                 .name(collection.getName())
                 .detail(collection.getDetail())
                 .pic(collection.getPic())
+                .model3d(collection.getModel3d())
                 .properties(collection.getProperties())
                 .privileges(collection.getPrivileges())
                 .category(collection.getCategory())
@@ -178,6 +184,7 @@ public class Asset extends BaseEntity {
                 .name(item.getName())
                 .detail(item.getDetail())
                 .pic(item.getPic())
+                .model3d(item.getModel3d())
                 .properties(item.getProperties())
                 .privileges(item.getPrivileges())
                 .category(item.getCategory())

+ 4 - 0
src/main/java/com/izouma/nineth/domain/BlindBoxItem.java

@@ -36,6 +36,10 @@ public class BlindBoxItem extends BaseEntity {
     @Convert(converter = FileObjectListConverter.class)
     private List<FileObject> pic;
 
+    @Column(columnDefinition = "TEXT")
+    @Convert(converter = FileObjectListConverter.class)
+    private FileObject model3d;
+
     @ApiModelProperty("铸造者")
     @Searchable
     private String minter;

+ 2 - 1
src/main/java/com/izouma/nineth/domain/Collection.java

@@ -1,6 +1,7 @@
 package com.izouma.nineth.domain;
 
 import com.izouma.nineth.annotations.Searchable;
+import com.izouma.nineth.converter.FileObjectConverter;
 import com.izouma.nineth.converter.FileObjectListConverter;
 import com.izouma.nineth.converter.PrivilegeListConverter;
 import com.izouma.nineth.converter.PropertyListConverter;
@@ -38,7 +39,7 @@ public class Collection extends BaseEntity {
     private List<FileObject> pic;
 
     @Column(columnDefinition = "TEXT")
-    @Convert(converter = FileObjectListConverter.class)
+    @Convert(converter = FileObjectConverter.class)
     private FileObject model3d;
 
     @ApiModelProperty("铸造者")

+ 16 - 3
src/main/java/com/izouma/nineth/repo/CollectionRepo.java

@@ -1,6 +1,9 @@
 package com.izouma.nineth.repo;
 
 import com.izouma.nineth.domain.Collection;
+import com.izouma.nineth.domain.CollectionProperty;
+import com.izouma.nineth.domain.FileObject;
+import com.izouma.nineth.domain.Privilege;
 import com.izouma.nineth.dto.RecommendCollection;
 import org.springframework.cache.annotation.CacheEvict;
 import org.springframework.cache.annotation.CachePut;
@@ -23,8 +26,18 @@ public interface CollectionRepo extends JpaRepository<Collection, Long>, JpaSpec
     @CacheEvict(value = {"collection", "recommend"}, allEntries = true)
     void softDelete(Long id);
 
+    @Transactional
+    @Modifying
+    @Query("update Collection c set c.onShelf = ?2, c.salable = ?3, c.startTime = ?4, " +
+            "c.scheduleSale = ?5, c.sort = ?6, c.detail = ?7, c.privileges = ?8, " +
+            "c.properties = ?9, c.model3d = ?10 where c.id = ?1")
+    @CacheEvict(value = {"collection", "recommend"}, allEntries = true)
+    void update(@Nonnull Long id, boolean onShelf, boolean salable, LocalDateTime startTime,
+                boolean schedule, int sort, String detail, List<Privilege> privileges,
+                List<CollectionProperty> properties, FileObject model3d);
+
     @Cacheable("collection")
-    Optional<Collection> findById(Long id);
+    Optional<Collection> findById(@Nonnull Long id);
 
     Optional<Collection> findByIdAndDelFalse(Long id);
 
@@ -80,9 +93,9 @@ public interface CollectionRepo extends JpaRepository<Collection, Long>, JpaSpec
 
     @Transactional
     @Modifying
-    @Query("update Collection c set c.onShelf = ?2, c.salable = ?2 where c.id = ?1")
+    @Query("update Collection c set c.onShelf = true, c.salable = true where c.id = ?1")
     @CacheEvict(value = "collection", key = "#id")
-    void scheduleOnShelf(Long id, boolean onShelf);
+    void scheduleOnShelf(Long id);
 
     @Query("select c.currentNumber from Collection c where c.id = ?1")
     Optional<Integer> getCurrentNumber(Long id);

+ 23 - 0
src/main/java/com/izouma/nineth/service/CacheService.java

@@ -0,0 +1,23 @@
+package com.izouma.nineth.service;
+
+import org.springframework.cache.annotation.CacheEvict;
+import org.springframework.stereotype.Service;
+
+@Service
+public class CacheService {
+    @CacheEvict(value = "collection", allEntries = true)
+    public void clearCollection() {
+    }
+
+    @CacheEvict(value = "collection", key = "#id")
+    public void clearCollection(Long id) {
+    }
+
+    @CacheEvict(value = "user", allEntries = true)
+    public void clearUser() {
+    }
+
+    @CacheEvict(value = "user", key = "#username")
+    public void clearUser(String username) {
+    }
+}

+ 46 - 17
src/main/java/com/izouma/nineth/service/CollectionService.java

@@ -1,7 +1,7 @@
 package com.izouma.nineth.service;
 
-import com.izouma.nineth.domain.*;
 import com.izouma.nineth.domain.Collection;
+import com.izouma.nineth.domain.*;
 import com.izouma.nineth.dto.CollectionDTO;
 import com.izouma.nineth.dto.CreateBlindBox;
 import com.izouma.nineth.dto.PageQuery;
@@ -17,13 +17,11 @@ import org.apache.commons.lang3.RandomUtils;
 import org.apache.commons.lang3.Range;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeanUtils;
-import org.springframework.cache.annotation.CacheEvict;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.PageImpl;
 import org.springframework.data.domain.PageRequest;
 import org.springframework.data.domain.Sort;
 import org.springframework.data.jpa.domain.Specification;
-import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.scheduling.TaskScheduler;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
@@ -32,28 +30,32 @@ import javax.annotation.PostConstruct;
 import javax.persistence.criteria.Predicate;
 import javax.transaction.Transactional;
 import java.time.LocalDateTime;
+import java.time.ZoneId;
 import java.util.*;
+import java.util.concurrent.ScheduledFuture;
 import java.util.stream.Collectors;
 
 @Service
 @AllArgsConstructor
 public class CollectionService {
 
-    private CollectionRepo                collectionRepo;
-    private LikeRepo                      likeRepo;
-    private BlindBoxItemRepo              blindBoxItemRepo;
-    private AppointmentRepo               appointmentRepo;
-    private UserRepo                      userRepo;
-    private AssetService                  assetService;
-    private RedisTemplate<String, Object> redisTemplate;
-    private TaskScheduler                 taskScheduler;
+    private CollectionRepo   collectionRepo;
+    private LikeRepo         likeRepo;
+    private BlindBoxItemRepo blindBoxItemRepo;
+    private AppointmentRepo  appointmentRepo;
+    private UserRepo         userRepo;
+    private AssetService     assetService;
+    private TaskScheduler    taskScheduler;
+    private CacheService     cacheService;
+
+    private final Map<Long, ScheduledFuture<?>> tasks = new HashMap<>();
 
     @PostConstruct
     public void init() {
-    }
-
-    @CacheEvict(value = "collection", allEntries = true)
-    public void clearCache() {
+        List<Collection> collections = collectionRepo.findByScheduleSaleTrueAndOnShelfFalseAndStartTimeBeforeAndDelFalse(LocalDateTime.now());
+        for (Collection collection : collections) {
+            onShelfTask(collection);
+        }
     }
 
     public Page<Collection> all(PageQuery pageQuery) {
@@ -105,7 +107,34 @@ public class CollectionService {
             }
             record.setOnShelf(record.getStartTime().isBefore(LocalDateTime.now()));
         }
-        return collectionRepo.save(record);
+        collectionRepo.save(record);
+        if (record.isScheduleSale()) {
+            onShelfTask(record);
+        }
+        return record;
+    }
+
+    private void onShelfTask(Collection record) {
+        if (record.getStartTime().minusSeconds(2).isAfter(LocalDateTime.now())) {
+            Date date = Date.from(record.getStartTime().atZone(ZoneId.systemDefault()).toInstant());
+            ScheduledFuture<?> future = taskScheduler.schedule(() -> {
+                collectionRepo.scheduleOnShelf(record.getId());
+                tasks.remove(record.getId());
+            }, date);
+            tasks.put(record.getId(), future);
+        } else {
+            collectionRepo.scheduleOnShelf(record.getId());
+        }
+    }
+
+    public Collection update(Collection record) {
+        Collection orig = collectionRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
+        collectionRepo.update(record.getId(), record.isOnShelf(), record.isSalable(),
+                record.getStartTime(), record.isScheduleSale(), record.getSort(),
+                record.getDetail(), record.getPrivileges(), record.getProperties(),
+                record.getModel3d());
+
+        return collectionRepo.save(orig);
     }
 
     public CollectionDTO toDTO(Collection collection) {
@@ -220,7 +249,7 @@ public class CollectionService {
     public void scheduleOnShelf() {
         List<Collection> collections = collectionRepo.findByScheduleSaleTrueAndOnShelfFalseAndStartTimeBeforeAndDelFalse(LocalDateTime.now());
         for (Collection collection : collections) {
-            collectionRepo.scheduleOnShelf(collection.getId(), true);
+            collectionRepo.scheduleOnShelf(collection.getId());
         }
     }
 

+ 2 - 5
src/main/java/com/izouma/nineth/service/UserService.java

@@ -65,6 +65,7 @@ public class UserService {
     private UserBankCardRepo  userBankCardRepo;
     private InviteRepo        inviteRepo;
     private NFTService        nftService;
+    private CacheService      cacheService;
 
     @CacheEvict(value = "user", key = "#user.username")
     public User update(User user) {
@@ -78,14 +79,10 @@ public class UserService {
         userRepo.updateOrderMinter(orig.getId());
         userRepo.updateHistoryFromUser(orig.getId());
         userRepo.updateHistoryToUser(orig.getId());
-        collectionService.clearCache();
+        cacheService.clearCollection();
         return orig;
     }
 
-    @CacheEvict(value = "user", allEntries = true)
-    public void clearCache() {
-    }
-
     public Page<User> all(PageQuery pageQuery) {
         Specification<User> specification = JpaUtils.toSpecification(pageQuery, User.class);
 

+ 1 - 0
src/main/vue/src/views/CollectionEdit.vue

@@ -35,6 +35,7 @@
                             v-model="formData.model3d"
                             :customUrl="customUrl"
                             accept="application/zip"
+                            format="json"
                         ></file-upload>
                         <div class="tip">请将FBX文件与贴图打包成zip压缩包上传</div>
                     </el-form-item>

+ 13 - 0
src/test/java/com/izouma/nineth/CommonTest.java

@@ -17,6 +17,7 @@ import lombok.SneakyThrows;
 import net.coobird.thumbnailator.Thumbnails;
 import org.apache.commons.codec.EncoderException;
 import org.apache.commons.codec.net.URLCodec;
+import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang3.RandomStringUtils;
 import org.apache.commons.lang3.RandomUtils;
 import org.apache.commons.lang3.Range;
@@ -377,4 +378,16 @@ public class CommonTest {
     public void random256() throws NoSuchAlgorithmException {
         System.out.println(new File("/Users/drew/Downloads/images").list((dir, name) -> Pattern.matches(".*\\.png", name)));
     }
+
+    @Test
+    public void dd() throws IOException {
+        String[] arr1 = FileUtils.readFileToString(new File("/Users/drew/Desktop/未命名文件夹 2/111")).split("\n");
+        List<String> arr2 = Arrays.asList(FileUtils.readFileToString(new File("/Users/drew/Desktop/未命名文件夹 2/222"))
+                .split("\n"));
+        for (String s : arr1) {
+            if (!arr2.contains(s)) {
+                System.out.println(s);
+            }
+        }
+    }
 }

+ 0 - 1
src/test/java/com/izouma/nineth/service/UserServiceTest.java

@@ -29,7 +29,6 @@ public class UserServiceTest extends ApplicationTests {
 
     @Test
     public void findByUsernameAndDelFalse1() {
-        userService.clearCache();
         userRepo.findByUsernameAndDelFalse("admin");
         userRepo.findByUsernameAndDelFalse("admin");
     }