feat(REQ-1980): excel导入表头问题修复
This commit is contained in:
parent
65797fa2bf
commit
e85bacd350
@ -64,7 +64,7 @@ public interface BlackAndWhiteListApi {
|
||||
/**
|
||||
* 通过excel导入黑白名单
|
||||
*
|
||||
* @param req excel文件 {@link BlackAndWhiteListExcelImportReq}
|
||||
*
|
||||
* @return 导入结果
|
||||
*/
|
||||
@PostMapping("api/black-white-list/import")
|
||||
|
||||
@ -20,9 +20,9 @@ import java.util.Map;
|
||||
@AllArgsConstructor
|
||||
public class BlackAndWhiteListExcelImportResp {
|
||||
/**
|
||||
* 导入结果,0表示全部导入成功,1表示部分导入成功,2表示全部导入失败
|
||||
* 导入结果,true表示导入成功,false表示导入失败
|
||||
*/
|
||||
private Integer result;
|
||||
private Boolean result;
|
||||
|
||||
/**
|
||||
* 只有result导入失败的行号和错误信息,key为行号,value为错误信息
|
||||
|
||||
@ -14,6 +14,8 @@ import com.alibaba.excel.EasyExcel;
|
||||
import com.alibaba.excel.context.AnalysisContext;
|
||||
import com.alibaba.excel.event.AnalysisEventListener;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
import org.json.JSONObject;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
@ -33,6 +35,7 @@ import java.util.Map;
|
||||
* @modifiedBy:
|
||||
* @version: 1.0`
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
public class BlackAndWhiteListController implements BlackAndWhiteListApi {
|
||||
@ -61,7 +64,8 @@ public class BlackAndWhiteListController implements BlackAndWhiteListApi {
|
||||
|
||||
@Override
|
||||
public ApiResult<BlackAndWhiteListExcelImportResp> importBlackAndWhiteListExcel(ListTypeEnum type, String module, String url, MultipartFile file) {
|
||||
return blackAndWhiteListService.importBlackAndWhiteListExcel(type, module, url, file);
|
||||
log.info("导入Excel数据参数:type={}, module={}, url={}, file={}", type, module, url, file);
|
||||
return ApiResult.ok(blackAndWhiteListService.importBlackAndWhiteListExcel(type, module, url, file));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -2,7 +2,6 @@ package cn.axzo.nanopart.server.service;
|
||||
|
||||
import cn.axzo.framework.domain.web.result.ApiResult;
|
||||
import cn.axzo.nanopart.api.constant.enums.ListTypeEnum;
|
||||
import cn.axzo.nanopart.api.request.BlackAndWhiteListExcelImportReq;
|
||||
import cn.axzo.nanopart.api.request.BlackAndWhiteListReq;
|
||||
import cn.axzo.nanopart.api.response.BlackAndWhiteListExcelImportResp;
|
||||
import cn.axzo.nanopart.api.response.BlackAndWhiteListResp;
|
||||
@ -28,6 +27,6 @@ public interface BlackAndWhiteListService {
|
||||
|
||||
ApiResult<Boolean> isInBlackOrWhiteList(BlackAndWhiteListReq req);
|
||||
|
||||
ApiResult<BlackAndWhiteListExcelImportResp> importBlackAndWhiteListExcel(ListTypeEnum type, String module, String url, MultipartFile file);
|
||||
BlackAndWhiteListExcelImportResp importBlackAndWhiteListExcel(ListTypeEnum type, String module, String url, MultipartFile file);
|
||||
|
||||
}
|
||||
|
||||
@ -4,7 +4,6 @@ import cn.axzo.basics.common.BeanMapper;
|
||||
import cn.axzo.basics.common.exception.ServiceException;
|
||||
import cn.axzo.framework.domain.web.result.ApiResult;
|
||||
import cn.axzo.nanopart.api.constant.enums.ListTypeEnum;
|
||||
import cn.axzo.nanopart.api.request.BlackAndWhiteListExcelImportReq;
|
||||
import cn.axzo.nanopart.api.request.BlackAndWhiteListReq;
|
||||
import cn.axzo.nanopart.api.response.BlackAndWhiteListExcelImportResp;
|
||||
import cn.axzo.nanopart.api.response.BlackAndWhiteListResp;
|
||||
@ -46,6 +45,7 @@ public class BlackAndWhiteListServiceImpl implements BlackAndWhiteListService {
|
||||
|
||||
/**
|
||||
* 新增一条黑白名单记录
|
||||
*
|
||||
* @param req 包含type,module和param三个字段
|
||||
* @return 记录id
|
||||
*/
|
||||
@ -53,12 +53,12 @@ public class BlackAndWhiteListServiceImpl implements BlackAndWhiteListService {
|
||||
public ApiResult<Long> create(BlackAndWhiteListReq req) {
|
||||
// 黑白名单创建互斥,若存在于黑名单中则不允许存在于白名单中,反之亦然
|
||||
BlackAndWhiteListReq reverseReq = BeanMapper.copyBean(req, BlackAndWhiteListReq.class, (req1, req2) -> req2.setType((req1.getType().equals(ListTypeEnum.BLACK_LIST)) ? ListTypeEnum.WHITE_LIST : ListTypeEnum.BLACK_LIST));
|
||||
if (isInBlackOrWhiteList(reverseReq).getData()){
|
||||
throw new ServiceException("该记录已存在于"+reverseReq.getType().getDescription()+"中,不能创建对应"+req.getType().getDescription());
|
||||
if (isInBlackOrWhiteList(reverseReq).getData()) {
|
||||
throw new ServiceException("该记录已存在于" + reverseReq.getType().getDescription() + "中,不能创建对应" + req.getType().getDescription());
|
||||
}
|
||||
// 同类型名单内唯一性校验
|
||||
if (isInBlackOrWhiteList(req).getData()){
|
||||
throw new ServiceException("该记录已存在于"+req.getType().getDescription()+"中");
|
||||
if (isInBlackOrWhiteList(req).getData()) {
|
||||
throw new ServiceException("该记录已存在于" + req.getType().getDescription() + "中");
|
||||
}
|
||||
return ApiResult.ok(blackAndWhiteListRepository.create(req));
|
||||
}
|
||||
@ -76,6 +76,7 @@ public class BlackAndWhiteListServiceImpl implements BlackAndWhiteListService {
|
||||
|
||||
/**
|
||||
* 查询指定条件的所有黑白名单记录
|
||||
*
|
||||
* @param req 包含type,module和param三个字段
|
||||
* @return 记录列表
|
||||
*/
|
||||
@ -88,6 +89,7 @@ public class BlackAndWhiteListServiceImpl implements BlackAndWhiteListService {
|
||||
|
||||
/**
|
||||
* 判断是否存在指定条件的黑白名单记录
|
||||
*
|
||||
* @param req 包含type,module和param三个字段
|
||||
* @return 记录存在返回true,否则返回false
|
||||
*/
|
||||
@ -97,13 +99,21 @@ public class BlackAndWhiteListServiceImpl implements BlackAndWhiteListService {
|
||||
return ApiResult.ok(CollectionUtil.isNotEmpty(blackWhiteLists));
|
||||
}
|
||||
|
||||
/**
|
||||
* 导入黑白名单Excel文件
|
||||
*
|
||||
* @param type 名单类型
|
||||
* @param module 模块
|
||||
* @param url 文件url,如果为空则从文件流中读取
|
||||
* @param file 文件流
|
||||
*/
|
||||
@Override
|
||||
public ApiResult<BlackAndWhiteListExcelImportResp> importBlackAndWhiteListExcel(ListTypeEnum type, String module, String url, MultipartFile file) {
|
||||
public BlackAndWhiteListExcelImportResp importBlackAndWhiteListExcel(ListTypeEnum type, String module, String url, MultipartFile file) {
|
||||
InputStream inputStream = null;
|
||||
BlackAndWhiteListExcelImportResp importResp = new BlackAndWhiteListExcelImportResp();
|
||||
// 获取文件
|
||||
try {
|
||||
inputStream = file.getInputStream();
|
||||
inputStream = file.getInputStream();
|
||||
if (StringUtils.hasText(url)) {
|
||||
inputStream = new URL(url).openStream();
|
||||
}
|
||||
@ -113,8 +123,8 @@ public class BlackAndWhiteListServiceImpl implements BlackAndWhiteListService {
|
||||
// 使用EasyExcel解析文件
|
||||
List<Map<String, Object>> excelData = readExcel(inputStream, importResp);
|
||||
// 将数据写入数据库
|
||||
writeDataToDatabase(excelData, module, type.getValue());
|
||||
return null;
|
||||
importResp.setResult(writeDataToDatabase(excelData, module, type.getValue()));
|
||||
return importResp;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -130,10 +140,15 @@ public class BlackAndWhiteListServiceImpl implements BlackAndWhiteListService {
|
||||
|
||||
// 使用EasyExcel读取数据
|
||||
EasyExcel.read(inputStream, new AnalysisEventListener<Map<Integer, String>>() {
|
||||
private boolean firstRow = true;
|
||||
|
||||
// 所有列名
|
||||
private List<String> headerList = new ArrayList<>();
|
||||
// 表头
|
||||
private Map<Integer, String> header;
|
||||
|
||||
@Override
|
||||
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
|
||||
log.info("表头信息:{}", headMap);
|
||||
header = headMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取每一行数据
|
||||
@ -142,17 +157,19 @@ public class BlackAndWhiteListServiceImpl implements BlackAndWhiteListService {
|
||||
*/
|
||||
@Override
|
||||
public void invoke(Map<Integer, String> rowData, AnalysisContext context) {
|
||||
if (firstRow) {
|
||||
headerList.addAll(rowData.values());
|
||||
firstRow = false;
|
||||
} else {
|
||||
try {
|
||||
log.info("第{}行数据:{}", context.readRowHolder().getRowIndex(), rowData);
|
||||
Map<String, Object> dataMap = new HashMap<>();
|
||||
for (Map.Entry<Integer, String> entry : rowData.entrySet()) {
|
||||
int columnIndex = entry.getKey();
|
||||
String columnName = headerList.get(columnIndex);
|
||||
String columnName = header.get(columnIndex);
|
||||
dataMap.put(columnName, entry.getValue());
|
||||
}
|
||||
dataList.add(dataMap);
|
||||
} catch (Exception e) {
|
||||
HashMap<Long, String> errorMap = new HashMap<>();
|
||||
errorMap.put(Long.valueOf(context.readRowHolder().getRowIndex()), e.getMessage());
|
||||
importResp.setErrorMap(errorMap);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user