|
@@ -1,7 +1,13 @@
|
|
|
package org.springblade.business.sync;
|
|
|
|
|
|
+import cn.hutool.core.date.DateTime;
|
|
|
+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;
|
|
@@ -9,60 +15,150 @@ import org.springblade.business.service.IInformationQueryService;
|
|
|
import org.springblade.core.log.exception.ServiceException;
|
|
|
import org.springblade.core.redis.cache.BladeRedis;
|
|
|
import org.springblade.core.tool.api.R;
|
|
|
+import org.springblade.manager.entity.WbsTreeSynchronousRecord;
|
|
|
import org.springblade.manager.feign.ExcelTabClient;
|
|
|
+import org.springblade.system.user.feign.IUserClient;
|
|
|
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.ArrayList;
|
|
|
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;
|
|
|
+
|
|
|
+ //用户
|
|
|
+ @Resource
|
|
|
+ private IUserClient userClient;
|
|
|
+
|
|
|
+
|
|
|
+ public void reSigningEVisaStatusSync(List<InformationQuery> dtos, String header) {
|
|
|
+ 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::getUpdateTime, DateTime.now())
|
|
|
+
|
|
|
+ .set(InformationQuery::getPdfUrl, null));
|
|
|
+ R result = this.saveNodePdf(dto.getClassify() + "", dto.getWbsId() + "", dto.getContractId() + "", dto.getProjectId() + "", header);
|
|
|
+
|
|
|
+ LambdaUpdateWrapper<InformationQuery> lambda = Wrappers.<InformationQuery>update().lambda();
|
|
|
+ if (result == null || result.getCode() != 200) {
|
|
|
+ //重签失败
|
|
|
+ lambda.set(InformationQuery::getSaveAgain, 3)
|
|
|
+ .set(InformationQuery::getEVisaPdfUrl, dto.getEVisaPdfUrl())
|
|
|
+ .setSql("save_again_count = save_again_count + 1")
|
|
|
+ .set(InformationQuery::getPdfUrl, dto.getPdfUrl());
|
|
|
+ } else {
|
|
|
+ //成功重签
|
|
|
+ lambda.set(InformationQuery::getSaveAgain, 2);
|
|
|
+ }
|
|
|
+ lambda.set(InformationQuery::getUpdateTime, DateTime.now());
|
|
|
+ lambda.eq(InformationQuery::getId, dto.getId());
|
|
|
+
|
|
|
+ informationQueryService.update(lambda);
|
|
|
+ }
|
|
|
+ log.info("数据重刷完毕,线程名称:{}", Thread.currentThread().getName());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 定时检查save-again = 1 的数据 查看更新时间与当前时间是否超过30分钟,如果超过 30分钟,则修改状态为0
|
|
|
+ * 一个小时检查一次
|
|
|
+ */
|
|
|
+ @Scheduled(cron = "0 0 * * * ?")
|
|
|
+ public void updateStuckTask(){
|
|
|
+ List<InformationQuery> list = informationQueryService.list(Wrappers.<InformationQuery>lambdaQuery()
|
|
|
+ .eq(InformationQuery::getSaveAgain, 1)
|
|
|
+ .eq(InformationQuery::getIsDeleted, 0));
|
|
|
+
|
|
|
+ List<Long> ids = new ArrayList<>();
|
|
|
+ for (InformationQuery informationQuery : list) {
|
|
|
+ //更新时间 + 半个小时 < 当前时间
|
|
|
+ if (informationQuery.getUpdateTime().getTime() + 1800000 < System.currentTimeMillis()) {
|
|
|
+ ids.add(informationQuery.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ informationQueryService.update(null, Wrappers.<InformationQuery>lambdaUpdate()
|
|
|
+ .set(InformationQuery::getSaveAgain, 0)
|
|
|
+ .in(InformationQuery::getId, ids));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 重刷定时任务 使用多线程的方式去跑
|
|
|
+ */
|
|
|
+ @Scheduled(fixedDelay = 60000)
|
|
|
+ public void saveAgainTask() {
|
|
|
+ //获取token
|
|
|
+ String header = userClient.getTokenByUser("admin");
|
|
|
+
|
|
|
+ List<InformationQuery> list = informationQueryService.list(Wrappers.<InformationQuery>lambdaQuery()
|
|
|
+ .in(InformationQuery::getSaveAgain, 0, 3)
|
|
|
+ .eq(InformationQuery::getIsDeleted, 0)
|
|
|
+ //失败重刷次数小于5次
|
|
|
+ .lt(InformationQuery::getSaveAgainCount, 5)
|
|
|
+ .last("limit 50"));
|
|
|
+ if(CollectionUtils.isEmpty(list)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ 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)
|
|
|
+ .set(InformationQuery::getUpdateTime, DateTime.now())
|
|
|
+ .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, header);
|
|
|
} 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 {
|
|
|
+ return excelTabClient.synPDFInfo(contractId, nodePKeyIds, classify, projectId, header);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|
|
|
}
|