sunkean %!s(int64=2) %!d(string=hai) anos
pai
achega
d16c284c85

+ 47 - 0
src/main/java/com/izouma/nineth/aspect/MetaTaskAspect.java

@@ -0,0 +1,47 @@
+package com.izouma.nineth.aspect;
+
+import com.izouma.nineth.service.MetaTaskToUserNewService;
+import lombok.extern.slf4j.Slf4j;
+import org.aspectj.lang.annotation.Aspect;
+import org.springframework.stereotype.Component;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.Future;
+import java.util.concurrent.ScheduledExecutorService;
+
+@Aspect
+@Component
+@Slf4j
+public class MetaTaskAspect {
+
+    private final MetaTaskToUserNewService metaTaskToUserNewService;
+    private final ScheduledExecutorService executorService;
+    private final Map<Long, Future>        futureMap;
+
+    public MetaTaskAspect(MetaTaskToUserNewService metaTaskToUserNewService, ScheduledExecutorService executorService) {
+        this.metaTaskToUserNewService = metaTaskToUserNewService;
+        this.executorService = executorService;
+        this.futureMap = new HashMap<>();
+    }
+
+//    @After("execution(* com.izouma.nineth.repo.MetaUserPropRepo.save(..))")
+//    public void redisLock(JoinPoint joinPoint) {
+//        log.info("enter MetaTaskAspect");
+//        try {
+//            MetaUserProp metaUserProp = (MetaUserProp) joinPoint.getArgs()[0];
+//            synchronized (MetaTaskAspect.class) {
+//                Optional.ofNullable(futureMap.get(metaUserProp.getUserId()))
+//                        .ifPresent(f -> {
+//                            log.info("debouncing previous task {}", metaUserProp.getUserId());
+//                            f.cancel(false);
+//                        });
+//                futureMap.put(metaUserProp.getUserId(), executorService.schedule(() -> {
+//                    metaTaskToUserNewService.handlePropTask(metaUserProp.getUserId());
+//                }, 500, TimeUnit.MILLISECONDS));
+//            }
+//        } catch (Exception e) {
+//            log.error("MetaTaskAspect error", e);
+//        }
+//    }
+}

+ 0 - 1
src/main/java/com/izouma/nineth/domain/MetaAtomTask.java

@@ -3,7 +3,6 @@ package com.izouma.nineth.domain;
 
 import com.alibaba.excel.annotation.ExcelProperty;
 import com.izouma.nineth.annotations.Searchable;
-import com.izouma.nineth.converter.MetaLuckyDrawAwardListConverter;
 import com.izouma.nineth.converter.MetaTargetConfigListConverter;
 import com.izouma.nineth.dto.MetaServiceResult;
 import com.izouma.nineth.enums.MetaAwardTypeEnum;

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

@@ -1,6 +1,7 @@
 package com.izouma.nineth.repo;
 
 import com.izouma.nineth.domain.MetaAtomTask;
+import com.izouma.nineth.enums.MetaTaskTarget;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
 import org.springframework.data.jpa.repository.Modifying;
@@ -18,4 +19,6 @@ public interface MetaAtomTaskRepo extends JpaRepository<MetaAtomTask, Long>, Jpa
     List<MetaAtomTask> findAllByDel(boolean del);
 
     MetaAtomTask findByIdAndDel(Long id, boolean del);
+
+    List<MetaAtomTask> findAllByTargetTypeAndDel(MetaTaskTarget targetType, boolean del);
 }

+ 2 - 0
src/main/java/com/izouma/nineth/repo/MetaTaskToUserNewRepo.java

@@ -28,4 +28,6 @@ public interface MetaTaskToUserNewRepo extends JpaRepository<MetaTaskToUserNew,
 
     @Query(value = "select m.id from meta_task_to_user_new m where m.task_id in ?1 and m.pigeonhole = false and m.del = false", nativeQuery = true)
     List<Long> findIdByTaskIds(List<Long> taskIds);
+
+    List<MetaTaskToUserNew> findAllByUserIdAndAtomTaskIdAndPigeonholeAndDel(Long userId, Long atomTaskId, boolean pigeonhole, boolean del);
 }

+ 39 - 0
src/main/java/com/izouma/nineth/service/MetaTaskToUserNewService.java

@@ -10,6 +10,7 @@ import com.izouma.nineth.dto.MetaServiceResult;
 import com.izouma.nineth.dto.PageQuery;
 import com.izouma.nineth.enums.MetaAwardTypeEnum;
 import com.izouma.nineth.enums.MetaTaskStatus;
+import com.izouma.nineth.enums.MetaTaskTarget;
 import com.izouma.nineth.enums.MetaTaskType;
 import com.izouma.nineth.exception.BusinessException;
 import com.izouma.nineth.repo.*;
@@ -27,6 +28,7 @@ import java.time.LocalDateTime;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Objects;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 @Service
 @AllArgsConstructor
@@ -39,6 +41,7 @@ public class MetaTaskToUserNewService {
     private MetaPropRepo                metaPropRepo;
     private MetaTaskBindRepo            metaTaskBindRepo;
     private MetaUserTaskProgressNewRepo metaUserTaskProgressNewRepo;
+    private MetaUserPropRepo            metaUserPropRepo;
 
     public Page<MetaTaskToUserNew> all(PageQuery pageQuery) {
         return metaTaskToUserNewRepo.findAll(JpaUtils.toSpecification(pageQuery, MetaTaskToUserNew.class), JpaUtils
@@ -303,4 +306,40 @@ public class MetaTaskToUserNewService {
         log.info(String.format("[%S]归档日常任务进度[%S]", LocalDateTime.now(), JSONUtils.toJSONString(metaTaskToUserIds)));
         metaUserTaskProgressNewRepo.updatePigeonhole(metaTaskToUserIds);
     }
+
+    public void handlePropTask(Long userId) {
+        List<MetaAtomTask> metaAtomTasks = metaAtomTaskRepo.findAllByTargetTypeAndDel(MetaTaskTarget.COLLECT_PROP, false);
+        metaAtomTasks.forEach(metaAtomTask -> {
+            // 任务配置
+            List<MetaTargetConfig> metaTargetConfigs = metaAtomTask.getMetaTargetConfig();
+            if (CollectionUtil.isEmpty(metaTargetConfigs)) {
+                throw new BusinessException("");
+            }
+            // 任务领取记录
+            List<MetaTaskToUserNew> metaTaskToUserNews = metaTaskToUserNewRepo.findAllByUserIdAndAtomTaskIdAndPigeonholeAndDel(userId, metaAtomTask.getId(), false, false);
+            AtomicBoolean completed = new AtomicBoolean(true);
+            if (CollectionUtil.isNotEmpty(metaTaskToUserNews)) {
+                // 是否完成
+                metaTargetConfigs.forEach(metaTargetConfig -> {
+                    MetaUserProp metaUserProp = metaUserPropRepo.findByUserIdAndMetaPropIdAndDel(userId, Long.parseLong(metaTargetConfig.getMetaPropId()), false);
+                    if (Objects.isNull(metaUserProp) || metaUserProp.getNum() < metaTargetConfig.getNum()) {
+                        completed.set(false);
+                    }
+                });
+                // 调整任务状态
+                metaTaskToUserNews.forEach(metaTaskToUserNew -> {
+                    if (MetaTaskStatus.FINISH.equals(metaTaskToUserNew.getStatus())) {
+                        return;
+                    }
+                    if (completed.get()) {
+                        metaTaskToUserNew.setStatus(MetaTaskStatus.COMPLETION);
+                    } else {
+                        metaTaskToUserNew.setStatus(MetaTaskStatus.PROGRESS);
+                    }
+                });
+            }
+            // 重新保存任务状态
+            metaTaskToUserNewRepo.saveAll(metaTaskToUserNews);
+        });
+    }
 }