Files
lg-cxdash-back/src/main/java/com/kri/lge/penetration/service/UploadService.java

244 lines
7.0 KiB
Java

package com.kri.lge.penetration.service;
import static com.kri.lge.penetration.common.consts.LgePenetrationConst.EXCEL_ID;
import static com.kri.lge.penetration.common.consts.LgePenetrationConst.INSERT_CHUNK_SIZE;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kri.common.datetime.DateTimeUtil;
import com.kri.common.excel.ExcelMapper;
import com.kri.lge.penetration.dto.DataItemDto;
import com.kri.lge.penetration.dto.ExcelUploadResultDto;
import com.kri.lge.penetration.persistence.jdbc.DataJdbcRepository;
import com.kri.lge.penetration.persistence.jdbc.DataJdbcRepository.TDataBatchInsertResult;
import com.kri.lge.penetration.persistence.jdbc.DataJdbcRepository.TDataInsertFailure;
import com.kri.lge.penetration.persistence.jdbc.DataJdbcRepository.TDataInsertRow;
import com.kri.lge.penetration.service.file.DataFileService;
import java.io.ByteArrayOutputStream;
import java.nio.file.Path;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
@Log4j2
@Service
@RequiredArgsConstructor
public class UploadService {
private final DataFileService dataFileService;
private final ObjectMapper objectMapper;
private final DataJdbcRepository tDataJdbcRepository;
@Transactional
public ExcelUploadResultDto uploadData(MultipartFile file) {
Path dataFilePath = dataFileService.saveListFile(file);
List<RowContext> buffer = new ArrayList<>(INSERT_CHUNK_SIZE);
List<Map<String, String>> failRows = new ArrayList<>();
AtomicInteger total = new AtomicInteger();
AtomicInteger success = new AtomicInteger();
AtomicInteger fail = new AtomicInteger();
final LocalDateTime now = DateTimeUtil.nowKstLocalDateTime();
final String regUser = "system";
ExcelMapper.readStreaming(dataFilePath, (row, rowIndex) -> {
total.incrementAndGet();
ParsedRow parsed = parseRow(row, regUser, now);
if (parsed.reason != null) {
fail.incrementAndGet();
failRows.add(buildFailRow(row, rowIndex, parsed.reason));
return;
}
buffer.add(new RowContext(rowIndex, row, parsed.insertRow));
if (buffer.size() >= INSERT_CHUNK_SIZE) {
flushBuffer(buffer, success, fail, failRows);
}
});
if (!buffer.isEmpty()) {
flushBuffer(buffer, success, fail, failRows);
}
byte[] errorFile = null;
if (!failRows.isEmpty()) {
errorFile = toXlsx(failRows);
}
return ExcelUploadResultDto.of(total.get(), success.get(), fail.get(), errorFile);
}
private void flushBuffer(
List<RowContext> buffer,
AtomicInteger success,
AtomicInteger fail,
List<Map<String, String>> failRows
) {
if (buffer.isEmpty()) {
return;
}
try {
List<TDataInsertRow> rows = buffer.stream()
.map(RowContext::insertRow)
.toList();
TDataBatchInsertResult result = tDataJdbcRepository.bulkInsert(rows);
success.addAndGet(result.successCount());
for (TDataInsertFailure failure : result.failures()) {
RowContext rc = buffer.get(failure.index());
fail.incrementAndGet();
failRows.add(buildFailRow(rc.rawRow(), rc.rowIndex(), "DB 저장 실패: " + failure.reason()));
}
} finally {
buffer.clear();
}
}
private ParsedRow parseRow(
Map<String, String> row,
String regUser,
LocalDateTime regDt
) {
if (row == null || row.isEmpty()) {
return ParsedRow.fail("빈 행");
}
String sid = firstNonBlank(row.get(EXCEL_ID), row.get("sid"), row.get("SID"));
if (sid == null) {
return ParsedRow.fail("ID(sid) 누락");
}
if (sid.length() > 32) {
return ParsedRow.fail("ID(sid) 길이 초과(>32): " + sid);
}
Map<String, List<DataItemDto>> rawData = new LinkedHashMap<>();
for (Map.Entry<String, String> e : row.entrySet()) {
String key = e.getKey();
if (key == null) continue;
if (EXCEL_ID.equals(key)) continue;
String v = e.getValue();
if (v == null || v.isBlank()) {
continue;
}
Double s = parseDoubleLoose(v);
if (s == null) {
return ParsedRow.fail("숫자 파싱 실패: key=" + key + ", value=" + safe(v));
}
if (key.contains("#")) {
key = key.substring(0, key.indexOf("#"));
}
rawData.computeIfAbsent(key, k -> new ArrayList<>())
.add(DataItemDto.from(null, null, s));
}
String rawDataJson;
try {
rawDataJson = objectMapper.writeValueAsString(rawData);
} catch (JsonProcessingException e) {
return ParsedRow.fail("rawData JSON 직렬화 실패: " + rootMessage(e));
}
TDataInsertRow insertRow = new TDataInsertRow(
UUID.randomUUID().toString(),
sid,
rawDataJson,
regUser,
regDt
);
return ParsedRow.ok(insertRow);
}
private Map<String, String> buildFailRow(Map<String, String> row, int rowIndex, String reason) {
Map<String, String> m = new LinkedHashMap<>();
m.put("rowIndex", String.valueOf(rowIndex));
m.put("reason", reason);
if (row != null) {
m.putAll(row);
}
return m;
}
private byte[] toXlsx(List<Map<String, String>> failRows) {
Workbook wb = ExcelMapper.writeFromMapList(failRows);
try (wb; ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
wb.write(bos);
return bos.toByteArray();
} catch (Exception e) {
throw new RuntimeException("에러 엑셀(byte[]) 생성 실패", e);
}
}
private static String rootMessage(Throwable t) {
Throwable cur = t;
while (cur.getCause() != null) {
cur = cur.getCause();
}
return cur.getMessage();
}
private static Double parseDoubleLoose(String s) {
if (s == null) return null;
String t = s.trim();
if (t.isEmpty()) return null;
t = t.replace(",", "");
try {
return Double.parseDouble(t);
} catch (NumberFormatException e) {
return null;
}
}
private static String firstNonBlank(String... values) {
if (values == null) return null;
for (String v : values) {
if (v != null && !v.isBlank()) return v.trim();
}
return null;
}
private static String safe(String s) {
return s == null ? "null" : s;
}
private record RowContext(
int rowIndex,
Map<String, String> rawRow,
TDataInsertRow insertRow
) {}
private record ParsedRow(
TDataInsertRow insertRow,
String reason
) {
static ParsedRow ok(TDataInsertRow insertRow) {
return new ParsedRow(insertRow, null);
}
static ParsedRow fail(String reason) {
return new ParsedRow(null, reason);
}
}
}