feat: 老法规系统数据导入v3.0
This commit is contained in:
+225
-134
@@ -1,7 +1,6 @@
|
||||
package com.jero.modules.laws.standard.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.jero.common.api.CommonAPI;
|
||||
import com.jero.common.constant.CommonConstant;
|
||||
@@ -12,7 +11,9 @@ import com.jero.modules.laws.common.constant.FieldCommon;
|
||||
import com.jero.modules.laws.enterprise.entity.OldSystemImportDTO;
|
||||
import com.jero.modules.laws.enterprise.service.ESSequenceNumberProvider;
|
||||
import com.jero.modules.laws.enterprise.util.EnterpriseStandardUtil;
|
||||
import com.jero.modules.laws.standard.entity.LawsDomesticStandard;
|
||||
import com.jero.modules.laws.standard.entity.LawsEnterpriseStandard;
|
||||
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;
|
||||
@@ -28,8 +29,6 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -37,19 +36,22 @@ import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
/**
|
||||
* @author ThinkBook
|
||||
* @description 针对表【laws_enterprise_standard(企业标准)】的数据库操作Service实现
|
||||
* @createDate 2023-09-18 14:49:51
|
||||
*/
|
||||
* @author ThinkBook
|
||||
* @description 针对表【laws_enterprise_standard(企业标准)】的数据库操作Service实现
|
||||
* @createDate 2023-09-18 14:49:51
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@Transactional(rollbackFor = JeroBootException.class)
|
||||
public class LawsEnterpriseStandardServiceImpl extends ServiceImpl<LawsEnterpriseStandardMapper, LawsEnterpriseStandard>
|
||||
implements ILawsEnterpriseStandardService, ESSequenceNumberProvider {
|
||||
implements ILawsEnterpriseStandardService, ESSequenceNumberProvider {
|
||||
|
||||
@Resource
|
||||
private LawsEnterpriseStandardMapper esMapper;
|
||||
|
||||
@Resource
|
||||
private ILawsDomesticStandardService domesticStandardService;
|
||||
|
||||
@Resource
|
||||
private ExcelUtils excelUtils;
|
||||
|
||||
@@ -61,7 +63,7 @@ public class LawsEnterpriseStandardServiceImpl extends ServiceImpl<LawsEnterpris
|
||||
|
||||
@Override
|
||||
public LawsEnterpriseStandard queryByStandardNumber(String standardNumber) {
|
||||
if (StringUtils.isBlank(standardNumber)){
|
||||
if (StringUtils.isBlank(standardNumber)) {
|
||||
return null;
|
||||
}
|
||||
LambdaQueryWrapper<LawsEnterpriseStandard> wrapper = new LambdaQueryWrapper<>();
|
||||
@@ -72,131 +74,6 @@ public class LawsEnterpriseStandardServiceImpl extends ServiceImpl<LawsEnterpris
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void importExcelOldSystem(MultipartFile zipFile) {
|
||||
// 验证文件是否为压缩格式
|
||||
if (!Objects.requireNonNull(zipFile.getContentType()).contains("zip")) {
|
||||
throw new JeroBootException("文件格式不正确,请上传zip格式的压缩包。");
|
||||
}
|
||||
ImportParams params = new ImportParams();
|
||||
params.setTitleRows(0);
|
||||
params.setNeedSave(true);
|
||||
// 获取数据
|
||||
List<OldSystemImportDTO> list = null;
|
||||
// 解压缩文件,查找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();
|
||||
list = ExcelImportUtil.importExcel(file.getInputStream(), OldSystemImportDTO.class, params);
|
||||
// 删除空行
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
OldSystemImportDTO oldSystemImportDTO = list.get(i);
|
||||
if (oldSystemImportDTO.isEmptyRow()) {
|
||||
list.remove(i);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
// 删除国标
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
OldSystemImportDTO oldSystemImportDTO = list.get(i);
|
||||
if (oldSystemImportDTO.getBzfl().startsWith("国家标准")
|
||||
|| oldSystemImportDTO.getBzh().startsWith("GB")
|
||||
|| oldSystemImportDTO.getBzh().startsWith("海外标准")
|
||||
|| oldSystemImportDTO.getBzh().startsWith("FB")) {
|
||||
list.remove(i);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
log.info("获取数据成功,数据条数:{}", list.size());
|
||||
// 获取当前时间戳(毫秒)
|
||||
long endTime = System.currentTimeMillis();
|
||||
// 计算执行时间(毫秒)
|
||||
long executionTime = endTime - startTime;
|
||||
log.info("代码执行时间:" + executionTime + " 毫秒");
|
||||
} catch (Exception e) {
|
||||
log.error("导入失败;{}", e.getMessage());
|
||||
}
|
||||
// 转换数据
|
||||
List<LawsEnterpriseStandard> esList = new ArrayList<>();
|
||||
// list判空
|
||||
if (list == null || list.isEmpty()) {
|
||||
throw new JeroBootException("导入数据为空");
|
||||
}
|
||||
for (OldSystemImportDTO dto : list) {
|
||||
LawsEnterpriseStandard es = new LawsEnterpriseStandard();
|
||||
es.setStandardNumber(dto.getBzh());
|
||||
es.setStandardName(dto.getBzmc());
|
||||
es.setEnglishName(dto.getBzywmc());
|
||||
es.setReleaseDate(dto.getFbrq());
|
||||
es.setImplementationDate(dto.getSsrq());
|
||||
String standardState = dto.getBzzt();
|
||||
es.setStandardState(standardState.equals("现行有效") ? 8 : 9);
|
||||
//代替被代替特殊逻辑
|
||||
String tdbzh = dto.getTdbzh();
|
||||
String btdbzh = dto.getBtdbzh();
|
||||
// 代替标准号
|
||||
if (StringUtils.isNotBlank(tdbzh)) {
|
||||
lawsReplacedStandardService.addRelation(btdbzh, tdbzh, 1);
|
||||
}
|
||||
// 被代替标准号
|
||||
if (StringUtils.isNotBlank(btdbzh)) {
|
||||
List<String> replaceCodeList = Arrays.asList(btdbzh.split(";"));
|
||||
replaceCodeList.forEach(replaceCode -> {
|
||||
lawsReplacedStandardService.addRelation(replaceCode, btdbzh, 1);
|
||||
});
|
||||
}
|
||||
// 等级分类默认值
|
||||
es.setGradeClassification("1");
|
||||
esList.add(es);
|
||||
}
|
||||
// 处理数据
|
||||
this.handleESData(esList);
|
||||
// 保存数据
|
||||
// 1. 首先获取所有现有的标准号
|
||||
List<LawsEnterpriseStandard> existingEntities = esMapper.selectList(new QueryWrapper<>());
|
||||
|
||||
// 2. 创建一个Map,用于快速查找是否存在相同的标准号
|
||||
Map<String, LawsEnterpriseStandard> standardNumberToEntityMap = new HashMap<>();
|
||||
for (LawsEnterpriseStandard existingEntity : existingEntities) {
|
||||
standardNumberToEntityMap.put(existingEntity.getStandardNumber(), existingEntity);
|
||||
}
|
||||
|
||||
// 3. 遍历待保存或更新的列表,设置ID
|
||||
List<LawsEnterpriseStandard> toSaveOrUpdate = new ArrayList<>();
|
||||
for (LawsEnterpriseStandard entity : esList) {
|
||||
LawsEnterpriseStandard existingEntity = standardNumberToEntityMap.get(entity.getStandardNumber());
|
||||
if (existingEntity != null) {
|
||||
// 如果存在,则设置ID以进行更新
|
||||
entity.setId(existingEntity.getId());
|
||||
}
|
||||
toSaveOrUpdate.add(entity);
|
||||
}
|
||||
|
||||
// 4. 最后,执行批量保存或更新
|
||||
saveOrUpdateBatch(toSaveOrUpdate);
|
||||
}
|
||||
|
||||
private void handleESData(List<LawsEnterpriseStandard> esList) {
|
||||
List<SysDictItemCore> listDict = commonAPI.getDictItemAll();
|
||||
// 编号拆分
|
||||
@@ -227,6 +104,220 @@ public class LawsEnterpriseStandardServiceImpl extends ServiceImpl<LawsEnterpris
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void importExcelOldSystem(MultipartFile zipFile) {
|
||||
// 验证文件是否为压缩格式
|
||||
if (!Objects.requireNonNull(zipFile.getContentType()).contains("zip")) {
|
||||
throw new JeroBootException("文件格式不正确,请上传zip格式的压缩包。");
|
||||
}
|
||||
ImportParams params = new ImportParams();
|
||||
params.setTitleRows(0);
|
||||
params.setNeedSave(true);
|
||||
// 获取数据
|
||||
List<OldSystemImportDTO> allDatalist = null;
|
||||
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();
|
||||
allDatalist = ExcelImportUtil.importExcel(file.getInputStream(), OldSystemImportDTO.class, params);
|
||||
// 删除空行
|
||||
for (int i = 0; i < allDatalist.size(); i++) {
|
||||
OldSystemImportDTO oldSystemImportDTO = allDatalist.get(i);
|
||||
if (oldSystemImportDTO.isEmptyRow()) {
|
||||
allDatalist.remove(i);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
// 区分数据
|
||||
for (OldSystemImportDTO oldSystemImportDTO : allDatalist) {
|
||||
if (oldSystemImportDTO.getBzfl().startsWith("企业标准")) {
|
||||
esDatalist.add(oldSystemImportDTO);
|
||||
} else if (oldSystemImportDTO.getBzfl().startsWith("国家标准")) {
|
||||
gbDatalist.add(oldSystemImportDTO);
|
||||
} else {
|
||||
fbDatalist.add(oldSystemImportDTO);
|
||||
}
|
||||
}
|
||||
log.info("获取数据成功,数据条数:{}", allDatalist.size());
|
||||
log.info("获取数据成功,企标数据条数:{}", esDatalist.size());
|
||||
log.info("获取数据成功,国标数据条数:{}", gbDatalist.size());
|
||||
log.info("获取数据成功,其他数据条数:{}", fbDatalist.size());
|
||||
// 获取当前时间戳(毫秒)
|
||||
long endTime = System.currentTimeMillis();
|
||||
// 计算执行时间(毫秒)
|
||||
long executionTime = endTime - startTime;
|
||||
log.info("数据转换执行时间:" + executionTime + " 毫秒");
|
||||
} catch (Exception e) {
|
||||
log.error("导入失败;{}", e.getMessage());
|
||||
}
|
||||
// 导入企标
|
||||
// 判断是否需要跳过
|
||||
if (esDatalist.isEmpty()) {
|
||||
log.info("企标数据为空,跳过导入");
|
||||
} else {
|
||||
this.oldSysESImport(esDatalist);
|
||||
}
|
||||
// 导入国标
|
||||
// 判断是否需要跳过
|
||||
if (gbDatalist.isEmpty()) {
|
||||
log.info("国标数据为空,跳过导入");
|
||||
} else {
|
||||
this.oldSysGBImport(gbDatalist);
|
||||
}
|
||||
}
|
||||
|
||||
private void oldSysGBImport(List<OldSystemImportDTO> gbDatalist) {
|
||||
long startTime = System.currentTimeMillis();
|
||||
//TODO 转换数据
|
||||
List<LawsDomesticStandard> gbList = new ArrayList<>();
|
||||
for (OldSystemImportDTO dto : gbDatalist) {
|
||||
|
||||
}
|
||||
// 保存数据
|
||||
// 1. 首先获取所有现有的企标标准号
|
||||
List<LawsDomesticStandard> existingEntities = domesticStandardService.list();
|
||||
|
||||
// 2. 创建一个Map,用于快速查找是否存在相同的标准号
|
||||
Map<String, LawsDomesticStandard> standardNumberToEntityMap = new HashMap<>();
|
||||
for (LawsDomesticStandard existingEntity : existingEntities) {
|
||||
standardNumberToEntityMap.put(existingEntity.getStandardNumber(), existingEntity);
|
||||
}
|
||||
|
||||
// 3. 遍历待保存或更新的列表,设置ID
|
||||
for (LawsDomesticStandard entity : gbList) {
|
||||
LawsDomesticStandard existingEntity = standardNumberToEntityMap.get(entity.getStandardNumber());
|
||||
if (existingEntity != null) {
|
||||
// 如果存在,则先删后增
|
||||
this.removeById(existingEntity.getId());
|
||||
// TODO 删除替代被替代
|
||||
lawsReplacedStandardService.removeRelation(existingEntity.getStandardNumber(), 1);
|
||||
}
|
||||
String tdbzh = entity.getSubstituteStandardNumber();
|
||||
String btdbzh = entity.getReplacedByStandardNumber();
|
||||
// 代替标准号
|
||||
if (StringUtils.isNotBlank(tdbzh)) {
|
||||
lawsReplacedStandardService.addRelation(btdbzh, tdbzh, 1);
|
||||
}
|
||||
// 被代替标准号
|
||||
if (StringUtils.isNotBlank(btdbzh)) {
|
||||
List<String> replaceCodeList = Arrays.asList(btdbzh.split(";"));
|
||||
replaceCodeList.forEach(replaceCode -> {
|
||||
lawsReplacedStandardService.addRelation(replaceCode, btdbzh, 1);
|
||||
});
|
||||
}
|
||||
entity.setSubstituteStandardNumber(null);
|
||||
entity.setReplacedByStandardNumber(null);
|
||||
domesticStandardService.save(entity);
|
||||
}
|
||||
log.info("老法规国标覆盖导入完成");
|
||||
// 获取当前时间戳(毫秒)
|
||||
long endTime = System.currentTimeMillis();
|
||||
// 计算执行时间(毫秒)
|
||||
long executionTime = endTime - startTime;
|
||||
log.info("代码执行时间:" + executionTime + " 毫秒");
|
||||
}
|
||||
|
||||
private void oldSysESImport(List<OldSystemImportDTO> esDatalist) {
|
||||
long startTime = System.currentTimeMillis();
|
||||
// 转换数据
|
||||
List<LawsEnterpriseStandard> esList = new ArrayList<>();
|
||||
|
||||
// 导入企标
|
||||
for (OldSystemImportDTO dto : esDatalist) {
|
||||
LawsEnterpriseStandard es = new LawsEnterpriseStandard();
|
||||
es.setStandardNumber(dto.getBzh());
|
||||
es.setStandardName(dto.getBzmc());
|
||||
es.setEnglishName(dto.getBzywmc());
|
||||
es.setReleaseDate(dto.getFbrq());
|
||||
es.setImplementationDate(dto.getSsrq());
|
||||
String standardState = dto.getBzzt();
|
||||
es.setStandardState(standardState.equals("现行有效") ? 8 : 9);
|
||||
// 代替被代替特殊逻辑
|
||||
es.setSubstituteStandardNumber(dto.getTdbzh());
|
||||
es.setReplacedByStandardNumber(dto.getBtdbzh());
|
||||
// 等级分类默认值
|
||||
es.setGradeClassification("1");
|
||||
esList.add(es);
|
||||
}
|
||||
// 处理数据
|
||||
this.handleESData(esList);
|
||||
// 保存数据
|
||||
// 1. 首先获取所有现有的企标标准号
|
||||
List<LawsEnterpriseStandard> existingEntities = this.list();
|
||||
|
||||
// 2. 创建一个Map,用于快速查找是否存在相同的标准号
|
||||
Map<String, LawsEnterpriseStandard> standardNumberToEntityMap = new HashMap<>();
|
||||
for (LawsEnterpriseStandard existingEntity : existingEntities) {
|
||||
standardNumberToEntityMap.put(existingEntity.getStandardNumber(), existingEntity);
|
||||
}
|
||||
|
||||
// 3. 遍历待保存或更新的列表,设置ID
|
||||
for (LawsEnterpriseStandard entity : esList) {
|
||||
String curStandardNumber = entity.getStandardNumber();
|
||||
LawsEnterpriseStandard existingEntity = standardNumberToEntityMap.get(entity.getStandardNumber());
|
||||
if (existingEntity != null) {
|
||||
// 如果存在,则先删后增
|
||||
this.removeById(existingEntity.getId());
|
||||
// 代替
|
||||
lawsReplacedStandardService.removeRelation(existingEntity.getStandardNumber(), 1);
|
||||
// 删除被代替
|
||||
String replacedBy = lawsReplacedStandardService.getReplacedBy(curStandardNumber, 1);
|
||||
if (StringUtils.isNotBlank(replacedBy)) {
|
||||
List<String> replaceCodeList = Arrays.asList(replacedBy.split(";"));
|
||||
replaceCodeList.forEach(replaceCode -> {
|
||||
lawsReplacedStandardService.removeRelation(replaceCode, 1);
|
||||
});
|
||||
}
|
||||
}
|
||||
String tdbzh = entity.getSubstituteStandardNumber();
|
||||
String btdbzh = entity.getReplacedByStandardNumber();
|
||||
// 代替标准号
|
||||
if (StringUtils.isNotBlank(tdbzh)) {
|
||||
lawsReplacedStandardService.addRelation(tdbzh, curStandardNumber, 1);
|
||||
}
|
||||
// 被代替标准号
|
||||
if (StringUtils.isNotBlank(btdbzh)) {
|
||||
List<String> replaceCodeList = Arrays.asList(btdbzh.split(";"));
|
||||
replaceCodeList.forEach(replaceCode -> {
|
||||
lawsReplacedStandardService.addRelation(curStandardNumber, replaceCode, 1);
|
||||
});
|
||||
}
|
||||
entity.setSubstituteStandardNumber(null);
|
||||
entity.setReplacedByStandardNumber(null);
|
||||
save(entity);
|
||||
}
|
||||
log.info("老法规企标覆盖导入完成");
|
||||
// 获取当前时间戳(毫秒)
|
||||
long endTime = System.currentTimeMillis();
|
||||
// 计算执行时间(毫秒)
|
||||
long executionTime = endTime - startTime;
|
||||
log.info("代码执行时间:" + executionTime + " 毫秒");
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getSequenceNumbers(String esCodeDictValue, String esNameCodeDictValue, String categoryCodeDictValue) {
|
||||
// 已经存在的标准
|
||||
|
||||
Reference in New Issue
Block a user