143 lines
5.6 KiB
Java
143 lines
5.6 KiB
Java
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.consts.KartConstant;
|
|
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.keyword() != null && !search.keyword().isEmpty();
|
|
if (search.projectType() == null
|
|
&& search.intranetCode() == null
|
|
&& search.userTeam() == null
|
|
&& !hasKeyword) {
|
|
return list(ctx);
|
|
}
|
|
if (search.projectType() == null
|
|
|| search.intranetCode() == null
|
|
|| search.userTeam() == null) {
|
|
throw BusinessException.badRequest("연동 조회에는 projectType/intranetCode/userTeam을 함께 입력해 주세요.");
|
|
}
|
|
String intranetCode = search.intranetCode().split("_", -1)[0];
|
|
String keyword = search.keyword() == null ? "" : search.keyword();
|
|
var rows =
|
|
projectRepository.linkedList(
|
|
search.projectType(),
|
|
intranetCode,
|
|
search.userTeam(),
|
|
keyword,
|
|
ctx.getUserRole(),
|
|
ctx.getUserTeam(),
|
|
assignedPkeys(ctx));
|
|
for (int index = 0; index < rows.size(); index++) {
|
|
rows.get(index).setProjectType(search.projectType());
|
|
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(KartConstant.REPORT_MEMBER_ROLE).equals(ctx.getUserRole())
|
|
&& StringUtil.normalizeBlank(ctx.getUserId()) == null)) {
|
|
throw BusinessException.unauthorized("PROJECT_IDENTITY_REQUIRED", "프로젝트 조회 인증 정보가 부족합니다.");
|
|
}
|
|
}
|
|
|
|
private List<String> assignedPkeys(RequestUserContext ctx) {
|
|
return Integer.valueOf(KartConstant.REPORT_MEMBER_ROLE).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 "";
|
|
}
|
|
}
|
|
}
|