licailing 4 лет назад
Родитель
Сommit
0a0ebc52ee

+ 3 - 0
src/main/java/com/izouma/nineth/repo/AssetRepo.java

@@ -45,4 +45,7 @@ public interface AssetRepo extends JpaRepository<Asset, Long>, JpaSpecificationE
     Asset findFirstByTokenIdAndCreatedAtAfterOrderByCreatedAt(String tokenId, LocalDateTime time);
     Asset findFirstByTokenIdAndCreatedAtAfterOrderByCreatedAt(String tokenId, LocalDateTime time);
 
 
     List<Asset> findByTxHashIsNullAndTokenIdNotNullAndCreatedAtBefore(LocalDateTime time);
     List<Asset> findByTxHashIsNullAndTokenIdNotNullAndCreatedAtBefore(LocalDateTime time);
+
+    @Query("select id from Asset where status = ?1")
+    List<Long> findAllByStatus(AssetStatus status);
 }
 }

+ 8 - 0
src/main/java/com/izouma/nineth/repo/CollectionRepo.java

@@ -95,4 +95,12 @@ public interface CollectionRepo extends JpaRepository<Collection, Long>, JpaSpec
 
 
     @Query("select c.currentNumber from Collection c where c.id = ?1")
     @Query("select c.currentNumber from Collection c where c.id = ?1")
     Optional<Integer> getCurrentNumber(Long id);
     Optional<Integer> getCurrentNumber(Long id);
+
+    @Query("select c.id from Collection c where c.assetId in ?1")
+    List<Long> findAllByDelFalseAndAssetIdIn(java.util.Collection<Long> assetId);
+
+    @Query("update Collection t set t.del = true where t.id in ?1")
+    @Modifying
+    @Transactional
+    void softDeleteByIdIn(java.util.Collection<Long> id);
 }
 }

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

@@ -6,6 +6,7 @@ import com.izouma.nineth.domain.*;
 import com.izouma.nineth.dto.CollectionDTO;
 import com.izouma.nineth.dto.CollectionDTO;
 import com.izouma.nineth.dto.CreateBlindBox;
 import com.izouma.nineth.dto.CreateBlindBox;
 import com.izouma.nineth.dto.PageQuery;
 import com.izouma.nineth.dto.PageQuery;
+import com.izouma.nineth.enums.AssetStatus;
 import com.izouma.nineth.enums.CollectionSource;
 import com.izouma.nineth.enums.CollectionSource;
 import com.izouma.nineth.enums.CollectionType;
 import com.izouma.nineth.enums.CollectionType;
 import com.izouma.nineth.exception.BusinessException;
 import com.izouma.nineth.exception.BusinessException;
@@ -13,6 +14,8 @@ import com.izouma.nineth.repo.*;
 import com.izouma.nineth.utils.JpaUtils;
 import com.izouma.nineth.utils.JpaUtils;
 import com.izouma.nineth.utils.SecurityUtils;
 import com.izouma.nineth.utils.SecurityUtils;
 import lombok.AllArgsConstructor;
 import lombok.AllArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.collections.MapUtils;
 import org.apache.commons.collections.MapUtils;
 import org.apache.commons.lang3.RandomUtils;
 import org.apache.commons.lang3.RandomUtils;
 import org.apache.commons.lang3.Range;
 import org.apache.commons.lang3.Range;
@@ -24,6 +27,7 @@ import org.springframework.data.domain.PageRequest;
 import org.springframework.data.domain.Sort;
 import org.springframework.data.domain.Sort;
 import org.springframework.data.jpa.domain.Specification;
 import org.springframework.data.jpa.domain.Specification;
 import org.springframework.scheduling.TaskScheduler;
 import org.springframework.scheduling.TaskScheduler;
+import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 
 
 import javax.annotation.PostConstruct;
 import javax.annotation.PostConstruct;
@@ -35,6 +39,7 @@ import java.util.*;
 import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.ScheduledFuture;
 import java.util.stream.Collectors;
 import java.util.stream.Collectors;
 
 
+@Slf4j
 @Service
 @Service
 @AllArgsConstructor
 @AllArgsConstructor
 public class CollectionService {
 public class CollectionService {
@@ -46,6 +51,7 @@ public class CollectionService {
     private UserRepo         userRepo;
     private UserRepo         userRepo;
     private TaskScheduler    taskScheduler;
     private TaskScheduler    taskScheduler;
     private CacheService     cacheService;
     private CacheService     cacheService;
+    private AssetRepo        assetRepo;
 
 
     private final Map<Long, ScheduledFuture<?>> tasks = new HashMap<>();
     private final Map<Long, ScheduledFuture<?>> tasks = new HashMap<>();
 
 
@@ -335,4 +341,15 @@ public class CollectionService {
         collection.setStock(collection.getStock() + number);
         collection.setStock(collection.getStock() + number);
         collection.setTotal(collection.getTotal() + number);
         collection.setTotal(collection.getTotal() + number);
     }
     }
+
+    @Scheduled(cron = "0 0 1 ?")
+    public void delCollection() {
+        List<Long> assetIds = assetRepo.findAllByStatus(AssetStatus.TRANSFERRED);
+        List<Long> collections = collectionRepo.findAllByDelFalseAndAssetIdIn(assetIds);
+        if (CollectionUtils.isEmpty(collections)) {
+            return;
+        }
+        log.info("定时任务删除未删除藏品:{}", collections);
+        collectionRepo.softDeleteByIdIn(collections);
+    }
 }
 }