From b5a12c11647e041b5e5a305e5cdf5c42ad050822 Mon Sep 17 00:00:00 2001 From: WendyYang Date: Fri, 20 Sep 2024 10:43:51 +0800 Subject: [PATCH] =?UTF-8?q?modify:=201.=20=E5=8C=BA=E5=8E=BF=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E5=A4=87=E6=A1=88=E6=95=B0=E6=8D=AE=E5=AF=BC=E5=85=A5?= =?UTF-8?q?;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../projectlib/controller/QxProjectController.java | 11 ++ .../com/hz/pm/api/projectlib/entity/QxProject.java | 5 +- .../projectlib/manage/QxProjectImportManage.java | 117 +++++++++++++++++++++ 3 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 hz-pm-api/src/main/java/com/hz/pm/api/projectlib/manage/QxProjectImportManage.java diff --git a/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/controller/QxProjectController.java b/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/controller/QxProjectController.java index db2dee7..084f101 100644 --- a/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/controller/QxProjectController.java +++ b/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/controller/QxProjectController.java @@ -1,5 +1,6 @@ package com.hz.pm.api.projectlib.controller; +import com.hz.pm.api.projectlib.manage.QxProjectImportManage; import com.hz.pm.api.projectlib.manage.QxProjectManage; import com.hz.pm.api.projectlib.model.dto.MhSystemReplaceInfoVO; import com.hz.pm.api.projectlib.model.req.*; @@ -10,7 +11,9 @@ import com.ningdatech.log.annotation.WebLog; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; +import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import javax.validation.Valid; import java.util.List; @@ -30,6 +33,7 @@ import java.util.List; public class QxProjectController { private final QxProjectManage qxProjectManage; + private final QxProjectImportManage qxProjectImportManage; @PostMapping("/submitProjectRecord") @ApiOperation("项目备案") @@ -77,4 +81,11 @@ public class QxProjectController { return qxProjectManage.listReplaceInfo(projectId); } + @PostMapping("importProject") + @ApiOperation("导入项目") + @PreAuthorize("hasAuthority('SUPER_ADMIN')") + public void importProject(MultipartFile file) { + qxProjectImportManage.importProject(file); + } + } diff --git a/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/entity/QxProject.java b/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/entity/QxProject.java index 5fc57d2..b81fdae 100644 --- a/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/entity/QxProject.java +++ b/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/entity/QxProject.java @@ -1,11 +1,11 @@ package com.hz.pm.api.projectlib.entity; import com.baomidou.mybatisplus.annotation.*; -import com.hz.pm.api.projectlib.model.enumeration.QxProjectStatus; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; +import java.math.BigDecimal; import java.time.LocalDateTime; /** @@ -54,6 +54,9 @@ public class QxProject { @ApiModelProperty("立项批复文件") private String approvedFile; + @ApiModelProperty("投资总额") + private BigDecimal totalInvest; + @ApiModelProperty("创建时间") @TableField(fill = FieldFill.INSERT) private LocalDateTime createOn; diff --git a/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/manage/QxProjectImportManage.java b/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/manage/QxProjectImportManage.java new file mode 100644 index 0000000..427d942 --- /dev/null +++ b/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/manage/QxProjectImportManage.java @@ -0,0 +1,117 @@ +package com.hz.pm.api.projectlib.manage; + +import cn.hutool.core.date.LocalDateTimeUtil; +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.map.MapUtil; +import cn.hutool.core.util.NumberUtil; +import cn.hutool.poi.excel.ExcelReader; +import cn.hutool.poi.excel.ExcelUtil; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.hz.pm.api.external.MhFileClient; +import com.hz.pm.api.projectlib.entity.QxProject; +import com.hz.pm.api.projectlib.model.enumeration.QxProjectStatus; +import com.hz.pm.api.projectlib.service.IQxProjectService; +import com.hz.pm.api.user.helper.MhUnitCache; +import com.hz.pm.api.user.model.dto.UnitDTO; +import com.hz.pm.api.user.model.entity.UserInfo; +import com.hz.pm.api.user.service.IUserInfoService; +import com.ningdatech.basic.exception.BizException; +import com.ningdatech.basic.model.ApiResponse; +import com.ningdatech.file.controller.FileController; +import com.ningdatech.file.entity.vo.result.FileResultVO; +import lombok.RequiredArgsConstructor; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + *

+ * QxProjectManage + *

+ * + * @author WendyYang + * @since 10:42 2024/8/31 + */ +@Component +@RequiredArgsConstructor +public class QxProjectImportManage { + + private final IQxProjectService qxProjectService; + private final IUserInfoService userInfoService; + private final MhUnitCache mhUnitCache; + private final MhFileClient mhFileClient; + private final FileController fileController; + + + private UserInfo getUserInfo(String mhUserId) { + return userInfoService.getOne(Wrappers.lambdaQuery(UserInfo.class) + .eq(UserInfo::getMhUserId, mhUserId) + .last("limit 1")); + } + + @Transactional(rollbackFor = Exception.class) + public void importProject(MultipartFile file) { + try (InputStream is = file.getInputStream(); + ExcelReader reader = ExcelUtil.getReader(is)) { + List projects = new ArrayList<>(); + for (Map map : reader.readAll()) { + QxProject project = new QxProject(); + project.setStatus(QxProjectStatus.DECLARED.getCode()); + project.setProjectName(MapUtil.getStr(map, "项目名称")); + project.setProjectYear(MapUtil.getInt(map, "年度")); + project.setBuildOrgCode(MapUtil.getLong(map, "单位ID(同单位同步接口)")); + UnitDTO unit = mhUnitCache.getById(project.getBuildOrgCode()); + if (unit != null) { + project.setUnitStrip(unit.getType().getStrip().getCode()); + } + String totalInvestStr = MapUtil.getStr(map, "项目总投资估算"); + if (NumberUtil.isNumber(totalInvestStr)) { + project.setTotalInvest(new BigDecimal(totalInvestStr)); + } + project.setBuildOrgName(MapUtil.getStr(map, "单位名称")); + project.setCreateOn(LocalDateTimeUtil.of(MapUtil.getDate(map, "备案时间(时间戳)"))); + project.setUpdateOn(project.getCreateOn()); + UserInfo userInfo = getUserInfo(MapUtil.getStr(map, "创建人(同用户同步接口)")); + if (userInfo != null) { + project.setCreateBy(userInfo.getCreateBy()); + project.setUpdateBy(userInfo.getCreateBy()); + } + String constructionFileIds = MapUtil.getStr(map, "建设方案附件(多附件逗号隔开)"); + String[] split = constructionFileIds.split(","); + if (split.length > 0) { + String fileIds = Arrays.stream(split).map(w -> { + File tmpFile = mhFileClient.downloadToTmpFile(w); + try (FileInputStream inputStream = new FileInputStream(tmpFile)) { + String mimeType = FileUtil.getMimeType(tmpFile.getPath()); + MockMultipartFile multipartFile = new MockMultipartFile(tmpFile.getName(), tmpFile.getName(), mimeType, inputStream); + ApiResponse fileRet = fileController.upload(multipartFile, "default"); + return fileRet.getData().getId().toString(); + } catch (Exception e) { + throw BizException.wrap("建设方案上传失败"); + } finally { + tmpFile.deleteOnExit(); + } + }).collect(Collectors.joining(",")); + project.setConstructionPlanFile(fileIds); + } + projects.add(project); + } + qxProjectService.saveBatch(projects); + } catch (IOException e) { + throw BizException.wrap("数据导入失败"); + } + } + +}