|
@@ -9,9 +9,10 @@ import cn.hutool.poi.excel.cell.CellUtil;
|
|
|
import cn.tr.module.api.sys.storage.SysStorageApi;
|
|
import cn.tr.module.api.sys.storage.SysStorageApi;
|
|
|
import cn.tr.module.excel.row.dto.SysExportRowDTO;
|
|
import cn.tr.module.excel.row.dto.SysExportRowDTO;
|
|
|
import cn.tr.module.excel.row.service.ISysExportRowService;
|
|
import cn.tr.module.excel.row.service.ISysExportRowService;
|
|
|
-import org.apache.commons.io.IOUtils;
|
|
|
|
|
|
|
+import cn.tr.module.excel.sheet.enums.SheetImportStatus;
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.context.annotation.Lazy;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import cn.tr.module.excel.sheet.repository.SysExportSheetRepository;
|
|
import cn.tr.module.excel.sheet.repository.SysExportSheetRepository;
|
|
@@ -21,6 +22,8 @@ import cn.tr.module.excel.sheet.dto.SysExportSheetDTO;
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.ByteArrayOutputStream;
|
|
|
import java.io.File;
|
|
import java.io.File;
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+
|
|
|
import cn.tr.module.excel.sheet.service.ISysExportSheetService;
|
|
import cn.tr.module.excel.sheet.service.ISysExportSheetService;
|
|
|
import cn.tr.module.excel.mapper.SysExportSheetMapper;
|
|
import cn.tr.module.excel.mapper.SysExportSheetMapper;
|
|
|
/**
|
|
/**
|
|
@@ -32,12 +35,15 @@ import cn.tr.module.excel.mapper.SysExportSheetMapper;
|
|
|
@Service
|
|
@Service
|
|
|
public class SysExportSheetServiceImpl implements ISysExportSheetService {
|
|
public class SysExportSheetServiceImpl implements ISysExportSheetService {
|
|
|
@Autowired
|
|
@Autowired
|
|
|
|
|
+ @Lazy
|
|
|
private SysExportSheetRepository baseRepository;
|
|
private SysExportSheetRepository baseRepository;
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
|
|
|
+ @Lazy
|
|
|
private SysStorageApi sysStorageApi;
|
|
private SysStorageApi sysStorageApi;
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
|
|
|
+ @Lazy
|
|
|
private ISysExportRowService rowService;
|
|
private ISysExportRowService rowService;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -49,7 +55,16 @@ public class SysExportSheetServiceImpl implements ISysExportSheetService {
|
|
|
@Override
|
|
@Override
|
|
|
public SysExportSheetDTO selectSysExportSheetById(String id){
|
|
public SysExportSheetDTO selectSysExportSheetById(String id){
|
|
|
return SysExportSheetMapper.INSTANCE.convertDto(baseRepository.selectById(id));
|
|
return SysExportSheetMapper.INSTANCE.convertDto(baseRepository.selectById(id));
|
|
|
- };
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public boolean cancelImport(String id) throws Exception{
|
|
|
|
|
+ SysExportSheetPO exportSheet = baseRepository.selectById(id);
|
|
|
|
|
+ exportSheet.setStatus(SheetImportStatus.importCancel.getValue());
|
|
|
|
|
+ //取消导入生成导入excel
|
|
|
|
|
+ exportSheet.setExportFileId(createImportFile(exportSheet));
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 新增导入工作簿记录表
|
|
* 新增导入工作簿记录表
|
|
@@ -86,7 +101,8 @@ public class SysExportSheetServiceImpl implements ISysExportSheetService {
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public void updateRowResult(String sheetId, boolean success) throws Exception {
|
|
public void updateRowResult(String sheetId, boolean success) throws Exception {
|
|
|
SysExportSheetPO sheet = baseRepository.selectById(sheetId);
|
|
SysExportSheetPO sheet = baseRepository.selectById(sheetId);
|
|
|
- if(sheet==null){
|
|
|
|
|
|
|
+ if(sheet==null||SheetImportStatus.importCancel.getValue().equals(sheet.getStatus())){
|
|
|
|
|
+ //导入记录不存在或导入已取消
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
if(success){
|
|
if(success){
|
|
@@ -95,12 +111,36 @@ public class SysExportSheetServiceImpl implements ISysExportSheetService {
|
|
|
sheet.setFailCount(sheet.getFailCount()+1);
|
|
sheet.setFailCount(sheet.getFailCount()+1);
|
|
|
}
|
|
}
|
|
|
int importCount=sheet.getSuccessCount()+sheet.getFailCount();
|
|
int importCount=sheet.getSuccessCount()+sheet.getFailCount();
|
|
|
- sheet.setFinish(importCount==sheet.getTotalCount());
|
|
|
|
|
- if(Boolean.TRUE.equals(sheet.getFinish())){
|
|
|
|
|
|
|
+ if(importCount==sheet.getTotalCount()&&
|
|
|
|
|
+ //解析完成并且导入数目相同
|
|
|
|
|
+ SheetImportStatus.parsingSuccess.getValue().equals(sheet.getStatus())){
|
|
|
|
|
+ //导入完成
|
|
|
|
|
+ sheet.setStatus(SheetImportStatus.importSuccess.getValue());
|
|
|
|
|
+ sheet.setEndTime(new Date());
|
|
|
|
|
+ //生成导出文件
|
|
|
|
|
+ sheet.setExportFileId(createImportFile(sheet));
|
|
|
|
|
+ }
|
|
|
|
|
+ baseRepository.updateById(sheet);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void doSheetCallback(String sheetId, int totalCount) throws Exception {
|
|
|
|
|
+ SysExportSheetPO sheet = baseRepository.selectById(sheetId);
|
|
|
|
|
+ if(sheet==null){
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ int importCount=sheet.getSuccessCount()+sheet.getFailCount();
|
|
|
|
|
+ if(importCount==totalCount){
|
|
|
|
|
+ //导入成功
|
|
|
|
|
+ sheet.setStatus(SheetImportStatus.importSuccess.getValue());
|
|
|
sheet.setEndTime(new Date());
|
|
sheet.setEndTime(new Date());
|
|
|
//生成导出文件
|
|
//生成导出文件
|
|
|
sheet.setExportFileId(createImportFile(sheet));
|
|
sheet.setExportFileId(createImportFile(sheet));
|
|
|
|
|
+ }else if(!SheetImportStatus.importCancel.getValue().equals(sheet.getStatus())){
|
|
|
|
|
+ //没有导入成功且导入未取消
|
|
|
|
|
+ sheet.setStatus(SheetImportStatus.parsingSuccess.getValue());
|
|
|
}
|
|
}
|
|
|
|
|
+ sheet.setTotalCount(totalCount);
|
|
|
baseRepository.updateById(sheet);
|
|
baseRepository.updateById(sheet);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -108,29 +148,49 @@ public class SysExportSheetServiceImpl implements ISysExportSheetService {
|
|
|
String sheetId = sheetPO.getId();
|
|
String sheetId = sheetPO.getId();
|
|
|
byte[] fileContent = sysStorageApi.obtainContent(sheetPO.getImportFileId());
|
|
byte[] fileContent = sysStorageApi.obtainContent(sheetPO.getImportFileId());
|
|
|
List<SysExportRowDTO> rowRecords = rowService.selectSysExportRowBySheetId(sheetId);
|
|
List<SysExportRowDTO> rowRecords = rowService.selectSysExportRowBySheetId(sheetId);
|
|
|
|
|
+ //导入的行结果映射
|
|
|
|
|
+ Map<Integer, SysExportRowDTO> rowIndexMap = rowRecords.stream()
|
|
|
|
|
+ .collect(Collectors.groupingBy(SysExportRowDTO::getRowIndex, Collectors.collectingAndThen(Collectors.toList(), CollectionUtil::getFirst)));
|
|
|
|
|
+
|
|
|
File tempFile = FileUtil.createTempFile();
|
|
File tempFile = FileUtil.createTempFile();
|
|
|
|
|
+
|
|
|
IoUtil.write(FileUtil.getOutputStream(tempFile),true,fileContent);
|
|
IoUtil.write(FileUtil.getOutputStream(tempFile),true,fileContent);
|
|
|
ExcelWriter excelWriter = ExcelUtil.getReader(tempFile).getWriter();
|
|
ExcelWriter excelWriter = ExcelUtil.getReader(tempFile).getWriter();
|
|
|
|
|
+ excelWriter.getWorkbook().removeSheetAt(1);
|
|
|
|
|
+ excelWriter.renameSheet(0,"导入结果");
|
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
byte[] content=null;
|
|
byte[] content=null;
|
|
|
try {
|
|
try {
|
|
|
- rowRecords.forEach(rowRecord->{
|
|
|
|
|
|
|
+ Sheet sheet = excelWriter.getSheet();
|
|
|
|
|
+ int lastRowNum =sheet.getLastRowNum();
|
|
|
|
|
+ int maxRowCount=lastRowNum;
|
|
|
|
|
+ while (lastRowNum>0){
|
|
|
String value="";
|
|
String value="";
|
|
|
- if(Boolean.TRUE.equals(rowRecord.getSuccess())){
|
|
|
|
|
- value="导入成功";
|
|
|
|
|
- }else {
|
|
|
|
|
- value="失败原因:{"+rowRecord.getErrorMsg()+"}";
|
|
|
|
|
|
|
+ SysExportRowDTO rowRecord = rowIndexMap.get(lastRowNum);
|
|
|
|
|
+ if(rowRecord==null||!Boolean.TRUE.equals(rowRecord.getSuccess())){
|
|
|
|
|
+ value="失败原因:{"+
|
|
|
|
|
+ (Objects.isNull(rowRecord)?"导入中断":rowRecord.getErrorMsg())
|
|
|
|
|
+ +"}";
|
|
|
|
|
+ Cell cell = excelWriter.getOrCreateCell(sheetPO.getColumnCount() + 3, lastRowNum);
|
|
|
|
|
+ CellUtil.setCellValue(cell,value);
|
|
|
|
|
+ cell.setCellStyle(createFailCellStyle(excelWriter,false));
|
|
|
|
|
+ }else if(Boolean.TRUE.equals(rowRecord.getSuccess())){
|
|
|
|
|
+ if(lastRowNum==maxRowCount){
|
|
|
|
|
+ sheet.removeRow(sheet.getRow(lastRowNum));
|
|
|
|
|
+ }else {
|
|
|
|
|
+ sheet.shiftRows(lastRowNum+1,maxRowCount,-1);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
- Cell cell = excelWriter.getOrCreateCell(rowRecord.getMaxColumnIndex() + 3, rowRecord.getRowIndex());
|
|
|
|
|
- CellUtil.setCellValue(cell,value);
|
|
|
|
|
- cell.setCellStyle(createFailCellStyle(excelWriter,Boolean.TRUE.equals(rowRecord.getSuccess())));
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ --lastRowNum;
|
|
|
|
|
+ }
|
|
|
excelWriter.autoSizeColumn(CollectionUtil.getFirst(rowRecords).getMaxColumnIndex() + 3);
|
|
excelWriter.autoSizeColumn(CollectionUtil.getFirst(rowRecords).getMaxColumnIndex() + 3);
|
|
|
}finally {
|
|
}finally {
|
|
|
excelWriter.flush(out);
|
|
excelWriter.flush(out);
|
|
|
out.flush();
|
|
out.flush();
|
|
|
content = out.toByteArray();
|
|
content = out.toByteArray();
|
|
|
- IOUtils.close(out,excelWriter);
|
|
|
|
|
|
|
+ IoUtil.close(out);
|
|
|
|
|
+ IoUtil.close(excelWriter);
|
|
|
}
|
|
}
|
|
|
if(content!=null){
|
|
if(content!=null){
|
|
|
return sysStorageApi.upload(content,sheetPO.getFileName());
|
|
return sysStorageApi.upload(content,sheetPO.getFileName());
|