fix:78298

This commit is contained in:
梁琦涛
2023-11-08 11:41:09 +08:00
parent 4403d2a767
commit 13fd044078
2 changed files with 111 additions and 1 deletions
@@ -6,8 +6,21 @@ import com.jero.common.api.vo.Result;
import com.jero.common.constant.CommonConstant;
import com.jero.common.exception.JeroBootException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
/**
@@ -96,4 +109,74 @@ public class ImportExcelUtil {
}
return errorMessage;
}
/**
* 检查模板标题行模型是否匹配 不检查顺序(需要检查顺序的可自己优化)
*
* @param file 上传的excel 文件 对象
* @param pojoClass 导入模型
* @param titleRow 标题行下标
* @param redundantNum 冗余数 默认为0
* @return
*/
public static Boolean checkTemplateTitle(MultipartFile file, Class<?> pojoClass, Integer titleRow, int redundantNum) {
//excel中标题行的所有标题
List<String> excelList = new ArrayList<>();
//注解中所有标题
List<String> list = new ArrayList<>();
Field[] fields = pojoClass.getDeclaredFields();
for (Field field : fields) {
Annotation excelAnn = field.getAnnotation(Excel.class);
if (null != excelAnn) {
Excel ec = (Excel) excelAnn;
String name = ec.name();
if (StringUtils.isNotBlank(name)) {
list.add(name);
}
}
}
if (CollectionUtils.isEmpty(list)) {
return true;
}
InputStream inputStream = null;
try {
inputStream = file.getInputStream();
if (!(inputStream.markSupported())) {
inputStream = new PushbackInputStream(inputStream, 8);
}
Workbook book = WorkbookFactory.create(inputStream);
Sheet sheet = book.getSheetAt(0);
Row row = sheet.getRow(titleRow);
for (int i = 0; i < row.getPhysicalNumberOfCells(); i++) {
String value = row.getCell(i).getStringCellValue();
if (StringUtils.isNotBlank(value)) {
excelList.add(value);
}
}
book.close();
} catch (Exception e) {
throw new JeroBootException("excel表头错误");
}
if (CollectionUtils.isEmpty(excelList)) {
return false;
}
boolean flag = true;
flag = excelContainsList(redundantNum, excelList, list, flag);
return flag;
}
private static boolean excelContainsList(int redundantNum, List<String> excelList, List<String> list,boolean flag) {
if(excelList.size() != list.size()){
flag = false;
}
int i = 0;
for (String name : list) {
if (!excelList.contains(name)) {
i++;
}
}
if (i > redundantNum) {
flag = false;
}
return flag;
}
}
@@ -12,6 +12,7 @@ import com.jero.common.constant.CommonConstant;
import com.jero.common.exception.JeroBootException;
import com.jero.common.system.base.controller.JeroController;
import com.jero.common.util.MessageUtils;
import com.jero.modules.activiti.util.ExcelUtils;
import com.jero.modules.laws.common.constant.ResultCommon;
import com.jero.modules.sys.entity.LawsWorkingGroupUser;
import com.jero.modules.sys.service.ILawsWorkingGroupUserService;
@@ -20,6 +21,7 @@ import com.jero.modules.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.authz.annotation.RequiresPermissions;
@@ -170,6 +172,21 @@ public class LawsWorkingGroupUserController extends JeroController<LawsWorkingGr
if (fileName.lastIndexOf(".") != -1 && !".xlsx".equals(fileName.substring(fileName.lastIndexOf(".")))) {
throw new JeroBootException(ResultCommon.PLEASE_USE_A_FILE_WITH_THE_SUFFIX_XLSX);
}
// 获取Excel的header
List<String> headers;
try {
headers = ExcelUtils.getHeaderFromExcel(file.getInputStream(), 0);
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new JeroBootException("导入模板错误");
}
// 使用静态方法验证header
boolean isMatched = headers.equals(ExcelUtils.getSortedHeadersFromClass(LawsWorkingGroupUser.class));
if (!isMatched) {
throw new JeroBootException("导入模板错误");
}
InputStream inputStream = file.getInputStream();
List<LawsWorkingGroupUser> all;
try {
@@ -303,7 +320,17 @@ public class LawsWorkingGroupUserController extends JeroController<LawsWorkingGr
return String.join(";", errorMessages);
}
private List<String> validateWorkNo(List<LawsWorkingGroupUser> all) {
Set<String> existingWorkNos = lawsWorkingGroupUserService.list().stream().map(LawsWorkingGroupUser::getWorkingGroupId).collect(Collectors.toSet());
List<SysUser> listUser = sysUserService.list();
all.stream().forEach(p->{
p.setUsername("");
if(!CollectionUtils.isEmpty(listUser)){
Optional<SysUser> op = listUser.stream().filter(o->Objects.equals(o.getUsername(),p.getUsername())).findFirst();
if(op.isPresent()){
p.setUsername(op.get().getId());
}
}
});
Set<String> existingWorkNos = lawsWorkingGroupUserService.list().stream().map(LawsWorkingGroupUser::getUserId).collect(Collectors.toSet());
return IntStream.range(0, all.size())
.mapToObj(i -> {
LawsWorkingGroupUser lawsWorkingGroupUser = all.get(i);