| | |
| | | import org.springblade.common.param.CommonParamSet; |
| | | import org.springblade.common.utils.NodeTreeUtil; |
| | | import org.springblade.common.utils.SpringUtils; |
| | | import org.springblade.common.utils.ThreadPoolUtil; |
| | | import org.springblade.core.secure.utils.AuthUtil; |
| | | import org.springblade.core.tool.utils.BeanUtil; |
| | | import org.springblade.core.tool.utils.Func; |
| | |
| | | import org.springblade.modules.system.service.IUserService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Propagation; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.StopWatch; |
| | | |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import java.util.concurrent.*; |
| | | import java.util.concurrent.Future; |
| | | import java.util.concurrent.atomic.AtomicBoolean; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | |
| | | @Autowired |
| | | private IHouseRentalService iHouseRentalService; |
| | | |
| | | |
| | | @Autowired |
| | | private ThreadPoolUtil threadPoolUtil; |
| | | |
| | | |
| | | @Override |
| | |
| | | int importNum = 0; |
| | | int updateNum = 0; |
| | | int errorNum = 0; |
| | | |
| | | // 创建一个固定大小的线程池 |
| | | int numberOfThreads = Runtime.getRuntime().availableProcessors(); // 使用可用处理器数量作为线程数 |
| | | ExecutorService executorService = Executors.newFixedThreadPool(numberOfThreads); |
| | | |
| | | int runErrorNum = 0; |
| | | // 创建Future列表来保存每个任务的结果 |
| | | List<Future<Map<String, String>>> futures = new ArrayList<>(); |
| | | |
| | | for (ImportHouseholdExcel houseHoldExcel : data) { |
| | | Callable<Map<String, String>> task = () -> importHouseHold(houseHoldExcel, houseService, isCovered, isTenant); |
| | | futures.add(executorService.submit(task)); |
| | | // 提交任务 |
| | | futures.add(threadPoolUtil.submit(() -> { |
| | | // 这里执行你的异步任务 |
| | | return importHouseHold(houseHoldExcel, houseService, isCovered, isTenant); |
| | | })); |
| | | } |
| | | // 收集并打印结果 |
| | | for (Future<Map<String, String>> future : futures) { |
| | |
| | | } |
| | | // 获取并打印每个任务的结果 |
| | | } catch (Exception e) { |
| | | errorNum++; |
| | | runErrorNum++; |
| | | logger.error("获取异常-----》", e); |
| | | } |
| | | } |
| | | // 关闭线程池 |
| | | executorService.shutdown(); |
| | | try { |
| | | if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) { |
| | | executorService.shutdownNow(); |
| | | } |
| | | } catch (InterruptedException e) { |
| | | executorService.shutdownNow(); |
| | | } |
| | | StringBuilder builder = new StringBuilder("导入完成!"); |
| | | builder.append("其中本次表格共有 ").append(totalNum).append(" 条数据,") |
| | |
| | | if (errorNum > 0) { |
| | | builder.append("共有 ").append(errorNum).append(" 条数据由于无姓名或门牌地址编码信息未导入!"); |
| | | } |
| | | if (runErrorNum > 0) { |
| | | builder.append("共有 ").append(runErrorNum).append(" 条数据由于数据其他信息异常未导入!"); |
| | | } |
| | | return builder.toString(); |
| | | } |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @Transactional(propagation = Propagation.REQUIRES_NEW) |
| | | public Map<String, String> importHouseHold(ImportHouseholdExcel houseHoldExcel, IHouseService houseService, Boolean isCovered, String isTenant) { |
| | | Map<String, String> objectObjectHashMap = new HashMap<>(); |
| | | HouseholdEntity householdEntity = Objects.requireNonNull(BeanUtil.copy(houseHoldExcel, HouseholdEntity.class)); |