|
|
@@ -4,6 +4,7 @@ import com.izouma.nineth.annotations.Debounce;
|
|
|
import com.izouma.nineth.config.RedisKeys;
|
|
|
import com.izouma.nineth.domain.Asset;
|
|
|
import com.izouma.nineth.domain.AuctionActivity;
|
|
|
+import com.izouma.nineth.domain.Collection;
|
|
|
import com.izouma.nineth.domain.User;
|
|
|
import com.izouma.nineth.dto.PageQuery;
|
|
|
import com.izouma.nineth.dto.auction.AuctionInputDTO;
|
|
|
@@ -22,13 +23,19 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.redis.core.BoundValueOperations;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.scheduling.TaskScheduler;
|
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.time.ZoneId;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
import java.util.Optional;
|
|
|
+import java.util.concurrent.ScheduledFuture;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
@Slf4j
|
|
|
@@ -42,6 +49,9 @@ public class AuctionActivityService {
|
|
|
private final PasswordEncoder passwordEncoder;
|
|
|
private final RedisTemplate<String, Object> redisTemplate;
|
|
|
private final CacheService cacheService;
|
|
|
+ private final TaskScheduler taskScheduler;
|
|
|
+
|
|
|
+ private final Map<Long, ScheduledFuture<?>> tasks = new HashMap<>();
|
|
|
|
|
|
public Page<AuctionActivity> all(PageQuery pageQuery) {
|
|
|
return auctionActivityRepo
|
|
|
@@ -103,8 +113,13 @@ public class AuctionActivityService {
|
|
|
asset.setStatus(AssetStatus.AUCTIONING);
|
|
|
assetRepo.save(asset);
|
|
|
}
|
|
|
- record.setStatus(AuctionStatus.NOTSTARTED);
|
|
|
- return auctionActivityRepo.save(record);
|
|
|
+ AuctionActivity saved = auctionActivityRepo.save(record);
|
|
|
+ if (saved.getStatus().equals(AuctionStatus.NOTSTARTED)) {
|
|
|
+ onShelfTask(saved);
|
|
|
+ } else if (saved.getStatus().equals(AuctionStatus.ONGOING)) {
|
|
|
+ offShelfTask(saved);
|
|
|
+ }
|
|
|
+ return saved;
|
|
|
}
|
|
|
|
|
|
public synchronized String changeStatus(Long id, AuctionStatus status) {
|
|
|
@@ -128,4 +143,49 @@ public class AuctionActivityService {
|
|
|
cacheService.clearAuction(id);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ private void onShelfTask(AuctionActivity record) {
|
|
|
+ ScheduledFuture<?> task = tasks.get(record.getId());
|
|
|
+ if (task != null) {
|
|
|
+ if (!task.cancel(true)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (record.getStatus().equals(AuctionStatus.NOTSTARTED)) {
|
|
|
+ if (record.getStartTime().minusSeconds(2).isAfter(LocalDateTime.now())) {
|
|
|
+ Date date = Date.from(record.getStartTime().atZone(ZoneId.systemDefault()).toInstant());
|
|
|
+ ScheduledFuture<?> future = taskScheduler.schedule(() -> {
|
|
|
+ auctionActivityRepo.scheduleOnShelf(record.getId(), AuctionStatus.ONGOING);
|
|
|
+ tasks.remove(record.getId());
|
|
|
+ offShelfTask(record);
|
|
|
+ }, date);
|
|
|
+ tasks.put(record.getId(), future);
|
|
|
+ } else {
|
|
|
+ auctionActivityRepo.scheduleOnShelf(record.getId(), AuctionStatus.ONGOING);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void offShelfTask(AuctionActivity record) {
|
|
|
+ ScheduledFuture<?> task = tasks.get(record.getId());
|
|
|
+ if (task != null) {
|
|
|
+ if (!task.cancel(true)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ AuctionActivity nowRecord = auctionActivityRepo.findById(record.getId())
|
|
|
+ .orElseThrow(new BusinessException("无数据"));
|
|
|
+ if (nowRecord.getStatus().equals(AuctionStatus.ONGOING)) {
|
|
|
+ if (nowRecord.getEndTime().minusSeconds(2).isAfter(LocalDateTime.now())) {
|
|
|
+ Date date = Date.from(record.getStartTime().atZone(ZoneId.systemDefault()).toInstant());
|
|
|
+ ScheduledFuture<?> future = taskScheduler.schedule(() -> {
|
|
|
+ auctionActivityRepo.scheduleOffShelf(nowRecord.getId(), AuctionStatus.PURCHASED);
|
|
|
+ tasks.remove(record.getId());
|
|
|
+ }, date);
|
|
|
+ tasks.put(record.getId(), future);
|
|
|
+ } else {
|
|
|
+ auctionActivityRepo.scheduleOffShelf(nowRecord.getId(), AuctionStatus.PURCHASED);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|