251210_01_ENH: Project CRUD 구현 및 테스트 진행중
This commit is contained in:
177
src/main/java/com/kri/contact/server/service/ProjectService.java
Normal file
177
src/main/java/com/kri/contact/server/service/ProjectService.java
Normal file
@@ -0,0 +1,177 @@
|
||||
package com.kri.contact.server.service;
|
||||
|
||||
import com.kri.common.datetime.DateTimeUtil;
|
||||
import com.kri.common.web.BusinessException;
|
||||
import com.kri.contact.server.common.consts.AppConstant;
|
||||
import com.kri.contact.server.dto.ProjectOwnerResponseDto;
|
||||
import com.kri.contact.server.dto.common.PageResponseDto;
|
||||
import com.kri.contact.server.dto.ProjectCreateRequestDto;
|
||||
import com.kri.contact.server.dto.ProjectInfoDto;
|
||||
import com.kri.contact.server.dto.ProjectSearchRequestDto;
|
||||
import com.kri.contact.server.persistence.entity.TPrjctEntity;
|
||||
import com.kri.contact.server.persistence.query.ProjectSorts;
|
||||
import com.kri.contact.server.persistence.query.ProjectSpecs;
|
||||
import com.kri.contact.server.persistence.repository.ProjectRepository;
|
||||
import org.springframework.data.domain.*;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class ProjectService {
|
||||
|
||||
private final ProjectRepository projectRepository;
|
||||
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);
|
||||
|
||||
@Transactional
|
||||
public ProjectInfoDto createProject(ProjectCreateRequestDto req, String regUser){
|
||||
String itrnt = req.getItrntPrjctCd();
|
||||
|
||||
for (int retry = 0; retry < AppConstant.INSERT_RETRY_COUNT; retry++) {
|
||||
String prjctCd = this.makePrjctCd(itrnt);
|
||||
TPrjctEntity entity = TPrjctEntity.create(req, prjctCd, regUser);
|
||||
|
||||
try {
|
||||
return ProjectInfoDto.from(
|
||||
projectRepository.save(entity)
|
||||
);
|
||||
} catch (org.springframework.dao.DataIntegrityViolationException e) {
|
||||
if (retry == (AppConstant.INSERT_RETRY_COUNT - 1)) throw e;
|
||||
}
|
||||
}
|
||||
|
||||
throw BusinessException.conflict(
|
||||
"PROJECT_CODE_CONFLICT",
|
||||
"프로젝트 코드 생성 중 충돌이 발생했습니다. 다시 시도해주세요."
|
||||
)
|
||||
.with("itrntPrjctCd", itrnt);
|
||||
}
|
||||
|
||||
public ProjectInfoDto getProject(Long prjctIdx){
|
||||
return ProjectInfoDto.from(projectRepository.findByPrjctIdxAndDelFalse(prjctIdx));
|
||||
}
|
||||
|
||||
public List<ProjectInfoDto> getTopProject(int topCount, String keyword, String userId, Integer userRole){
|
||||
Specification<TPrjctEntity> spec =
|
||||
ProjectSpecs.keywordLike(keyword)
|
||||
.and(ProjectSpecs.isDelFalse());
|
||||
|
||||
if (userRole >= AppConstant.OUTSOURCE_USER_ROLE) {
|
||||
if (userRole >= AppConstant.FIELDWORK_USER_ROLE) {
|
||||
spec = spec.and(ProjectSpecs.statusIn(STATUS_1));
|
||||
} else {
|
||||
spec = spec.and(ProjectSpecs.statusIn(STATUS_0_1));
|
||||
}
|
||||
spec = spec.and(ProjectSpecs.assignedToUserAndNotExcluded(userId));
|
||||
}
|
||||
|
||||
Pageable pageable = PageRequest.of(0, topCount, Sort.by(Sort.Direction.DESC, "regDt"));
|
||||
|
||||
return projectRepository.findAll(spec, pageable)
|
||||
.getContent()
|
||||
.stream()
|
||||
.map(ProjectInfoDto::from) // <-- 너네 매핑 방식에 맞게 수정
|
||||
.toList();
|
||||
}
|
||||
|
||||
public PageResponseDto<ProjectInfoDto> searchProject(ProjectSearchRequestDto req, String userId, 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 >= AppConstant.OUTSOURCE_USER_ROLE) {
|
||||
if (userRole >= AppConstant.FIELDWORK_USER_ROLE) {
|
||||
spec = spec.and(ProjectSpecs.statusIn(STATUS_1));
|
||||
} else {
|
||||
spec = spec.and(ProjectSpecs.statusIn(STATUS_0_1));
|
||||
}
|
||||
spec = spec.and(ProjectSpecs.assignedToUserAndNotExcluded(userId));
|
||||
}
|
||||
|
||||
Pageable pageable = PageRequest.of(
|
||||
req.getPageIndex(),
|
||||
req.getPageSize(),
|
||||
ProjectSorts.toSort(req.getSortBy(), req.getSortDir())
|
||||
);
|
||||
|
||||
Page<ProjectInfoDto> page = projectRepository.findAll(spec, pageable)
|
||||
.map(ProjectInfoDto::from);
|
||||
|
||||
return PageResponseDto.from(page);
|
||||
}
|
||||
|
||||
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 < AppConstant.OUTSOURCE_USER_ROLE) ? null :
|
||||
(userRole < AppConstant.FIELDWORK_USER_ROLE) ? STATUS_0_1 :
|
||||
STATUS_1;
|
||||
|
||||
return projectRepository.findDistinctOwnerDept(statusList);
|
||||
}
|
||||
|
||||
public List<String> getOwnerFwList(Integer userRole) {
|
||||
final List<Short> statusList =
|
||||
(userRole < AppConstant.OUTSOURCE_USER_ROLE) ? null :
|
||||
(userRole < AppConstant.FIELDWORK_USER_ROLE) ? STATUS_0_1 :
|
||||
STATUS_1;
|
||||
|
||||
return projectRepository.findDistinctOwnerFw(statusList);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void updateProject(ProjectInfoDto req, String modUser){
|
||||
// dirty checking 업데이트
|
||||
TPrjctEntity entity = projectRepository.findById(req.getPrjctIdx())
|
||||
.orElseThrow(() ->
|
||||
BusinessException.notFound("PROJECT_NOT_FOUND", "프로젝트를 찾을 수 없습니다.")
|
||||
.with("prjctIdx", req.getPrjctIdx())
|
||||
);
|
||||
|
||||
entity.updateFrom(req, modUser);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteProject(Long prjctIdx, String delUser){
|
||||
int deleteCount = projectRepository.softDeleteByPrjctIdx(prjctIdx, delUser, DateTimeUtil.nowKstLocalDateTime());
|
||||
|
||||
if(deleteCount == 0){
|
||||
throw BusinessException.notFound("PROJECT_NOT_FOUND", "프로젝트를 찾을 수 없습니다.")
|
||||
.with("prjctIdx", prjctIdx);
|
||||
}
|
||||
}
|
||||
|
||||
/***************************private************************************/
|
||||
private String makePrjctCd(String itrntPrjctCd){
|
||||
String prefix = itrntPrjctCd + "_";
|
||||
|
||||
List<TPrjctEntity> sameItrnt = projectRepository.findByItrntPrjctCd(itrntPrjctCd);
|
||||
int maxSubCode = sameItrnt.stream()
|
||||
.map(TPrjctEntity::getPrjctCd)
|
||||
.filter(Objects::nonNull)
|
||||
.filter(cd -> cd.startsWith(prefix))
|
||||
.map(cd -> cd.substring(prefix.length()))
|
||||
.filter(s -> s.matches("\\d+"))
|
||||
.mapToInt(Integer::parseInt)
|
||||
.max()
|
||||
.orElse(0);
|
||||
|
||||
return prefix + String.format("%03d", (maxSubCode + 1));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user