From b83ace2edbc3101ff3572fd260b914f871160fc5 Mon Sep 17 00:00:00 2001 From: yinzuomei <576302893@qq.com> Date: Mon, 20 Feb 2023 17:17:13 +0800 Subject: [PATCH] =?UTF-8?q?=E8=81=94=E5=BB=BA=E6=B4=BB=E5=8A=A8=E5=AF=BC?= =?UTF-8?q?=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/IcPartyActivityController.java | 96 ++--- .../excel/IcPartyActivityImportExcel.java | 71 ++-- .../IcPartyActivityImportListener.java | 111 ++++++ .../epmet/service/IcPartyActivityService.java | 21 +- .../impl/IcPartyActivityServiceImpl.java | 377 ++++++------------ .../templates/icpartyactivity_import_tem.xlsx | Bin 9091 -> 9088 bytes 6 files changed, 324 insertions(+), 352 deletions(-) create mode 100644 epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/excel/handler/IcPartyActivityImportListener.java 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 1d04c7c15d9349ff3e64e60391b15e6d6dcc760a..c7ef327c10ebf733cd261e809a102ac14ff3dc61 100644 GIT binary patch delta 1547 zcmV+m2K4!ZM}SALlK}<7WA_70lb8W4e-@~$B$WVlMI|JHKXP`nhmS|{i>Tlh2N8OxMlMX4Y` z4Hz^}kB?$rQC@1e)@23S=3qQWQik#>K^to;im~AvkjwPZ(JKyVBNNKn?`FbMIF*MgSj&xTGJIc08}%I&m^ zJ7x>si-UV>^1)cwbx=>b7%qQ^@0V9M-Ku`B+FNlzX)HM98kjA0+PugZkLvbz1v7T( z&b-P&oHyy$$Lr_YdyIc84%FrDK4~C4mxT7Y_sZSm;xb#HG$PT&Cy^hXW#ojCGfK!q zJNTf#P1Zli-S~eHBa)FBolfaA91-_`w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