[BO] 임가현황 - 엑셀 다운로드 작업 중 (2)
This commit is contained in:
@@ -6,7 +6,7 @@ import lombok.RequiredArgsConstructor;
|
|||||||
@Getter
|
@Getter
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public enum UsageType {
|
public enum UsageType {
|
||||||
MOBILE("모바일"),
|
MOBILE("모바일웹"),
|
||||||
PAPER("지류");
|
PAPER("지류");
|
||||||
private final String desc;
|
private final String desc;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
package app.module.admin.usecase.household.excelDownload.getHouseholdsExcel.adapters.web;
|
package app.module.admin.usecase.household.excelDownload.getHouseholdsExcel.adapters.web;
|
||||||
|
|
||||||
|
import app.module.admin.config.application.enums.ManagementType;
|
||||||
|
import app.module.admin.config.application.enums.UsageType;
|
||||||
import app.module.admin.config.application.util.AdminUtil;
|
import app.module.admin.config.application.util.AdminUtil;
|
||||||
|
import app.module.admin.config.application.util.ExcelWriterUtil;
|
||||||
|
import app.module.admin.config.application.util.model.ExcelSheetDto;
|
||||||
import app.module.admin.usecase.household.excelDownload.getHouseholdsExcel.adapters.web.model.GetHouseholdsExcelReqVO;
|
import app.module.admin.usecase.household.excelDownload.getHouseholdsExcel.adapters.web.model.GetHouseholdsExcelReqVO;
|
||||||
import app.module.admin.usecase.household.excelDownload.getHouseholdsExcel.ports.GetHouseholdsExcelPortIn;
|
import app.module.admin.usecase.household.excelDownload.getHouseholdsExcel.ports.GetHouseholdsExcelPortIn;
|
||||||
import app.module.admin.usecase.household.excelDownload.getHouseholdsExcel.ports.application.domain.GetHouseholdsExcelCmd;
|
import app.module.admin.usecase.household.excelDownload.getHouseholdsExcel.ports.application.domain.GetHouseholdsExcelCmd;
|
||||||
@@ -10,21 +14,40 @@ import app.module.admin.usecase.household.excelDownload.getHouseholdsExcel.ports
|
|||||||
import app.module.admin.usecase.household.getHouseholdsStatus.ports.GetHouseholdsStatusPortIn;
|
import app.module.admin.usecase.household.getHouseholdsStatus.ports.GetHouseholdsStatusPortIn;
|
||||||
import app.module.admin.usecase.household.getHouseholdsStatus.ports.application.domain.GetHouseholdsStatus;
|
import app.module.admin.usecase.household.getHouseholdsStatus.ports.application.domain.GetHouseholdsStatus;
|
||||||
import app.module.admin.usecase.household.getHouseholdsStatus.ports.application.domain.GetHouseholdsStatusCmd;
|
import app.module.admin.usecase.household.getHouseholdsStatus.ports.application.domain.GetHouseholdsStatusCmd;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.apache.poi.ss.usermodel.IndexedColors;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
|
@Slf4j
|
||||||
class GetHouseholdsExcelController {
|
class GetHouseholdsExcelController {
|
||||||
|
|
||||||
private final GetHouseholdsStatusPortIn getHouseholdsStatusPortIn;
|
private final GetHouseholdsStatusPortIn getHouseholdsStatusPortIn;
|
||||||
private final GetHouseholdsExcelPortIn getDailyInfoExcelPortIn;
|
private final GetHouseholdsExcelPortIn getDailyInfoExcelPortIn;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 엑셀 다운로드 응답용 데이터 클래스.
|
||||||
|
* - file: 엑셀 파일 바이트 배열
|
||||||
|
* - encodedFileName: Content-Disposition에 들어갈 인코딩된 파일명
|
||||||
|
*/
|
||||||
|
public record ExcelResult(byte[] excel, String encodedFileName) {}
|
||||||
|
|
||||||
@GetMapping(path = "/api/excel/household")
|
@GetMapping(path = "/api/excel/household")
|
||||||
ResponseEntity<byte[]> operate(Pageable pageable, GetHouseholdsExcelReqVO reqVO) {
|
ResponseEntity<byte[]> operate(Pageable pageable, GetHouseholdsExcelReqVO reqVO) {
|
||||||
|
|
||||||
@@ -51,34 +74,229 @@ class GetHouseholdsExcelController {
|
|||||||
reqVO.getUserRegionMainCode(),
|
reqVO.getUserRegionMainCode(),
|
||||||
Boolean.FALSE)); // 임가 전체 목록을 가져온다.
|
Boolean.FALSE)); // 임가 전체 목록을 가져온다.
|
||||||
|
|
||||||
|
// 임가의 seq 값만 추출
|
||||||
List<Long> householdSeqList =
|
List<Long> householdSeqList =
|
||||||
households.getContent().stream()
|
households.getContent().stream()
|
||||||
.map(GetHouseholdsStatus::getHouseholdSeq)
|
.map(GetHouseholdsStatus::getHouseholdSeq)
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
|
ExcelResult result =
|
||||||
switch (reqVO.getTemplateType()) {
|
switch (reqVO.getTemplateType()) {
|
||||||
case HOUSEHOLD -> { // 임가 목록 리스트
|
case HOUSEHOLD -> // 임가 목록
|
||||||
List<GetHouseholds> excelHouseholds = getDailyInfoExcelPortIn.households(
|
householdExcel(
|
||||||
new GetHouseholdsExcelCmd(householdSeqList, reqVO.getRecordMonth()));
|
getDailyInfoExcelPortIn.households(
|
||||||
}
|
new GetHouseholdsExcelCmd(householdSeqList, reqVO.getRecordMonth()))
|
||||||
case DAILY_STATUS -> { // 일계부 등록 현황
|
);
|
||||||
List<GetDailyRecords> excelDailyRecords = getDailyInfoExcelPortIn.dailyRecords(
|
case DAILY_STATUS -> // 일계부 등록 현황
|
||||||
new GetHouseholdsExcelCmd(householdSeqList, reqVO.getRecordMonth()));
|
dailyStatusExcel(
|
||||||
}
|
getDailyInfoExcelPortIn.dailyRecords(
|
||||||
case VISIT -> { // 임가방문정보
|
new GetHouseholdsExcelCmd(householdSeqList, reqVO.getRecordMonth()))
|
||||||
List<GetVisitInfos> excelVisitInfos = getDailyInfoExcelPortIn.visitInfos(
|
);
|
||||||
new GetHouseholdsExcelCmd(householdSeqList, reqVO.getRecordMonth()));
|
case VISIT -> // 임가방문정보
|
||||||
}
|
visitExcel(
|
||||||
case DAILY_RECORD -> { // 일계부 등록 데이터
|
getDailyInfoExcelPortIn.visitInfos(
|
||||||
// TODO : 일계부 등록 데이터는 항목별로 시트를 나누고 파일 하나로 다운로드
|
new GetHouseholdsExcelCmd(householdSeqList, reqVO.getRecordMonth()))
|
||||||
}
|
);
|
||||||
|
case DAILY_RECORD -> // 일계부 등록 데이터
|
||||||
|
// TODO : 추가하기
|
||||||
|
null;
|
||||||
|
default -> null;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (result == null) {
|
||||||
|
log.warn("[엑셀 다운로드 실패] 템플릿 타입 '{}' 에 대한 처리 결과가 null입니다.", reqVO.getTemplateType());
|
||||||
|
return ResponseEntity.badRequest().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
// return ResponseEntity.ok()
|
return ResponseEntity.ok()
|
||||||
// .contentType(MediaType.APPLICATION_OCTET_STREAM)
|
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
||||||
// .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename*=UTF-8''" + encodedFileName)
|
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename*=UTF-8''" + result.encodedFileName)
|
||||||
// .body(excel);
|
.body(result.excel);
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
/**
|
||||||
|
* 엑셀 다운로드 템플릿 - 임가목록
|
||||||
|
* */
|
||||||
|
private ExcelResult householdExcel(List<GetHouseholds> items) {
|
||||||
|
|
||||||
|
List<List<String>> rows = new ArrayList<>();
|
||||||
|
for(GetHouseholds item : items) {
|
||||||
|
rows.add(List.of(
|
||||||
|
item.getHouseholdNum(),
|
||||||
|
item.getHouseholdName(),
|
||||||
|
StringUtils.defaultString(item.getHouseholdAge()),
|
||||||
|
StringUtils.defaultString(item.getHouseholdPhone()),
|
||||||
|
StringUtils.defaultString(item.getRegionMainCodeText()),
|
||||||
|
StringUtils.defaultString(item.getRegionSubCodeText()),
|
||||||
|
StringUtils.defaultString(item.getRegionDetailCodeText()),
|
||||||
|
StringUtils.defaultString(item.getAddress()),
|
||||||
|
StringUtils.defaultString(item.getIndustryLargeCodeText()),
|
||||||
|
StringUtils.defaultString(item.getIndustryMiddleCodeText()),
|
||||||
|
StringUtils.defaultString(item.getIndustrySubMiddle()),
|
||||||
|
StringUtils.defaultString(item.getIndustryDetail()),
|
||||||
|
StringUtils.defaultString(item.getResearcherName()),
|
||||||
|
StringUtils.defaultString(item.getResearcherPhone()),
|
||||||
|
StringUtils.defaultString(item.getCompletionRate()),
|
||||||
|
StringUtils.defaultString(item.getFirstVisitDate()),
|
||||||
|
StringUtils.defaultString(item.getSecondVisitDate()),
|
||||||
|
StringUtils.defaultString(item.getGiftSend()),
|
||||||
|
item.getUsageType() != null ? UsageType.valueOf(item.getUsageType()).getDesc() : "",
|
||||||
|
StringUtils.defaultString(item.getIsPanel())
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 시트 1: 임가 목록
|
||||||
|
ExcelSheetDto sheet1 = new ExcelSheetDto("임가목록",
|
||||||
|
List.of(
|
||||||
|
"임가번호",
|
||||||
|
"경영주 성명",
|
||||||
|
"연령",
|
||||||
|
"연락처",
|
||||||
|
"실사",
|
||||||
|
"지역",
|
||||||
|
"세부지역",
|
||||||
|
"전체 주소",
|
||||||
|
"업종_대분류",
|
||||||
|
"업종_중분류1",
|
||||||
|
"업종_중분류2",
|
||||||
|
"규모/세부품목",
|
||||||
|
"조사원",
|
||||||
|
"조사원 연락처",
|
||||||
|
"당월 일계부 현황",
|
||||||
|
"당월 1차 임가방문",
|
||||||
|
"당월 2차 임가방문",
|
||||||
|
"답례품 전달",
|
||||||
|
"이용 구분",
|
||||||
|
"유지여부"
|
||||||
|
),
|
||||||
|
rows,
|
||||||
|
null,
|
||||||
|
Map.of(0, IndexedColors.LIGHT_YELLOW));
|
||||||
|
|
||||||
|
List<ExcelSheetDto> sheets = List.of(sheet1);
|
||||||
|
byte[] excel = ExcelWriterUtil.writeExcel(sheets);
|
||||||
|
|
||||||
|
String rawFileName = "임가목록_" + LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")) + ".xlsx";
|
||||||
|
String encodedFileName = URLEncoder.encode(rawFileName, StandardCharsets.UTF_8).replaceAll("\\+", "%20");
|
||||||
|
|
||||||
|
return new ExcelResult(excel, encodedFileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 엑셀 다운로드 템플릿 - 일계부등록현황
|
||||||
|
* */
|
||||||
|
private ExcelResult dailyStatusExcel(List<GetDailyRecords> items) {
|
||||||
|
|
||||||
|
List<List<String>> rows = new ArrayList<>();
|
||||||
|
for(GetDailyRecords item : items) {
|
||||||
|
rows.add(List.of(
|
||||||
|
StringUtils.defaultString(item.getHouseholdNum()),
|
||||||
|
StringUtils.defaultString(item.getOwnerName()),
|
||||||
|
StringUtils.defaultString(item.getOwnerAge()),
|
||||||
|
StringUtils.defaultString(item.getHouseholdPhone()),
|
||||||
|
StringUtils.defaultString(item.getRegionMainCodeText()),
|
||||||
|
StringUtils.defaultString(item.getRegionSubCodeText()),
|
||||||
|
StringUtils.defaultString(item.getRegionDetailCodeText()),
|
||||||
|
StringUtils.defaultString(item.getResearcherName()),
|
||||||
|
StringUtils.defaultString(item.getResearcherPhone()),
|
||||||
|
StringUtils.defaultString(item.getDayGroup()),
|
||||||
|
StringUtils.defaultString(item.getAllStatusType().getDesc()),
|
||||||
|
StringUtils.defaultString(item.getIncomeStatus()),
|
||||||
|
StringUtils.defaultString(item.getWorkStatus()),
|
||||||
|
StringUtils.defaultString(item.getExpenseStatus()),
|
||||||
|
StringUtils.defaultString(item.getWorkStatus()),
|
||||||
|
StringUtils.defaultString(item.getForestryStatus()),
|
||||||
|
StringUtils.defaultString(item.getFarmingStatus())
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 시트 1: 일계부 등록 현황
|
||||||
|
ExcelSheetDto sheet1 = new ExcelSheetDto("일계부등록현황",
|
||||||
|
List.of(
|
||||||
|
"임가번호",
|
||||||
|
"경영주 성명",
|
||||||
|
"연령",
|
||||||
|
"연락처",
|
||||||
|
"실사",
|
||||||
|
"세부지역",
|
||||||
|
"조사원",
|
||||||
|
"조사원 연락처",
|
||||||
|
"입력 날짜",
|
||||||
|
"완료상태",
|
||||||
|
"수입",
|
||||||
|
"노동투입",
|
||||||
|
"지출",
|
||||||
|
"동력사용시간",
|
||||||
|
"임업 자가소비량",
|
||||||
|
"농업 자가소비량"
|
||||||
|
),
|
||||||
|
rows,
|
||||||
|
null,
|
||||||
|
Map.of(0, IndexedColors.LIGHT_YELLOW));
|
||||||
|
|
||||||
|
List<ExcelSheetDto> sheets = List.of(sheet1);
|
||||||
|
byte[] excel = ExcelWriterUtil.writeExcel(sheets);
|
||||||
|
|
||||||
|
String rawFileName = "일계부등록현황_" + LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")) + ".xlsx";
|
||||||
|
String encodedFileName = URLEncoder.encode(rawFileName, StandardCharsets.UTF_8).replaceAll("\\+", "%20");
|
||||||
|
|
||||||
|
return new ExcelResult(excel, encodedFileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 엑셀 다운로드 템플릿 - 임가방문정보
|
||||||
|
* */
|
||||||
|
private ExcelResult visitExcel(List<GetVisitInfos> items) {
|
||||||
|
|
||||||
|
List<List<String>> rows = new ArrayList<>();
|
||||||
|
for(GetVisitInfos item : items) {
|
||||||
|
rows.add(List.of(
|
||||||
|
StringUtils.defaultString(item.getHouseholdNum()),
|
||||||
|
StringUtils.defaultString(item.getOwnerName()),
|
||||||
|
StringUtils.defaultString(item.getOwnerAge()),
|
||||||
|
StringUtils.defaultString(item.getHouseholdPhone()),
|
||||||
|
StringUtils.defaultString(item.getRegionMainCodeText()),
|
||||||
|
StringUtils.defaultString(item.getRegionSubCodeText()),
|
||||||
|
StringUtils.defaultString(item.getRegionDetailCodeText()),
|
||||||
|
StringUtils.defaultString(item.getResearcherName()),
|
||||||
|
StringUtils.defaultString(item.getResearcherPhone()),
|
||||||
|
StringUtils.defaultString(item.getVisitDate()),
|
||||||
|
item.getManagementType() != null ? ManagementType.valueOf(item.getManagementType()).getDesc() : "",
|
||||||
|
StringUtils.defaultString(item.getContent()),
|
||||||
|
StringUtils.defaultString(item.getUpdatedBy()),
|
||||||
|
StringUtils.defaultString(item.getUpdatedAt()),
|
||||||
|
item.getIsGiftSent() ? "전달 완료" : "-"
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 시트 1: 임가방문정보
|
||||||
|
ExcelSheetDto sheet1 = new ExcelSheetDto("임가방문정보",
|
||||||
|
List.of(
|
||||||
|
"임가번호",
|
||||||
|
"경영주 성명",
|
||||||
|
"연령",
|
||||||
|
"연락처",
|
||||||
|
"실사",
|
||||||
|
"세부지역",
|
||||||
|
"조사원",
|
||||||
|
"조사원 연락처",
|
||||||
|
"방문일",
|
||||||
|
"관리분야",
|
||||||
|
"내용",
|
||||||
|
"최종 작성자",
|
||||||
|
"작성일",
|
||||||
|
"답례품 전달"
|
||||||
|
),
|
||||||
|
rows,
|
||||||
|
null,
|
||||||
|
Map.of(0, IndexedColors.LIGHT_YELLOW));
|
||||||
|
|
||||||
|
List<ExcelSheetDto> sheets = List.of(sheet1);
|
||||||
|
byte[] excel = ExcelWriterUtil.writeExcel(sheets);
|
||||||
|
|
||||||
|
String rawFileName = "임가방문정보_" + LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")) + ".xlsx";
|
||||||
|
String encodedFileName = URLEncoder.encode(rawFileName, StandardCharsets.UTF_8).replaceAll("\\+", "%20");
|
||||||
|
|
||||||
|
return new ExcelResult(excel, encodedFileName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5,7 +5,7 @@
|
|||||||
namespace="app.module.admin.usecase.household.excelDownload.getHouseholdsExcel.adapters.persistence.GetHouseholdsExcelMapper">
|
namespace="app.module.admin.usecase.household.excelDownload.getHouseholdsExcel.adapters.persistence.GetHouseholdsExcelMapper">
|
||||||
<select id="households"
|
<select id="households"
|
||||||
resultType="app.module.admin.usecase.household.excelDownload.getHouseholdsExcel.ports.application.domain.GetHouseholds">
|
resultType="app.module.admin.usecase.household.excelDownload.getHouseholdsExcel.ports.application.domain.GetHouseholds">
|
||||||
/* 엑셀 다운로드 - 일계부 정보 (01. 임가 목록) 조회 */
|
/* 엑셀 다운로드 - 임가 현황 (01. 임가 목록) 조회 */
|
||||||
SELECT
|
SELECT
|
||||||
vha.household_num
|
vha.household_num
|
||||||
, vha.owner_name AS household_name
|
, vha.owner_name AS household_name
|
||||||
@@ -39,7 +39,7 @@
|
|||||||
|
|
||||||
<select id="dailyRecords"
|
<select id="dailyRecords"
|
||||||
resultType="app.module.admin.usecase.household.excelDownload.getHouseholdsExcel.ports.application.domain.GetDailyRecords">
|
resultType="app.module.admin.usecase.household.excelDownload.getHouseholdsExcel.ports.application.domain.GetDailyRecords">
|
||||||
/* 엑셀 다운로드 - 일계부 정보 (02. 일계부 등록 현황) 조회 */
|
/* 엑셀 다운로드 - 임가 현황 (02. 일계부 등록 현황) 조회 */
|
||||||
WITH data_counts AS (
|
WITH data_counts AS (
|
||||||
SELECT
|
SELECT
|
||||||
dr.id,
|
dr.id,
|
||||||
@@ -118,6 +118,7 @@
|
|||||||
|
|
||||||
<select id="visitInfos"
|
<select id="visitInfos"
|
||||||
resultType="app.module.admin.usecase.household.excelDownload.getHouseholdsExcel.ports.application.domain.GetVisitInfos">
|
resultType="app.module.admin.usecase.household.excelDownload.getHouseholdsExcel.ports.application.domain.GetVisitInfos">
|
||||||
|
/* 엑셀 다운로드 - 임가 현황 (03. 임가방문정보) 조회 */
|
||||||
SELECT household_num,
|
SELECT household_num,
|
||||||
owner_name,
|
owner_name,
|
||||||
owner_age,
|
owner_age,
|
||||||
@@ -132,7 +133,7 @@
|
|||||||
content,
|
content,
|
||||||
is_gift_sent,
|
is_gift_sent,
|
||||||
updated_by,
|
updated_by,
|
||||||
updated_at
|
created_at
|
||||||
FROM v_household_all_visit
|
FROM v_household_all_visit
|
||||||
WHERE visit_month = #{recordMonth}
|
WHERE visit_month = #{recordMonth}
|
||||||
AND seq IN
|
AND seq IN
|
||||||
|
|||||||
Reference in New Issue
Block a user