Commit 4162f3a5 by 林冲

feat:校验导入合同的字段表头

1 parent bad237e0
...@@ -19,13 +19,18 @@ import com.gsc4cloud.gsc.vpc.api.req.pad.contract.*; ...@@ -19,13 +19,18 @@ import com.gsc4cloud.gsc.vpc.api.req.pad.contract.*;
import com.gsc4cloud.gsc.vpc.api.req.pad.evalorder.EvalContractQueryRequest; import com.gsc4cloud.gsc.vpc.api.req.pad.evalorder.EvalContractQueryRequest;
import com.gsc4cloud.gsc.vpc.api.req.web.evalorder.EvalContractExportRequest; import com.gsc4cloud.gsc.vpc.api.req.web.evalorder.EvalContractExportRequest;
import com.gsc4cloud.gsc.vpc.api.req.web.evalorder.EvalContractImportRequest; import com.gsc4cloud.gsc.vpc.api.req.web.evalorder.EvalContractImportRequest;
import com.gsc4cloud.gsc.vpc.api.exception.BusinessException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
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.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.core.task.TaskExecutor; import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
...@@ -35,7 +40,6 @@ import java.util.ArrayList; ...@@ -35,7 +40,6 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -222,9 +226,20 @@ public class EvalElectronicContractService { ...@@ -222,9 +226,20 @@ public class EvalElectronicContractService {
if (responseCode != HttpURLConnection.HTTP_OK) { if (responseCode != HttpURLConnection.HTTP_OK) {
throw new RuntimeException("获取文件失败,HTTP状态码: " + responseCode); throw new RuntimeException("获取文件失败,HTTP状态码: " + responseCode);
} }
InputStream inputStream = connection.getInputStream(); InputStream inputStream = connection.getInputStream();
// 校验表头字段
validateExcelHeader(inputStream);
// 重新获取输入流(因为上面的校验已经读取过了)
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(30000);
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36");
inputStream = connection.getInputStream();
// 使用EasyExcel读取输入流 // 使用EasyExcel读取输入流
EasyExcel.read(inputStream, ContractImportRow.class, listener) EasyExcel.read(inputStream, ContractImportRow.class, listener)
.sheet(0) .sheet(0)
...@@ -245,6 +260,49 @@ public class EvalElectronicContractService { ...@@ -245,6 +260,49 @@ public class EvalElectronicContractService {
return list; return list;
} }
/**
* 校验Excel表头字段是否符合模板
*/
private void validateExcelHeader(InputStream inputStream) throws IOException {
// 定义期望的表头字段(第8行)
String[] expectedHeaders = {
"*商户号", "*车牌号", "*车架号", "*品牌", "*车辆类别",
"*支付方式", "*定金金额", "*尾款金额", "*收款人",
"*开户行", "*收款银行账号"
};
try (Workbook workbook = WorkbookFactory.create(inputStream)) {
Sheet sheet = workbook.getSheetAt(0);
// 第8行,索引为7
Row headerRow = sheet.getRow(7);
if (headerRow == null) {
throw new BusinessException("Excel文件格式错误:缺少表头行(第8行)");
}
// 校验每个表头字段
for (int i = 0; i < expectedHeaders.length; i++) {
Cell cell = headerRow.getCell(i);
String actualHeader = (cell == null) ? "" : cell.getStringCellValue().trim();
String expectedHeader = expectedHeaders[i];
if (!expectedHeader.equals(actualHeader)) {
throw new BusinessException(
String.format("Excel表头字段不符合模板要求:第%d列期望为\"%s\",实际为\"%s\"",
i + 1, expectedHeader, actualHeader)
);
}
}
log.info("Excel表头校验通过");
} catch (BusinessException e) {
throw e;
} catch (Exception e) {
log.error("校验Excel表头失败", e);
throw new BusinessException("校验Excel表头失败: " + e.getMessage());
}
}
private EvalElectronicContractImportDTO convertToImportDTO(ContractImportRow row, private EvalElectronicContractImportDTO convertToImportDTO(ContractImportRow row,
LoginAccountResponse accountInfo) { LoginAccountResponse accountInfo) {
EvalElectronicContractImportDTO dto = new EvalElectronicContractImportDTO(); EvalElectronicContractImportDTO dto = new EvalElectronicContractImportDTO();
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!