提交 64d956f9 authored 作者: zhoushaopan's avatar zhoushaopan

feat(自查模块): 优化了发起自查业务时间

优化了发起自查业务时间
上级 7a3ced7a
package com.tykj.dev.misc.utils;
import com.beust.jcommander.internal.Lists;
import org.springframework.util.CollectionUtils;
import java.util.List;
/**
* @author zsp
* @version 1.0
* @date 2022/7/5 9:23
*/
public class SplitListUtils {
/**
* 拆分集合
*
* @param <T> 泛型对象
* @param resList 需要拆分的集合
* @param subListLength 每个子集合的元素个数
* @return 返回拆分后的各个集合组成的列表
**/
public static <T> List<List<T>> split(List<T> resList, int subListLength) {
if (CollectionUtils.isEmpty(resList) || subListLength <= 0) {
return Lists.newArrayList();
}
List<List<T>> ret = Lists.newArrayList();
int size = resList.size();
if (size <= subListLength) {
// 数据量不足 subListLength 指定的大小
ret.add(resList);
} else {
int pre = size / subListLength;
int last = size % subListLength;
// 前面pre个集合,每个大小都是 subListLength 个元素
for (int i = 0; i < pre; i++) {
List<T> itemList = Lists.newArrayList();
for (int j = 0; j < subListLength; j++) {
itemList.add(resList.get(i * subListLength + j));
}
ret.add(itemList);
}
// last的进行处理
if (last > 0) {
List<T> itemList = Lists.newArrayList();
for (int i = 0; i < last; i++) {
itemList.add(resList.get(pre * subListLength + i));
}
ret.add(itemList);
}
}
return ret;
}
}
...@@ -41,10 +41,7 @@ import com.tykj.dev.misc.base.BusinessEnum; ...@@ -41,10 +41,7 @@ import com.tykj.dev.misc.base.BusinessEnum;
import com.tykj.dev.misc.base.StatusEnum; import com.tykj.dev.misc.base.StatusEnum;
import com.tykj.dev.misc.exception.ApiException; import com.tykj.dev.misc.exception.ApiException;
import com.tykj.dev.misc.qrcode.vo.TaskData; import com.tykj.dev.misc.qrcode.vo.TaskData;
import com.tykj.dev.misc.utils.JacksonUtil; import com.tykj.dev.misc.utils.*;
import com.tykj.dev.misc.utils.ResultUtil;
import com.tykj.dev.misc.utils.StringSplitUtil;
import com.tykj.dev.misc.utils.TaskDisposeUtil;
import com.tykj.dev.socket.MyWebSocket; import com.tykj.dev.socket.MyWebSocket;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
...@@ -57,6 +54,7 @@ import org.springframework.http.ResponseEntity; ...@@ -57,6 +54,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.BatchPreparedStatementSetter; import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StopWatch;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
...@@ -65,6 +63,7 @@ import java.sql.PreparedStatement; ...@@ -65,6 +63,7 @@ import java.sql.PreparedStatement;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.*; import java.util.*;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -339,7 +338,7 @@ public class SelfCheckController { ...@@ -339,7 +338,7 @@ public class SelfCheckController {
@ApiOperation(value = "发起自查业务", notes = "可以通过这个接口发起自查业务") @ApiOperation(value = "发起自查业务", notes = "可以通过这个接口发起自查业务")
@PostMapping(value = "/addBill") @PostMapping(value = "/addBill")
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public ResponseEntity addSelfExaminationBill(@RequestBody @Validated SelfCheckSaveVo selfCheckSaveVo) { public ResponseEntity addSelfExaminationBill(@RequestBody @Validated SelfCheckSaveVo selfCheckSaveVo) throws InterruptedException {
//当前登录的用户ID //当前登录的用户ID
Integer currentUserId = userUtils.getCurrentUserId(); Integer currentUserId = userUtils.getCurrentUserId();
//当前登录的单位ID //当前登录的单位ID
...@@ -411,7 +410,9 @@ public class SelfCheckController { ...@@ -411,7 +410,9 @@ public class SelfCheckController {
// executor.execute(()->{ // executor.execute(()->{
// historyDeviceBillService.batchSave(historyDeviceBillList); // historyDeviceBillService.batchSave(historyDeviceBillList);
// }); // });
historyDeviceBillService.batchSave(historyDeviceBillList); //批量保存
batchSave(historyDeviceBillList);
// historyDeviceBillService.batchSave(historyDeviceBillList);
} }
}else { }else {
// 是0 和1 // 是0 和1
...@@ -433,7 +434,9 @@ public class SelfCheckController { ...@@ -433,7 +434,9 @@ public class SelfCheckController {
historyDeviceBillList.add(historyDeviceBill); historyDeviceBillList.add(historyDeviceBill);
}); });
//保存 //保存
historyDeviceBillService.batchSave(historyDeviceBillList); batchSave(historyDeviceBillList);
// historyDeviceBillService.batchSave(historyDeviceBillList);
// executor.execute(()->{ // executor.execute(()->{
// historyDeviceBillService.batchSave(historyDeviceBillList); // historyDeviceBillService.batchSave(historyDeviceBillList);
// }); // });
...@@ -456,7 +459,9 @@ public class SelfCheckController { ...@@ -456,7 +459,9 @@ public class SelfCheckController {
historyDeviceBillList.add(historyDeviceBill); historyDeviceBillList.add(historyDeviceBill);
}); });
//保存 //保存
historyDeviceBillService.batchSave(historyDeviceBillList); batchSave(historyDeviceBillList);
// historyDeviceBillService.batchSave(historyDeviceBillList);
// executor.execute(()->{ // executor.execute(()->{
// historyDeviceBillService.batchSave(historyDeviceBillList); // historyDeviceBillService.batchSave(historyDeviceBillList);
// }); // });
...@@ -567,6 +572,23 @@ public class SelfCheckController { ...@@ -567,6 +572,23 @@ public class SelfCheckController {
return ResultUtil.success(selfExaminationBillEntity1); return ResultUtil.success(selfExaminationBillEntity1);
} }
private void batchSave(List<HistoryDeviceBill> historyDeviceBillList) throws InterruptedException {
StopWatch stopWatch = new StopWatch("保存历史存单时间");
stopWatch.start();
List<List<HistoryDeviceBill>> split = SplitListUtils.split(historyDeviceBillList, 200);
CountDownLatch countDownLatch = new CountDownLatch(split.size());
for (int i = 0; i < split.size(); i++) {
int finalI = i;
executor.execute(()->{
historyDeviceBillService.batchSave(split.get(finalI));
countDownLatch.countDown();
});
}
countDownLatch.await();
stopWatch.stop();
log.info("保存历史存单时间:{}",stopWatch.getTotalTimeMillis()+"ms");
}
@ApiOperation(value = "自查审核", notes = "可以通过这个接口自查审核") @ApiOperation(value = "自查审核", notes = "可以通过这个接口自查审核")
@PostMapping(value = "/selfExaminationConfirm") @PostMapping(value = "/selfExaminationConfirm")
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论