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.ContactConstant; import com.kri.contact.server.dto.*; import com.kri.contact.server.dto.common.PageResponseDto; 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.dao.DataIntegrityViolationException; 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.time.LocalDateTime; import java.util.*; @Service @RequiredArgsConstructor public class ProjectService { private final AssignService assignService; private final ResultCodeService resultCodeService; private final ProjectRepository projectRepository; private static final List STATUS_0_1 = List.of((short) 0, (short) 1); private static final List STATUS_1 = List.of((short) 1); @Transactional public ProjectInfoDto createProject(ProjectCreateRequestDto req, String regUser, LocalDateTime regDt) { String itrnt = req.getItrntPrjctCd(); try { String prjctCd = this.makePrjctCd(itrnt); TPrjctEntity entity = TPrjctEntity.create(req, prjctCd, regUser, regDt); ProjectInfoDto savedProject = ProjectInfoDto.from(projectRepository.save(entity)); resultCodeService.copyResultCodeFromDefault(savedProject.getPrjctIdx(), regUser, regDt); return savedProject; } catch (DataIntegrityViolationException e) { throw BusinessException.conflict( "PROJECT_CODE_CONFLICT", "프로젝트 코드 생성 중 충돌이 발생했습니다. 다시 시도해주세요." ).with("itrntPrjctCd", itrnt); } } public ProjectInfoDto getProject(Long prjctIdx) { return ProjectInfoDto.from(projectRepository.findByPrjctIdxAndDelFalse(prjctIdx)); } public List getTopProject(int topCount, String keyword, String userId, Integer userRole) { Specification spec = ProjectSpecs.keywordLike(keyword) .and(ProjectSpecs.isDelFalse()); if (userRole >= ContactConstant.OUTSOURCE_USER_ROLE) { if (userRole >= ContactConstant.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 searchProject(ProjectSearchRequestDto req, String userId, Integer userRole) { Specification 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) { 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 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 getOwnerDeptList(Integer userRole) { final List statusList = (userRole < ContactConstant.OUTSOURCE_USER_ROLE) ? null : (userRole < ContactConstant.FIELDWORK_USER_ROLE) ? STATUS_0_1 : STATUS_1; return projectRepository.findDistinctOwnerDept(statusList); } public List getOwnerFwList(Integer userRole) { final List statusList = (userRole < ContactConstant.OUTSOURCE_USER_ROLE) ? null : (userRole < ContactConstant.FIELDWORK_USER_ROLE) ? STATUS_0_1 : STATUS_1; return projectRepository.findDistinctOwnerFw(statusList); } @Transactional public void updateProject(long prjctIdx, ProjectUpdateRequestDto req, String modUser, LocalDateTime modDt) { // dirty checking 업데이트 TPrjctEntity entity = projectRepository.findById(prjctIdx) .orElseThrow(() -> BusinessException.notFound("PROJECT_NOT_FOUND", "프로젝트를 찾을 수 없습니다.") .with("prjctIdx", prjctIdx) ); entity.updateFrom(req.getProjectInfo(), modUser, modDt); assignService.upsertPrjctOutsourceAssign(prjctIdx, req.getAssignUserIdList(), modUser, modDt); resultCodeService.updateResultCode(prjctIdx, req.getResultCodeList(), modUser, modDt); } @Transactional public void deleteProject(Long prjctIdx, String delUser, LocalDateTime delDt) { int deleteCount = projectRepository.softDeleteByPrjctIdx(prjctIdx, delUser, delDt); if(deleteCount == 0) { throw BusinessException.notFound("PROJECT_NOT_FOUND", "프로젝트를 찾을 수 없습니다.") .with("prjctIdx", prjctIdx); } } /***************************private************************************/ private String makePrjctCd(String itrntPrjctCd) { String prefix = itrntPrjctCd + "_"; List 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)); } }