-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d30bd6
commit 850ae6c
Showing
13 changed files
with
301 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
pluginManagement { | ||
plugins { | ||
id 'org.jetbrains.kotlin.jvm' version '2.0.20' | ||
} | ||
} | ||
rootProject.name = 'onchung' |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/danpoong/onchung/domain/policy/controller/PolicyController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.danpoong.onchung.domain.policy.controller; | ||
|
||
import com.danpoong.onchung.global.csv.service.CsvService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api") | ||
public class PolicyController { | ||
private final CsvService csvService; | ||
|
||
// @PostMapping("/csv/policy") | ||
// public ResponseTemplate<?> readCsvAndSaveInfo(@RequestParam("file") MultipartFile file) { | ||
// csvService.loadDataFromCSV(file); | ||
// return new ResponseTemplate<>(HttpStatus.CREATED, "csv 파일 저장 성공"); | ||
// } | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/danpoong/onchung/domain/policy/domain/FilteringDetails.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.danpoong.onchung.domain.policy.domain; | ||
|
||
import jakarta.persistence.Embeddable; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Embeddable | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Getter | ||
@Builder | ||
public class FilteringDetails { | ||
private String ageInfo; // 연령 정보 | ||
private String employmentStatus; // 취업상태내용 | ||
private String specializationField; // 특화분야내용 | ||
private String educationRequirement; // 학력요건내용 | ||
} |
76 changes: 76 additions & 0 deletions
76
src/main/java/com/danpoong/onchung/domain/policy/domain/Policy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.danpoong.onchung.domain.policy.domain; | ||
|
||
import com.danpoong.onchung.domain.policy.domain.enums.PolicyCategory; | ||
import com.danpoong.onchung.domain.public_office.domain.PublicOffice; | ||
import jakarta.persistence.*; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.List; | ||
|
||
@Entity | ||
@NoArgsConstructor | ||
@Getter | ||
public class Policy { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "policy_id") | ||
private Long id; | ||
|
||
@Column(name = "policy_name") | ||
private String name; | ||
@Column(name = "policy_introduction") | ||
private String introduction; // 정책소개 | ||
|
||
@Column(name = "support_details") | ||
private String supportDetails; // 지원내용 | ||
@Column(name = "support_scale") | ||
private String supportScale; // 지원규모 | ||
@Column(name = "support_target") | ||
private String supportTarget; // 지원대상 | ||
|
||
@Column(name = "application_period") | ||
private String applicationPeriod; // 신청기간 | ||
@Column(name = "application_procedure") | ||
private String applicationProcedure; // 신청절차 | ||
@Column(name = "required_documents") | ||
private String requiredDocuments; // 제출서류내용 | ||
private String applicationSite; // 신청사이트주소 | ||
|
||
// 연락처 | ||
@Column(name = "contact_info") | ||
private String contactInfo; | ||
|
||
// 필터링 시 사용 - 연령, 취업상태, 특화분야, 학력요건 | ||
@Embedded | ||
private FilteringDetails filteringDetails; | ||
|
||
@Enumerated(EnumType.STRING) | ||
private PolicyCategory category; | ||
|
||
@ManyToMany | ||
@JoinTable( | ||
name = "policy_public_office", | ||
joinColumns = @JoinColumn(name = "policy_id"), | ||
inverseJoinColumns = @JoinColumn(name = "public_office_id") | ||
) | ||
private List<PublicOffice> publicOffices; | ||
|
||
@Builder | ||
public Policy(String name, String introduction, String supportDetails, String supportScale, String supportTarget, String applicationPeriod, String applicationProcedure, String requiredDocuments, String applicationSite, String contactInfo, FilteringDetails filteringDetails, String category, List<PublicOffice> publicOffices) { | ||
this.name = name; | ||
this.introduction = introduction; | ||
this.supportDetails = supportDetails; | ||
this.supportScale = supportScale; | ||
this.supportTarget = supportTarget; | ||
this.applicationPeriod = applicationPeriod; | ||
this.applicationProcedure = applicationProcedure; | ||
this.requiredDocuments = requiredDocuments; | ||
this.applicationSite = applicationSite; | ||
this.contactInfo = contactInfo; | ||
this.filteringDetails = filteringDetails; | ||
this.category = PolicyCategory.findCategory(category); | ||
this.publicOffices = publicOffices; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/danpoong/onchung/domain/policy/domain/enums/PolicyCategory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.danpoong.onchung.domain.policy.domain.enums; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public enum PolicyCategory { | ||
Employment("23010"), // (일자리 분야) | ||
Housing("23020"), // 주거 분야 | ||
Education("23030"), //교육 분야 | ||
WelfareCulture("23040"), // 복지, 문화 분야 | ||
ParticipationRights("23050"); // 참여, 권리 분야 | ||
|
||
private final String code; | ||
|
||
PolicyCategory(String code) { | ||
this.code = code; | ||
} | ||
|
||
public static PolicyCategory findCategory(String code) { | ||
for (PolicyCategory category : PolicyCategory.values()) { | ||
if (category.getCode().equals(code)) { | ||
return category; | ||
} | ||
} | ||
|
||
throw new IllegalArgumentException("Invalid category code: " + code); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/danpoong/onchung/domain/policy/repository/PolicyRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.danpoong.onchung.domain.policy.repository; | ||
|
||
import com.danpoong.onchung.domain.policy.domain.Policy; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface PolicyRepository extends JpaRepository<Policy, Long> { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
src/main/java/com/danpoong/onchung/global/csv/service/CsvService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package com.danpoong.onchung.global.csv.service; | ||
|
||
import com.danpoong.onchung.domain.policy.domain.FilteringDetails; | ||
import com.danpoong.onchung.domain.policy.domain.Policy; | ||
import com.danpoong.onchung.domain.policy.repository.PolicyRepository; | ||
import com.danpoong.onchung.domain.public_office.domain.PublicOffice; | ||
import com.danpoong.onchung.domain.public_office.repository.PublicOfficeRepository; | ||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.poi.ss.usermodel.*; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class CsvService { | ||
private final PolicyRepository policyRepository; | ||
private final PublicOfficeRepository publicOfficeRepository; | ||
|
||
@Transactional | ||
public void loadDataFromCSV(MultipartFile file) { | ||
List<Policy> policyList = new ArrayList<>(); | ||
|
||
try (InputStream inputStream = file.getInputStream(); | ||
Workbook workbook = WorkbookFactory.create(inputStream)) { | ||
|
||
Sheet sheet = workbook.getSheetAt(0); | ||
boolean isFirstRow = true; | ||
|
||
for (Row row : sheet) { | ||
if (isFirstRow) { | ||
isFirstRow = false; | ||
continue; | ||
} | ||
|
||
List<PublicOffice> publicOffices = extractPublicOffices(row); | ||
FilteringDetails filteringDetails = createFilteringDetails(row); | ||
Policy policy = createPolicy(row, publicOffices, filteringDetails); | ||
|
||
for (PublicOffice publicOffice : publicOffices) { | ||
publicOffice.getPolicies().add(policy); | ||
} | ||
|
||
policyList.add(policy); | ||
} | ||
|
||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
if (!policyList.isEmpty()) { | ||
policyRepository.saveAll(policyList); | ||
} | ||
} | ||
} | ||
|
||
private List<PublicOffice> extractPublicOffices(Row row) { | ||
List<PublicOffice> publicOffices = new ArrayList<>(); | ||
String cellValue = getCellValue(row, 28); | ||
String[] values = cellValue.contains(",") ? cellValue.split(",") : new String[]{cellValue}; | ||
|
||
for (String value : values) { | ||
PublicOffice publicOffice = publicOfficeRepository.findByName(value) | ||
.orElseGet(() -> { | ||
PublicOffice newPublicOffice = PublicOffice.builder() | ||
.name(value) | ||
.build(); | ||
|
||
return publicOfficeRepository.save(newPublicOffice); | ||
}); | ||
|
||
publicOffices.add(publicOffice); | ||
} | ||
|
||
return publicOffices; | ||
} | ||
|
||
private String getPhoneNumber(Row row) { | ||
return !getCellValue(row, 30).isEmpty() && getCellValue(row, 30).contains("-") | ||
? getCellValue(row, 30) | ||
: getCellValue(row, 27); | ||
} | ||
|
||
private FilteringDetails createFilteringDetails(Row row) { | ||
return FilteringDetails.builder() | ||
.ageInfo(getCellValue(row, 11)) | ||
.employmentStatus(getCellValue(row, 13)) | ||
.specializationField(getCellValue(row, 14)) | ||
.educationRequirement(getCellValue(row, 15)) | ||
.build(); | ||
} | ||
|
||
private Policy createPolicy(Row row, List<PublicOffice> publicOffices, FilteringDetails filteringDetails) { | ||
return Policy.builder() | ||
.name(getCellValue(row, 4)) | ||
.introduction(getCellValue(row, 5)) | ||
.supportDetails(getCellValue(row, 6)) | ||
.supportScale(getCellValue(row, 7)) | ||
.supportTarget(getSupportTarget(row)) | ||
.applicationPeriod(getCellValue(row, 8)) | ||
.applicationProcedure(getCellValue(row, 19)) | ||
.requiredDocuments(getCellValue(row, 20)) | ||
.applicationSite(getCellValue(row, 22)) | ||
.contactInfo(getPhoneNumber(row)) | ||
.filteringDetails(filteringDetails) | ||
.category(getCellValue(row, 32)) | ||
.publicOffices(publicOffices) | ||
.build(); | ||
} | ||
|
||
private String getSupportTarget(Row row) { | ||
return !getCellValue(row, 16).isEmpty() ? getCellValue(row, 16) : getCellValue(row, 17); | ||
} | ||
|
||
private String getCellValue(Row row, int columnIndex) { | ||
Cell cell = row.getCell(columnIndex); | ||
return cell == null ? "" : cell.toString().trim(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.