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 204845c1fb..f7b664de4b 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 @@ -83,4 +83,9 @@ public interface ImportTaskConstants { * 未做核酸比对 */ String IC_NAT_COMPARE_RECORD="ic_nat_compare_record"; + + /** + * 物业表:ic_property_management + */ + String IC_PROPERTY_MANAGEMENT="ic_property_management"; } diff --git a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/controller/PropertyManagementController.java b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/controller/PropertyManagementController.java index e7175b3e82..e342791fb2 100644 --- a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/controller/PropertyManagementController.java +++ b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/controller/PropertyManagementController.java @@ -26,19 +26,26 @@ import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy import com.alibaba.fastjson.JSON; import com.epmet.commons.tools.annotation.LoginUser; 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.feign.ResultDataResolver; import com.epmet.commons.tools.page.PageData; import com.epmet.commons.tools.security.dto.TokenDto; import com.epmet.commons.tools.utils.DateUtils; import com.epmet.commons.tools.utils.ExcelUtils; +import com.epmet.commons.tools.utils.FileUtils; import com.epmet.commons.tools.utils.Result; import com.epmet.commons.tools.utils.poi.excel.handler.FreezeAndFilter; import com.epmet.commons.tools.validator.ValidatorUtils; +import com.epmet.constants.ImportTaskConstants; import com.epmet.dto.IcPropertyManagementDTO; import com.epmet.dto.form.IcPropertyManagementFormDTO; import com.epmet.dto.result.IcPropertyManagementResultDTO; +import com.epmet.dto.result.ImportTaskCommonResultDTO; import com.epmet.service.PropertyManagementService; +import com.epmet.utils.ImportTaskUtils; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.io.IOUtils; @@ -48,17 +55,17 @@ import org.apache.poi.ss.usermodel.VerticalAlignment; 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.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.nio.file.Path; +import java.util.*; /** @@ -70,7 +77,7 @@ import java.util.Map; @Slf4j @RestController @RequestMapping("propertymanagement") -public class PropertyManagementController { +public class PropertyManagementController implements ResultDataResolver { @Autowired private PropertyManagementService propertyManagementService; @@ -226,6 +233,54 @@ public class PropertyManagementController { return new Result().ok(propertyManagementService.getDetail(id)); } + /** + * 导入excel + * + * @return + */ + @PostMapping("import") + public Result importExcel(MultipartFile file) { + // 1.暂存文件 + String originalFilename = file.getOriginalFilename(); + String extName = originalFilename.substring(originalFilename.lastIndexOf(".")); + + Path fileSavePath; + try { + Path importPath = FileUtils.getAndCreateDirUnderEpmetFilesDir(ImportTaskConstants.IC_PROPERTY_MANAGEMENT, "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(ImportTaskConstants.IC_PROPERTY_MANAGEMENT + "表 importExcel exception", e); + } finally { + org.apache.poi.util.IOUtils.closeQuietly(is); + org.apache.poi.util.IOUtils.closeQuietly(os); + } + + // 2.生成导入任务记录 + ImportTaskCommonResultDTO rstData = getResultDataOrThrowsException( + ImportTaskUtils.createImportTask(originalFilename, ImportTaskConstants.IC_PROPERTY_MANAGEMENT), + ServiceConstant.EPMET_COMMON_SERVICE, + EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), + "物业管理导入excel错误", + "物业管理导入excel错误"); + + // 3.执行导入 + propertyManagementService.execAsyncExcelImport(fileSavePath, rstData.getTaskId()); + return new Result(); + } + } \ No newline at end of file diff --git a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/excel/handler/IcPropertyManagementImportListener.java b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/excel/handler/IcPropertyManagementImportListener.java new file mode 100644 index 0000000000..c591d54331 --- /dev/null +++ b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/excel/handler/IcPropertyManagementImportListener.java @@ -0,0 +1,120 @@ +package com.epmet.excel.handler; + +import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.read.listener.ReadListener; +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.utils.ConvertUtils; +import com.epmet.commons.tools.utils.ObjectUtil; +import com.epmet.commons.tools.validator.ValidatorUtils; +import com.epmet.entity.IcPropertyManagementEntity; +import com.epmet.excel.yt.IcPropertyManagementImportExcelData; +import com.epmet.service.impl.IcPropertyManagementServiceImpl; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.collections4.CollectionUtils; + +import java.util.ArrayList; +import java.util.List; + +/** + * @Description + * @Author yzm + * @Date 2023/5/8 14:23 + */ +@Slf4j +public class IcPropertyManagementImportListener implements ReadListener { + + // 最大条数阈值 + public static final int MAX_THRESHOLD = 200; + private String currentCustomerId; + private IcPropertyManagementServiceImpl propertyManagementService; + // 错误项列表 + private List errorRows = new ArrayList<>(); + // 要插入的数据 + private List insertDatas = new ArrayList<>(); + private List updateDatas = new ArrayList<>(); + + public IcPropertyManagementImportListener(String customerId, IcPropertyManagementServiceImpl propertyManagementService) { + this.currentCustomerId = customerId; + this.propertyManagementService = propertyManagementService; + } + + @Override + public void invoke(IcPropertyManagementImportExcelData data, AnalysisContext analysisContext) { + try { + // log.warn("有数据吗?"+JSON.toJSONString(data)); + // 不能为空先校验数据 + ValidatorUtils.validateEntity(data); + // 去除空格 + ObjectUtil.objectToTrim(data); + //物业名称唯一 + IcPropertyManagementEntity origin=propertyManagementService.getByName(currentCustomerId, data.getName()); + + IcPropertyManagementEntity propertyManagementEntity = ConvertUtils.sourceToTarget(data, IcPropertyManagementEntity.class); + propertyManagementEntity.setCustomerId(currentCustomerId); + if (null != origin) { + origin.setContactMobile(data.getContactMobile()); + origin.setContactName(data.getContactName()); + insertDatas.add(origin); + } else { + insertDatas.add(propertyManagementEntity); + } + + if (insertDatas.size() == MAX_THRESHOLD) { + execPersist(); + } + if (updateDatas.size() == MAX_THRESHOLD) { + execPersist(); + } + } catch (Exception e) { + String errorMsg = null; + if (e instanceof ValidateException) { + errorMsg = ((ValidateException) e).getMsg(); + } else if (e instanceof EpmetException) { + errorMsg = ((EpmetException) e).getInternalMsg(); + } else { + errorMsg = "未知错误"; + log.error("【物业管理表ic_property_management导入】出错:{}", ExceptionUtils.getErrorStackTrace(e)); + } + IcPropertyManagementImportExcelData.ErrorRow errorRow = ConvertUtils.sourceToTarget(data, IcPropertyManagementImportExcelData.ErrorRow.class); + errorRow.setErrorInfo(errorMsg); + errorRows.add(errorRow); + } + + } + + @Override + public void doAfterAllAnalysed(AnalysisContext analysisContext) { + // 最后几条达不到阈值,这里必须再调用一次 + execPersist(); + } + + /** + * 执行持久化 + */ + private void execPersist() { + try { + if (CollectionUtils.isNotEmpty(insertDatas)) { + propertyManagementService.insertBatch(insertDatas); + } + + if (CollectionUtils.isNotEmpty(updateDatas)) { + propertyManagementService.updateBatchById(updateDatas); + } + } finally { + insertDatas.clear(); + updateDatas.clear(); + } + } + + /** + * 获取错误行 + * + * @return + */ + public List getErrorRows() { + return errorRows; + } +} + diff --git a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/excel/yt/IcPropertyManagementImportExcelData.java b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/excel/yt/IcPropertyManagementImportExcelData.java new file mode 100644 index 0000000000..e172260c16 --- /dev/null +++ b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/excel/yt/IcPropertyManagementImportExcelData.java @@ -0,0 +1,58 @@ +package com.epmet.excel.yt; + +import com.alibaba.excel.annotation.ExcelProperty; +import com.alibaba.excel.annotation.write.style.ColumnWidth; +import lombok.Data; + +/** + * @Description + * @Author yzm + * @Date 2023/5/8 14:24 + */ +@Data +public class IcPropertyManagementImportExcelData { + /** + * 物业名称 + */ + @ExcelProperty(value = "物业名称") + private String name; + + /** + * 烟台需求:物业联系人姓名 + */ + @ExcelProperty(value = "物业联系人") + private String contactName; + + /** + * 烟台需求:物业联系人电话 + */ + @ExcelProperty(value = "联系电话") + private String contactMobile; + + @Data + public static class ErrorRow { + /** + * 物业名称 + */ + @ExcelProperty(value = "物业名称") + private String name; + + /** + * 烟台需求:物业联系人姓名 + */ + @ExcelProperty(value = "物业联系人") + private String contactName; + + /** + * 烟台需求:物业联系人电话 + */ + @ExcelProperty(value = "联系电话") + private String contactMobile; + + + @ColumnWidth(60) + @ExcelProperty("错误信息") + private String errorInfo; + } +} + diff --git a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/IcPropertyManagementService.java b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/IcPropertyManagementService.java index bc808af034..83ea49e041 100644 --- a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/IcPropertyManagementService.java +++ b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/IcPropertyManagementService.java @@ -92,4 +92,13 @@ public interface IcPropertyManagementService extends BaseService wrapper = new LambdaQueryWrapper<>(); + wrapper.eq(IcPropertyManagementEntity::getCustomerId,customerId).eq(IcPropertyManagementEntity::getName,name); + return baseDao.selectOne(wrapper); + } } \ No newline at end of file diff --git a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/impl/PropertyManagementServiceImpl.java b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/impl/PropertyManagementServiceImpl.java index 324f9e3fff..557692cc65 100644 --- a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/impl/PropertyManagementServiceImpl.java +++ b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/impl/PropertyManagementServiceImpl.java @@ -1,34 +1,56 @@ package com.epmet.service.impl; +import com.alibaba.excel.EasyExcel; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; +import com.epmet.commons.tools.constant.NumConstant; import com.epmet.commons.tools.constant.StrConstant; 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.utils.ConvertUtils; -import com.epmet.commons.tools.utils.EpmetRequestHolder; +import com.epmet.commons.tools.utils.*; +import com.epmet.constants.ImportTaskConstants; import com.epmet.dao.IcNeighborHoodPropertyDao; import com.epmet.dao.IcPropertyManagementDao; import com.epmet.dto.IcNeighborHoodDTO; import com.epmet.dto.IcPropertyManagementDTO; import com.epmet.dto.form.IcPropertyManagementFormDTO; +import com.epmet.dto.form.ImportTaskCommonFormDTO; import com.epmet.dto.result.IcPropertyManagementResultDTO; +import com.epmet.dto.result.UploadImgResultDTO; import com.epmet.entity.IcNeighborHoodPropertyEntity; import com.epmet.entity.IcPropertyManagementEntity; +import com.epmet.excel.handler.IcPropertyManagementImportListener; +import com.epmet.excel.yt.IcPropertyManagementImportExcelData; +import com.epmet.feign.EpmetCommonServiceOpenFeignClient; +import com.epmet.feign.OssFeignClient; import com.epmet.service.PropertyManagementService; +import com.epmet.utils.ImportTaskUtils; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.fileupload.FileItem; +import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.lang3.StringUtils; +import org.apache.http.entity.ContentType; +import org.apache.poi.util.IOUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.commons.CommonsMultipartFile; import javax.annotation.Resource; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.UUID; import java.util.stream.Collectors; @Slf4j @@ -42,6 +64,10 @@ public class PropertyManagementServiceImpl implements PropertyManagementService private IcPropertyManagementDao icPropertyManagementDao; @Resource private IcNeighborHoodPropertyDao icNeighborHoodPropertyDao; + @Autowired + private EpmetCommonServiceOpenFeignClient commonServiceOpenFeignClient; + @Autowired + private OssFeignClient ossFeignClient; /** * 查询当前客户下的物业 @@ -118,7 +144,7 @@ public class PropertyManagementServiceImpl implements PropertyManagementService LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(IcNeighborHoodPropertyEntity::getPropertyId, formDTO.getId()) .eq(IcNeighborHoodPropertyEntity::getNeighborHoodId, neighborHoodId); - if (icNeighborHoodPropertyDao.selectCount(queryWrapper) < 1) { + if (icNeighborHoodPropertyDao.selectCount(queryWrapper) < NumConstant.ONE) { // 插入小区物业关系表 IcNeighborHoodPropertyEntity neighborHoodPropertyEntity = new IcNeighborHoodPropertyEntity(); neighborHoodPropertyEntity.setPropertyId(formDTO.getId()); @@ -196,5 +222,99 @@ public class PropertyManagementServiceImpl implements PropertyManagementService return resultDto; } + /** + * 执行Excel导入 + * + * @param filePath + * @param importTaskId + */ + @Async + @Override + public void execAsyncExcelImport(Path filePath, String importTaskId) { + String userId = null; + try { + userId = EpmetRequestHolder.getLoginUserId(); + String customerId = EpmetRequestHolder.getLoginUserCustomerId(); + + IcPropertyManagementImportListener listener = new IcPropertyManagementImportListener(customerId, SpringContextUtils.getBean(IcPropertyManagementServiceImpl.class)); + + EasyExcel.read(filePath.toFile(), IcPropertyManagementImportExcelData.class, listener).headRowNumber(1).sheet(0).doRead(); + + String errorDesFileUrl = null; + + List errorRows = listener.getErrorRows(); + + boolean failed = errorRows.size() > 0; + + // 合并到一起写入 + // errorRows.addAll(otherRows); + + // 生成并上传描述文件 + OutputStream os = null; + FileItem fileItem = null; + if (errorRows.size() > 0) { + try { + // 文件生成 + Path errorDescDir = FileUtils.getAndCreateDirUnderEpmetFilesDir(ImportTaskConstants.IC_PROPERTY_MANAGEMENT, "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, IcPropertyManagementImportExcelData.ErrorRow.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("【物业管理表ic_property_management】删除错误描述临时文件失败:{}", 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("【物业管理表ic_property_management】finishImportTask失败"); + } + } catch (Exception e) { + String errorMsg = ExceptionUtils.getErrorStackTrace(e); + log.error("【物业管理表ic_property_management】出错:{}", 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("【物业管理表ic_property_management】导入记录状态修改为'完成'失败"); + } + } finally { + // 删除临时文件 + if (Files.exists(filePath)) { + try { + Files.delete(filePath); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + }