Skip to content

Commit

Permalink
refactor : Policy 클래스에서 SupportPolicyType -> Set 자료형 필드로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
LHS-11 committed Dec 21, 2023
1 parent 5ab9675 commit 0d2fa84
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ public Map<SupportPolicyType, Policy> recommendPolicy(User user) {


Policy maxBenefitMoneyPolicy = recommendPolicy.stream()
.filter(p -> p.getSupportPolicyType().equals(SupportPolicyType.MONEY))
.filter(p -> p.getSupportPolicyTypes().contains((SupportPolicyType.MONEY)))
.max(Comparator.comparingInt(Policy::getBenefit))
.orElse(null);

Policy maxBenefitLoansPolicy = recommendPolicy.stream()
.filter(p -> p.getSupportPolicyType().equals(SupportPolicyType.LOANS))
.filter(p -> p.getSupportPolicyTypes().contains(SupportPolicyType.LOANS))
.max(Comparator.comparingInt(Policy::getBenefit))
.orElse(null);

LocalDate now = LocalDate.now();
Policy mostImminentEndDateSocialServicePolicy = recommendPolicy.stream()
.filter(p -> p.getSupportPolicyType().equals(SupportPolicyType.SOCIAL_SERVICE))
.filter(p -> p.getSupportPolicyTypes().contains(SupportPolicyType.SOCIAL_SERVICE))
.filter(p -> p.getApplyEndDate().isAfter(now))
.min(Comparator.comparing(p -> ChronoUnit.DAYS.between(now, p.getApplyEndDate())))
.orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,15 @@ public PolicyCountResponseDto getRecommendCountAndNickname(User user) {
}

public RecommendPolicyInfoResponseDto getRecommendPolicyDummy() {
Policy loansPolicy = policyRepository.findAllBySupportPolicyType(SupportPolicyType.LOANS).get(0);
Policy moneyPolicy = policyRepository.findAllBySupportPolicyType(SupportPolicyType.MONEY).get(0);
Policy socialServicePolicy = policyRepository.findAllBySupportPolicyType(SupportPolicyType.SOCIAL_SERVICE).get(0);
Policy loansPolicy = policyRepository.findAllBySupportPolicyTypesContains(SupportPolicyType.LOANS).get(0);
Policy moneyPolicy = policyRepository.findAllBySupportPolicyTypesContains(SupportPolicyType.MONEY).get(0);
Policy socialServicePolicy = policyRepository.findAllBySupportPolicyTypesContains(SupportPolicyType.SOCIAL_SERVICE).get(0);

List<RecommendPolicyInfoResponseDto.recommendPolicyInfo> result = new ArrayList<>();
result.add(
RecommendPolicyInfoResponseDto.recommendPolicyInfo.builder()
.supportTypeDescription(loansPolicy.getSupportPolicyType().getDescription())
.supportType(loansPolicy.getSupportPolicyType())
.supportTypeDescription(SupportPolicyType.LOANS.getDescription())
.supportType(SupportPolicyType.LOANS)
.policyId(loansPolicy.getId())
.policyName(loansPolicy.getPolicyName())
.policyLogo(loansPolicy.getPolicyLogo())
Expand All @@ -219,8 +219,8 @@ public RecommendPolicyInfoResponseDto getRecommendPolicyDummy() {
);
result.add(
RecommendPolicyInfoResponseDto.recommendPolicyInfo.builder()
.supportType(moneyPolicy.getSupportPolicyType())
.supportTypeDescription(moneyPolicy.getSupportPolicyType().getDescription())
.supportType(SupportPolicyType.MONEY)
.supportTypeDescription(SupportPolicyType.MONEY.getDescription())
.policyId(moneyPolicy.getId())
.policyName(moneyPolicy.getPolicyName())
.policyLogo(moneyPolicy.getPolicyLogo())
Expand All @@ -234,8 +234,8 @@ public RecommendPolicyInfoResponseDto getRecommendPolicyDummy() {
);
result.add(
RecommendPolicyInfoResponseDto.recommendPolicyInfo.builder()
.supportType(socialServicePolicy.getSupportPolicyType())
.supportTypeDescription(socialServicePolicy.getSupportPolicyType().getDescription())
.supportType(SupportPolicyType.SOCIAL_SERVICE)
.supportTypeDescription(SupportPolicyType.SOCIAL_SERVICE.getDescription())
.policyId(socialServicePolicy.getId())
.policyName(socialServicePolicy.getPolicyName())
.policyLogo(socialServicePolicy.getPolicyLogo())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Page<PolicyListResponseDto> searchByPaging(User user, SupportPolicyType s
.where(
// getPolicyId(lastPolicyId, pageable),
// 조건
policy.supportPolicyType.eq(supportPolicyType)
policy.supportPolicyTypes.contains(supportPolicyType)
);

if (PolicyCode.NONE != policyCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public interface PolicyRepository extends JpaRepository<Policy, Long> {

// jpql
@Query("SELECT p FROM Policy p " +
"WHERE p.supportPolicyType = :policyType " +
"WHERE p.supportPolicyTypes in (:policyType) " +
"AND p.applyEndDate >= :currentDate " +
"AND NOT EXISTS (SELECT up FROM UserPolicy up WHERE up.policy = p AND up.user.userId = :userId AND up.applyFlag=true) " +
"ORDER BY p.applyEndDate ASC ")
Expand All @@ -29,7 +29,7 @@ public interface PolicyRepository extends JpaRepository<Policy, Long> {
// native query
@Query(value =
"SELECT p.* FROM policy p " +
"WHERE p.support_policy_type = :policyType " +
"WHERE p.support_policy_types in (:policyType) " +
"AND p.apply_end_date >= :currentDate " +
"AND NOT EXISTS (SELECT 1 FROM user_policy up WHERE up.policy_id = p.id AND up.user_id = :userId AND up.apply_flag = true) " +
"ORDER BY p.apply_end_date ASC " +
Expand All @@ -41,15 +41,15 @@ public interface PolicyRepository extends JpaRepository<Policy, Long> {

List<Policy> findAllByApplySttDate(LocalDate sttDate);

List<Policy> findAllBySupportPolicyType(SupportPolicyType supportPolicyType);
List<Policy> findAllBySupportPolicyTypesContains(SupportPolicyType supportPolicyType);

@EntityGraph(attributePaths = {"jobTypes", "educationTypes", "policySplzTypes", "supportPolicyTypes"})
@Query(value = "SELECT p FROM Policy p " +
"WHERE (p.areaCode = :areaCode AND p.cityCode = :cityCode) OR (p.areaCode = :areaCode AND p.cityCode IS NULL) OR (p.areaCode = :central) " +
"AND :age between p.minAge and p.maxAge")
List<Policy> findByAreaCodeAndCityCodeAndAge(@Param("areaCode") AreaCode areaCode, @Param("central") AreaCode central, @Param("cityCode") CityCode cityCode, @Param("age") int age);

@Query("SELECT COUNT(p) FROM Policy p WHERE p.supportPolicyType = :supportPolicyType")
@Query("SELECT COUNT(p) FROM Policy p WHERE p.supportPolicyTypes in (:supportPolicyType)")
int getPolicyCntBySupportPolicyType(@Param("supportPolicyType") SupportPolicyType supportPolicyType);

@Query("SELECT P FROM Policy P LEFT JOIN P.userPolicies UP " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,6 @@ public class Policy extends BaseEntity implements Serializable {
@Enumerated(EnumType.STRING)
private PolicyCode policyCode; // 정책 유형

@Enumerated(EnumType.STRING)
private SupportPolicyType supportPolicyType; // 지원 정책 유형

@Column(columnDefinition = "TEXT")
private String policyLogo; // 정책 로고

Expand Down Expand Up @@ -166,8 +163,8 @@ public void updateLogo(String imageUrl) {
this.policyLogo = imageUrl;
}

public void updateSupportType(SupportPolicyType supportPolicyType) {
this.supportPolicyType = supportPolicyType;
public void updateSupportTypes(Set<SupportPolicyType> supportPolicyTypes) {
this.supportPolicyTypes.addAll(supportPolicyTypes);
}

public void updateRemark(String remark) {
Expand All @@ -188,7 +185,7 @@ public void updateDateType(PolicyDateType policyDateType) {
}

@Builder
public Policy(String bizId, String policyName, String policyIntroduction, String operatingAgencyName, String applicationPeriodContent, String organizationType, String supportContent, String ageInfo, String employmentStatusContent, String specializedFieldContent, String educationalRequirementContent, String residentialAndIncomeRequirementContent, String additionalClauseContent, String eligibilityTargetContent, String duplicatePolicyCode, String applicationSiteAddress, String referenceSiteUrlAddress, String applicationProcedureContent, String submissionDocumentContent, int minAge, int maxAge, AreaCode areaCode, CityCode cityCode, Set<JobType> jobTypes, Set<EducationType> educationTypes, Set<PolicySplzType> policySplzTypes, PolicyCode policyCode, SupportPolicyType supportPolicyType, String policyLogo, String policyApplyDenialReason, String applyStatus, LocalDate applySttDate, LocalDate applyEndDate, Set<UserPolicy> userPolicies, int benefit, String remark, List<ApplyPeriod> applyPeriods, PolicyDateType policyDateType) {
public Policy(String bizId, String policyName, String policyIntroduction, String operatingAgencyName, String applicationPeriodContent, String organizationType, String supportContent, String ageInfo, String employmentStatusContent, String specializedFieldContent, String educationalRequirementContent, String residentialAndIncomeRequirementContent, String additionalClauseContent, String eligibilityTargetContent, String duplicatePolicyCode, String applicationSiteAddress, String referenceSiteUrlAddress, String applicationProcedureContent, String submissionDocumentContent, int minAge, int maxAge, AreaCode areaCode, CityCode cityCode, Set<JobType> jobTypes, Set<EducationType> educationTypes, Set<PolicySplzType> policySplzTypes, PolicyCode policyCode, String policyLogo, String policyApplyDenialReason, String applyStatus, LocalDate applySttDate, LocalDate applyEndDate, Set<UserPolicy> userPolicies, int benefit, String remark, List<ApplyPeriod> applyPeriods, PolicyDateType policyDateType) {
this.bizId = bizId;
this.policyName = policyName;
this.policyIntroduction = policyIntroduction;
Expand All @@ -213,7 +210,6 @@ public Policy(String bizId, String policyName, String policyIntroduction, String
this.areaCode = areaCode;
this.cityCode = cityCode;
this.policyCode = policyCode;
this.supportPolicyType = supportPolicyType;
this.policyLogo = policyLogo;
this.policyApplyDenialReason = policyApplyDenialReason;
this.applyStatus = applyStatus;
Expand All @@ -226,5 +222,4 @@ public Policy(String bizId, String policyName, String policyIntroduction, String
this.policyDateType = policyDateType;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,38 @@
@Getter
public enum SupportPolicyType {

MONEY("현금","money"),
LOANS("대출","loan"),
SOCIAL_SERVICE("사회복지","social service");
MONEY("현금", "money", "1"),
LOANS("대출", "loan", "2"),
SOCIAL_SERVICE("사회복지", "social service", "3");

private String description;
private String name;
private String order;

SupportPolicyType(String description, String name) {
SupportPolicyType(String description, String name, String order) {
this.description = description;
this.name = name;
this.order = order;
}

public static SupportPolicyType fromString(String value){
public static SupportPolicyType fromString(String value) {
return Arrays.stream(SupportPolicyType.values())
.filter(s -> s.name().equals(value))
.findFirst()
.orElseThrow(() -> new BusinessException(NOT_FOUND_SUPPORT_POLICY_TYPE_ENUM_VALUE));
}

public static SupportPolicyType findSupportPolicyTypeContains(String name){
public static SupportPolicyType findSupportPolicyTypeContains(String name) {
return Arrays.stream(SupportPolicyType.values())
.filter(s -> name.contains(s.getName()))
.findFirst()
.orElseThrow(() -> new BusinessException(NOT_FOUND_SUPPORT_POLICY_TYPE_ENUM_VALUE));
}

public static SupportPolicyType findSupportPolicyTypeByOrder(String order) {
return Arrays.stream(SupportPolicyType.values())
.filter(s -> s.order.equals(order))
.findFirst()
.orElseThrow(() -> new BusinessException(NOT_FOUND_SUPPORT_POLICY_TYPE_ENUM_VALUE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ public HomeInfoResponseDto getHomeInfo(User user) {
.policyId(findPolicy.getId())
.policyName(findPolicy.getPolicyName())
.policyLogo(findPolicy.getPolicyLogo())
.supportPolicyType(findPolicy.getSupportPolicyType())
.supportPolicyTypeName(findPolicy.getSupportPolicyType().getDescription())
.supportPolicyType(type)
.supportPolicyTypeName(type.getDescription())
.dueDate(ChronoUnit.DAYS.between(currentTime, findPolicy.getApplyEndDate()))
.build();
return homePolicyInfo;
Expand Down Expand Up @@ -221,7 +221,7 @@ public void delete(User user) {
}

@Transactional
public void addPolicyDummy(User user){
public void addPolicyDummy(User user) {
Policy policy = policyRepository.findById(1L).get();
Policy policy2 = policyRepository.findById(2L).get();
Policy policy3 = policyRepository.findById(3L).get();
Expand All @@ -244,8 +244,8 @@ public void addPolicyDummy(User user){
Policy policy19 = policyRepository.findById(19L).get();
Policy policy20 = policyRepository.findById(20L).get();

List<Policy> interestPolices = List.of(policy, policy2, policy3, policy4, policy5, policy6, policy7, policy8, policy9 , policy10);
List<Policy> appliyPolices = List.of(policy11, policy12, policy13, policy14, policy15, policy16, policy17, policy18, policy19 , policy20);
List<Policy> interestPolices = List.of(policy, policy2, policy3, policy4, policy5, policy6, policy7, policy8, policy9, policy10);
List<Policy> appliyPolices = List.of(policy11, policy12, policy13, policy14, policy15, policy16, policy17, policy18, policy19, policy20);

for (Policy interestPolicy : interestPolices) {
UserPolicy userPolicy = UserPolicy.builder()
Expand All @@ -270,24 +270,24 @@ public void addPolicyDummy(User user){
}

public List<HomeInfoResponseDto.HomePolicyInfo> getRecommendPolicyDummy() {
Policy loansPolicy = policyRepository.findAllBySupportPolicyType(SupportPolicyType.LOANS).get(0);
Policy moneyPolicy = policyRepository.findAllBySupportPolicyType(SupportPolicyType.MONEY).get(0);
Policy socialServicePolicy = policyRepository.findAllBySupportPolicyType(SupportPolicyType.SOCIAL_SERVICE).get(0);
Policy loansPolicy = policyRepository.findAllBySupportPolicyTypesContains(SupportPolicyType.LOANS).get(0);
Policy moneyPolicy = policyRepository.findAllBySupportPolicyTypesContains(SupportPolicyType.MONEY).get(0);
Policy socialServicePolicy = policyRepository.findAllBySupportPolicyTypesContains(SupportPolicyType.SOCIAL_SERVICE).get(0);

List<HomeInfoResponseDto.HomePolicyInfo> result = new ArrayList<>();
result.add(get(loansPolicy));
result.add(get(moneyPolicy));
result.add(get(socialServicePolicy));
result.add(get(loansPolicy, SupportPolicyType.LOANS));
result.add(get(moneyPolicy, SupportPolicyType.MONEY));
result.add(get(socialServicePolicy, SupportPolicyType.SOCIAL_SERVICE));
return result;
}

public HomeInfoResponseDto.HomePolicyInfo get(Policy policy) {
public HomeInfoResponseDto.HomePolicyInfo get(Policy policy, SupportPolicyType supportPolicyType) {
return HomeInfoResponseDto.HomePolicyInfo.builder()
.policyId(policy.getId())
.policyName(policy.getPolicyName())
.policyLogo(policy.getPolicyLogo())
.supportPolicyTypeName(policy.getSupportPolicyType().getDescription())
.supportPolicyType(policy.getSupportPolicyType())
.supportPolicyTypeName(supportPolicyType.getDescription())
.supportPolicyType(supportPolicyType)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,13 @@ public class ApplyPolicyListResponseDto {
@ApiModelProperty(notes = "정책 수혜 금액", example = "1000000")
private int benefit;

@ApiModelProperty(notes = "정책 지원 유형", example = "SOCAIL_SERVICE")
private SupportPolicyType supportPolicyType;

@ApiModelProperty(notes = "정책 지원 유형 이름", example = "사회복지")
private String supportPolicyTypeDescription;

@Builder
public ApplyPolicyListResponseDto(Long policyId, String policyName, String policyIntroduction, String policyLogo, int benefit, SupportPolicyType supportPolicyType, String supportPolicyTypeDescription) {
public ApplyPolicyListResponseDto(Long policyId, String policyName, String policyIntroduction, String policyLogo, int benefit) {
this.policyId = policyId;
this.policyName = policyName;
this.policyIntroduction = policyIntroduction;
this.policyLogo = policyLogo;
this.benefit = benefit;
this.supportPolicyType = supportPolicyType;
this.supportPolicyTypeDescription = supportPolicyTypeDescription;
}

public static ApplyPolicyListResponseDto of(Policy policy) {
Expand All @@ -55,8 +47,6 @@ public static ApplyPolicyListResponseDto of(Policy policy) {
.policyLogo(policy.getPolicyLogo())
.policyIntroduction(policy.getPolicyIntroduction())
.benefit(policy.getBenefit())
.supportPolicyType(policy.getSupportPolicyType())
.supportPolicyTypeDescription(policy.getSupportPolicyType().getDescription())
.build();
}
}

0 comments on commit 0d2fa84

Please sign in to comment.