260225_01_ENH: 최초 구현 완료
This commit is contained in:
256
src/main/java/com/kri/lge/penetration/service/UploadService.java
Normal file
256
src/main/java/com/kri/lge/penetration/service/UploadService.java
Normal file
@@ -0,0 +1,256 @@
|
||||
package com.kri.lge.penetration.service;
|
||||
|
||||
import static com.kri.lge.penetration.common.consts.LgePenetrationConst.BATCH_SIZE;
|
||||
import static com.kri.lge.penetration.common.consts.LgePenetrationConst.EXCEL_ID;
|
||||
|
||||
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.common.web.BusinessException;
|
||||
import com.kri.lge.penetration.dto.DataItemDto;
|
||||
import com.kri.lge.penetration.dto.ExcelUploadResultDto;
|
||||
import com.kri.lge.penetration.persistence.entity.TDataEntity;
|
||||
import com.kri.lge.penetration.persistence.repository.DataRepository;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@Log4j2
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class UploadService {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final DataRepository dataRepository;
|
||||
|
||||
public ExcelUploadResultDto uploadData(MultipartFile file){
|
||||
try (InputStream in = file.getInputStream()) {
|
||||
List<RowContext> buffer = new ArrayList<>(BATCH_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(in, (row, rowIndex) -> {
|
||||
total.incrementAndGet();
|
||||
|
||||
ParsedRow parsed = parseRowToEntity(row, regUser, now);
|
||||
if (parsed.reason != null) {
|
||||
fail.incrementAndGet();
|
||||
failRows.add(buildFailRow(row, rowIndex, parsed.reason));
|
||||
return;
|
||||
}
|
||||
|
||||
buffer.add(new RowContext(rowIndex, row, parsed.entity));
|
||||
|
||||
if (buffer.size() >= BATCH_SIZE) {
|
||||
flushBuffer(buffer, success, fail, failRows);
|
||||
}
|
||||
});
|
||||
|
||||
if (!buffer.isEmpty()) {
|
||||
flushBuffer(buffer, success, fail, failRows);
|
||||
}
|
||||
|
||||
byte[] errorFile = null;
|
||||
boolean hasErrorFile = !failRows.isEmpty();
|
||||
|
||||
if (hasErrorFile) {
|
||||
errorFile = toXlsx(failRows);
|
||||
}
|
||||
|
||||
return ExcelUploadResultDto.of(total.get(), success.get(), fail.get(), errorFile);
|
||||
|
||||
} catch (IOException ex){
|
||||
throw BusinessException.badRequest("CAN_NOT_OPEN", "파일 스트림을 열 수 없습니다.")
|
||||
.with("fileName",file.getOriginalFilename())
|
||||
.with("errorMessage",ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void flushBuffer(
|
||||
List<RowContext> buffer,
|
||||
AtomicInteger success,
|
||||
AtomicInteger fail,
|
||||
List<Map<String, String>> failRows
|
||||
) {
|
||||
try {
|
||||
List<TDataEntity> entities = buffer.stream().map(rc -> rc.entity).toList();
|
||||
|
||||
dataRepository.saveAll(entities);
|
||||
success.addAndGet(buffer.size());
|
||||
} catch (DataAccessException e) {
|
||||
// 배치가 한 번에 터지면, 개별 저장으로 폴백해서 "어느 행이 문제인지" 잡는다
|
||||
for (RowContext rc : buffer) {
|
||||
try {
|
||||
dataRepository.saveAndFlush(rc.entity);
|
||||
success.incrementAndGet();
|
||||
} catch (DataAccessException ex) {
|
||||
fail.incrementAndGet();
|
||||
failRows.add(buildFailRow(rc.rawRow, rc.rowIndex, "DB 저장 실패: " + rootMessage(ex)));
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
em.clear();
|
||||
buffer.clear();
|
||||
}
|
||||
}
|
||||
|
||||
private ParsedRow parseRowToEntity(
|
||||
Map<String, String> row,
|
||||
String regUser,
|
||||
LocalDateTime regDt
|
||||
) {
|
||||
if (row == null || row.isEmpty()) {
|
||||
return ParsedRow.fail("빈 행");
|
||||
}
|
||||
|
||||
// sid
|
||||
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);
|
||||
}
|
||||
|
||||
// rawData 구성
|
||||
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;
|
||||
//if (EXCEL_YEAR.equals(key)) continue;
|
||||
//if (EXCEL_PRODUCT.equals(key)) continue;
|
||||
|
||||
String v = e.getValue();
|
||||
if (v == null || v.isBlank()) {
|
||||
continue; // 빈 값은 rawData에 안 넣음
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
TDataEntity entity = new TDataEntity();
|
||||
entity.setSid(sid);
|
||||
entity.setRawData(rawData);
|
||||
entity.setRegUser(regUser);
|
||||
entity.setRegDt(regDt);
|
||||
|
||||
return ParsedRow.ok(entity);
|
||||
}
|
||||
|
||||
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 void doDebugLog(TDataEntity e){
|
||||
try {
|
||||
String json = objectMapper
|
||||
.writerWithDefaultPrettyPrinter()
|
||||
.writeValueAsString(e.getRawData());
|
||||
log.info("rawDataJson={}", json);
|
||||
} catch (JsonProcessingException ex) {
|
||||
log.warn("rawData json serialize failed", ex);
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
TDataEntity entity
|
||||
) {}
|
||||
|
||||
private record ParsedRow(
|
||||
TDataEntity entity,
|
||||
String reason
|
||||
) {
|
||||
static ParsedRow ok(TDataEntity entity) {
|
||||
return new ParsedRow(entity, null);
|
||||
}
|
||||
|
||||
static ParsedRow fail(String reason) {
|
||||
return new ParsedRow(null, reason);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user