feat: 企标导入-文件导入
This commit is contained in:
+31
-13
@@ -364,7 +364,9 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
|
||||
LambdaQueryWrapper<SysDictItem> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(SysDictItem::getId, value);
|
||||
SysDictItem dictItem = sysDictItemService.getOne(queryWrapper);
|
||||
resultMap.put(key + FieldCommon._DICT_TEXT, dictItem.getItemText());
|
||||
if (dictItem != null) {
|
||||
resultMap.put(key + FieldCommon._DICT_TEXT, dictItem.getItemText());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1340,8 +1342,9 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
|
||||
public List<String> importExcelWithData(MultipartFile file, String module) {
|
||||
try {
|
||||
List<Map<String, Object>> insertDataList = new ArrayList<>();
|
||||
List<Map<String, Object>> fileList = new ArrayList<>();
|
||||
String tableName = TableNameEnum.getTableName(module);
|
||||
List<String> errorMsgsList = this.validDataFormat(insertDataList, file, tableName);
|
||||
List<String> errorMsgsList = this.validDataFormat(insertDataList, file, fileList, tableName);
|
||||
// 判断如果校验有问题则返回校验错误信息,没问题则将数据转化为导入方法所需参数
|
||||
if (errorMsgsList.size() > 0) {
|
||||
return errorMsgsList;
|
||||
@@ -1362,12 +1365,14 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
|
||||
|
||||
/**
|
||||
* 校验导入数据有效性
|
||||
*
|
||||
* @param insertDataList
|
||||
* @param file
|
||||
* @param fileList
|
||||
* @param tableName
|
||||
* @throws IOException
|
||||
*/
|
||||
private List<String> validDataFormat(List<Map<String, Object>> insertDataList, MultipartFile file, String tableName) throws IOException {
|
||||
private List<String> validDataFormat(List<Map<String, Object>> insertDataList, MultipartFile file, List<Map<String, Object>> fileList, String tableName) throws IOException {
|
||||
List<String> errMsgList = new ArrayList<>();
|
||||
|
||||
// 判断上传的是不是压缩包
|
||||
@@ -1402,22 +1407,24 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
|
||||
errMsgList.add("无法确定压缩包的字符编码");
|
||||
return errMsgList;
|
||||
}
|
||||
|
||||
Map<String, File> fileMap = new HashMap<>();
|
||||
try (ZipFile zipFile = new ZipFile(tempFile, correctCharset)) {
|
||||
Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
||||
|
||||
// 文件数量校验
|
||||
while (entries.hasMoreElements()) {
|
||||
ZipEntry entry = entries.nextElement();
|
||||
String entryName = entry.getName();
|
||||
boolean isRootLevel = !entryName.contains("/"); // 假设 '/' 作为目录分隔符
|
||||
|
||||
if (entry.isDirectory()) {
|
||||
folderCount++;
|
||||
}
|
||||
if (folderCount > 1) {
|
||||
if (folderCount > 1 && isRootLevel) {
|
||||
errMsgList.add("压缩包根目录下仅能存在一个文件夹");
|
||||
return errMsgList;
|
||||
}
|
||||
|
||||
if (!entry.isDirectory() && entry.getName().endsWith(".xlsx")) {
|
||||
if (!entry.isDirectory() && entryName.endsWith(".xlsx") && isRootLevel) {
|
||||
excelCount++;
|
||||
targetExcelEntry = entry;
|
||||
}
|
||||
@@ -1425,20 +1432,32 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
|
||||
errMsgList.add("压缩包根目录下仅能存在一个 excel 为导入数据");
|
||||
return errMsgList;
|
||||
}
|
||||
|
||||
if (!entry.isDirectory()) {
|
||||
String relativePath = entry.getName(); // Use the entry's name as the relative path
|
||||
|
||||
File tempOutputFile = Files.createTempFile("tempFile_", "_" + new File(relativePath).getName()).toFile();
|
||||
try (InputStream is = zipFile.getInputStream(entry);
|
||||
OutputStream os = Files.newOutputStream(tempOutputFile.toPath())) {
|
||||
byte[] buffer = new byte[1024];
|
||||
int length;
|
||||
while ((length = is.read(buffer)) > 0) {
|
||||
os.write(buffer, 0, length);
|
||||
}
|
||||
}
|
||||
fileMap.put(relativePath, tempOutputFile); // Use relative path as key
|
||||
}
|
||||
}
|
||||
|
||||
// 数据内容格式校验
|
||||
if (targetExcelEntry != null) {
|
||||
try (Workbook workbook = new XSSFWorkbook(zipFile.getInputStream(targetExcelEntry))) {
|
||||
Sheet sheet = workbook.getSheetAt(0);
|
||||
|
||||
// 获取标题行中文 转换为LawsTag对象
|
||||
Row hearderRow = sheet.getRow(0);
|
||||
|
||||
// 获取全部字段
|
||||
List<LawsTag> fieldList = lawsCommonMapper.getFieldList(tableName);
|
||||
// 筛选表单显示的字段
|
||||
|
||||
// 找到所有标题Map
|
||||
Map<String, LawsTag> headerMap = fieldList.stream().filter(lawsTag -> YesOrNoEnum.YES.getValue()
|
||||
.equals(lawsTag.getIsShowForm().toString())).collect(Collectors.toMap(
|
||||
@@ -1449,17 +1468,16 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
|
||||
for (Cell cell : hearderRow) {
|
||||
headerList.add(headerMap.get(cell.toString().replace("*", "")));
|
||||
}
|
||||
|
||||
// 从数据行开始遍历
|
||||
for (int i = 3; i < sheet.getPhysicalNumberOfRows(); i++) {
|
||||
Map<String, Object> parameterMap = new HashMap<>();
|
||||
for (int j = 0; j < sheet.getRow(i).getPhysicalNumberOfCells(); j++) {
|
||||
for (int j = 0; j < headerList.size(); j++) {
|
||||
// 获取当前单元格
|
||||
Cell cell = sheet.getRow(i).getCell(j);
|
||||
// 获取当前单元格内容对应的字段
|
||||
LawsTag tag = headerList.get(j);
|
||||
// 调用导入工具类的校验方法
|
||||
errMsgList.addAll(importUtil.allColumnValid(cell, tag, i, j, parameterMap));
|
||||
errMsgList.addAll(importUtil.allColumnValid(cell, tag, i, j, parameterMap, fileMap));
|
||||
}
|
||||
insertDataList.add(parameterMap);
|
||||
}
|
||||
|
||||
@@ -5,19 +5,29 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.jero.common.constant.enums.YesOrNoEnum;
|
||||
import com.jero.common.system.api.ISysBaseAPI;
|
||||
import com.jero.common.system.vo.DictModel;
|
||||
import com.jero.common.util.CommonUtils;
|
||||
import com.jero.common.util.oConvertUtils;
|
||||
import com.jero.modules.laws.common.constant.FieldCommon;
|
||||
import com.jero.modules.laws.standard.entity.LawsTreeNode;
|
||||
import com.jero.modules.laws.standard.service.ILawsTreeNodeService;
|
||||
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 com.jero.modules.tag.entity.LawsTag;
|
||||
import com.jero.modules.tag.enums.FieldShowTypeEnum;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -26,9 +36,13 @@ import java.util.stream.Collectors;
|
||||
* @author: Mzaxd
|
||||
* @Date: 2023/9/26 17:31
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class ImportUtil {
|
||||
|
||||
@Value(value="${jero.uploadType}")
|
||||
private String uploadType;
|
||||
|
||||
@Resource
|
||||
private ILawsTreeNodeService lawsTreeNodeService;
|
||||
|
||||
@@ -41,7 +55,11 @@ public class ImportUtil {
|
||||
@Resource
|
||||
private ISysDepartService sysDepartService;
|
||||
|
||||
public List<String> allColumnValid(Cell cell, LawsTag tag, int i, int j, Map<String, Object> parameterMap) {
|
||||
@Resource
|
||||
private IOSSFileService ossFileService;
|
||||
|
||||
|
||||
public List<String> allColumnValid(Cell cell, LawsTag tag, int i, int j, Map<String, Object> parameterMap, Map<String, File> fileMap) {
|
||||
List<String> errMsgList = new ArrayList<>();
|
||||
// 必填验证
|
||||
if (YesOrNoEnum.YES.getValue().equals(tag.getFieldMustInput())) {
|
||||
@@ -139,12 +157,49 @@ public class ImportUtil {
|
||||
cellValue = this.convertStandardSystem(cellValue);
|
||||
}
|
||||
}
|
||||
// 文件
|
||||
if (FieldShowTypeEnum.FILE_UPLOAD.getValue().equals(tag.getFieldShowType())) {
|
||||
if (!this.fileValid(cellValue, fileMap)) {
|
||||
errMsgList.add("第" + (i + 1) + "行第" + (j + 1) + "列填写错误");
|
||||
} else {
|
||||
// 转为id
|
||||
cellValue = this.convertFileIds(cellValue, fileMap);
|
||||
}
|
||||
}
|
||||
// TODO 多选时间
|
||||
// 将转好的值加入Map
|
||||
parameterMap.put(tag.getDbFieldName(), cellValue);
|
||||
return errMsgList;
|
||||
}
|
||||
|
||||
private String convertFileIds(String cellValue, Map<String, File> fileMap) {
|
||||
String[] pathList = cellValue.split(",");
|
||||
StringBuilder fileIds = new StringBuilder();
|
||||
for (String path : pathList) {
|
||||
try {
|
||||
String bizPath = "temp";
|
||||
File file = fileMap.get(path);
|
||||
FileInputStream fileInputStream = new FileInputStream(file);
|
||||
MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "application/octet-stream", fileInputStream);
|
||||
String savePath = CommonUtils.upload(multipartFile, bizPath, uploadType);
|
||||
if (oConvertUtils.isNotEmpty(savePath)) {
|
||||
//上传成功 进行数据库存储
|
||||
OSSFile ossFile = new OSSFile();
|
||||
// 文件名
|
||||
String fileName = multipartFile.getOriginalFilename();
|
||||
fileName = CommonUtils.getFileName(fileName);
|
||||
ossFile.setFileName(fileName);
|
||||
ossFile.setUrl(savePath);
|
||||
ossFileService.save(ossFile);
|
||||
fileIds.append(ossFile.getId()).append(",");
|
||||
}
|
||||
}catch (Exception e) {
|
||||
log.error("导入文件上传失败", e);
|
||||
}
|
||||
}
|
||||
// 去掉最后一个逗号
|
||||
return fileIds.deleteCharAt(fileIds.length() - 1).toString();
|
||||
}
|
||||
|
||||
// 体系转换
|
||||
public String convertStandardSystem(String cellValue) {
|
||||
@@ -298,4 +353,22 @@ public class ImportUtil {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
private boolean fileValid(String cellValue, Map<String, File> fileMap) {
|
||||
String[] pathList = cellValue.split(",");
|
||||
for (String path : pathList) {
|
||||
// 校验是否真的有这个文件
|
||||
File file = fileMap.get(path);
|
||||
if (file == null) {
|
||||
return false;
|
||||
}
|
||||
if (path.endsWith(".pdf")
|
||||
|| path.endsWith(".doc") || path.endsWith(".docx")
|
||||
|| path.endsWith(".xls") || path.endsWith(".xlsx")
|
||||
|| path.endsWith(".png") || path.endsWith(".jpg")) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user