1+ package com .gotogether .global .util ;
2+
3+ import java .io .ByteArrayOutputStream ;
4+ import java .io .IOException ;
5+ import java .util .List ;
6+ import java .util .Map ;
7+ import java .util .Set ;
8+
9+ import org .apache .poi .ss .usermodel .Row ;
10+ import org .apache .poi .ss .usermodel .Sheet ;
11+ import org .apache .poi .ss .usermodel .Workbook ;
12+ import org .apache .poi .xssf .usermodel .XSSFWorkbook ;
13+
14+ import com .gotogether .domain .order .entity .Order ;
15+ import com .gotogether .domain .ticketoptionanswer .entity .TicketOptionAnswer ;
16+
17+ import lombok .AccessLevel ;
18+ import lombok .NoArgsConstructor ;
19+
20+ @ NoArgsConstructor (access = AccessLevel .PRIVATE )
21+ public class ExcelGenerator {
22+
23+ private static final String SHEET_NAME = "구매참가자목록" ;
24+ private static final String [] HEADERS = {"이름" , "이메일" , "휴대폰 번호" , "구매 일자" , "티켓 이름" };
25+
26+ public static byte [] generateParticipantExcel (List <Order > orders , Set <String > optionNames ,
27+ java .util .function .Function <Long , List <TicketOptionAnswer >> answerProvider ) {
28+ try (Workbook workbook = new XSSFWorkbook ();
29+ ByteArrayOutputStream out = new ByteArrayOutputStream ()) {
30+
31+ Sheet sheet = workbook .createSheet (SHEET_NAME );
32+
33+ createHeaderRow (sheet , optionNames );
34+ createDataRows (sheet , orders , optionNames , answerProvider );
35+
36+ workbook .write (out );
37+ return out .toByteArray ();
38+ } catch (IOException e ) {
39+ throw new RuntimeException ("엑셀 파일 생성 중 오류 발생: " , e );
40+ }
41+ }
42+
43+ private static void createHeaderRow (Sheet sheet , Set <String > optionNames ) {
44+ Row headerRow = sheet .createRow (0 );
45+ int colIdx = 0 ;
46+
47+ for (String header : HEADERS ) {
48+ headerRow .createCell (colIdx ++).setCellValue (header );
49+ }
50+
51+ for (String optionName : optionNames ) {
52+ headerRow .createCell (colIdx ++).setCellValue (optionName );
53+ }
54+ }
55+
56+ private static void createDataRows (Sheet sheet , List <Order > orders , Set <String > optionNames ,
57+ java .util .function .Function <Long , List <TicketOptionAnswer >> answerProvider ) {
58+ int rowIdx = 1 ;
59+
60+ for (Order order : orders ) {
61+ Row row = sheet .createRow (rowIdx ++);
62+ int cellIdx = 0 ;
63+
64+ row .createCell (cellIdx ++).setCellValue (order .getUser ().getName ());
65+ row .createCell (cellIdx ++).setCellValue (order .getUser ().getEmail ());
66+ row .createCell (cellIdx ++).setCellValue (order .getUser ().getPhoneNumber ());
67+ row .createCell (cellIdx ++).setCellValue (order .getCreatedAt ().toLocalDate ().toString ());
68+ row .createCell (cellIdx ++).setCellValue (order .getTicket ().getName ());
69+
70+ Map <String , String > optionAnswerMap = createOptionAnswerMap (order , answerProvider );
71+ for (String optionName : optionNames ) {
72+ row .createCell (cellIdx ++).setCellValue (optionAnswerMap .getOrDefault (optionName , "" ));
73+ }
74+ }
75+ }
76+
77+ private static Map <String , String > createOptionAnswerMap (Order order ,
78+ java .util .function .Function <Long , List <TicketOptionAnswer >> answerProvider ) {
79+ Map <String , String > optionAnswerMap = new java .util .HashMap <>();
80+
81+ List <TicketOptionAnswer > answers = answerProvider .apply (order .getId ());
82+ for (TicketOptionAnswer answer : answers ) {
83+ String optionName = answer .getTicketOption ().getName ();
84+ String value = getAnswerValue (answer );
85+
86+ if (optionAnswerMap .containsKey (optionName )) {
87+ value = optionAnswerMap .get (optionName ) + ", " + value ;
88+ }
89+ optionAnswerMap .put (optionName , value );
90+ }
91+
92+ return optionAnswerMap ;
93+ }
94+
95+ private static String getAnswerValue (TicketOptionAnswer answer ) {
96+ if (answer .getAnswerText () != null ) {
97+ return answer .getAnswerText ();
98+ }
99+ if (answer .getTicketOptionChoice () != null ) {
100+ return answer .getTicketOptionChoice ().getName ();
101+ }
102+ return "" ;
103+ }
104+ }
0 commit comments