|
@@ -1,20 +1,15 @@
|
|
|
package com.fjhx.service.excel.impl;
|
|
|
|
|
|
-import cn.hutool.core.io.IoUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
-import com.alibaba.excel.EasyExcel;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.fjhx.base.BaseEntity;
|
|
|
import com.fjhx.base.Condition;
|
|
|
-import com.fjhx.config.TaskPoolConfig;
|
|
|
import com.fjhx.constants.VRedisKeyConstant;
|
|
|
import com.fjhx.entity.FileInfo;
|
|
|
import com.fjhx.entity.excel.ExcelImportLog;
|
|
|
import com.fjhx.feign.IFileClient;
|
|
|
-import com.fjhx.listener.EasyExcelListener;
|
|
|
import com.fjhx.mapper.excel.ExcelImportLogMapper;
|
|
|
-import com.fjhx.params.excel.BaseExcelVo;
|
|
|
import com.fjhx.params.excel.ExcelProcessingProgress;
|
|
|
import com.fjhx.service.excel.ExcelImportLogService;
|
|
|
import com.fjhx.utils.Assert;
|
|
@@ -24,20 +19,11 @@ import org.springblade.core.redis.cache.BladeRedis;
|
|
|
import org.springblade.core.secure.utils.AuthUtil;
|
|
|
import org.springblade.core.tool.api.R;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
-import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.util.ObjectUtils;
|
|
|
-import org.springframework.util.StringUtils;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
-import java.io.BufferedInputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
-import java.util.function.Consumer;
|
|
|
|
|
|
/**
|
|
|
* <p>
|
|
@@ -50,10 +36,6 @@ import java.util.function.Consumer;
|
|
|
@Service
|
|
|
public class ExcelImportLogServiceImpl extends ServiceImpl<ExcelImportLogMapper, ExcelImportLog> implements ExcelImportLogService {
|
|
|
|
|
|
- @Qualifier(TaskPoolConfig.excelExecutor)
|
|
|
- @Autowired
|
|
|
- private ThreadPoolTaskExecutor executor;
|
|
|
-
|
|
|
@Resource
|
|
|
private BladeRedis bladeRedis;
|
|
|
|
|
@@ -61,112 +43,6 @@ public class ExcelImportLogServiceImpl extends ServiceImpl<ExcelImportLogMapper,
|
|
|
private IFileClient fileClient;
|
|
|
|
|
|
@Override
|
|
|
- public <T extends BaseExcelVo> void asyncRead(int pageSize, Long flag, MultipartFile file, Class<T> cls, Integer businessType,
|
|
|
- Consumer<List<T>> handleFun, Runnable errorFun) {
|
|
|
-
|
|
|
- String filename = file.getOriginalFilename();
|
|
|
- if (ObjectUtils.isEmpty(filename)) {
|
|
|
- throw new ExcelException("请上传文件!");
|
|
|
- }
|
|
|
- if ((!StringUtils.endsWithIgnoreCase(filename, ".xls") && !StringUtils.endsWithIgnoreCase(filename, ".xlsx"))) {
|
|
|
- throw new ExcelException("请上传正确的excel文件!");
|
|
|
- }
|
|
|
-
|
|
|
- InputStream inputStream;
|
|
|
- try {
|
|
|
- inputStream = new BufferedInputStream(file.getInputStream());
|
|
|
- } catch (IOException e) {
|
|
|
- throw new ExcelException("文件读取失败!");
|
|
|
- }
|
|
|
-
|
|
|
- // 导入信息放入redis缓存
|
|
|
- ExcelProcessingProgress excelProcessingProgress = new ExcelProcessingProgress();
|
|
|
- excelProcessingProgress.setStatus(1); // 预处理阶段
|
|
|
- excelProcessingProgress.setPercentage(5); // 进度 5%
|
|
|
- setProgress(flag, excelProcessingProgress); // 保存
|
|
|
-
|
|
|
- executor.execute(() -> {
|
|
|
- try {
|
|
|
- // 开始时间
|
|
|
- long start = System.currentTimeMillis();
|
|
|
-
|
|
|
- // 信息保存到数据库
|
|
|
- saveLog(flag, businessType, file);
|
|
|
-
|
|
|
- // 开始处理 进度 10%
|
|
|
- verificationAndUpdateProgress(flag, 2, 10);
|
|
|
-
|
|
|
- EasyExcel.read(inputStream, cls, new EasyExcelListener<T>(
|
|
|
- pageSize,
|
|
|
- (list, totalLine, readLine) -> {
|
|
|
-
|
|
|
- try {
|
|
|
- handleFun.accept(list);
|
|
|
- } catch (Exception e) {
|
|
|
- // 已完成
|
|
|
- excelProcessingProgress.setStatus(4);
|
|
|
- excelProcessingProgress.setPercentage(0);
|
|
|
- excelProcessingProgress.setErrorMsg(e.getMessage());
|
|
|
- setProgress(flag, excelProcessingProgress); // 保存
|
|
|
- throw e;
|
|
|
- }
|
|
|
-
|
|
|
- if (totalLine == readLine) {
|
|
|
- editLog(start, flag, 1);
|
|
|
- // 已完成
|
|
|
- verificationAndUpdateProgress(flag, 5, 100);
|
|
|
- } else {
|
|
|
- // 处理中
|
|
|
- verificationAndUpdateProgress(flag, 3, 10 + readLine * 90 / totalLine);
|
|
|
- }
|
|
|
- },
|
|
|
- (exception) -> {
|
|
|
- exception.printStackTrace();
|
|
|
- errorFun.run();
|
|
|
- // 用户取消
|
|
|
- if (exception instanceof ExcelException) {
|
|
|
- editLog(start, flag, 3);
|
|
|
- verificationAndUpdateProgress(flag, 6, 0);
|
|
|
- }
|
|
|
- // 发生异常
|
|
|
- else {
|
|
|
- editLog(start, flag, 2);
|
|
|
- verificationAndUpdateProgress(flag, 4, 0);
|
|
|
- exception.printStackTrace();
|
|
|
- throw new ExcelException("导入异常");
|
|
|
- }
|
|
|
- })).doReadAll();
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- log.error(e.getMessage());
|
|
|
- } finally {
|
|
|
- IoUtil.close(inputStream);
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public ExcelProcessingProgress getReadRate(Long flag) {
|
|
|
- ExcelProcessingProgress excelProcessingProgress = bladeRedis.get(VRedisKeyConstant.EXCEL_FLAG_KEY + flag);
|
|
|
-
|
|
|
- // 如果处理完成、用户取消、发生异常,删除缓存
|
|
|
- if (excelProcessingProgress != null && excelProcessingProgress.getStatus() > 3) {
|
|
|
- bladeRedis.del(VRedisKeyConstant.EXCEL_FLAG_KEY + flag);
|
|
|
- }
|
|
|
-
|
|
|
- return excelProcessingProgress;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void stopRead(Long flag) {
|
|
|
- // 导入信息放入redis缓存
|
|
|
- ExcelProcessingProgress excelProcessingProgress = new ExcelProcessingProgress();
|
|
|
- excelProcessingProgress.setStatus(6); // 预处理阶段
|
|
|
- excelProcessingProgress.setPercentage(0); // 进度 0%
|
|
|
- setProgress(flag, excelProcessingProgress);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
public void saveLog(Long flag, Integer businessType, MultipartFile file) {
|
|
|
|
|
|
R<FileInfo> fileInfoR = fileClient.uploadFile(file);
|