|
|
@@ -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());
|
|
|
}
|
|
|
}
|
|
|
|