261016_01_ENH: 1차 마이그레이션 완료
This commit is contained in:
141
src/main/java/com/kri/kart/server/service/ProjectService.java
Normal file
141
src/main/java/com/kri/kart/server/service/ProjectService.java
Normal file
@@ -0,0 +1,141 @@
|
||||
package com.kri.kart.server.service;
|
||||
|
||||
import com.kri.common.feature.util.StringUtil;
|
||||
import com.kri.common.feature.web.BusinessException;
|
||||
import com.kri.common.feature.web.RequestUserContext;
|
||||
import com.kri.kart.server.common.enums.ProjectType;
|
||||
import com.kri.kart.server.common.external.intranet.IntranetExtensionApiClient;
|
||||
import com.kri.kart.server.dto.project.ProjectLinkSearchDto;
|
||||
import com.kri.kart.server.dto.project.ProjectListDto;
|
||||
import com.kri.kart.server.dto.project.ProjectSummaryDto;
|
||||
import com.kri.kart.server.persistence.repository.ProjectRepository;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@ConditionalOnProperty(prefix = "kri.kart.database", name = "enabled", havingValue = "true")
|
||||
public class ProjectService {
|
||||
private final ProjectRepository projectRepository;
|
||||
private final IntranetExtensionApiClient intranetExtensionApiClient;
|
||||
|
||||
public List<ProjectListDto> list(ProjectLinkSearchDto search, RequestUserContext ctx) {
|
||||
requireIdentity(ctx);
|
||||
boolean hasKeyword = search.getKeyword() != null && !search.getKeyword().isEmpty();
|
||||
if (search.getProjectType() == null
|
||||
&& search.getIntranetCode() == null
|
||||
&& search.getUserTeam() == null
|
||||
&& !hasKeyword) {
|
||||
return list(ctx);
|
||||
}
|
||||
if (search.getProjectType() == null
|
||||
|| search.getIntranetCode() == null
|
||||
|| search.getUserTeam() == null) {
|
||||
throw BusinessException.badRequest("연동 조회에는 projectType/intranetCode/userTeam을 함께 입력해 주세요.");
|
||||
}
|
||||
String intranetCode = search.getIntranetCode().split("_", -1)[0];
|
||||
String keyword = search.getKeyword() == null ? "" : search.getKeyword();
|
||||
var rows =
|
||||
projectRepository.linkedList(
|
||||
search.getProjectType(),
|
||||
intranetCode,
|
||||
search.getUserTeam(),
|
||||
keyword,
|
||||
ctx.getUserRole(),
|
||||
ctx.getUserTeam(),
|
||||
assignedPkeys(ctx));
|
||||
for (int index = 0; index < rows.size(); index++) {
|
||||
rows.get(index).setProjectType(search.getProjectType());
|
||||
rows.get(index).setRowNumber(Integer.toString(index + 1));
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
|
||||
public List<ProjectListDto> list(RequestUserContext ctx) {
|
||||
requireIdentity(ctx);
|
||||
List<String> assignedPkeys = assignedPkeys(ctx);
|
||||
// Fetch once for both sources and filter in SQL before ordering/selecting candidates.
|
||||
List<String> excluded = intranetExtensionApiClient.getExcludedOwnerIds();
|
||||
List<ProjectListDto> results = new ArrayList<>();
|
||||
for (ProjectType type : ProjectType.values()) {
|
||||
List<ProjectListDto> rows =
|
||||
projectRepository.list(
|
||||
type,
|
||||
excluded,
|
||||
projectRepository.findMustViewPkeys(type),
|
||||
ctx.getUserRole(),
|
||||
ctx.getUserTeam(),
|
||||
assignedPkeys);
|
||||
for (ProjectListDto row : rows) {
|
||||
row.setProjectType(type);
|
||||
}
|
||||
results.addAll(rows);
|
||||
}
|
||||
results.sort(
|
||||
Comparator.comparing(
|
||||
ProjectListDto::getCreateDate, Comparator.nullsLast(Comparator.reverseOrder())));
|
||||
for (int i = 0; i < results.size(); i++) {
|
||||
results.get(i).setRowNumber(Integer.toString(i + 1));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
public List<ProjectSummaryDto> summary(RequestUserContext ctx) {
|
||||
requireIdentity(ctx);
|
||||
List<String> assignedPkeys = assignedPkeys(ctx);
|
||||
List<String> excluded = intranetExtensionApiClient.getExcludedOwnerIds();
|
||||
List<ProjectSummaryDto> results = new ArrayList<>();
|
||||
for (ProjectType type : ProjectType.values()) {
|
||||
List<ProjectSummaryDto> rows =
|
||||
projectRepository.summary(
|
||||
type, excluded, ctx.getUserRole(), ctx.getUserTeam(), assignedPkeys);
|
||||
for (ProjectSummaryDto row : rows) {
|
||||
row.setProjectType(type);
|
||||
}
|
||||
results.addAll(rows);
|
||||
}
|
||||
// v2: each source TOP 6 after status/date ordering, then merge by date and take 6.
|
||||
results.sort(
|
||||
Comparator.comparing(
|
||||
ProjectSummaryDto::getCreateDate, Comparator.nullsLast(Comparator.reverseOrder())));
|
||||
List<ProjectSummaryDto> selected =
|
||||
new ArrayList<>(results.subList(0, Math.min(6, results.size())));
|
||||
for (ProjectSummaryDto row : selected) {
|
||||
row.setSurveyType(label(row.getSurveyType(), new String[] {"온라인", "CAPI", "GANG"}));
|
||||
row.setStatus(label(row.getStatus(), new String[] {"준비", "진행", "완료"}));
|
||||
}
|
||||
return selected;
|
||||
}
|
||||
|
||||
private void requireIdentity(RequestUserContext ctx) {
|
||||
if (ctx.getUserRole() == null
|
||||
|| (!Integer.valueOf(1001).equals(ctx.getUserRole())
|
||||
&& StringUtil.normalizeBlank(ctx.getUserTeam()) == null)
|
||||
|| (Integer.valueOf(9001).equals(ctx.getUserRole())
|
||||
&& StringUtil.normalizeBlank(ctx.getUserId()) == null)) {
|
||||
throw BusinessException.unauthorized("PROJECT_IDENTITY_REQUIRED", "프로젝트 조회 인증 정보가 부족합니다.");
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> assignedPkeys(RequestUserContext ctx) {
|
||||
return Integer.valueOf(9001).equals(ctx.getUserRole())
|
||||
? projectRepository.findAssignedPkeys(ctx.getUserId())
|
||||
: List.of();
|
||||
}
|
||||
|
||||
private String label(String code, String[] labels) {
|
||||
if (code == null) {
|
||||
return "";
|
||||
}
|
||||
try {
|
||||
int index = Integer.parseInt(code);
|
||||
return index >= 1 && index <= labels.length ? labels[index - 1] : "";
|
||||
} catch (NumberFormatException ignored) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user