diff --git a/epmet-module/epmet-common-service/common-service-client/src/main/java/com/epmet/constants/ImportTaskConstants.java b/epmet-module/epmet-common-service/common-service-client/src/main/java/com/epmet/constants/ImportTaskConstants.java index 8c439aa218..86417c86ea 100644 --- a/epmet-module/epmet-common-service/common-service-client/src/main/java/com/epmet/constants/ImportTaskConstants.java +++ b/epmet-module/epmet-common-service/common-service-client/src/main/java/com/epmet/constants/ImportTaskConstants.java @@ -19,6 +19,7 @@ public interface ImportTaskConstants { String BIZ_TYPE_ATTENTION_VACCINATION = "attention_vaccination"; String BIZ_TYPE_ATTENTION_TRIP_REPORT = "attention_vaccination"; String BIZ_TYPE_IC_PARTY_MEMBER = "ic_party_member"; + String BIZ_TYPE_IC_DANGEROUS_CHEMICALS = "ic_dangerous_chemicals"; /** * 核酸检测 */ diff --git a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/controller/IcDangerousChemicalsController.java b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/controller/IcDangerousChemicalsController.java index 97e34f384b..d7679b31fa 100644 --- a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/controller/IcDangerousChemicalsController.java +++ b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/controller/IcDangerousChemicalsController.java @@ -7,40 +7,48 @@ import com.alibaba.fastjson.JSON; 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.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; import com.epmet.commons.tools.validator.group.DefaultGroup; import com.epmet.commons.tools.validator.group.UpdateGroup; +import com.epmet.constants.ImportTaskConstants; import com.epmet.dto.IcDangerousChemicalsDTO; import com.epmet.dto.form.IcDangerousChemicalsAddEditFormDTO; import com.epmet.dto.form.IcDangerousChemicalsListFormDTO; +import com.epmet.dto.form.ImportTaskCommonFormDTO; import com.epmet.dto.result.IcDangerousChemicalsListResultDTO; +import com.epmet.dto.result.ImportTaskCommonResultDTO; import com.epmet.excel.IcDangerousChemicalsExcel; +import com.epmet.feign.EpmetCommonServiceOpenFeignClient; import com.epmet.service.IcDangerousChemicalsService; +import com.epmet.utils.ImportTaskUtils; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.io.IOUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.net.URLEncoder; +import java.nio.file.Path; import java.util.Date; import java.util.List; +import java.util.UUID; /** @@ -56,6 +64,8 @@ public class IcDangerousChemicalsController { @Autowired private IcDangerousChemicalsService icDangerousChemicalsService; + @Autowired + private EpmetCommonServiceOpenFeignClient epmetCommonServiceOpenFeignClient; @RequestMapping("list") public Result> list(@LoginUser TokenDto tokenDto, @RequestBody IcDangerousChemicalsListFormDTO formDTO) { @@ -159,7 +169,51 @@ public class IcDangerousChemicalsController { } } - + /** + * Desc: 重点危化品企业导入 + * @param file + * @param tokenDto + * @author zxc + * @date 2022/6/20 10:25 + */ + @PostMapping("import") + public Result dangerousChemicalsImport(@RequestParam("file")MultipartFile file,@LoginUser TokenDto tokenDto){ + // 1.暂存文件 + String originalFilename = file.getOriginalFilename(); + String extName = originalFilename.substring(originalFilename.lastIndexOf(".")); + Path fileSavePath; + try { + Path importPath = FileUtils.getAndCreateDirUnderEpmetFilesDir("ic_dangerous_chemicals", "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(), "文件上传失败", "文件上传失败"); + } + InputStream is = null; + FileOutputStream os = null; + try { + is = file.getInputStream(); + os = new FileOutputStream(fileSavePath.toString()); + IOUtils.copy(is, os); + } catch (Exception e) { + log.error("importExcel exception", e); + } finally { + org.apache.poi.util.IOUtils.closeQuietly(is); + org.apache.poi.util.IOUtils.closeQuietly(os); + } + // 2.生成导入任务记录 + ImportTaskCommonFormDTO importFormDTO = new ImportTaskCommonFormDTO(); + importFormDTO.setBizType(ImportTaskConstants.BIZ_TYPE_IC_DANGEROUS_CHEMICALS); + importFormDTO.setOperatorId(tokenDto.getUserId()); + importFormDTO.setOriginFileName(file.getOriginalFilename()); + Result importTask = epmetCommonServiceOpenFeignClient.createImportTask(importFormDTO); + if (!importTask.success()) { + throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), "", "存在进行中的导入"); + } + icDangerousChemicalsService.dangerousChemicalsImport(fileSavePath,importTask.getData().getTaskId(),tokenDto); + return new Result(); + } } diff --git a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/entity/IcDangerousChemicalsEntity.java b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/entity/IcDangerousChemicalsEntity.java index e4e9e35252..a3961735a8 100644 --- a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/entity/IcDangerousChemicalsEntity.java +++ b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/entity/IcDangerousChemicalsEntity.java @@ -1,5 +1,6 @@ package com.epmet.entity; +import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableName; import com.epmet.commons.mybatis.entity.BaseEpmetEntity; import lombok.Data; @@ -88,4 +89,10 @@ public class IcDangerousChemicalsEntity extends BaseEpmetEntity { */ private String remark; + @TableField(exist = false) + private String dangerTypeName; + + @TableField(exist = false) + private String categoryName; + } diff --git a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/model/DangerousChemicalsImportListener.java b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/model/DangerousChemicalsImportListener.java new file mode 100644 index 0000000000..a3df4b3b18 --- /dev/null +++ b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/model/DangerousChemicalsImportListener.java @@ -0,0 +1,113 @@ +package com.epmet.model; + +import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.event.AnalysisEventListener; +import com.epmet.commons.tools.constant.NumConstant; +import com.epmet.commons.tools.enums.DictTypeEnum; +import com.epmet.commons.tools.exception.EpmetException; +import com.epmet.commons.tools.redis.common.bean.AgencyInfoCache; +import com.epmet.commons.tools.utils.ConvertUtils; +import com.epmet.commons.tools.utils.Result; +import com.epmet.commons.tools.validator.ValidatorUtils; +import com.epmet.dto.result.IcCoverageCategoryDictListResultDTO; +import com.epmet.entity.IcDangerousChemicalsEntity; +import com.epmet.feign.EpmetAdminOpenFeignClient; +import com.epmet.service.CoverageService; +import com.epmet.service.IcDangerousChemicalsService; +import lombok.Data; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.collections4.MapUtils; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * @Author zxc + * @DateTime 2022/6/20 10:31 + * @DESC + */ +@Data +@Slf4j +public class DangerousChemicalsImportListener extends AnalysisEventListener { + + private IcDangerousChemicalsService icDangerousChemicalsService; + private EpmetAdminOpenFeignClient adminOpenFeignClient; + private CoverageService coverageService; + private AgencyInfoCache agencyInfo; + + private List errorRows = new ArrayList<>(); + private List otherRows = new ArrayList<>(); + private List insertList = new ArrayList<>(); + + public DangerousChemicalsImportListener(AgencyInfoCache agencyInfo, IcDangerousChemicalsService icDangerousChemicalsService, CoverageService coverageService,EpmetAdminOpenFeignClient adminOpenFeignClient){ + this.agencyInfo = agencyInfo; + this.icDangerousChemicalsService = icDangerousChemicalsService; + this.coverageService = coverageService; + this.adminOpenFeignClient = adminOpenFeignClient; + } + + @Override + public void invoke(DangerousChemicalsModel data, AnalysisContext context) { + IcDangerousChemicalsEntity e = ConvertUtils.sourceToTarget(data, IcDangerousChemicalsEntity.class); + e.setSourceType("import"); + e.setAgencyId(agencyInfo.getId()); + e.setCustomerId(agencyInfo.getCustomerId()); + e.setAgencyIdPath(agencyInfo.getPids().equals(NumConstant.ZERO_STR) || agencyInfo.getPids().equals("") ? agencyInfo.getId() : agencyInfo.getPids().concat(":").concat(agencyInfo.getId())); + insertList.add(e); + if (insertList.size() == NumConstant.ONE_HUNDRED){ + execPersist(); + } + + } + + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + execPersist(); + } + + /** + * Desc: 数据库插入 + */ + private void execPersist() { + try { + if (CollectionUtils.isNotEmpty(insertList)) { + //危化品种类字典 + Result> statusRes = adminOpenFeignClient.dictMap(DictTypeEnum.IC_DANGER_TYPE.getCode()); + if (!statusRes.success()){ + throw new EpmetException("获取IC_DANGER_TYPE字典表失败"); + } + Map statusMap = statusRes.success() && MapUtils.isNotEmpty(statusRes.getData()) ? statusRes.getData() : new HashMap<>(16); + //企业类别字典数据 + List dictList = coverageService.dictMap(agencyInfo.getCustomerId(), "dangerous_chemicals"); + Map dictMap = dictList.stream().collect(Collectors.toMap(IcCoverageCategoryDictListResultDTO::getCategoryKey, IcCoverageCategoryDictListResultDTO::getCategoryName)); + insertList.forEach(i -> { + statusMap.forEach((k,v) -> { + if (i.getDangerTypeName().equals(v)){ + i.setDangerType(k); + } + }); + dictMap.forEach((k,v) -> { + if (i.getCategoryName().equals(v)){ + i.setCategory(k); + } + }); + }); + icDangerousChemicalsService.insertBatch(insertList); + } + } finally { + insertList.clear(); + } + } + + /** + * 获取错误行 + * @return + */ + public List getErrorRows() { + return errorRows; + } +} diff --git a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/model/DangerousChemicalsModel.java b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/model/DangerousChemicalsModel.java new file mode 100644 index 0000000000..4f74294aeb --- /dev/null +++ b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/model/DangerousChemicalsModel.java @@ -0,0 +1,56 @@ +package com.epmet.model; + +import com.alibaba.excel.annotation.ExcelProperty; +import com.alibaba.excel.annotation.write.style.ColumnWidth; +import com.epmet.commons.tools.constant.NumConstant; +import lombok.Data; +import org.hibernate.validator.constraints.Length; + +import javax.validation.constraints.NotBlank; + +/** + * @Author zxc + * @DateTime 2022/6/20 10:33 + * @DESC + */ +@Data +public class DangerousChemicalsModel { + + @NotBlank(message = "企业名称为必填项") + @Length(max = 50,message = "企业名称长度超出50字限制") + @ExcelProperty(value = "企业名称") + private String name; + + @NotBlank(message = "企业类型为必填项") + @ExcelProperty(value = "企业类型") + private String categoryName; + + @ExcelProperty(value = "周边安全间距(公里)") + private String safeDistance = NumConstant.ZERO_STR; + + @ExcelProperty(value = "危化品种类") + private String dangerTypeName; + + @ExcelProperty(value = "负责人") + private String principalName; + + @Length(max = 50,message = "联系电话长度超出50字限制") + @ExcelProperty(value = "联系电话") + private String principalMobile; + + @Length(max = 50,message = "经营地址长度超出50字限制") + @ExcelProperty(value = "经营地址") + private String address; + + @ExcelProperty(value = "备注") + private String remark; + + @Data + public static class RowRemarkMessage { + + @ColumnWidth(60) + @ExcelProperty("错误信息") + private String errorInfo; + } + +} diff --git a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/IcDangerousChemicalsService.java b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/IcDangerousChemicalsService.java index 321437f1f6..2f91dc22dd 100644 --- a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/IcDangerousChemicalsService.java +++ b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/IcDangerousChemicalsService.java @@ -2,12 +2,15 @@ package com.epmet.service; import com.epmet.commons.mybatis.service.BaseService; import com.epmet.commons.tools.page.PageData; +import com.epmet.commons.tools.security.dto.TokenDto; import com.epmet.dto.IcDangerousChemicalsDTO; import com.epmet.dto.form.IcDangerousChemicalsAddEditFormDTO; import com.epmet.dto.form.IcDangerousChemicalsListFormDTO; import com.epmet.dto.result.IcDangerousChemicalsListResultDTO; import com.epmet.entity.IcDangerousChemicalsEntity; +import java.nio.file.Path; + /** * 重点危化品企业 * @@ -67,4 +70,13 @@ public interface IcDangerousChemicalsService extends BaseService implements IcDangerousChemicalsService { @Autowired private EpmetAdminOpenFeignClient adminOpenFeignClient; @Autowired private CoverageService coverageService; + @Autowired + private EpmetCommonServiceOpenFeignClient commonServiceOpenFeignClient; + @Autowired + private OssFeignClient ossFeignClient; @Override public PageData list(IcDangerousChemicalsListFormDTO formDTO) { @@ -182,4 +213,93 @@ public class IcDangerousChemicalsServiceImpl extends BaseServiceImpl errorRows = listener.getErrorRows(); + List otherRows = listener.getOtherRows(); + boolean failed = errorRows.size() > 0; + // 合并到一起写入 + errorRows.addAll(otherRows); + // 生成并上传描述文件 + OutputStream os = null; + FileItem fileItem = null; + if (errorRows.size() > 0) { + try { + // 文件生成 + Path errorDescDir = FileUtils.getAndCreateDirUnderEpmetFilesDir("ic_dangerous_chemicals", "import", "error_des"); + String fileName = UUID.randomUUID().toString().concat(".xlsx"); + fileItem = new DiskFileItemFactory(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD, errorDescDir.toFile()) + .createItem("file", ContentType.APPLICATION_OCTET_STREAM.toString(), false, fileName); + os = fileItem.getOutputStream(); + EasyExcel.write(os, DangerousChemicalsModel.RowRemarkMessage.class).sheet("信息列表").doWrite(errorRows); + // 文件上传oss + Result errorDesFileUploadResult = ossFeignClient.uploadImportTaskDescFile(new CommonsMultipartFile(fileItem)); + if (errorDesFileUploadResult.success()) { + errorDesFileUrl = errorDesFileUploadResult.getData().getUrl(); + } + } finally { + IOUtils.closeQuietly(os); + if (!fileItem.isInMemory()) { + try { + fileItem.delete(); + } catch (Exception e){ + log.error("【重点危化品企业导入】删除错误描述临时文件失败:{}", ExceptionUtils.getErrorStackTrace(e)); + } + } + } + } + Result result = ImportTaskUtils.finishImportTask( + importTaskId, + failed ? ImportTaskConstants.PROCESS_STATUS_FINISHED_FAIL : ImportTaskConstants.PROCESS_STATUS_FINISHED_SUCCESS, + errorDesFileUrl, + ""); + 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(tokenDto.getUserId()); + importFinishTaskForm.setResultDesc("导入失败"); + + Result result = commonServiceOpenFeignClient.finishImportTask(importFinishTaskForm); + if (!result.success()) { + log.error("【重点危化品企业导入】导入记录状态修改为'完成'失败"); + } + } finally { + // 删除临时文件 + if (Files.exists(filePath)) { + try { + Files.delete(filePath); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + } \ No newline at end of file