feat: 企标导入支持导入稽查内容Excel

代码优化,删除部分方法中的无用参数
This commit is contained in:
2023-10-08 09:11:29 +08:00
parent dbc8e4911b
commit ceb19a753a
3 changed files with 124 additions and 13 deletions
@@ -7,7 +7,6 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jero.common.constant.CommonConstant;
import com.jero.common.constant.enums.LanguageEnum;
import com.jero.common.constant.enums.YesOrNoEnum;
import com.jero.common.exception.JeroBootException;
@@ -515,8 +514,9 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
}
// 提前处理企标的稽查内容,否则后面转换会报错
List<LinkedHashMap> checkList = (List<LinkedHashMap>) parameterMap.get(FieldCommon.CHECK_LIST);
List<EnterpriseStandardInspectContent> inspectContents = null;
if (null != checkList) {
List<EnterpriseStandardInspectContent> inspectContents = this.convertList(checkList);
inspectContents = this.convertList(checkList);
parameterMap.put(FieldCommon.CHECK_LIST, null);
}
// 法规编号不能为空
@@ -584,9 +584,9 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
lawsNodeRelationService.addRelation(standardSystem, standardNumber, id);
// 执行企标独有的逻辑
// if (module.equals(TableNameEnum.ENTERPRISE_STANDARDS.getValue())) {
// this.esInspectContentSaveOrUpdate(inspectContents, id, standardNumber);
// }
if (module.equals(TableNameEnum.ENTERPRISE_STANDARDS.getValue()) && inspectContents != null) {
this.esInspectContentSaveOrUpdate(inspectContents, id, standardNumber);
}
return MessageUtils.getMessage(ResultCommon.OK);
}
@@ -1256,9 +1256,8 @@ 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, fileList, tableName);
List<String> errorMsgsList = this.validDataFormat(insertDataList, file, tableName);
// 判断如果校验有问题则返回校验错误信息,没问题则将数据转化为导入方法所需参数
if (errorMsgsList.size() > 0) {
return errorMsgsList;
@@ -1301,11 +1300,10 @@ 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, List<Map<String, Object>> fileList, String tableName) throws IOException {
private List<String> validDataFormat(List<Map<String, Object>> insertDataList, MultipartFile file, String tableName) throws IOException {
List<String> errMsgList = new ArrayList<>();
// 判断上传的是不是压缩包
@@ -1401,6 +1399,16 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
for (Cell cell : hearderRow) {
headerList.add(headerMap.get(cell.toString().replace("*", "")));
}
// 如果是企标的话手动添加稽查内容字段
if (TableNameEnum.ENTERPRISE_STANDARDS.getTableName().equals(tableName)) {
LawsTag inspectContentTag = new LawsTag();
inspectContentTag.setDbFieldTxt("稽查内容");
inspectContentTag.setFieldShowType(FieldShowTypeEnum.INSPECT_CONTENT_FILE_UPLOAD.getValue());
inspectContentTag.setDbFieldName(FieldCommon.CHECK_LIST);
inspectContentTag.setFieldMustInput(YesOrNoEnum.NO.getValue());
headerList.remove(headerList.size() - 1);
headerList.add(inspectContentTag);
}
// 从数据行开始遍历
for (int i = 3; i < sheet.getPhysicalNumberOfRows(); i++) {
Map<String, Object> parameterMap = new HashMap<>();
@@ -1931,7 +1939,7 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
// 添加稽查内容字段
LawsTag inspectContentTag = new LawsTag();
inspectContentTag.setDbFieldTxt("稽查内容");
inspectContentTag.setFieldShowType(FieldShowTypeEnum.FILE_UPLOAD.getValue());
inspectContentTag.setFieldShowType(FieldShowTypeEnum.INSPECT_CONTENT_FILE_UPLOAD.getValue());
fieldList.add(inspectContentTag);
// 创建一个Excel工作簿
@@ -1953,7 +1961,7 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
// 标准内容或要求
LawsTag requirementTag = new LawsTag();
requirementTag.setDbFieldTxt("内容或要求");
requirementTag.setDbFieldTxt("内容或要求");
requirementTag.setFieldShowType(FieldShowTypeEnum.TEXT_BOX.getValue());
requirementTag.setFieldMustInput(YesOrNoEnum.YES.getValue());
fieldList.add(requirementTag);
@@ -1,6 +1,7 @@
package com.jero.modules.laws.common.util;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jero.common.constant.enums.YesOrNoEnum;
import com.jero.common.system.api.ISysBaseAPI;
@@ -22,12 +23,11 @@ import com.jero.modules.tag.entity.LawsTag;
import com.jero.modules.tag.enums.FieldShowTypeEnum;
import com.jero.modules.tag.enums.TableNameEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.*;
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;
@@ -220,9 +220,48 @@ public class ImportUtil {
// 将转好的值加入Map
parameterMap.put(tag.getDbFieldName(), cellValue);
// 企标稽查内容文件导入
if (FieldShowTypeEnum.INSPECT_CONTENT_FILE_UPLOAD.getValue().equals(tag.getFieldShowType())) {
if (!this.inspectContentFileValid(cellValue, fileMap)) {
log.error("" + (i + 1) + "行第" + (j + 1) + "列选项" + tag.getDbFieldTxt() + "填写错误");
errMsgList.add("" + (i + 1) + "行第" + (j + 1) + "列选项" + tag.getDbFieldTxt() + "填写错误");
} else {
// 转为List
// 将转好的值加入Map
parameterMap.put(tag.getDbFieldName(), this.convertInspectContentFile(cellValue, fileMap));
}
}
return errMsgList;
}
private Object convertInspectContentFile(String cellValue, Map<String, File> fileMap) {
List<LinkedHashMap<String, String>> checkList = new ArrayList<>();
String filePath = Arrays.asList(cellValue.split(",")).get(0);
// 根据路径取出excel文件
File excelFile = fileMap.get(filePath);
try (FileInputStream fis = new FileInputStream(excelFile);
Workbook workbook = WorkbookFactory.create(fis)) {
Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表
// 从第四行开始循环检查
for (int i = 3; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
if (row == null) {
continue; // 跳过空行
}
// 获取第四行前两个单元格的内容
String cellContent1 = getCellContent(row.getCell(0));
String cellContent2 = getCellContent(row.getCell(1));
LinkedHashMap<String, String> inspectContentMap = new LinkedHashMap<>();
inspectContentMap.put("clause", cellContent1);
inspectContentMap.put("requirement", cellContent2);
checkList.add(inspectContentMap);
}
} catch (Exception e) {
e.printStackTrace();
}
return checkList;
}
private String multiDateConvert(String cellValue) {
String[] dates = cellValue.split("");
// 用英文逗号拼接
@@ -451,4 +490,65 @@ public class ImportUtil {
}
return true;
}
// 文件校验
private boolean inspectContentFileValid(String cellValue, Map<String, File> fileMap) {
// 检查是否真的有这个文件
if (!this.fileValid(cellValue, fileMap)) {
return false;
}
String filePath = Arrays.asList(cellValue.split(",")).get(0);
// 根据路径取出excel文件
File excelFile = fileMap.get(filePath);
if (excelFile == null) {
return false;
}
try (FileInputStream fis = new FileInputStream(excelFile);
Workbook workbook = WorkbookFactory.create(fis)) {
Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表
// 从第四行开始循环检查
for (int i = 3; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
if (row == null) {
continue; // 跳过空行
}
// 获取第四行前两个单元格的内容
String cellContent1 = getCellContent(row.getCell(0));
String cellContent2 = getCellContent(row.getCell(1));
// 执行valid方法进行校验
if (!valid(cellContent1, cellContent2)) {
return false;
}
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
// 辅助方法,获取单元格的内容
private String getCellContent(Cell cell) {
if (cell == null) {
return null;
}
switch (cell.getCellType()) {
case STRING:
return cell.getStringCellValue();
case NUMERIC:
return Double.toString(cell.getNumericCellValue());
case BOOLEAN:
return Boolean.toString(cell.getBooleanCellValue());
default:
return null;
}
}
// 必填校验
private boolean valid(String content1, String content2) {
// 这里只做简单的非空校验
return content1 != null && !content1.trim().isEmpty() && content2 != null && !content2.trim().isEmpty();
}
}
@@ -16,6 +16,9 @@ public enum FieldShowTypeEnum {
MULTI_STANDARD_DIALOG("多选标准弹框","6","请填写选项中包含的文本,多填以中文逗号隔开"),
MULTI_ORGANIZATION_DIALOG("多选组织机构弹框","7","请填写选项中包含的文本,多填以中文逗号隔开"),
FILE_UPLOAD("上传文件","8","文件属性,请输入:标准号/文件名称.文件格式"),
// 企标稽查内容导入
INSPECT_CONTENT_FILE_UPLOAD("上传稽查内容导入模板","ZZ","文件属性,请输入:标准号/稽查内容.xlsx"),
TEXT_BOX("文本框","9","文本输入"),
DROPDOWN_SINGLE_SELECT("下拉单选","10","请填写选项中包含的文本"),
DROPDOWN_MULTI_SELECT("下拉多选","11","请填写选项中包含的文本,多填以中文逗号隔开"),