Przeglądaj źródła

定时取消订单

wangqifan 3 lat temu
rodzic
commit
20e9fde81a

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

@@ -150,7 +150,7 @@ public interface CollectionRepo extends JpaRepository<Collection, Long>, JpaSpec
     @Query("select c.vipQuota from Collection c where c.id = ?1")
     Integer getVipQuota(Long id);
 
-    @Query("select c.assetId from Collection  c where c.price >= ?1 and c.source = ?2 and c.startTime <= ?3 and c.salable = ?4")
+    @Query("select c.assetId from Collection  c where c.price >= ?1 and c.source = ?2 and c.createdAt <= ?3 and c.salable = ?4")
     Set<Long> findResaleCollectionPriceOver20K(BigDecimal price, CollectionSource source, LocalDateTime startTime, boolean salable);
 
     List<Collection> findAllByNameLike(String name);

+ 27 - 7
src/main/java/com/izouma/nineth/service/AssetService.java

@@ -60,10 +60,12 @@ public class AssetService {
 
 
     public Page<Asset> all(PageQuery pageQuery) {
-        Page<Asset> all = assetRepo.findAll(JpaUtils.toSpecification(pageQuery, Asset.class), JpaUtils.toPageRequest(pageQuery));
+        Page<Asset> all = assetRepo
+                .findAll(JpaUtils.toSpecification(pageQuery, Asset.class), JpaUtils.toPageRequest(pageQuery));
         Map<String, Object> query = pageQuery.getQuery();
         if (query.containsKey("userId")) {
-            List<Long> orderId = orderRepo.findAllByUserIdAndOpenedFalse(Convert.convert(Long.class, query.get("userId")));
+            List<Long> orderId = orderRepo
+                    .findAllByUserIdAndOpenedFalse(Convert.convert(Long.class, query.get("userId")));
             return all.map(asset -> {
                 if (orderId.contains(asset.getOrderId())) {
                     asset.setOpened(false);
@@ -427,10 +429,12 @@ public class AssetService {
 
     public Page<UserHistory> userHistory(Long userId, PageQuery pageQuery) {
         Page<TokenHistory> page = tokenHistoryRepo.findAll(((root, criteriaQuery, criteriaBuilder) -> {
-            List<Predicate> and = JpaUtils.toPredicates(pageQuery, TokenHistory.class, root, criteriaQuery, criteriaBuilder);
+            List<Predicate> and = JpaUtils
+                    .toPredicates(pageQuery, TokenHistory.class, root, criteriaQuery, criteriaBuilder);
             Map<String, Object> query = pageQuery.getQuery();
             if (ObjectUtils.isEmpty(query.get("toUserId")) && ObjectUtils.isEmpty(query.get("fromUserId"))) {
-                and.add(criteriaBuilder.or(criteriaBuilder.equal(root.get("toUserId"), userId), criteriaBuilder.equal(root.get("fromUserId"), userId)));
+                and.add(criteriaBuilder.or(criteriaBuilder.equal(root.get("toUserId"), userId), criteriaBuilder
+                        .equal(root.get("fromUserId"), userId)));
             } else {
                 if (ObjectUtils.isNotEmpty(query.get("toUserId"))) {
                     and.add(criteriaBuilder.and(criteriaBuilder.equal(root.get("toUserId"), userId)));
@@ -530,14 +534,30 @@ public class AssetService {
     }
 
 
-    //    @Scheduled(cron = "0 0 0/2 * * ?")
-    @Scheduled(cron = "0 */1 * * * ?")
+    @Scheduled(cron = "0 0 0/1 * * ?")
     public void offTheShelf() {
         LocalDateTime lastTime = LocalDateTime.now().minusHours(1);
         Set<Long> assetIds = collectionRepo
                 .findResaleCollectionPriceOver20K(BigDecimal
                         .valueOf(20000L), CollectionSource.TRANSFER, lastTime, true);
-        assetIds.forEach(this::cancelConsignment);
+        assetIds.forEach(this::cancelConsignmentBySystem);
     }
 
+
+    public void cancelConsignmentBySystem(Long id) {
+        Asset asset = assetRepo.findById(id).orElseThrow(new BusinessException("无记录"));
+        if (asset.getPublicCollectionId() != null) {
+            List<Order> orders = orderRepo.findByCollectionId(asset.getPublicCollectionId());
+            if (orders.stream().anyMatch(o -> o.getStatus() != OrderStatus.CANCELLED)) {
+                throw new BusinessException("已有订单不可取消");
+            }
+            collectionRepo.findById(asset.getPublicCollectionId())
+                    .ifPresent(collection -> {
+                        collection.setSalable(false);
+                        collectionRepo.save(collection);
+                    });
+        }
+        asset.setConsignment(false);
+        assetRepo.save(asset);
+    }
 }