feat : querydsl 멀티디비 설정 추가.
parent
38a67b7763
commit
de555ea965
@ -0,0 +1,161 @@
|
||||
package com.worker.scheduler.epost.service;
|
||||
|
||||
import com.worker.domain.entity.CpSetinfo;
|
||||
import com.worker.domain.entity.CpSetinfoId;
|
||||
import com.worker.domain.repo.cp.CpBdongRepository;
|
||||
import com.worker.domain.repo.cp.CpSetinfoRepository;
|
||||
import com.worker.domain.repo.ep.EpBdongRepository;
|
||||
import com.worker.domain.repo.ep.EpSetinfoRepository;
|
||||
import com.worker.scheduler.epost.dto.EPostDto;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class EpostSetinfoService {
|
||||
|
||||
private final Environment env;
|
||||
|
||||
private final CpSetinfoRepository cpSetinfoRepository;
|
||||
private final EpSetinfoRepository epSetinfoRepository;
|
||||
|
||||
// DataSources (가용성 체크용)
|
||||
@Qualifier("cpDataSource")
|
||||
private final DataSource cpDataSource;
|
||||
@Qualifier("epDataSource")
|
||||
private final DataSource epDataSource;
|
||||
|
||||
public EPostDto.SetInfo findSetInfo() {
|
||||
|
||||
CpSetinfoId cpInfoId = CpSetinfoId.builder()
|
||||
.codeName(env.getProperty("e-post.info.cp.codeName"))
|
||||
.groupCode(env.getProperty("e-post.info.cp.groupCode"))
|
||||
.detailCode(env.getProperty("e-post.info.cp.detailCode"))
|
||||
.build();
|
||||
|
||||
CpSetinfoId epInfoId = CpSetinfoId.builder()
|
||||
.codeName(env.getProperty("e-post.info.ep.codeName"))
|
||||
.groupCode(env.getProperty("e-post.info.ep.groupCode"))
|
||||
.detailCode(env.getProperty("e-post.info.ep.detailCode"))
|
||||
.build();
|
||||
|
||||
CpSetinfoId cpDocId = CpSetinfoId.builder()
|
||||
.codeName(env.getProperty("e-post.docCode.cp.codeName"))
|
||||
.groupCode(env.getProperty("e-post.docCode.cp.groupCode"))
|
||||
.detailCode(env.getProperty("e-post.docCode.cp.detailCode"))
|
||||
.build();
|
||||
|
||||
CpSetinfoId epDocId = CpSetinfoId.builder()
|
||||
.codeName(env.getProperty("e-post.docCode.ep.codeName"))
|
||||
.groupCode(env.getProperty("e-post.docCode.ep.groupCode"))
|
||||
.detailCode(env.getProperty("e-post.docCode.ep.detailCode"))
|
||||
.build();
|
||||
|
||||
// ----- CP -----
|
||||
CpSetinfo cpEpostInfo = null;
|
||||
CpSetinfo cpEpostDocInfo = null;
|
||||
EPostDto.EPostInfo cpEPostInfo = null;
|
||||
|
||||
if (isDsAvailable(cpDataSource)) {
|
||||
try {
|
||||
cpEpostInfo = cpSetinfoRepository.findById(cpInfoId).orElse(null);
|
||||
cpEpostDocInfo = cpSetinfoRepository.findById(cpDocId).orElse(null);
|
||||
cpEPostInfo = buildEPostInfo(cpEpostInfo, cpEpostDocInfo);
|
||||
} catch (Exception e) {
|
||||
log.warn("[CP] 조회/빌드 실패. CP스킵: {}", e.getMessage());
|
||||
}
|
||||
} else {
|
||||
log.info("[CP] DB 비가용. CP 스킵.");
|
||||
}
|
||||
|
||||
// ----- EP -----
|
||||
CpSetinfo epEpostInfo = null;
|
||||
CpSetinfo epEpostDocInfo = null;
|
||||
EPostDto.EPostInfo epEPostInfo = null;
|
||||
|
||||
if (isDsAvailable(epDataSource)) {
|
||||
try {
|
||||
epEpostInfo = epSetinfoRepository.findById(epInfoId).orElse(null);
|
||||
epEpostDocInfo = epSetinfoRepository.findById(epDocId).orElse(null);
|
||||
epEPostInfo = buildEPostInfo(epEpostInfo, epEpostDocInfo);
|
||||
} catch (Exception e) {
|
||||
log.warn("[EP] 조회/빌드 실패. EP 스킵: {}", e.getMessage());
|
||||
}
|
||||
} else {
|
||||
log.info("[EP] DB 비가용. EP 조회 스킵");
|
||||
}
|
||||
|
||||
// ----- 결과 -----
|
||||
return EPostDto.SetInfo.builder()
|
||||
.cpSetinfo(cpEpostInfo) // null 허용
|
||||
.cpEPostInfo(cpEPostInfo) // null 허용
|
||||
.epSetinfo(epEpostInfo) // null 허용
|
||||
.epEPostInfo(epEPostInfo) // null 허용
|
||||
.build();
|
||||
}
|
||||
|
||||
/** DataSource 가용성 체크: 커넥션 시도만 해보고 실패하면 비가용 */
|
||||
private boolean isDsAvailable(DataSource ds) {
|
||||
if (ds == null) return false;
|
||||
try (Connection c = ds.getConnection()) {
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** null-safe EPostInfo 빌드: 입력 둘 중 하나라도 없으면 null 반환 */
|
||||
private EPostDto.EPostInfo buildEPostInfo(CpSetinfo ePostInfo, CpSetinfo ePostDocInfo) {
|
||||
if (ePostInfo == null || ePostDocInfo == null) {
|
||||
return null;
|
||||
}
|
||||
return EPostDto.EPostInfo.builder()
|
||||
.conOrg(trimOrNull(ePostDocInfo.getStrValue1()))
|
||||
.rceptId(trimOrNull(ePostDocInfo.getStrValue2()))
|
||||
.apvlNb(trimOrNull(ePostDocInfo.getStrValue3()))
|
||||
.filePath(trimOrNull(ePostDocInfo.getStrValue4()))
|
||||
.postId(trimOrNull(ePostDocInfo.getStrValue5()))
|
||||
.addPath(trimOrNull(ePostDocInfo.getStrValue6()))
|
||||
.docCodes(findDocCodes(ePostDocInfo))
|
||||
.build();
|
||||
}
|
||||
|
||||
/** 문서코드 리스트: null/공백 제거 + trim + 중복 제거 + 불변화 */
|
||||
private List<String> findDocCodes(CpSetinfo ePostDocInfo) {
|
||||
if (ePostDocInfo == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
List<String> codes = Stream.of(
|
||||
ePostDocInfo.getStrValue1(),
|
||||
ePostDocInfo.getStrValue2(),
|
||||
ePostDocInfo.getStrValue3(),
|
||||
ePostDocInfo.getStrValue4(),
|
||||
ePostDocInfo.getStrValue5()
|
||||
)
|
||||
.filter(Objects::nonNull)
|
||||
.map(String::trim)
|
||||
.filter(s -> !s.isEmpty())
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
return Collections.unmodifiableList(codes);
|
||||
}
|
||||
|
||||
private String trimOrNull(String s) {
|
||||
if (s == null) return null;
|
||||
String t = s.trim();
|
||||
return t.isEmpty() ? null : t;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue