|
|
@@ -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; |
|
|
|
|
|
|
|
/** |
|
|
|
* <p> |
|
|
|
* QxProjectManage |
|
|
|
* </p> |
|
|
|
* |
|
|
|
* @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<QxProject> projects = new ArrayList<>(); |
|
|
|
for (Map<String, Object> 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<FileResultVO> 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("数据导入失败"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |