|
|
@ -45,15 +45,19 @@ import com.google.common.cache.CacheBuilder; |
|
|
|
import lombok.Data; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.collections4.CollectionUtils; |
|
|
|
import org.apache.commons.fileupload.FileItem; |
|
|
|
import org.apache.commons.fileupload.FileItemFactory; |
|
|
|
import org.apache.commons.fileupload.disk.DiskFileItemFactory; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.apache.http.entity.ContentType; |
|
|
|
import org.apache.poi.ss.usermodel.Workbook; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.web.multipart.commons.CommonsMultipartFile; |
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
import java.io.File; |
|
|
|
import java.io.IOException; |
|
|
|
import java.net.URLEncoder; |
|
|
|
import java.io.*; |
|
|
|
import java.nio.file.Path; |
|
|
|
import java.util.*; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
@ -125,6 +129,8 @@ public class IcResiUserImportServiceImpl implements IcResiUserImportService, Res |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private EpmetCommonServiceOpenFeignClient commonServiceOpenFeignClient; |
|
|
|
@Autowired |
|
|
|
private OssFeignClient ossFeignClient; |
|
|
|
|
|
|
|
/** |
|
|
|
* 字表中不需要的列 |
|
|
@ -217,7 +223,7 @@ public class IcResiUserImportServiceImpl implements IcResiUserImportService, Res |
|
|
|
* @param response 响应对象 |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
public void importIcResiInfoFromExcel(String importTaskId, List<FormItemResult> formItemList, String excelPathName, HttpServletResponse response) { |
|
|
|
public void importIcResiInfoFromExcel(String importTaskId, List<FormItemResult> formItemList, String excelPathName, HttpServletResponse response, Path importTempPath) { |
|
|
|
|
|
|
|
String loginUserId = loginUserUtil.getLoginUserId(); |
|
|
|
String loginUserApp = loginUserUtil.getLoginUserApp(); |
|
|
@ -268,26 +274,22 @@ public class IcResiUserImportServiceImpl implements IcResiUserImportService, Res |
|
|
|
Map<String, ResiImportChangedData> categoryChangedResis = newlyOrChangedResi.get().getCategoryChangedResis(); |
|
|
|
Map<String, ResiImportChangedData> transferedResis = newlyOrChangedResi.get().getTransferedResis(); |
|
|
|
|
|
|
|
log.info("类别变动居民数:{}", categoryChangedResis.size()); |
|
|
|
log.info("调动居民数:{}", transferedResis.size()); |
|
|
|
|
|
|
|
//保存调动或者变更记录
|
|
|
|
saveNewResiCategoryRecord(); |
|
|
|
saveResiCategoryChangedRecord(categoryChangedResis); |
|
|
|
saveTransferedResiRecord(transferedResis); |
|
|
|
|
|
|
|
hasErrorRows = hasErrorRows(); |
|
|
|
|
|
|
|
try { |
|
|
|
// todo 做了导入记录之后,这里就要判断,没有错误就不生成文件了
|
|
|
|
downLoadResults(hasErrorRows, response); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("【导入IC居民附加信息】下载导入结果信息失败:{}", ExceptionUtils.getErrorStackTrace(e)); |
|
|
|
} |
|
|
|
|
|
|
|
// 更新上传记录
|
|
|
|
if (hasErrorRows) { |
|
|
|
finishImportTask(importTaskId, loginUserId, ImportTaskConstants.PROCESS_STATUS_FINISHED_FAIL,null, null); |
|
|
|
// 生成错误描述文件 && 更新上传记录
|
|
|
|
LinkedList<ErrorRow> errorRows = getErrorRows(); |
|
|
|
if (CollectionUtils.isNotEmpty(errorRows)) { |
|
|
|
String resultDescFilePath = null; |
|
|
|
try { |
|
|
|
resultDescFilePath = generateResultDescFile(errorRows, importTempPath); |
|
|
|
} catch (Exception e) { |
|
|
|
String errorMsg = ExceptionUtils.getErrorStackTrace(e); |
|
|
|
log.error("【居民信息导入】", errorMsg); |
|
|
|
} |
|
|
|
finishImportTask(importTaskId, loginUserId, ImportTaskConstants.PROCESS_STATUS_FINISHED_FAIL,null, resultDescFilePath); |
|
|
|
} else { |
|
|
|
finishImportTask(importTaskId, loginUserId, ImportTaskConstants.PROCESS_STATUS_FINISHED_SUCCESS,null, null); |
|
|
|
} |
|
|
@ -314,19 +316,16 @@ public class IcResiUserImportServiceImpl implements IcResiUserImportService, Res |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 是否有错误行 |
|
|
|
* 返回错误行 |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
private Boolean hasErrorRows() { |
|
|
|
boolean hasError = false; |
|
|
|
Map<String, List<ErrorRow>> tableAndErrorRows = errorRows.get(); |
|
|
|
for (Map.Entry<String, List<ErrorRow>> entry:tableAndErrorRows.entrySet()) { |
|
|
|
if (entry.getValue().size() != 0) { |
|
|
|
hasError = true; |
|
|
|
} |
|
|
|
private LinkedList<ErrorRow> getErrorRows() { |
|
|
|
LinkedList<ErrorRow> list = new LinkedList<>(); |
|
|
|
for (Map.Entry<String, List<ErrorRow>> entry : errorRows.get().entrySet()) { |
|
|
|
list.addAll(entry.getValue()); |
|
|
|
} |
|
|
|
|
|
|
|
return hasError; |
|
|
|
return list; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -1027,34 +1026,47 @@ public class IcResiUserImportServiceImpl implements IcResiUserImportService, Res |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 下载导入结果 |
|
|
|
* @param response |
|
|
|
* 生成导入结果 |
|
|
|
* @throws IOException |
|
|
|
*/ |
|
|
|
public void downLoadResults(Boolean hasErrorRows, HttpServletResponse response) throws IOException { |
|
|
|
String fileName; |
|
|
|
|
|
|
|
// 判断是否有错误信息,以确定文件名
|
|
|
|
if (hasErrorRows) { |
|
|
|
fileName = "导入失败条目清单.xls"; |
|
|
|
} else { |
|
|
|
fileName = "导入成功.xls"; |
|
|
|
public String generateResultDescFile(LinkedList<ErrorRow> errorRows, Path importTempDir) throws IOException { |
|
|
|
errorRows.forEach(row -> {row.setSheetName(IcResiUserTableEnum.getObjectByTableName(row.tableName).getTableComment());}); |
|
|
|
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("导入失败的数据列表","导入失败的数据列表"), |
|
|
|
ErrorRow.class, errorRows); |
|
|
|
|
|
|
|
// 文件名
|
|
|
|
String resultDescFileName = UUID.randomUUID().toString().concat(".xls"); |
|
|
|
|
|
|
|
FileItemFactory factory = new DiskFileItemFactory(16, null); |
|
|
|
FileItem fileItem = factory.createItem("file", ContentType.APPLICATION_OCTET_STREAM.toString(), true, resultDescFileName); |
|
|
|
OutputStream os = fileItem.getOutputStream(); |
|
|
|
Result<UploadImgResultDTO> uploadResult = null; |
|
|
|
try { |
|
|
|
workbook.write(os); |
|
|
|
uploadResult = ossFeignClient.uploadImportTaskDescFile(new CommonsMultipartFile(fileItem)); |
|
|
|
} catch (Exception e) { |
|
|
|
String errormsg = ExceptionUtils.getErrorStackTrace(e); |
|
|
|
log.error("【居民信息导入】上传错误描述文件:{}", errormsg); |
|
|
|
} finally { |
|
|
|
try { |
|
|
|
os.close(); |
|
|
|
} catch (IOException e) { |
|
|
|
String errormsg = ExceptionUtils.getErrorStackTrace(e); |
|
|
|
log.error("【居民信息导入】上传错误描述文件关闭输出流:{}", errormsg); |
|
|
|
} |
|
|
|
try { |
|
|
|
fileItem.delete(); |
|
|
|
} catch (Exception e) { |
|
|
|
String errormsg = ExceptionUtils.getErrorStackTrace(e); |
|
|
|
log.error("【居民信息导入】上传错误描述文件删除临时文件:{}", errormsg); |
|
|
|
} |
|
|
|
} |
|
|
|
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); |
|
|
|
response.setHeader("content-Type", "application/vnd.ms-excel"); |
|
|
|
response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8")); |
|
|
|
|
|
|
|
LinkedList<ErrorRow> list = new LinkedList<>(); |
|
|
|
for (Map.Entry<String, List<ErrorRow>> entry : errorRows.get().entrySet()) { |
|
|
|
list.addAll(entry.getValue()); |
|
|
|
if (uploadResult == null || !uploadResult.success()) { |
|
|
|
log.error("【居民信息导入】调用OSS上传结果描述文件失败"); |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
list.forEach(row -> {row.setSheetName(IcResiUserTableEnum.getObjectByTableName(row.tableName).getTableComment());}); |
|
|
|
|
|
|
|
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("导入失败的列表","导入失败列表(没有数据说明全部成功)"), |
|
|
|
ErrorRow.class, list); |
|
|
|
|
|
|
|
workbook.write(response.getOutputStream()); |
|
|
|
return uploadResult.getData().getUrl(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|