feat: 老法规导入存发布稿

This commit is contained in:
2023-11-20 09:22:22 +08:00
parent 14d3bdfd3a
commit 0ca26b1a6c
2 changed files with 95 additions and 31 deletions
@@ -56,9 +56,9 @@ public class OldSystemController {
@Resource
private ILawsEnterpriseStandardService enterpriseStandardService;
@PostMapping("/importOldSystemESZip")
@AutoLog(value = "老法规系统-老法规企标国标数据库Zip导入")
@ApiOperation(value="老法规系统-老法规企标国标数据库Zip导入", notes="老法规系统-老法规企标国标数据库Zip导入", produces = "application/octet-stream")
@PostMapping("/importOldSystemExcel")
@AutoLog(value = "老法规系统-老法规企标国标数据导入")
@ApiOperation(value="老法规系统-老法规企标国标数据导入", notes="老法规系统-老法规企标国标数据导入", produces = "application/octet-stream")
@ApiOperationSupport(order = 2)
public void importOldSystemESZip(@RequestParam("file") MultipartFile file,
@ApiParam(value = "0不导入 1导入企标 2导入国标 3一起导入", required = true)
@@ -1,5 +1,6 @@
package com.jero.modules.laws.standard.service.impl;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -9,6 +10,7 @@ import com.jero.common.constant.CommonConstant;
import com.jero.common.exception.JeroBootException;
import com.jero.common.system.vo.SysDictItemCore;
import com.jero.common.util.MessageUtils;
import com.jero.common.util.MinioUtil;
import com.jero.modules.activiti.util.ExcelUtils;
import com.jero.modules.laws.common.constant.FieldCommon;
import com.jero.modules.laws.enterprise.entity.OldSystemImportDTO;
@@ -18,14 +20,18 @@ import com.jero.modules.laws.oldSystem.entity.OldSystemDepartRelation;
import com.jero.modules.laws.oldSystem.service.OldSystemDepartRelationService;
import com.jero.modules.laws.standard.entity.LawsDomesticStandard;
import com.jero.modules.laws.standard.entity.LawsEnterpriseStandard;
import com.jero.modules.laws.standard.entity.LawsStandardFile;
import com.jero.modules.laws.standard.service.ILawsDomesticStandardService;
import com.jero.modules.laws.standard.service.ILawsEnterpriseStandardService;
import com.jero.modules.laws.standard.mapper.LawsEnterpriseStandardMapper;
import com.jero.modules.laws.standard.service.ILawsReplacedStandardService;
import com.jero.modules.oss.entity.OSSFile;
import com.jero.modules.oss.service.IOSSFileService;
import com.jero.modules.system.entity.SysDepart;
import com.jero.modules.system.entity.SysUser;
import com.jero.modules.system.service.ISysDepartService;
import com.jero.modules.system.service.ISysUserService;
import io.minio.errors.MinioException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.lang3.StringUtils;
@@ -34,6 +40,7 @@ import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -43,6 +50,7 @@ import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -85,6 +93,9 @@ public class LawsEnterpriseStandardServiceImpl extends ServiceImpl<LawsEnterpris
@Resource
private ISysUserService sysUserService;
@Resource
private IOSSFileService ossFileService;
@Override
public LawsEnterpriseStandard queryByStandardNumber(String standardNumber) {
if (StringUtils.isBlank(standardNumber)) {
@@ -98,12 +109,12 @@ public class LawsEnterpriseStandardServiceImpl extends ServiceImpl<LawsEnterpris
}
@Override
public void importExcelOldSystem(MultipartFile zipFile, String type, HttpServletResponse response) {
public void importExcelOldSystem(MultipartFile file, String type, HttpServletResponse response) {
List<OldSystemImportDTO> errImportList = new ArrayList<>();
// 验证文件是否为压缩格式
if (!Objects.requireNonNull(zipFile.getContentType()).contains("zip")) {
throw new JeroBootException("文件格式不正确,请上传zip格式的压缩包");
// 验证文件是否为压缩格式file.getContentType()).contains("xls")
if (!Objects.requireNonNull(file.getOriginalFilename()).contains("xls")) {
throw new JeroBootException("文件格式不正确,请上传Excel");
}
ImportParams params = new ImportParams();
params.setTitleRows(0);
@@ -113,28 +124,7 @@ public class LawsEnterpriseStandardServiceImpl extends ServiceImpl<LawsEnterpris
List<OldSystemImportDTO> esDatalist = new ArrayList<>();
List<OldSystemImportDTO> gbDatalist = new ArrayList<>();
List<OldSystemImportDTO> fbDatalist = new ArrayList<>();
// 解压缩文件,查找Excel文件
MultipartFile file = null;
try (ZipInputStream zis = new ZipInputStream(zipFile.getInputStream())) {
ZipEntry zipEntry;
while ((zipEntry = zis.getNextEntry()) != null) {
if (zipEntry.getName().endsWith(".xls") || zipEntry.getName().endsWith(".xlsx")) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IOUtils.copy(zis, baos);
byte[] excelData = baos.toByteArray();
file = new MockMultipartFile(zipEntry.getName(), zipEntry.getName(), "application/vnd.ms-excel", excelData);
zis.closeEntry();
break; // 一旦找到Excel文件,就可以跳出循环
}
zis.closeEntry();
}
} catch (IOException e) {
throw new JeroBootException("处理压缩文件时发生错误:" + e.getMessage());
}
if (file == null) {
throw new JeroBootException("压缩包中未发现Excel文件。");
}
try {
// 获取当前时间戳(毫秒)
long startTime = System.currentTimeMillis();
@@ -239,12 +229,15 @@ public class LawsEnterpriseStandardServiceImpl extends ServiceImpl<LawsEnterpris
list = list.stream().filter(o -> StrUtil.isNotBlank(o.getOldOrgName())).collect(Collectors.toList());
Map<String, List<OldSystemDepartRelation>> oldDeptMap = list.stream().collect(Collectors.groupingBy(OldSystemDepartRelation::getOldOrgName));
// 文件关联
List<OSSFile> ossFileList = new ArrayList<>();
long startTime = System.currentTimeMillis();
// 转换数据
List<LawsDomesticStandard> gbList = new ArrayList<>();
for (OldSystemImportDTO dto : gbDatalist) {
try {
LawsDomesticStandard gb = new LawsDomesticStandard();
gb.setId(dto.getId());
gb.setStandardNumber(dto.getBzh());
String[] parts = dto.getBzh().split("[-\\s]"); // 使用空格或破折号作为分隔符
@@ -284,6 +277,21 @@ public class LawsEnterpriseStandardServiceImpl extends ServiceImpl<LawsEnterpris
gb.setResponsibleDepartment(allUserMap.get(zrr + zrrid).getId());
}
// 根据文件路径获取文件Id
String originPath = dto.getSclj();
if (StrUtil.isNotBlank(originPath)) {
String filePath = this.getFilePath(originPath);
if (MinioUtil.doesObjectExist(filePath)) {
// 存入关系表
OSSFile ossFile = new OSSFile();
ossFile.setUrl(filePath);
ossFile.setStandardNo(dto.getBzh());
ossFile.setFileName(FileUtil.getName(filePath));
ossFile.setStandardFileType("publish_of_original");
ossFileList.add(ossFile);
}
}
gbList.add(gb);
} catch (Exception e) {
log.error("导入转换国标数据时发生错误:{}", e.getMessage());
log.error("错误数据:{}", dto.toString());
@@ -300,7 +308,10 @@ public class LawsEnterpriseStandardServiceImpl extends ServiceImpl<LawsEnterpris
standardNumberToEntityMap.put(existingEntity.getStandardNumber(), existingEntity);
}
// 3. 遍历待保存或更新的列表,设置ID
// 3. 将文件关系列表转为Map,用于快速查找
Map<String, List<OSSFile>> standardNumberToFileMap = ossFileList.stream().collect(Collectors.groupingBy(OSSFile::getStandardNo));
// 4. 遍历待保存或更新的列表,设置ID
for (LawsDomesticStandard entity : gbList) {
LawsDomesticStandard existingEntity = standardNumberToEntityMap.get(entity.getStandardNumber());
if (existingEntity != null) {
@@ -325,6 +336,11 @@ public class LawsEnterpriseStandardServiceImpl extends ServiceImpl<LawsEnterpris
entity.setSubstituteStandardNumber(null);
entity.setReplacedByStandardNumber(null);
domesticStandardService.save(entity);
String standardId = entity.getId();
// 保存文件关联
OSSFile ossFile = standardNumberToFileMap.get(entity.getStandardNumber()).get(0);
ossFile.setStandardId(standardId);
ossFileService.save(ossFile);
}
log.info("老法规国标覆盖导入完成");
// 获取当前时间戳(毫秒)
@@ -339,11 +355,13 @@ public class LawsEnterpriseStandardServiceImpl extends ServiceImpl<LawsEnterpris
// 转换数据
List<LawsEnterpriseStandard> esList = new ArrayList<>();
List<SysDictItemCore> listDict = commonAPI.getDictItemAll();
// 文件关联
List<OSSFile> ossFileList = new ArrayList<>();
// 导入企标
for (OldSystemImportDTO dto : esDatalist) {
try {
LawsEnterpriseStandard es = new LawsEnterpriseStandard();
es.setId(dto.getId());
es.setStandardNumber(dto.getBzh());
es.setStandardName(dto.getBzmc());
es.setStandardEnglishName(dto.getBzywmc());
@@ -390,6 +408,20 @@ public class LawsEnterpriseStandardServiceImpl extends ServiceImpl<LawsEnterpris
if (StringUtils.isNotBlank(categoryCode)) {
es.setCategoryCode(excelUtils.translateDictValue("standard_category_code", categoryCode, listDict));
}
// 根据文件路径获取文件Id
String originPath = dto.getSclj();
if (StrUtil.isNotBlank(originPath)) {
String filePath = this.getFilePath(originPath);
if (MinioUtil.doesObjectExist(filePath)) {
// 存入关系表
OSSFile ossFile = new OSSFile();
ossFile.setUrl(filePath);
ossFile.setStandardNo(dto.getBzh());
ossFile.setFileName(FileUtil.getName(filePath));
ossFile.setStandardFileType("publish_of_original");
ossFileList.add(ossFile);
}
}
esList.add(es);
} catch (Exception e) {
log.error("导入转换企标数据时发生错误:{}", e.getMessage());
@@ -407,7 +439,10 @@ public class LawsEnterpriseStandardServiceImpl extends ServiceImpl<LawsEnterpris
standardNumberToEntityMap.put(existingEntity.getStandardNumber(), existingEntity);
}
// 3. 遍历待保存或更新的列表,设置ID
// 3. 将文件关系列表转为Map,用于快速查找
Map<String, List<OSSFile>> standardNumberToFileMap = ossFileList.stream().collect(Collectors.groupingBy(OSSFile::getStandardNo));
// 4. 遍历待保存或更新的列表,设置ID
for (LawsEnterpriseStandard entity : esList) {
String curStandardNumber = entity.getStandardNumber();
LawsEnterpriseStandard existingEntity = standardNumberToEntityMap.get(entity.getStandardNumber());
@@ -432,7 +467,14 @@ public class LawsEnterpriseStandardServiceImpl extends ServiceImpl<LawsEnterpris
}
entity.setSubstituteStandardNumber(null);
entity.setReplacedByStandardNumber(null);
// 保存标准
save(entity);
// 保存文件关联
String standardId = entity.getId();
// 保存文件关联
OSSFile ossFile = standardNumberToFileMap.get(entity.getStandardNumber()).get(0);
ossFile.setStandardId(standardId);
ossFileService.save(ossFile);
}
log.info("老法规企标覆盖导入完成");
// 获取当前时间戳(毫秒)
@@ -454,6 +496,28 @@ public class LawsEnterpriseStandardServiceImpl extends ServiceImpl<LawsEnterpris
return esList.stream().map(LawsEnterpriseStandard::getStandardNumber)
.map(EnterpriseStandardUtil::getSequenceNoFromStandardNo).collect(Collectors.toList());
}
/**
* 截取正确的文件路径
*
* @param originalPath 文件路径
* @return 文件的InputStream
* @throws MinioException
*/
private String getFilePath(String originalPath) {
// 将所有的反斜杠转换为正斜杠
String normalizedPath = originalPath.replace('\\', '/');
// 检查路径是否以"/Document"或"Document/"开头
if (normalizedPath.startsWith("/Document/")) {
// 移除开头的"/Document/"
normalizedPath = normalizedPath.substring("/Document/".length());
} else if (normalizedPath.startsWith("Document/")) {
// 移除开头的"Document/"
normalizedPath = normalizedPath.substring("Document/".length());
}
return normalizedPath;
}
}