260722_01_ENH: 버그 수정 및 개선 사항 수정
This commit is contained in:
@@ -1,24 +1,36 @@
|
||||
package com.kri.contact.server.service;
|
||||
|
||||
import com.kri.common.feature.web.BusinessException;
|
||||
import com.kri.common.feature.web.RequestUserContext;
|
||||
import com.kri.common.feature.web.page.PageRequestDto;
|
||||
import com.kri.common.feature.web.page.PageResponseDto;
|
||||
import com.kri.common.feature.web.page.Pageables;
|
||||
import com.kri.contact.server.common.consts.ContactConstant;
|
||||
import com.kri.contact.server.common.external.shorturl.ShortUrlApiClient;
|
||||
import com.kri.contact.server.dto.project.ProjectCreateRequestDto;
|
||||
import com.kri.contact.server.dto.project.ProjectInfoDto;
|
||||
import com.kri.contact.server.dto.project.ProjectMainTemplateUpdateRequestDto;
|
||||
import com.kri.contact.server.dto.project.ProjectOwnerResponseDto;
|
||||
import com.kri.contact.server.dto.project.ProjectSearchRequestDto;
|
||||
import com.kri.contact.server.dto.project.ProjectSurveyDto;
|
||||
import com.kri.contact.server.dto.project.ProjectUpdateRequestDto;
|
||||
import com.kri.contact.server.persistence.entity.TPrjctEntity;
|
||||
import com.kri.contact.server.persistence.entity.TPrjctMainTmplatEntity;
|
||||
import com.kri.contact.server.persistence.entity.TPrjctSurveyEntity;
|
||||
import com.kri.contact.server.persistence.query.ProjectSpecs;
|
||||
import com.kri.contact.server.persistence.repository.ProjectMainTemplateRepository;
|
||||
import com.kri.contact.server.persistence.repository.ProjectRepository;
|
||||
import com.kri.contact.server.persistence.repository.ProjectSurveyRepository;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.data.domain.Page;
|
||||
@@ -29,19 +41,25 @@ import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class ProjectService {
|
||||
|
||||
private static final List<Short> STATUS_0_1 = List.of((short) 0, (short) 1);
|
||||
private static final List<Short> STATUS_1 = List.of((short) 1);
|
||||
private static final String PRJCT_SURVEY_IDX_TOKEN = "[$prjctSurveyIdx]";
|
||||
private static final String KON_URL_TOKEN = "[$KON-URL]";
|
||||
private static final String KIWI_URL_TOKEN = "[$KIWI-URL]";
|
||||
private static final String KON_SURVEY_BASE_URL = "https://kon.kric.com/project/";
|
||||
private static final String KIWI_SURVEY_BASE_URL = "https://kon.kiwisurvey.kr/project/";
|
||||
|
||||
private final AssignService assignService;
|
||||
private final ResultCodeService resultCodeService;
|
||||
private final ListHeaderService listHeaderService;
|
||||
private final ProjectRepository projectRepository;
|
||||
private final ProjectMainTemplateRepository projectMainTemplateRepository;
|
||||
private final ProjectSurveyRepository projectSurveyRepository;
|
||||
private final ShortUrlApiClient shortUrlApiClient;
|
||||
|
||||
@Transactional
|
||||
public ProjectInfoDto createProject(
|
||||
@@ -55,9 +73,8 @@ public class ProjectService {
|
||||
String prjctCd = this.makePrjctCd(itrnt);
|
||||
TPrjctEntity entity = TPrjctEntity.create(req, prjctCd, regUser, regDt);
|
||||
|
||||
ProjectInfoDto savedProject = ProjectInfoDto.from(projectRepository.save(entity));
|
||||
ProjectInfoDto savedProject = withSurveys(projectRepository.save(entity), List.of());
|
||||
resultCodeService.copyResultCodeFromDefault(savedProject.getPrjctIdx(), regUser, regDt);
|
||||
|
||||
listHeaderService.copyDefaultHeader(savedProject.getPrjctIdx(), regUser, regDt);
|
||||
|
||||
return savedProject;
|
||||
@@ -69,15 +86,21 @@ public class ProjectService {
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public ProjectInfoDto getProject(Long prjctIdx) {
|
||||
return ProjectInfoDto.from(projectRepository.findByPrjctIdxAndDelFalse(prjctIdx));
|
||||
return withSurveys(projectRepository.findByPrjctIdxAndDelFalse(prjctIdx));
|
||||
}
|
||||
|
||||
public List<ProjectInfoDto> getTopProject(int topCount, String keyword, String userId,
|
||||
String userCmpn, Integer userRole) {
|
||||
Specification<TPrjctEntity> spec =
|
||||
ProjectSpecs.keywordLike(keyword)
|
||||
.and(ProjectSpecs.isDelFalse());
|
||||
@Transactional(readOnly = true)
|
||||
public List<ProjectInfoDto> getTopProject(
|
||||
int topCount,
|
||||
String keyword,
|
||||
String userId,
|
||||
String userCmpn,
|
||||
Integer userRole
|
||||
) {
|
||||
Specification<TPrjctEntity> spec = ProjectSpecs.keywordLike(keyword)
|
||||
.and(ProjectSpecs.isDelFalse());
|
||||
|
||||
if (userRole >= ContactConstant.OUTSOURCE_USER_ROLE) {
|
||||
if (userRole >= ContactConstant.FIELDWORK_USER_ROLE) {
|
||||
@@ -91,22 +114,21 @@ public class ProjectService {
|
||||
}
|
||||
|
||||
Pageable pageable = PageRequest.of(0, topCount, Sort.by(Sort.Direction.DESC, "regDt"));
|
||||
|
||||
return projectRepository.findAll(spec, pageable)
|
||||
.getContent()
|
||||
.stream()
|
||||
.map(ProjectInfoDto::from) // <-- 너네 매핑 방식에 맞게 수정
|
||||
.toList();
|
||||
return withSurveys(projectRepository.findAll(spec, pageable).getContent());
|
||||
}
|
||||
|
||||
public PageResponseDto<ProjectInfoDto> searchProject(ProjectSearchRequestDto req, String userId,
|
||||
String userCmpn, Integer userRole) {
|
||||
Specification<TPrjctEntity> spec =
|
||||
ProjectSpecs.keywordLike(req.getPrjctText())
|
||||
.and(ProjectSpecs.isDelFalse())
|
||||
.and(ProjectSpecs.statusIn(req.getPrjctStatusList()))
|
||||
.and(ProjectSpecs.ownerDeptIn(req.getOwnerDeptList()))
|
||||
.and(ProjectSpecs.ownerFwIn(req.getOwnerFwList()));
|
||||
@Transactional(readOnly = true)
|
||||
public PageResponseDto<ProjectInfoDto> searchProject(
|
||||
ProjectSearchRequestDto req,
|
||||
String userId,
|
||||
String userCmpn,
|
||||
Integer userRole
|
||||
) {
|
||||
Specification<TPrjctEntity> spec = ProjectSpecs.keywordLike(req.getPrjctText())
|
||||
.and(ProjectSpecs.isDelFalse())
|
||||
.and(ProjectSpecs.statusIn(req.getPrjctStatusList()))
|
||||
.and(ProjectSpecs.ownerDeptIn(req.getOwnerDeptList()))
|
||||
.and(ProjectSpecs.ownerFwIn(req.getOwnerFwList()));
|
||||
|
||||
if (userRole >= ContactConstant.OUTSOURCE_USER_ROLE) {
|
||||
if (userRole >= ContactConstant.FIELDWORK_USER_ROLE) {
|
||||
@@ -120,38 +142,39 @@ public class ProjectService {
|
||||
}
|
||||
|
||||
PageRequestDto pageReq = req.getPageRequest();
|
||||
Pageable pageable = Pageables.of(pageReq, TPrjctEntity.class);
|
||||
Pageable pageable = Pageables.of(pageReq, TPrjctEntity.class, Map.of(
|
||||
"prjctName", "prjctNm"
|
||||
));
|
||||
|
||||
Page<ProjectInfoDto> page = projectRepository.findAll(spec, pageable)
|
||||
.map(ProjectInfoDto::from);
|
||||
Page<TPrjctEntity> page = projectRepository.findAll(spec, pageable);
|
||||
Map<Long, List<ProjectSurveyDto>> surveyMap = findSurveyMap(
|
||||
page.getContent().stream()
|
||||
.map(TPrjctEntity::getPrjctIdx)
|
||||
.toList()
|
||||
);
|
||||
|
||||
return PageResponseDto.from(page);
|
||||
return PageResponseDto.from(page.map(entity -> withSurveys(
|
||||
entity,
|
||||
surveyMap.getOrDefault(entity.getPrjctIdx(), List.of())
|
||||
)));
|
||||
}
|
||||
|
||||
public ProjectOwnerResponseDto getOwnerList(Integer userRole) {
|
||||
ProjectOwnerResponseDto responseDto = new ProjectOwnerResponseDto();
|
||||
|
||||
responseDto.setOwnerDeptList(this.getOwnerDeptList(userRole));
|
||||
responseDto.setOwnerFwList(this.getOwnerFwList(userRole));
|
||||
|
||||
return responseDto;
|
||||
}
|
||||
|
||||
public List<String> getOwnerDeptList(Integer userRole) {
|
||||
final List<Short> statusList =
|
||||
(userRole < ContactConstant.OUTSOURCE_USER_ROLE) ? null :
|
||||
(userRole < ContactConstant.FIELDWORK_USER_ROLE) ? STATUS_0_1 :
|
||||
STATUS_1;
|
||||
|
||||
final List<Short> statusList = userRole < ContactConstant.OUTSOURCE_USER_ROLE ? null
|
||||
: userRole < ContactConstant.FIELDWORK_USER_ROLE ? STATUS_0_1 : STATUS_1;
|
||||
return projectRepository.findDistinctOwnerDept(statusList);
|
||||
}
|
||||
|
||||
public List<String> getOwnerFwList(Integer userRole) {
|
||||
final List<Short> statusList =
|
||||
(userRole < ContactConstant.OUTSOURCE_USER_ROLE) ? null :
|
||||
(userRole < ContactConstant.FIELDWORK_USER_ROLE) ? STATUS_0_1 :
|
||||
STATUS_1;
|
||||
|
||||
final List<Short> statusList = userRole < ContactConstant.OUTSOURCE_USER_ROLE ? null
|
||||
: userRole < ContactConstant.FIELDWORK_USER_ROLE ? STATUS_0_1 : STATUS_1;
|
||||
return projectRepository.findDistinctOwnerFw(statusList);
|
||||
}
|
||||
|
||||
@@ -162,16 +185,19 @@ public class ProjectService {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void updateProject(long prjctIdx, ProjectUpdateRequestDto req, String modUser,
|
||||
LocalDateTime modDt) {
|
||||
// dirty checking 업데이트
|
||||
public void updateProject(
|
||||
long prjctIdx,
|
||||
ProjectUpdateRequestDto req,
|
||||
String modUser,
|
||||
LocalDateTime modDt,
|
||||
RequestUserContext ctx
|
||||
) {
|
||||
TPrjctEntity entity = projectRepository.findById(prjctIdx)
|
||||
.orElseThrow(() ->
|
||||
BusinessException.notFound("PROJECT_NOT_FOUND", "프로젝트를 찾을 수 없습니다.")
|
||||
.with("prjctIdx", prjctIdx)
|
||||
);
|
||||
.orElseThrow(() -> BusinessException.notFound("PROJECT_NOT_FOUND", "프로젝트를 찾을 수 없습니다.")
|
||||
.with("prjctIdx", prjctIdx));
|
||||
|
||||
entity.updateFrom(req.getProjectInfo(), modUser, modDt);
|
||||
updateProjectSurveys(prjctIdx, req.getProjectInfo().getSurveys(), modUser, modDt, ctx);
|
||||
|
||||
assignService.upsertPrjctCmpnAssign(prjctIdx, req.getAssignCmpnIdList(), modUser, modDt);
|
||||
resultCodeService.updateResultCode(prjctIdx, req.getResultCodeList(), modUser, modDt);
|
||||
@@ -201,7 +227,184 @@ public class ProjectService {
|
||||
}
|
||||
}
|
||||
|
||||
/***************************private************************************/
|
||||
private ProjectInfoDto withSurveys(TPrjctEntity entity) {
|
||||
if (entity == null) {
|
||||
return null;
|
||||
}
|
||||
List<ProjectSurveyDto> surveys = projectSurveyRepository
|
||||
.findByPrjctIdxOrderBySurveyOrderAscPrjctSurveyIdxAsc(entity.getPrjctIdx())
|
||||
.stream()
|
||||
.map(ProjectSurveyDto::from)
|
||||
.toList();
|
||||
return withSurveys(entity, surveys);
|
||||
}
|
||||
|
||||
private ProjectInfoDto withSurveys(TPrjctEntity entity, List<ProjectSurveyDto> surveys) {
|
||||
ProjectInfoDto dto = ProjectInfoDto.from(entity);
|
||||
if (dto != null) {
|
||||
dto.setSurveys(surveys == null ? new ArrayList<>() : surveys);
|
||||
}
|
||||
return dto;
|
||||
}
|
||||
|
||||
private List<ProjectInfoDto> withSurveys(List<TPrjctEntity> entities) {
|
||||
Map<Long, List<ProjectSurveyDto>> surveyMap = findSurveyMap(
|
||||
entities.stream()
|
||||
.map(TPrjctEntity::getPrjctIdx)
|
||||
.toList()
|
||||
);
|
||||
return entities.stream()
|
||||
.map(entity -> withSurveys(entity, surveyMap.getOrDefault(entity.getPrjctIdx(), List.of())))
|
||||
.toList();
|
||||
}
|
||||
|
||||
private Map<Long, List<ProjectSurveyDto>> findSurveyMap(Collection<Long> prjctIdxs) {
|
||||
if (prjctIdxs == null || prjctIdxs.isEmpty()) {
|
||||
return Map.of();
|
||||
}
|
||||
return projectSurveyRepository
|
||||
.findByPrjctIdxInOrderByPrjctIdxAscSurveyOrderAscPrjctSurveyIdxAsc(prjctIdxs)
|
||||
.stream()
|
||||
.collect(Collectors.groupingBy(
|
||||
TPrjctSurveyEntity::getPrjctIdx,
|
||||
LinkedHashMap::new,
|
||||
Collectors.mapping(ProjectSurveyDto::from, Collectors.toList())
|
||||
));
|
||||
}
|
||||
|
||||
private void updateProjectSurveys(
|
||||
Long prjctIdx,
|
||||
List<ProjectSurveyDto> surveys,
|
||||
String modUser,
|
||||
LocalDateTime modDt,
|
||||
RequestUserContext ctx
|
||||
) {
|
||||
List<ProjectSurveyDto> requestSurveys = surveys == null ? List.of() : surveys;
|
||||
Map<Long, TPrjctSurveyEntity> existingMap = projectSurveyRepository
|
||||
.findByPrjctIdxOrderBySurveyOrderAscPrjctSurveyIdxAsc(prjctIdx)
|
||||
.stream()
|
||||
.collect(Collectors.toMap(
|
||||
TPrjctSurveyEntity::getPrjctSurveyIdx,
|
||||
Function.identity(),
|
||||
(left, right) -> left,
|
||||
LinkedHashMap::new
|
||||
));
|
||||
|
||||
Set<Long> requestedIds = requestSurveys.stream()
|
||||
.map(ProjectSurveyDto::getPrjctSurveyIdx)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
List<TPrjctSurveyEntity> deleteTargets = existingMap.values().stream()
|
||||
.filter(entity -> !requestedIds.contains(entity.getPrjctSurveyIdx()))
|
||||
.toList();
|
||||
if (!deleteTargets.isEmpty()) {
|
||||
projectSurveyRepository.deleteAllInBatch(deleteTargets);
|
||||
}
|
||||
|
||||
List<TPrjctSurveyEntity> saveTargets = new ArrayList<>();
|
||||
for (ProjectSurveyDto survey : requestSurveys) {
|
||||
if (survey.getPrjctSurveyIdx() == null) {
|
||||
saveTargets.add(TPrjctSurveyEntity.create(prjctIdx, survey, modUser, modDt));
|
||||
continue;
|
||||
}
|
||||
|
||||
TPrjctSurveyEntity entity = existingMap.get(survey.getPrjctSurveyIdx());
|
||||
if (entity == null || !Objects.equals(entity.getPrjctIdx(), prjctIdx)) {
|
||||
throw BusinessException.badRequest("PROJECT_SURVEY_NOT_FOUND", "프로젝트 설문을 찾을 수 없습니다.")
|
||||
.with("prjctSurveyIdx", survey.getPrjctSurveyIdx());
|
||||
}
|
||||
entity.updateFrom(survey, modUser, modDt);
|
||||
saveTargets.add(entity);
|
||||
}
|
||||
if (!saveTargets.isEmpty()) {
|
||||
List<TPrjctSurveyEntity> savedTargets = projectSurveyRepository.saveAllAndFlush(saveTargets);
|
||||
replacePrjctSurveyIdxToken(savedTargets);
|
||||
resolveSurveyUrls(savedTargets, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
private void replacePrjctSurveyIdxToken(List<TPrjctSurveyEntity> surveys) {
|
||||
for (TPrjctSurveyEntity survey : surveys) {
|
||||
String surveyUrl = survey.getSurveyUrl();
|
||||
Long prjctSurveyIdx = survey.getPrjctSurveyIdx();
|
||||
if (surveyUrl == null || prjctSurveyIdx == null || !surveyUrl.contains(PRJCT_SURVEY_IDX_TOKEN)) {
|
||||
continue;
|
||||
}
|
||||
survey.setSurveyUrl(surveyUrl.replace(PRJCT_SURVEY_IDX_TOKEN, prjctSurveyIdx.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
private void resolveSurveyUrls(List<TPrjctSurveyEntity> surveys, RequestUserContext ctx) {
|
||||
for (TPrjctSurveyEntity survey : surveys) {
|
||||
ResolvedSurveyUrl resolved = resolveSurveyUrl(survey, ctx);
|
||||
if (resolved == null) {
|
||||
continue;
|
||||
}
|
||||
survey.setSurveyUrl(resolved.finalUrl());
|
||||
survey.setSurveyFullUrl(resolved.fullUrl());
|
||||
survey.setSurveyShortUrl(resolved.shortUrl());
|
||||
}
|
||||
}
|
||||
|
||||
private ResolvedSurveyUrl resolveSurveyUrl(TPrjctSurveyEntity survey, RequestUserContext ctx) {
|
||||
String requestedUrl = survey.getSurveyUrl();
|
||||
if (requestedUrl == null || requestedUrl.isBlank()) {
|
||||
survey.setSurveyFullUrl(null);
|
||||
survey.setSurveyShortUrl(null);
|
||||
return null;
|
||||
}
|
||||
|
||||
String baseUrl = resolveSurveyBaseUrl(survey);
|
||||
if (baseUrl == null) {
|
||||
survey.setSurveyFullUrl(null);
|
||||
survey.setSurveyShortUrl(null);
|
||||
return null;
|
||||
}
|
||||
|
||||
String queryString = extractQueryString(requestedUrl);
|
||||
String fullUrl = appendQueryString(baseUrl, queryString);
|
||||
String shortUrl = shortUrlApiClient.createShortUrl(baseUrl, ctx)
|
||||
.map(url -> appendQueryString(url, queryString))
|
||||
.orElse(null);
|
||||
|
||||
return new ResolvedSurveyUrl(shortUrl == null ? fullUrl : shortUrl, fullUrl, shortUrl);
|
||||
}
|
||||
|
||||
private String resolveSurveyBaseUrl(TPrjctSurveyEntity survey) {
|
||||
String surveyUrl = survey.getSurveyUrl();
|
||||
String surveyPrjctCd = survey.getSurveyPrjctCd();
|
||||
Long prjctSurveyIdx = survey.getPrjctSurveyIdx();
|
||||
if (surveyUrl == null || surveyPrjctCd == null || surveyPrjctCd.isBlank() || prjctSurveyIdx == null) {
|
||||
return null;
|
||||
}
|
||||
if (surveyUrl.contains(KON_URL_TOKEN)) {
|
||||
return KON_SURVEY_BASE_URL + surveyPrjctCd + "/INDEX.asp?psidx=" + prjctSurveyIdx;
|
||||
}
|
||||
if (surveyUrl.contains(KIWI_URL_TOKEN)) {
|
||||
return KIWI_SURVEY_BASE_URL + surveyPrjctCd + "/INDEX.asp?psidx=" + prjctSurveyIdx;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String extractQueryString(String url) {
|
||||
int queryIndex = url.indexOf('?');
|
||||
if (queryIndex < 0 || queryIndex + 1 >= url.length()) {
|
||||
return "";
|
||||
}
|
||||
return url.substring(queryIndex + 1);
|
||||
}
|
||||
|
||||
private String appendQueryString(String baseUrl, String queryString) {
|
||||
if (queryString == null || queryString.isBlank()) {
|
||||
return baseUrl;
|
||||
}
|
||||
return baseUrl + (baseUrl.contains("?") ? "&" : "?") + queryString;
|
||||
}
|
||||
|
||||
private record ResolvedSurveyUrl(String finalUrl, String fullUrl, String shortUrl) {
|
||||
}
|
||||
|
||||
private String makePrjctCd(String itrntPrjctCd) {
|
||||
String prefix = itrntPrjctCd + "_";
|
||||
|
||||
@@ -216,6 +419,6 @@ public class ProjectService {
|
||||
.max()
|
||||
.orElse(0);
|
||||
|
||||
return prefix + String.format("%03d", (maxSubCode + 1));
|
||||
return prefix + String.format("%03d", maxSubCode + 1);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user