|
@ -1,27 +1,49 @@ |
|
|
package com.epmet.service.impl; |
|
|
package com.epmet.service.impl; |
|
|
|
|
|
|
|
|
import com.alibaba.excel.EasyExcel; |
|
|
import com.alibaba.excel.EasyExcel; |
|
|
|
|
|
import com.alibaba.excel.ExcelWriter; |
|
|
import com.alibaba.excel.converters.Converter; |
|
|
import com.alibaba.excel.converters.Converter; |
|
|
import com.alibaba.excel.converters.ReadConverterContext; |
|
|
import com.alibaba.excel.converters.ReadConverterContext; |
|
|
import com.alibaba.excel.enums.CellDataTypeEnum; |
|
|
import com.alibaba.excel.enums.CellDataTypeEnum; |
|
|
import com.alibaba.excel.read.metadata.ReadSheet; |
|
|
import com.alibaba.excel.read.metadata.ReadSheet; |
|
|
|
|
|
import com.alibaba.excel.write.builder.ExcelWriterBuilder; |
|
|
|
|
|
import com.epmet.commons.tools.constant.ServiceConstant; |
|
|
|
|
|
import com.epmet.commons.tools.enums.BizTypeEnum; |
|
|
import com.epmet.commons.tools.exception.EpmetErrorCode; |
|
|
import com.epmet.commons.tools.exception.EpmetErrorCode; |
|
|
import com.epmet.commons.tools.exception.EpmetException; |
|
|
import com.epmet.commons.tools.exception.EpmetException; |
|
|
|
|
|
import com.epmet.commons.tools.exception.ExceptionUtils; |
|
|
import com.epmet.commons.tools.exception.ValidateException; |
|
|
import com.epmet.commons.tools.exception.ValidateException; |
|
|
|
|
|
import com.epmet.commons.tools.feign.ResultDataResolver; |
|
|
|
|
|
import com.epmet.commons.tools.utils.DateUtils; |
|
|
import com.epmet.commons.tools.utils.EpmetRequestHolder; |
|
|
import com.epmet.commons.tools.utils.EpmetRequestHolder; |
|
|
|
|
|
import com.epmet.commons.tools.utils.ExcelUtils; |
|
|
|
|
|
import com.epmet.commons.tools.utils.FileUtils; |
|
|
|
|
|
import com.epmet.constants.ImportTaskConstants; |
|
|
import com.epmet.dao.*; |
|
|
import com.epmet.dao.*; |
|
|
|
|
|
import com.epmet.dto.result.ImportTaskCommonResultDTO; |
|
|
|
|
|
import com.epmet.dto.result.UploadImgResultDTO; |
|
|
import com.epmet.entity.*; |
|
|
import com.epmet.entity.*; |
|
|
import com.epmet.enums.LingShanSpecialCrowdTypeEnums; |
|
|
import com.epmet.enums.LingShanSpecialCrowdTypeEnums; |
|
|
import com.epmet.excel.data.*; |
|
|
import com.epmet.excel.data.*; |
|
|
import com.epmet.excel.handler.*; |
|
|
import com.epmet.excel.handler.*; |
|
|
import com.epmet.exceptions.ReadExcelHeaderOnlyException; |
|
|
import com.epmet.exceptions.ReadExcelHeaderOnlyException; |
|
|
|
|
|
import com.epmet.feign.OssFeignClient; |
|
|
import com.epmet.service.LingShanSpecialCrowdService; |
|
|
import com.epmet.service.LingShanSpecialCrowdService; |
|
|
|
|
|
import com.epmet.utils.ImportTaskUtils; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
|
import org.apache.commons.collections4.CollectionUtils; |
|
|
import org.apache.commons.collections4.ListUtils; |
|
|
import org.apache.commons.collections4.ListUtils; |
|
|
|
|
|
import org.apache.commons.fileupload.FileItem; |
|
|
|
|
|
import org.apache.commons.fileupload.disk.DiskFileItemFactory; |
|
|
|
|
|
import org.apache.http.entity.ContentType; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
import org.springframework.web.multipart.commons.CommonsMultipartFile; |
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
import java.io.File; |
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
|
import java.io.OutputStream; |
|
|
|
|
|
import java.util.Date; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
import java.util.concurrent.CompletableFuture; |
|
|
import java.util.concurrent.CompletableFuture; |
|
|
import java.util.concurrent.ExecutorService; |
|
|
import java.util.concurrent.ExecutorService; |
|
@ -31,7 +53,7 @@ import java.util.concurrent.ExecutorService; |
|
|
*/ |
|
|
*/ |
|
|
@Slf4j |
|
|
@Slf4j |
|
|
@Service |
|
|
@Service |
|
|
public class LingShanSpecialCrowdServiceImpl implements LingShanSpecialCrowdService { |
|
|
public class LingShanSpecialCrowdServiceImpl implements LingShanSpecialCrowdService, ResultDataResolver { |
|
|
|
|
|
|
|
|
@Autowired |
|
|
@Autowired |
|
|
private LingshanSpecialCrowdPersonDao specialCrowdPersonDao; |
|
|
private LingshanSpecialCrowdPersonDao specialCrowdPersonDao; |
|
@ -60,8 +82,11 @@ public class LingShanSpecialCrowdServiceImpl implements LingShanSpecialCrowdServ |
|
|
@Autowired |
|
|
@Autowired |
|
|
private ExecutorService executorService; |
|
|
private ExecutorService executorService; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private OssFeignClient ossFeignClient; |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void importSpecialCrowd(String crowdCategory, String fileSavePath) { |
|
|
public void importSpecialCrowd(String crowdCategory, String fileSavePath, String originFilename) { |
|
|
Class<? extends LingShanSpecialCrowdDetailBaseExcelData> excelDataClass; |
|
|
Class<? extends LingShanSpecialCrowdDetailBaseExcelData> excelDataClass; |
|
|
AbstractLingShanSpecialCrowdExcelImportListener listener; |
|
|
AbstractLingShanSpecialCrowdExcelImportListener listener; |
|
|
LingShanSpecialCrowdTypeEnums specialCrowdTypeEnum; |
|
|
LingShanSpecialCrowdTypeEnums specialCrowdTypeEnum; |
|
@ -107,14 +132,83 @@ public class LingShanSpecialCrowdServiceImpl implements LingShanSpecialCrowdServ |
|
|
|
|
|
|
|
|
// 正式开始导入。异步导入
|
|
|
// 正式开始导入。异步导入
|
|
|
listener.setValidateHeaderOnly(false); |
|
|
listener.setValidateHeaderOnly(false); |
|
|
|
|
|
|
|
|
CompletableFuture.runAsync(() -> { |
|
|
CompletableFuture.runAsync(() -> { |
|
|
|
|
|
// 创建导入任务
|
|
|
|
|
|
ImportTaskCommonResultDTO importTaskRst = getResultDataOrThrowsException(ImportTaskUtils.createImportTask(originFilename, |
|
|
|
|
|
BizTypeEnum.SPECIAL_CROWD.getType()), |
|
|
|
|
|
ServiceConstant.EPMET_COMMON_SERVICE, |
|
|
|
|
|
EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), |
|
|
|
|
|
null, "【灵山街道-导入特殊人群】创建导入任务失败"); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
EasyExcel.read(fileSavePath, excelDataClass, listener) |
|
|
EasyExcel.read(fileSavePath, excelDataClass, listener) |
|
|
.headRowNumber(specialCrowdTypeEnum.getHeaderRowNumber()) |
|
|
.headRowNumber(specialCrowdTypeEnum.getHeaderRowNumber()) |
|
|
.sheet(0) |
|
|
.sheet(0) |
|
|
.doRead(); |
|
|
.doRead(); |
|
|
|
|
|
|
|
|
|
|
|
List<LingShanSpecialCrowdDetailBaseExcelData> errorDatas = listener.getErrorDatas(); |
|
|
|
|
|
if (CollectionUtils.isNotEmpty(errorDatas)) { |
|
|
|
|
|
// 有错误数据需要提示
|
|
|
|
|
|
String resultDescFilePath = uploadResultDescFilePath(errorDatas, listener.getTemplateFileName()); |
|
|
|
|
|
ImportTaskUtils.finishImportTask(importTaskRst.getTaskId(), |
|
|
|
|
|
ImportTaskConstants.PROCESS_STATUS_FINISHED_FAIL, resultDescFilePath, "失败,请导出文件查看详细信息"); |
|
|
|
|
|
} else { |
|
|
|
|
|
// 全部成功
|
|
|
|
|
|
ImportTaskUtils.finishImportTask(importTaskRst.getTaskId(), |
|
|
|
|
|
ImportTaskConstants.PROCESS_STATUS_FINISHED_SUCCESS, null, "成功"); |
|
|
|
|
|
} |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
log.error("【灵山街道-导入特殊人群】失败,错误信息:" + ExceptionUtils.getErrorStackTrace(e)); |
|
|
|
|
|
ImportTaskUtils.finishImportTask(importTaskRst.getTaskId(), |
|
|
|
|
|
ImportTaskConstants.PROCESS_STATUS_FINISHED_FAIL, null, "未知错误"); |
|
|
|
|
|
} |
|
|
}, executorService); |
|
|
}, executorService); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @description: 上传错误描述文件 |
|
|
|
|
|
* @param errorDatas: |
|
|
|
|
|
* @return |
|
|
|
|
|
* @author: WangXianZhang |
|
|
|
|
|
* @date: 2023/4/19 10:19 PM |
|
|
|
|
|
*/ |
|
|
|
|
|
private String uploadResultDescFilePath(List<LingShanSpecialCrowdDetailBaseExcelData> errorDatas, String templateFileName) { |
|
|
|
|
|
FileItem fileItem = null; |
|
|
|
|
|
try { |
|
|
|
|
|
String fileName = |
|
|
|
|
|
DateUtils.format(new Date(), "yyyyMMdd_HHmmss_") + System.nanoTime() + "社会维稳导入失败数据.xlsx"; |
|
|
|
|
|
|
|
|
|
|
|
// 创建临时文件
|
|
|
|
|
|
fileItem = new DiskFileItemFactory(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD, FileUtils.getAndCreateDirUnderEpmetFilesDir("temp").toFile()) |
|
|
|
|
|
.createItem("file", ContentType.APPLICATION_OCTET_STREAM.toString(), false, fileName); |
|
|
|
|
|
|
|
|
|
|
|
// 写入临时文件
|
|
|
|
|
|
try (OutputStream os = fileItem.getOutputStream()) { |
|
|
|
|
|
ExcelWriter excelWriter = EasyExcel.write(os).withTemplate(templateFileName).build(); |
|
|
|
|
|
excelWriter.finish(); |
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 上传文件
|
|
|
|
|
|
UploadImgResultDTO uploadRst = getResultDataOrThrowsException(ossFeignClient.uploadImportTaskDescFile(new CommonsMultipartFile(fileItem)), ServiceConstant.EPMET_COMMON_SERVICE, |
|
|
|
|
|
EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), |
|
|
|
|
|
null, "【灵山街道-导入特殊人群】上传错误描述文件失败。"); |
|
|
|
|
|
|
|
|
|
|
|
return uploadRst.getUrl(); |
|
|
|
|
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
throw new RuntimeException("【灵山街道-导入特殊人群】生成错误描述文件-创建临时目录失败"); |
|
|
|
|
|
} finally { |
|
|
|
|
|
try { |
|
|
|
|
|
fileItem.delete(); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
log.error("【灵山街道-导入特殊人群】删除fileItem临时文件失败"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public String validate(LingShanSpecialCrowdDetailBaseExcelData row) { |
|
|
public String validate(LingShanSpecialCrowdDetailBaseExcelData row) { |
|
|
String idCard = row.getIdCard(); |
|
|
String idCard = row.getIdCard(); |
|
|