[BO] 임가현황 - 엑셀 다운로드 작업 중 (2)
This commit is contained in:
@@ -6,7 +6,7 @@ import lombok.RequiredArgsConstructor;
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public enum UsageType {
|
||||
MOBILE("모바일"),
|
||||
MOBILE("모바일웹"),
|
||||
PAPER("지류");
|
||||
private final String desc;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
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.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.ports.GetHouseholdsExcelPortIn;
|
||||
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.application.domain.GetHouseholdsStatus;
|
||||
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.Map;
|
||||
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.Pageable;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
class GetHouseholdsExcelController {
|
||||
|
||||
private final GetHouseholdsStatusPortIn getHouseholdsStatusPortIn;
|
||||
private final GetHouseholdsExcelPortIn getDailyInfoExcelPortIn;
|
||||
|
||||
/**
|
||||
* 엑셀 다운로드 응답용 데이터 클래스.
|
||||
* - file: 엑셀 파일 바이트 배열
|
||||
* - encodedFileName: Content-Disposition에 들어갈 인코딩된 파일명
|
||||
*/
|
||||
public record ExcelResult(byte[] excel, String encodedFileName) {}
|
||||
|
||||
@GetMapping(path = "/api/excel/household")
|
||||
ResponseEntity<byte[]> operate(Pageable pageable, GetHouseholdsExcelReqVO reqVO) {
|
||||
|
||||
@@ -51,34 +74,229 @@ class GetHouseholdsExcelController {
|
||||
reqVO.getUserRegionMainCode(),
|
||||
Boolean.FALSE)); // 임가 전체 목록을 가져온다.
|
||||
|
||||
// 임가의 seq 값만 추출
|
||||
List<Long> householdSeqList =
|
||||
households.getContent().stream()
|
||||
.map(GetHouseholdsStatus::getHouseholdSeq)
|
||||
.toList();
|
||||
|
||||
switch (reqVO.getTemplateType()) {
|
||||
case HOUSEHOLD -> { // 임가 목록 리스트
|
||||
List<GetHouseholds> excelHouseholds = getDailyInfoExcelPortIn.households(
|
||||
new GetHouseholdsExcelCmd(householdSeqList, reqVO.getRecordMonth()));
|
||||
}
|
||||
case DAILY_STATUS -> { // 일계부 등록 현황
|
||||
List<GetDailyRecords> excelDailyRecords = getDailyInfoExcelPortIn.dailyRecords(
|
||||
new GetHouseholdsExcelCmd(householdSeqList, reqVO.getRecordMonth()));
|
||||
}
|
||||
case VISIT -> { // 임가방문정보
|
||||
List<GetVisitInfos> excelVisitInfos = getDailyInfoExcelPortIn.visitInfos(
|
||||
new GetHouseholdsExcelCmd(householdSeqList, reqVO.getRecordMonth()));
|
||||
}
|
||||
case DAILY_RECORD -> { // 일계부 등록 데이터
|
||||
// TODO : 일계부 등록 데이터는 항목별로 시트를 나누고 파일 하나로 다운로드
|
||||
}
|
||||
ExcelResult result =
|
||||
switch (reqVO.getTemplateType()) {
|
||||
case HOUSEHOLD -> // 임가 목록
|
||||
householdExcel(
|
||||
getDailyInfoExcelPortIn.households(
|
||||
new GetHouseholdsExcelCmd(householdSeqList, reqVO.getRecordMonth()))
|
||||
);
|
||||
case DAILY_STATUS -> // 일계부 등록 현황
|
||||
dailyStatusExcel(
|
||||
getDailyInfoExcelPortIn.dailyRecords(
|
||||
new GetHouseholdsExcelCmd(householdSeqList, reqVO.getRecordMonth()))
|
||||
);
|
||||
case VISIT -> // 임가방문정보
|
||||
visitExcel(
|
||||
getDailyInfoExcelPortIn.visitInfos(
|
||||
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()
|
||||
// .contentType(MediaType.APPLICATION_OCTET_STREAM)
|
||||
// .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename*=UTF-8''" + encodedFileName)
|
||||
// .body(excel);
|
||||
return ResponseEntity.ok()
|
||||
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename*=UTF-8''" + result.encodedFileName)
|
||||
.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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user