Selaa lähdekoodia

质检-资料查询-save-again功能
改成定时任务 表添加字段

LHB 2 kuukautta sitten
vanhempi
commit
5c7fc32d6b

+ 3 - 0
blade-service-api/blade-business-api/src/main/java/org/springblade/business/entity/InformationQuery.java

@@ -137,4 +137,7 @@ public class InformationQuery extends BaseEntity {
 
     @ApiModelProperty("节点pdf")
     private String nodePdfUrl;
+
+    @ApiModelProperty("重刷状态 0-待重刷,1-正在重刷,2-重刷成功,3-重刷失败")
+    private Integer saveAgain;
 }

+ 5 - 35
blade-service/blade-business/src/main/java/org/springblade/business/service/impl/TaskServiceImpl.java

@@ -138,12 +138,6 @@ public class TaskServiceImpl extends BaseServiceImpl<TaskMapper, Task> implement
 
     @Autowired
     StringRedisTemplate RedisTemplate;
-    @Autowired
-    private BladeRedis bladeRedis;
-    //异步类
-    @Autowired
-    private TaskSync taskSync;
-
 
 
     private final ITrialSelfInspectionRecordService iTrialSelfInspectionRecordService;
@@ -1978,37 +1972,13 @@ public class TaskServiceImpl extends BaseServiceImpl<TaskMapper, Task> implement
     }*/
         @Override
         public R reSigningEVisaStatus0(List<reSigningEVisaStatus> dtos, String header) throws Exception {
-         R result= new R();
          if(dtos.size()>0){
-
-             //添加缓存
              Set<Long> newIds = dtos.stream().map(reSigningEVisaStatus::getId).collect(Collectors.toSet());
-
-             //随机id
-             String s = "sign-reSigningEVisaStatus0:" + SnowFlakeUtil.getId();
-
-             //数据效验
-             Set<Long> oldIds = new HashSet<>();
-             Set<String> keys = bladeRedis.keys("sign-reSigningEVisaStatus0*");
-             for (String key : keys) {
-                 Set<Long> ids = bladeRedis.get(key);
-                 if(ids !=null ){
-                     oldIds.addAll(ids);
-                 }
-             }
-
-             if(CollectionUtils.isNotEmpty(oldIds)){
-                 //如果旧的id中存在新的id,则不允许添加
-                 long count = oldIds.stream().filter(f -> newIds.contains(f)).count();
-                 if(count > 0){
-                     return R.fail("当前提交数据中存在正在保存的数据,请勿重复提交");
-                 }
-             }
-             //10分钟过期
-             bladeRedis.setEx(s,newIds, 600L);
-
-             //执行异步
-             taskSync.reSigningEVisaStatusSync(dtos,header,s);
+             //正在重刷的不允许再次重刷
+             informationQueryService.update(Wrappers.<InformationQuery>update().lambda()
+                     .set(InformationQuery::getSaveAgain, 0)
+                     .ne(InformationQuery::getSaveAgain, 1)
+                     .in(InformationQuery::getId, newIds));
         }
          return R.success("操作成功");
     }

+ 85 - 32
blade-service/blade-business/src/main/java/org/springblade/business/sync/TaskSync.java

@@ -1,7 +1,12 @@
 package org.springblade.business.sync;
 
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.google.common.collect.Lists;
 import lombok.AllArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
 import org.springblade.business.dto.reSigningEVisaStatus;
 import org.springblade.business.entity.InformationQuery;
 import org.springblade.business.service.IInformationQueryFileService;
@@ -12,57 +17,105 @@ import org.springblade.core.tool.api.R;
 import org.springblade.manager.feign.ExcelTabClient;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.Async;
+import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 
+import javax.annotation.Resource;
 import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.stream.Collectors;
 
 /**
  * @author LHB
  */
 @Component
 @AllArgsConstructor
+@Slf4j
 public class TaskSync {
 
     private final IInformationQueryService informationQueryService;
-
     private final ExcelTabClient excelTabClient;
-    @Autowired
-    private BladeRedis bladeRedis;
-    @Async("taskExecutor1")
-    public void reSigningEVisaStatusSync(List<reSigningEVisaStatus> dtos, String header,String s) {
-        R result= null;
-        try {
-            for (reSigningEVisaStatus dto : dtos) {
-                InformationQuery iq = informationQueryService.getById(dto.getId());
-                if(iq!=null){
-                    informationQueryService.update(new LambdaUpdateWrapper<InformationQuery>()
-                            .eq(InformationQuery::getId, dto.getId())
-                            .set(InformationQuery::getEVisaPdfUrl, null)
-                            .set(InformationQuery::getPdfUrl, null));
+    // 线程池
+    @Resource(name = "taskExecutor1")
+    private ThreadPoolExecutor executor;
+
+
+    public void reSigningEVisaStatusSync(List<InformationQuery> dtos) {
+        log.info("数据正在重刷,线程名称:{}", Thread.currentThread().getName());
+
+        for (InformationQuery dto : dtos) {
+            informationQueryService.update(new LambdaUpdateWrapper<InformationQuery>()
+                    .eq(InformationQuery::getId, dto.getId())
+                    .set(InformationQuery::getEVisaPdfUrl, null)
+                    .set(InformationQuery::getPdfUrl, null));
+            R result = this.saveNodePdf(dto.getClassify() + "", dto.getWbsId() + "", dto.getContractId() + "", dto.getProjectId() + "", null);
+
+            LambdaUpdateWrapper<InformationQuery> lambda = Wrappers.<InformationQuery>update().lambda();
+            if (result == null || result.getCode() != 200) {
+                //重签失败
+                lambda.set(InformationQuery::getSaveAgain, 3)
+                        .set(InformationQuery::getEVisaPdfUrl, dto.getEVisaPdfUrl())
+                        .set(InformationQuery::getPdfUrl, dto.getPdfUrl());
+            } else {
+                //成功重签
+                lambda.set(InformationQuery::getSaveAgain, 2);
+            }
+            lambda.eq(InformationQuery::getId, dto.getId());
+            informationQueryService.update(lambda);
+        }
+        log.info("数据重刷完毕,线程名称:{}", Thread.currentThread().getName());
+    }
+
+    /**
+     * 重刷定时任务 使用多线程的方式去跑
+     */
+    @Scheduled(fixedDelay = 60000)
+    public void saveAgainTask() {
+        List<InformationQuery> list = informationQueryService.list(Wrappers.<InformationQuery>lambdaQuery()
+                .in(InformationQuery::getSaveAgain, 0, 3)
+                .eq(InformationQuery::getIsDeleted, 0)
+                .last("limit 50"));
+
+        List<List<InformationQuery>> partition = Lists.partition(list, 10);
+
+        for (List<InformationQuery> informationQueries : partition) {
+            List<Long> collect = informationQueries.stream().map(InformationQuery::getId).collect(Collectors.toList());
+            //修改状态之后开始重刷
+            boolean update = informationQueryService.update(Wrappers.<InformationQuery>update().lambda()
+                    .set(InformationQuery::getSaveAgain, 1)
+                    .in(InformationQuery::getId, collect));
+            if (update) {
+                CompletableFuture.runAsync(() -> {
                     try {
-                        result = this.saveNodePdf(iq.getClassify() + "", iq.getWbsId() + "", dto.getContractId() + "", dto.getProjectId() + "", header);
-                        if (result == null || (result != null && result.getCode() != 200)) {
-                           throw new ServiceException(iq.getName() + "重新保存PDF信息失败");
-                        }
+                        /*===============执行批量任务===============*/
+                        this.reSigningEVisaStatusSync(informationQueries);
                     } catch (Exception e) {
-                        e.printStackTrace();
-                        //如果失败 修改pdf和e_visa_pdf_url 路径
-                        informationQueryService.update(new LambdaUpdateWrapper<InformationQuery>()
-                                .eq(InformationQuery::getId, dto.getId())
-                                .set(InformationQuery::getEVisaPdfUrl, iq.getEVisaPdfUrl())
-                                .set(InformationQuery::getPdfUrl, iq.getPdfUrl()));
+                        log.error("执行重刷任务失败,任务ID列表:{}", collect, e);
+                        // 可选:回滚状态或标记为失败
                     }
-                }
+                }, executor).exceptionally(throwable -> {
+                    log.error("异步任务执行异常,任务ID列表:{}", collect, throwable);
+                    return null;
+                });
             }
-        } catch (Exception e) {
-            throw new RuntimeException(e);
-        }finally {
-            //删除缓f存
-            bladeRedis.del(s);
         }
+        log.info("队列数量{}", executor.getQueue().size());
+        log.info("活跃数量{}", executor.getActiveCount());
+        log.info("总共数量{}", executor.getTaskCount());
+        log.info("完成数量{}", executor.getCompletedTaskCount());
     }
 
-    R saveNodePdf(String classify, String nodePKeyIds, String contractId, String projectId, String header) throws Exception {
-        return excelTabClient.synPDFInfo(contractId, nodePKeyIds, classify, projectId, header);
+
+    R saveNodePdf(String classify, String nodePKeyIds, String contractId, String projectId, String header) {
+        try {
+            //创建一个历史用户token用户调用其他地方的controller  TODO 用户问题待解决
+            header = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0ZW5hbnRfaWQiOiIwMDAwMDAiLCJ1c2VyX25hbWUiOiJsaWhiIiwicmVhbF9uYW1lIjoi5p2O5rW35YW1IiwiYXZhdGFyIjoiIiwiYXV0aG9yaXRpZXMiOlsiYWRtaW5pc3RyYXRvciJdLCJjbGllbnRfaWQiOiJjbGllbnQiLCJyb2xlX25hbWUiOiJhZG1pbmlzdHJhdG9yIiwibGljZW5zZSI6InBvd2VyZWQgYnkgYmxhZGV4IiwicG9zdF9pZCI6IiIsInVzZXJfaWQiOiIxOTEyNzE1MDg3NjU0Mzk1OTA2Iiwicm9sZV9pZCI6IjExMjM1OTg4MTY3Mzg2NzUyMDEiLCJwaG9uZSI6IjE1MzIwMjU2NTEzIiwic2NvcGUiOlsiYWxsIl0sIm5pY2tfbmFtZSI6Iuadjua1t-WFtSIsIm9hdXRoX2lkIjoiIiwiZGV0YWlsIjp7InR5cGUiOiJ3ZWIifSwiZXhwIjoxNzUzNDUyNjIyLCJkZXB0X2lkIjoiMTUzNjk4MzA1NjM2MjM4MTMxMyIsImp0aSI6Ijc0ZjhkOGE5LWMwZWItNDI4Ni04NGFhLTBhMGI2ODMzMjc3NCIsImFjY291bnQiOiJsaWhiIn0.A9GM1dCZkww-HJoZvwe4yLV8fPA80lPOvfgd-gZyumI";
+
+            return excelTabClient.synPDFInfo(contractId, nodePKeyIds, classify, projectId, header);
+        } catch (Exception e) {
+            e.printStackTrace();
+            return null;
+        }
     }
 }