Skip to content

Commit

Permalink
refactor : 정책 리스트 조회 서비스 로직 중복 코드 제거 및 의도를 명확하게 하기 위한 변수명 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
LHS-11 committed Jul 22, 2024
1 parent 451e450 commit 6b829f7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,65 +40,33 @@ public Page<PolicyListInfoDto> getPolicyList(User user, PolicyListRequestDto pol
PageRequest pageable = PageRequest.of(page, size, sort);

return policyQueryRepository.searchByAppliedPaging(user, policyListRequestDto.getSupportPolicyType(), policyListRequestDto.getPolicyType(), null, pageable)
.map(dto -> {
Policy findPolicy = policyRepository.findById(dto.getPolicyId()).get();
DenialReasonType denialReasonType = PolicyDenialReasonClassifier.getDenialReasonType(user, findPolicy);
dto.updatePolicyApplyDenialReason(denialReasonType != null ? denialReasonType.getText() : null);
dto.updatePolicyDateTypeDescription(dto.getPolicyDateType());
dto.updateAreaCode(dto.getAreaCode());
dto.updateCityCode(dto.getCityCode());
dto.updatePolicyMethodType(dto.getPolicyMethodTypeDescription());
return dto;
});
.map(dto -> getPolicyListInfoDto(user, dto));
}

public PolicyListResponseDto getSearchPolicyList(User user, SearchPolicyListRequestDto searchPolicyDto, int page, int size, Sort sort) {
PageRequest appliedPageable = PageRequest.of(page, size, sort);
public PolicyListResponseDto getSearchPolicyList(User user, SearchPolicyListRequestDto searchPolicyDto, int currentPage, int size, Sort sort) {
PageRequest appliedPageable = PageRequest.of(currentPage, size, sort);

// 유저가 신청 가능한 정책 가져오기
Page<PolicyListInfoDto> appliedPolicyListInfo = policyQueryRepository.searchByAppliedPaging(user, searchPolicyDto.getSupportPolicyType(), searchPolicyDto.getPolicyType(), searchPolicyDto.getKeyword(), appliedPageable)
.map(dto -> {
Policy findPolicy = policyRepository.findById(dto.getPolicyId()).get();
DenialReasonType denialReasonType = PolicyDenialReasonClassifier.getDenialReasonType(user, findPolicy);
dto.updatePolicyApplyDenialReason(denialReasonType != null ? denialReasonType.getText() : null);
dto.updatePolicyDateTypeDescription(dto.getPolicyDateType());
dto.updateAreaCode(dto.getAreaCode());
dto.updateCityCode(dto.getCityCode());
dto.updatePolicyMethodType(dto.getPolicyMethodTypeDescription());
dto.updatePolicyLogo(imageClassifier.getLogo(findPolicy));
dto.updateBenefitPeriod(CashBenefitType.findCashBenefit(findPolicy));
// dto.updatePolicyUrl(dto.getPolicyUrl().startsWith("http"));
return dto;
});

// 신청할 수 있는 정책 총 페이지
int totalPages = appliedPolicyListInfo.getTotalPages();

// 신청할 수 있는 정책 리스트의 마지막 페이지 일때
if (totalPages - 1 < page) {

List<Long> savedPolicyIds = getPolicyIdsByRedis(user);

//신청 불가능한 정책 가져오기
PageRequest nonAppliedPageable = PageRequest.of(page - totalPages, size, sort);

Page<PolicyListInfoDto> nonAppliedPolicyListInfo = policyQueryRepository.searchByNonAppliedPaging(user, searchPolicyDto.getSupportPolicyType(), searchPolicyDto.getPolicyType(), searchPolicyDto.getKeyword(), nonAppliedPageable, savedPolicyIds)
.map(dto -> {
Policy findPolicy = policyRepository.findById(dto.getPolicyId()).get();
DenialReasonType denialReasonType = PolicyDenialReasonClassifier.getDenialReasonType(user, findPolicy);
dto.updatePolicyApplyDenialReason(denialReasonType != null ? denialReasonType.getText() : null);
dto.updatePolicyDateTypeDescription(dto.getPolicyDateType());
dto.updateAreaCode(dto.getAreaCode());
dto.updateCityCode(dto.getCityCode());
dto.updatePolicyMethodType(dto.getPolicyMethodTypeDescription());
dto.updatePolicyLogo(imageClassifier.getLogo(findPolicy));
dto.updateBenefitPeriod(CashBenefitType.findCashBenefit(findPolicy));
return dto;
});
.map(dto -> getPolicyListInfoDto(user, dto));

int appliedPolicyTotalPages = appliedPolicyListInfo.getTotalPages();

boolean isNonAppliedPolicy = currentPage >= appliedPolicyTotalPages;

if (isNonAppliedPolicy) {

List<Long> savedAppliedPolicyIds = getPolicyIdsByRedis(user);

int nonAppliedPolicyPage = currentPage - appliedPolicyTotalPages;

PageRequest nonAppliedPageable = PageRequest.of(nonAppliedPolicyPage, size, sort);

Page<PolicyListInfoDto> nonAppliedPolicyListInfo = policyQueryRepository.searchByNonAppliedPaging(user, searchPolicyDto.getSupportPolicyType(), searchPolicyDto.getPolicyType(), searchPolicyDto.getKeyword(), nonAppliedPageable, savedAppliedPolicyIds)
.map(dto -> getPolicyListInfoDto(user, dto));

return PolicyListResponseDto.builder()
.policyListInfoResponseDto(nonAppliedPolicyListInfo)
.pageNumber(page)
.pageNumber(currentPage)
.last(nonAppliedPolicyListInfo.isLast())
.build();
} else {
Expand All @@ -107,12 +75,25 @@ public PolicyListResponseDto getSearchPolicyList(User user, SearchPolicyListRequ

return PolicyListResponseDto.builder()
.policyListInfoResponseDto(appliedPolicyListInfo)
.pageNumber(page)
.pageNumber(currentPage)
.last(false)
.build();

}

private PolicyListInfoDto getPolicyListInfoDto(User user, PolicyListInfoDto dto) {
Policy findPolicy = policyRepository.findById(dto.getPolicyId()).get();
DenialReasonType denialReasonType = PolicyDenialReasonClassifier.getDenialReasonType(user, findPolicy);
dto.updatePolicyApplyDenialReason(denialReasonType != null ? denialReasonType.getText() : null);
dto.updatePolicyDateTypeDescription(dto.getPolicyDateType());
dto.updateAreaCode(dto.getAreaCode());
dto.updateCityCode(dto.getCityCode());
dto.updatePolicyMethodType(dto.getPolicyMethodTypeDescription());
dto.updatePolicyLogo(imageClassifier.getLogo(findPolicy));
dto.updateBenefitPeriod(CashBenefitType.findCashBenefit(findPolicy));
return dto;
}

private void savePolicyIdsToRedis(User user, Page<PolicyListInfoDto> policyListResponseDtos) {
List<Long> policyIds = policyListResponseDtos.stream()
.map(PolicyListInfoDto::getPolicyId)
Expand Down Expand Up @@ -161,8 +142,8 @@ public PolicyInfoResponseDto getPolicy(User user, Long policyId) {
return dto;
}

public String removeNullOrHyphenStr(String str){
if(str.trim().equals("-") || str.toLowerCase().trim().contains("null")){
public String removeNullOrHyphenStr(String str) {
if (str.trim().equals("-") || str.toLowerCase().trim().contains("null")) {
return "";
}
return str;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public Page<PolicyListInfoDto> searchByAppliedPaging(User user, SupportPolicyTyp
.limit(pageable.getPageSize())
.fetch();

OrderSpecifier orderSpecifier = new OrderSpecifier(Order.ASC, policy.id);

return PageableExecutionUtils.getPage(results, pageable, query::fetchCount);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,23 @@ public void updatePolicyApplyDenialReason(String policyApplyDenialReason) {
this.applyStatus = true;
}

public void updatePolicyMethodType(String applicationProcedureContent) {
PolicyMethodType findPolicyMethodType = PolicyMethodType.findPolicyMethodTypeByKeywords(applicationProcedureContent);
this.policyMethodType = findPolicyMethodType;
this.policyMethodTypeDescription = findPolicyMethodType.getDescription();
}

public void updateBenefitPeriod(String benefitPeriod) {
this.benefitPeriod = benefitPeriod;
}



public void updatePolicyUrl(String policyUrl) {
this.policyUrl = policyUrl;
}


@Builder
public PolicyListInfoDto(Long policyId, String policyName, String policyLogo, String policyIntroduction, String areaCode, String cityCode, PolicyDateType policyDateType, String policyDateTypeDescription, String policyApplyDenialReason, boolean applyStatus, BigDecimal benefit, String benefitPeriod, boolean applyFlag, boolean interestFlag, PolicyMethodType policyMethodType, String policyMethodTypeDescription, String policyUrl) {
this.policyId = policyId;
Expand Down Expand Up @@ -132,14 +145,4 @@ public PolicyListInfoDto(Long policyId, String policyName, String policyLogo, St
this.policyUrl = policyUrl;
}

public void updatePolicyMethodType(String applicationProcedureContent) {
PolicyMethodType findPolicyMethodType = PolicyMethodType.findPolicyMethodTypeByKeywords(applicationProcedureContent);
this.policyMethodType = findPolicyMethodType;
this.policyMethodTypeDescription = findPolicyMethodType.getDescription();
}

public void updateBenefitPeriod(String benefitPeriod) {
this.benefitPeriod = benefitPeriod;
}

}

0 comments on commit 6b829f7

Please sign in to comment.