diff --git a/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/controller/IcPartyActivityController.java b/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/controller/IcPartyActivityController.java index 2dce5600b5..7e428aa6a0 100644 --- a/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/controller/IcPartyActivityController.java +++ b/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/controller/IcPartyActivityController.java @@ -23,14 +23,14 @@ import com.alibaba.excel.write.metadata.WriteSheet; import com.epmet.commons.tools.annotation.LoginUser; import com.epmet.commons.tools.aop.NoRepeatSubmit; import com.epmet.commons.tools.constant.NumConstant; +import com.epmet.commons.tools.constant.ServiceConstant; +import com.epmet.commons.tools.exception.EpmetErrorCode; +import com.epmet.commons.tools.exception.EpmetException; import com.epmet.commons.tools.exception.ExceptionUtils; -import com.epmet.commons.tools.exception.RenException; +import com.epmet.commons.tools.feign.ResultDataResolver; import com.epmet.commons.tools.page.PageData; import com.epmet.commons.tools.security.dto.TokenDto; -import com.epmet.commons.tools.utils.ConvertUtils; -import com.epmet.commons.tools.utils.DateUtils; -import com.epmet.commons.tools.utils.ExcelUtils; -import com.epmet.commons.tools.utils.Result; +import com.epmet.commons.tools.utils.*; import com.epmet.commons.tools.utils.poi.excel.handler.FreezeAndFilter; import com.epmet.commons.tools.validator.ValidatorUtils; import com.epmet.commons.tools.validator.group.AddGroup; @@ -46,7 +46,6 @@ import com.epmet.feign.EpmetCommonServiceOpenFeignClient; import com.epmet.service.IcPartyActivityService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; @@ -56,12 +55,14 @@ import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URLEncoder; +import java.nio.file.Path; import java.util.Date; import java.util.List; -import java.util.concurrent.CompletableFuture; +import java.util.UUID; /** @@ -73,7 +74,7 @@ import java.util.concurrent.CompletableFuture; @Slf4j @RestController @RequestMapping("icpartyactivity") -public class IcPartyActivityController { +public class IcPartyActivityController implements ResultDataResolver { @Autowired private IcPartyActivityService icPartyActivityService; @@ -163,58 +164,49 @@ public class IcPartyActivityController { */ @PostMapping("import") public Result importData(@LoginUser TokenDto tokenDto, HttpServletResponse response, @RequestPart("file") MultipartFile file) throws IOException { - if (file.isEmpty()) { - throw new RenException("请上传文件"); + // 1.暂存文件 + String originalFilename = file.getOriginalFilename(); + String extName = originalFilename.substring(originalFilename.lastIndexOf(".")); + + Path fileSavePath; + try { + Path importPath = FileUtils.getAndCreateDirUnderEpmetFilesDir("ic_party_activity", "import"); + fileSavePath = importPath.resolve(UUID.randomUUID().toString().concat(extName)); + } catch (IOException e) { + String errorMsg = ExceptionUtils.getErrorStackTrace(e); + log.error("【联建活动导入】创建临时存储文件失败:{}", errorMsg); + throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), "文件上传失败", "文件上传失败"); } - String originalFilename = file.getOriginalFilename(); - // 校验文件类型 - String extension = FilenameUtils.getExtension(originalFilename); - if (!"xls".equals(extension) && !"xlsx".equals(extension)) { - throw new RenException("文件类型不匹配"); + InputStream is = null; + FileOutputStream os = null; + + try { + is = file.getInputStream(); + os = new FileOutputStream(fileSavePath.toString()); + IOUtils.copy(is, os); + } catch (Exception e) { + log.error("method exception", e); + } finally { + org.apache.poi.util.IOUtils.closeQuietly(is); + org.apache.poi.util.IOUtils.closeQuietly(os); } - //1.查询当前工作人员是否有再导入的党员先锋数据,有则不允许导入,没有则进行新的导入 + // 2.生成导入任务记录 ImportTaskCommonFormDTO importTaskForm = new ImportTaskCommonFormDTO(); - importTaskForm.setOriginFileName(file.getOriginalFilename()); importTaskForm.setOperatorId(tokenDto.getUserId()); - importTaskForm.setBizType(ImportTaskConstants.BIZ_TYPE_PARTY_UNIT); - Result result = commonServiceOpenFeignClient.createImportTask(importTaskForm); - if (!result.success()) { - throw new RenException(result.getInternalMsg()); - } + importTaskForm.setBizType(ImportTaskConstants.BIZ_TYPE_PARTY_ACTIVITY); + importTaskForm.setOriginFileName(originalFilename); - // 异步执行导入 - CompletableFuture.runAsync(() -> { - try { - Thread.sleep(1000L); - } catch (InterruptedException e) { - log.error("method exception", e); - } - submitResiImportTask(tokenDto, response, file, result.getData().getTaskId()); - }); - return new Result(); - } + ImportTaskCommonResultDTO rstData = getResultDataOrThrowsException(commonServiceOpenFeignClient.createImportTask(importTaskForm), + ServiceConstant.EPMET_COMMON_SERVICE, + EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), + "联建活动导入错误", + "联建活动导入失败"); - private void submitResiImportTask(TokenDto tokenDto, HttpServletResponse response, MultipartFile file, String importTaskId) { - - try { - icPartyActivityService.importData(tokenDto, response, file, importTaskId); - } catch (Throwable e) { - String errorMsg = ExceptionUtils.getThrowableErrorStackTrace(e); - log.error("【导入联建活动信息失败】导入失败:{}", errorMsg); - - ImportTaskCommonFormDTO importTaskForm = new ImportTaskCommonFormDTO(); - importTaskForm.setOperatorId(tokenDto.getUserId()); - importTaskForm.setBizType(ImportTaskConstants.BIZ_TYPE_PARTY_ACTIVITY); - importTaskForm.setTaskId(importTaskId); - importTaskForm.setProcessStatus(ImportTaskConstants.PROCESS_STATUS_FINISHED_FAIL); - importTaskForm.setResultDesc("联建活动信息导入失败,请查看系统日志"); - Result result = commonServiceOpenFeignClient.finishImportTask(importTaskForm); - if (!result.success()) { - throw new RenException(result.getInternalMsg()); - } - } + // 3.执行导入 + icPartyActivityService.execAsyncExcelImport(fileSavePath, rstData.getTaskId(),tokenDto.getCustomerId(),tokenDto.getUserId()); + return new Result(); } /** diff --git a/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/excel/IcPartyActivityImportExcel.java b/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/excel/IcPartyActivityImportExcel.java index 6a2d4116ef..6998d20078 100644 --- a/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/excel/IcPartyActivityImportExcel.java +++ b/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/excel/IcPartyActivityImportExcel.java @@ -17,9 +17,16 @@ package com.epmet.excel; -import cn.afterturn.easypoi.excel.annotation.Excel; +import com.alibaba.excel.annotation.ExcelProperty; +import com.alibaba.excel.annotation.write.style.ColumnWidth; import com.epmet.commons.tools.utils.ExcelVerifyInfo; +import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; +import org.hibernate.validator.constraints.Length; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; +import java.util.Date; /** * 联建活动 @@ -28,39 +35,57 @@ import lombok.Data; * @since v1.0.0 2021-11-19 */ @Data -public class IcPartyActivityImportExcel extends ExcelVerifyInfo { - - @Excel(name = "单位名称") - private String unitName; +public class IcPartyActivityImportExcel extends ExcelVerifyInfo { - @Excel(name = "服务事项") - private String serviceMatter; - - @Excel(name = "活动标题") + @NotBlank(message = "活动标题必填") + @Length(max = 50, message = "活动标题最多输入50字") + @ExcelProperty(value = "活动标题*") private String title; - @Excel(name = "活动目标") + @NotBlank(message = "活动目标必填") + @Length(max = 100, message = "活动目标最多输入100字") + @ExcelProperty(value = "活动目标*") private String target; - @Excel(name = "活动内容") - private String content; + @NotNull(message = "服务人数必填") + @ExcelProperty(value = "服务人数*") + private Integer peopleCount; - @Excel(name = "活动地址") + @NotNull(message = "活动时间不能为空") + @ExcelProperty("活动时间*") + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private Date activityTime; + + @NotBlank(message = "详细地址") + @Length(max = 250, message = "详细地址最多输入250字") + @ExcelProperty(value = "详细地址*") private String address; - @Excel(name = "活动地址经度") - private String longitude; - @Excel(name = "活动地址纬度") - private String latitude; + @Data + public static class ErrorRow { + @ColumnWidth(38) + @ExcelProperty(value = "活动标题*") + private String title; - @Excel(name = "服务人数") - private Integer peopleCount; + @ColumnWidth(40) + @ExcelProperty(value = "活动目标*") + private String target; + + @ColumnWidth(10) + @ExcelProperty(value = "服务人数*") + private Integer peopleCount; - @Excel(name = "活动时间") - private String activityTime; + @ColumnWidth(25) + @ExcelProperty("活动时间*") + private Date activityTime; - @Excel(name = "活动结果") - private String result; + @ColumnWidth(40) + @ExcelProperty(value = "详细地址*") + private String address; + @ColumnWidth(60) + @ExcelProperty("错误信息") + private String errorInfo; + } } \ No newline at end of file diff --git a/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/excel/handler/IcPartyActivityImportListener.java b/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/excel/handler/IcPartyActivityImportListener.java new file mode 100644 index 0000000000..26e1d9d2a3 --- /dev/null +++ b/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/excel/handler/IcPartyActivityImportListener.java @@ -0,0 +1,111 @@ +package com.epmet.excel.handler; + +import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.read.listener.ReadListener; +import com.epmet.commons.tools.constant.StrConstant; +import com.epmet.commons.tools.dto.result.CustomerStaffInfoCacheResult; +import com.epmet.commons.tools.exception.ExceptionUtils; +import com.epmet.commons.tools.exception.ValidateException; +import com.epmet.commons.tools.utils.ConvertUtils; +import com.epmet.commons.tools.validator.ValidatorUtils; +import com.epmet.entity.IcPartyActivityEntity; +import com.epmet.excel.IcPartyActivityImportExcel; +import com.epmet.service.impl.IcPartyActivityServiceImpl; +import lombok.extern.slf4j.Slf4j; + +import java.util.ArrayList; +import java.util.List; + +/** + * @Description + * @Author yzm + * @Date 2023/2/20 15:36 + */ +@Slf4j +public class IcPartyActivityImportListener implements ReadListener { + /** + * 最大条数阈值 + */ + public static final int MAX_THRESHOLD = 200; + /** + * 当前操作用户 + */ + private CustomerStaffInfoCacheResult staffInfo; + private String customerId; + /** + * 数据 + */ + private List datas = new ArrayList<>(); + + /** + * 错误项列表 + */ + private List errorRows = new ArrayList<>(); + private IcPartyActivityServiceImpl icPartyActivityService; + + public IcPartyActivityImportListener(String customerId, CustomerStaffInfoCacheResult staffInfo, IcPartyActivityServiceImpl icPartyActivityService) { + this.customerId=customerId; + this.staffInfo = staffInfo; + this.icPartyActivityService = icPartyActivityService; + } + + + @Override + public void invoke(IcPartyActivityImportExcel data, AnalysisContext context) { + try { + // 先校验数据 + ValidatorUtils.validateEntity(data); + IcPartyActivityEntity e = ConvertUtils.sourceToTarget(data, IcPartyActivityEntity.class); + e.setCustomerId(customerId); + e.setAgencyId(staffInfo.getAgencyId()); + e.setPids(staffInfo.getAgencyPIds()); + e.setContent(StrConstant.EPMETY_STR); + datas.add(e); + + if (datas.size() == MAX_THRESHOLD) { + execPersist(); + } + } catch (Exception e) { + String errorMsg = null; + if (e instanceof ValidateException) { + errorMsg = ((ValidateException) e).getMsg(); + } else { + errorMsg = "未知错误"; + log.error("【联建活动导入】出错:{}", ExceptionUtils.getErrorStackTrace(e)); + } + + IcPartyActivityImportExcel.ErrorRow errorRow = ConvertUtils.sourceToTarget(data,IcPartyActivityImportExcel.ErrorRow.class); + errorRow.setErrorInfo(errorMsg); + errorRows.add(errorRow); + } + } + + @Override + public void doAfterAllAnalysed(AnalysisContext analysisContext) { + // 最后几条达不到阈值,这里必须再调用一次 + execPersist(); + } + + + /** + * 执行持久化 + */ + private void execPersist() { + try { + if (datas != null && datas.size() > 0) { + icPartyActivityService.batchPersist(datas); + } + } finally { + datas.clear(); + } + } + + /** + * 获取错误行 + * @return + */ + public List getErrorRows() { + return errorRows; + } +} + diff --git a/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/service/IcPartyActivityService.java b/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/service/IcPartyActivityService.java index 6fe8225b11..22c559aace 100644 --- a/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/service/IcPartyActivityService.java +++ b/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/service/IcPartyActivityService.java @@ -24,10 +24,8 @@ import com.epmet.dto.IcPartyActivityDTO; import com.epmet.dto.form.PartyActivityFormDTO; import com.epmet.dto.result.demand.OptionDTO; import com.epmet.entity.IcPartyActivityEntity; -import org.springframework.web.multipart.MultipartFile; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; +import java.nio.file.Path; import java.util.List; /** @@ -88,17 +86,6 @@ public interface IcPartyActivityService extends BaseService option = icPartyUnitService.option(unitDTO).stream().collect(Collectors.toMap(OptionDTO::getValue, OptionDTO::getLabel)); dtoList.forEach(dto -> { - //联建单位ID与单位名匹配 - List unitIds = Arrays.asList(dto.getUnitId().split(StrConstant.COMMA)); - // List unitNames = unitIds.stream().map(option::get).collect(Collectors.toList()); - // if(CollectionUtils.isEmpty(unitNames)){ - List unitNames=icPartyUnitService.getUnitNames(unitIds); - // } - dto.setUnitIdList(unitIds); - dto.setUnitName(StringUtils.join(unitNames, StrConstant.COMMA)); - dto.setUnitNameList(unitNames); + if(StringUtils.isNotBlank(dto.getUnitId())){ + //联建单位ID与单位名匹配 + List unitIds = Arrays.asList(dto.getUnitId().split(StrConstant.COMMA)); + // List unitNames = unitIds.stream().map(option::get).collect(Collectors.toList()); + // if(CollectionUtils.isEmpty(unitNames)){ + List unitNames=icPartyUnitService.getUnitNames(unitIds); + // } + dto.setUnitIdList(unitIds); + dto.setUnitName(StringUtils.join(unitNames, StrConstant.COMMA)); + dto.setUnitNameList(unitNames); + } if (StringUtils.isNotEmpty(dto.getGridId())) { GridInfoCache gridInfo = CustomerOrgRedis.getGridInfo(dto.getGridId()); if (null != gridInfo) { @@ -283,248 +279,6 @@ public class IcPartyActivityServiceImpl extends BaseServiceImpl fileList = new ArrayList<>(); - ExcelImportResult importResult = ExcelPoiUtils.importExcelMore(file, 0, 1, IcPartyActivityImportExcel.class); - List failList = importResult.getFailList(); - //存放错误数据行号 - if (!org.springframework.util.CollectionUtils.isEmpty(failList)) { - for (IcPartyActivityImportExcel entity : failList) { - //打印失败的行 和失败的信息 - log.warn("第{}行,{}", entity.getRowNum(), entity.getErrorMsg()); - IcPartyActivityImportFailedExcel failed = ConvertUtils.sourceToTarget(entity, IcPartyActivityImportFailedExcel.class); - failed.setErrorInfo(entity.getErrorMsg()); - fileList.add(failed); - } - } - List result = importResult.getList(); - - CustomerStaffInfoCacheResult staffInfoCache = CustomerStaffRedis.getStaffInfo(tokenDto.getCustomerId(), tokenDto.getUserId()); - //获取服务事项 - List serviceItemList = icServiceItemDictService.queryDictList(tokenDto.getCustomerId()); - Map categoryMap = serviceItemList.stream().collect(Collectors.toMap(OptionDTO::getValue, OptionDTO::getLabel)); - //获取联建单位 - IcPartyUnitDTO unitDTO = new IcPartyUnitDTO(); - unitDTO.setAgencyId(staffInfoCache.getAgencyId()); - Map option = icPartyUnitService.option(unitDTO).stream().collect(Collectors.toMap(OptionDTO::getLabel, OptionDTO::getValue)); - - //1.数据校验 - Iterator iterator = result.iterator(); - while (iterator.hasNext()) { - IcPartyActivityImportExcel obj = iterator.next(); - //单位名称校验 - if (StringUtils.isBlank(obj.getUnitName())) { - IcPartyActivityImportFailedExcel failed = ConvertUtils.sourceToTarget(obj, IcPartyActivityImportFailedExcel.class); - failed.setErrorInfo("单位名称为空"); - fileList.add(failed); - log.warn(String.format("单位名称为空,行号->%s", obj.getRowNum())); - iterator.remove(); - } else { - List unitList = Arrays.asList(obj.getUnitName().split(StrConstant.COMMA)); - unitList.forEach(unit -> { - if (null == option.get(unit)) { - IcPartyActivityImportFailedExcel failed = ConvertUtils.sourceToTarget(obj, IcPartyActivityImportFailedExcel.class); - failed.setErrorInfo("单位名称不存在"); - fileList.add(failed); - log.warn(String.format("单位名称不存在,行号->%s", obj.getRowNum())); - iterator.remove(); - } - }); - } - //服务事项校验 - if (StringUtils.isBlank(obj.getServiceMatter())) { - IcPartyActivityImportFailedExcel failed = ConvertUtils.sourceToTarget(obj, IcPartyActivityImportFailedExcel.class); - failed.setErrorInfo("服务事项为空"); - fileList.add(failed); - log.warn(String.format("服务事项为空,行号->%s", obj.getRowNum())); - iterator.remove(); - } else { - List serviceList = Arrays.asList(obj.getServiceMatter().split(StrConstant.SEMICOLON)); - serviceList.forEach(service -> { - if (null == categoryMap.get(service)) { - IcPartyActivityImportFailedExcel failed = ConvertUtils.sourceToTarget(obj, IcPartyActivityImportFailedExcel.class); - failed.setErrorInfo("服务事项不存在"); - fileList.add(failed); - log.warn(String.format("服务事项不存在,行号->%s", obj.getRowNum())); - iterator.remove(); - } - }); - - } - //活动标题 活动目标 活动内容 活动时间 活动地址 活动地址经度 活动地址纬度 活动结果 - if (StringUtils.isBlank(obj.getTitle())) { - IcPartyActivityImportFailedExcel failed = ConvertUtils.sourceToTarget(obj, IcPartyActivityImportFailedExcel.class); - failed.setErrorInfo("活动标题为空"); - fileList.add(failed); - log.warn(String.format("活动标题为空,行号->%s", obj.getRowNum())); - iterator.remove(); - } else if (StringUtils.isBlank(obj.getTarget())) { - IcPartyActivityImportFailedExcel failed = ConvertUtils.sourceToTarget(obj, IcPartyActivityImportFailedExcel.class); - failed.setErrorInfo("活动目标为空"); - fileList.add(failed); - log.warn(String.format("活动目标为空,行号->%s", obj.getRowNum())); - } else if (StringUtils.isBlank(obj.getContent())) { - IcPartyActivityImportFailedExcel failed = ConvertUtils.sourceToTarget(obj, IcPartyActivityImportFailedExcel.class); - failed.setErrorInfo("活动内容为空"); - fileList.add(failed); - log.warn(String.format("活动内容为空,行号->%s", obj.getRowNum())); - } else if (StringUtils.isBlank(obj.getActivityTime())) { - IcPartyActivityImportFailedExcel failed = ConvertUtils.sourceToTarget(obj, IcPartyActivityImportFailedExcel.class); - failed.setErrorInfo("活动时间为空"); - fileList.add(failed); - log.warn(String.format("活动时间为空,行号->%s", obj.getRowNum())); - } else if (StringUtils.isBlank(obj.getAddress())) { - IcPartyActivityImportFailedExcel failed = ConvertUtils.sourceToTarget(obj, IcPartyActivityImportFailedExcel.class); - failed.setErrorInfo("活动地址为空"); - fileList.add(failed); - log.warn(String.format("活动地址为空,行号->%s", obj.getRowNum())); - } else if (StringUtils.isBlank(obj.getLatitude())) { - IcPartyActivityImportFailedExcel failed = ConvertUtils.sourceToTarget(obj, IcPartyActivityImportFailedExcel.class); - failed.setErrorInfo("活动地址纬度为空"); - fileList.add(failed); - log.warn(String.format("活动地址纬度为空,行号->%s", obj.getRowNum())); - } else if (StringUtils.isBlank(obj.getLongitude())) { - IcPartyActivityImportFailedExcel failed = ConvertUtils.sourceToTarget(obj, IcPartyActivityImportFailedExcel.class); - failed.setErrorInfo("活动地址经度为空"); - fileList.add(failed); - log.warn(String.format("活动地址经度为空,行号->%s", obj.getRowNum())); - } else if (StringUtils.isBlank(obj.getResult())) { - IcPartyActivityImportFailedExcel failed = ConvertUtils.sourceToTarget(obj, IcPartyActivityImportFailedExcel.class); - failed.setErrorInfo("活动结果为空"); - fileList.add(failed); - log.warn(String.format("活动结果为空,行号->%s", obj.getRowNum())); - } - } - if (CollectionUtils.isNotEmpty(result)) { - result.forEach(item -> { - IcPartyActivityEntity entity = new IcPartyActivityEntity(); - entity.setCustomerId(tokenDto.getCustomerId()); - entity.setAgencyId(staffInfoCache.getAgencyId()); - entity.setPids(staffInfoCache.getAgencyPIds()); - entity.setTitle(item.getTitle()); - entity.setTarget(item.getTarget()); - entity.setContent(item.getContent()); - entity.setPeopleCount(item.getPeopleCount()); - entity.setActivityTime(DateUtils.parse(item.getActivityTime(), DateUtils.DATE_TIME_PATTERN)); - entity.setAddress(item.getAddress()); - entity.setLatitude(item.getLatitude()); - entity.setLongitude(item.getLongitude()); - entity.setResult(item.getResult()); - insert(entity); - - //保存活动与单位关系 - icActivityUnitRelationService.deleteByActivity(entity.getId()); - AtomicInteger i = new AtomicInteger(NumConstant.ONE); - List unitRelationList = Arrays.stream(item.getUnitName().split(StrConstant.COMMA)).map(unit -> { - IcActivityUnitRelationEntity relation = new IcActivityUnitRelationEntity(); - relation.setCustomerId(entity.getCustomerId()); - relation.setAgencyId(entity.getAgencyId()); - relation.setPids(entity.getPids()); - relation.setActivityId(entity.getId()); - relation.setUnitId(option.get(unit)); - relation.setSort(i.getAndIncrement()); - return relation; - }).collect(Collectors.toList()); - icActivityUnitRelationService.insertBatch(unitRelationList); - - //保存活动与服务关系 - icActivityServiceRelationService.deleteByActivity(entity.getId()); - AtomicInteger j = new AtomicInteger(NumConstant.ONE); - List serviceRelationList = Arrays.stream(item.getServiceMatter().split(StrConstant.SEMICOLON)).map(service -> { - IcActivityServiceRelationEntity relation = new IcActivityServiceRelationEntity(); - relation.setCustomerId(entity.getCustomerId()); - relation.setAgencyId(entity.getAgencyId()); - relation.setPids(entity.getPids()); - relation.setActivityId(entity.getId()); - relation.setServiceMatter(categoryMap.get(service)); - relation.setSort(j.getAndIncrement()); - return relation; - }).collect(Collectors.toList()); - icActivityServiceRelationService.insertBatch(serviceRelationList); - }); - } - String str = String.format("共%s条,成功导入%s条。", fileList.size() + result.size(), fileList.size() + result.size() - fileList.size()); - - if (fileList.size() > NumConstant.ZERO) { - List numList = fileList.stream().map(IcPartyActivityImportFailedExcel::getRowNum).sorted().collect(Collectors.toList()); - String subList = numList.stream().map(String::valueOf).collect(Collectors.joining("、")); - log.warn(str + "第" + subList + "行未成功!"); - } - - //错误数据生成文件,修改导入任务状态 - erroeImport(fileList, taskId, tokenDto.getUserId()); - } - - private void erroeImport(List fileList, String importTaskId, String staffId) throws IOException { - String url = ""; - //1.有错误数据则生成错误数据存放文件传到阿里云服务 - if (CollectionUtils.isNotEmpty(fileList)) { - Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("导入失败的数据列表", "导入失败的数据列表"), - IcPartyActivityImportFailedExcel.class, fileList); - // 文件名 - String resultDescFileName = UUID.randomUUID().toString().concat(".xlsx"); - FileItemFactory factory = new DiskFileItemFactory(16, null); - FileItem fileItem = factory.createItem("file", ContentType.APPLICATION_OCTET_STREAM.toString(), true, resultDescFileName); - OutputStream os = fileItem.getOutputStream(); - Result 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); - } - } - - if (uploadResult == null || !uploadResult.success()) { - log.error("【联建活动信息导入】调用OSS上传结果描述文件失败"); - } else { - url = uploadResult.getData().getUrl(); - } - } - //2.更新导入任务数据 - ImportTaskCommonFormDTO importTaskForm = new ImportTaskCommonFormDTO(); - importTaskForm.setOperatorId(staffId); - importTaskForm.setBizType(ImportTaskConstants.BIZ_TYPE_PARTY_ACTIVITY); - importTaskForm.setTaskId(importTaskId); - importTaskForm.setResultDescFilePath(url); - importTaskForm.setProcessStatus(ImportTaskConstants.PROCESS_STATUS_FINISHED_SUCCESS); - if (CollectionUtils.isNotEmpty(fileList)) { - importTaskForm.setProcessStatus(ImportTaskConstants.PROCESS_STATUS_FINISHED_FAIL); - importTaskForm.setResultDesc("联建活动导入存在错误数据"); - } - Result result = commonServiceOpenFeignClient.finishImportTask(importTaskForm); - if (!result.success()) { - throw new RenException(result.getInternalMsg()); - } - } - /** * 联建活动统计 * @@ -656,4 +410,101 @@ public class IcPartyActivityServiceImpl extends BaseServiceImpl errorRows = listener.getErrorRows(); + + boolean failed = errorRows.size() > 0; + if (failed) { + // 生成并上传错误文件 + try { + // 文件生成 + Path errorDescDir = FileUtils.getAndCreateDirUnderEpmetFilesDir("ic_party_activity", "import", "error_des"); + String fileName = UUID.randomUUID().toString().concat(".xlsx"); + errorDescFile = errorDescDir.resolve(fileName); + + FileItemFactory factory = new DiskFileItemFactory(16, errorDescDir.toFile()); + FileItem fileItem = factory.createItem("file", ContentType.APPLICATION_OCTET_STREAM.toString(), true, fileName); + OutputStream os = fileItem.getOutputStream(); + + EasyExcel.write(os, IcPartyActivityImportExcel.ErrorRow.class).sheet("导入失败列表").doWrite(errorRows); + + // 文件上传oss + Result errorDesFileUploadResult = ossFeignClient.uploadImportTaskDescFile(new CommonsMultipartFile(fileItem)); + if (errorDesFileUploadResult.success()) { + errorDesFileUrl = errorDesFileUploadResult.getData().getUrl(); + } + } finally { + if (Files.exists(errorDescFile)) { + Files.delete(errorDescFile); + } + } + } + + ImportTaskCommonFormDTO importFinishTaskForm = new ImportTaskCommonFormDTO(); + importFinishTaskForm.setTaskId(importTaskId); + importFinishTaskForm.setProcessStatus(failed ? ImportTaskConstants.PROCESS_STATUS_FINISHED_FAIL : ImportTaskConstants.PROCESS_STATUS_FINISHED_SUCCESS); + importFinishTaskForm.setOperatorId(userId); + importFinishTaskForm.setResultDesc(""); + importFinishTaskForm.setResultDescFilePath(errorDesFileUrl); + + Result result = commonServiceOpenFeignClient.finishImportTask(importFinishTaskForm); + if (!result.success()) { + log.error("【联建活动导入】finishImportTask失败"); + } + } catch (Exception e) { + String errorMsg = ExceptionUtils.getErrorStackTrace(e); + log.error("【联建活动导入】出错:{}", errorMsg); + + ImportTaskCommonFormDTO importFinishTaskForm = new ImportTaskCommonFormDTO(); + importFinishTaskForm.setTaskId(importTaskId); + importFinishTaskForm.setProcessStatus(ImportTaskConstants.PROCESS_STATUS_FINISHED_FAIL); + importFinishTaskForm.setOperatorId(userId); + importFinishTaskForm.setResultDesc("导入失败"); + + Result result = commonServiceOpenFeignClient.finishImportTask(importFinishTaskForm); + if (!result.success()) { + log.error("【联建活动导入】导入记录状态修改为'完成'失败"); + } + } finally { + // 删除临时文件 + if (Files.exists(filePath)) { + try { + Files.delete(filePath); + } catch (IOException e) { + log.error("method exception", e); + } + } + } + } + + /** + * 批量持久化 + * @param entities + */ + public void batchPersist(List entities) { + entities.forEach(e -> { + String id = IdWorker.getIdStr(e); + e.setId(id); + baseDao.insert(e); + }); + } } diff --git a/epmet-module/epmet-heart/epmet-heart-server/src/main/resources/templates/icpartyactivity_import_tem.xlsx b/epmet-module/epmet-heart/epmet-heart-server/src/main/resources/templates/icpartyactivity_import_tem.xlsx index 1d04c7c15d..c7ef327c10 100644 Binary files a/epmet-module/epmet-heart/epmet-heart-server/src/main/resources/templates/icpartyactivity_import_tem.xlsx and b/epmet-module/epmet-heart/epmet-heart-server/src/main/resources/templates/icpartyactivity_import_tem.xlsx differ