Skip to content

Commit

Permalink
Batch-v1.0.5
Browse files Browse the repository at this point in the history
Batch-v1.0.5
  • Loading branch information
ImNM authored Apr 9, 2023
2 parents d4d862d + edd4390 commit 7be6653
Show file tree
Hide file tree
Showing 19 changed files with 435 additions and 440 deletions.
120 changes: 0 additions & 120 deletions DuDoong-Batch/src/main/java/band/gosrock/excel/ExcelOrderHelper.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package band.gosrock.helper;


import band.gosrock.common.annotation.Helper;
import band.gosrock.domain.domains.event.domain.Event;
import band.gosrock.infrastructure.config.s3.S3PrivateFileService;
import band.gosrock.infrastructure.config.ses.AwsSesUtils;
import band.gosrock.infrastructure.config.ses.RawEmailAttachmentDto;
import band.gosrock.infrastructure.config.ses.SendRawEmailDto;
import javax.mail.MessagingException;
import lombok.RequiredArgsConstructor;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;

@Helper
@RequiredArgsConstructor
public class SettlementEmailHelper {
private final SpringTemplateEngine templateEngine;

private final S3PrivateFileService s3PrivateFileService;
private final AwsSesUtils awsSesUtils;

private RawEmailAttachmentDto getSettlementPdfAttachment(Event event) {
return RawEmailAttachmentDto.builder()
.fileName(event.getEventName() + "_정산서.pdf")
.fileBytes(s3PrivateFileService.downloadEventSettlementPdf(event.getId()))
.type("application/pdf")
.build();
}

private RawEmailAttachmentDto getOrderListExcelAttachment(Event event) {
return RawEmailAttachmentDto.builder()
.fileName(event.getEventName() + "_주문목록.xlsx")
.fileBytes(s3PrivateFileService.downloadEventOrdersExcel(event.getId()))
.type("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
.build();
}

public void sendToAdmin(Event event) throws MessagingException {
SendRawEmailDto sendRawEmailDto =
SendRawEmailDto.builder()
.bodyHtml(templateEngine.process("eventSettlement", new Context()))
.recipient("support@dudoong.com")
.subject(event.getEventName() + "공연 정산서 어드민 발송 ( 관리자용 )")
.build();

sendRawEmailDto.addEmailAttachments(getSettlementPdfAttachment(event));
sendRawEmailDto.addEmailAttachments(getOrderListExcelAttachment(event));
awsSesUtils.sendRawEmails(sendRawEmailDto);
}

public void sendToHost(Event event, String hostUserEmail) throws MessagingException {
SendRawEmailDto sendRawEmailDto =
SendRawEmailDto.builder()
.bodyHtml(templateEngine.process("eventSettlement", new Context()))
.recipient(hostUserEmail)
.subject(event.getEventName() + "공연 정산관련 안내")
.build();

sendRawEmailDto.addEmailAttachments(getSettlementPdfAttachment(event));
sendRawEmailDto.addEmailAttachments(getOrderListExcelAttachment(event));
awsSesUtils.sendRawEmails(sendRawEmailDto);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package band.gosrock.helper;


import band.gosrock.common.annotation.Helper;
import band.gosrock.domain.common.vo.Money;
import band.gosrock.domain.domains.event.domain.Event;
import band.gosrock.domain.domains.settlement.domain.EventSettlement;
import band.gosrock.domain.domains.user.domain.User;
import band.gosrock.dto.SettlementPDFDto;
import band.gosrock.infrastructure.config.pdf.PdfRender;
import band.gosrock.infrastructure.config.s3.S3PrivateFileService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lowagie.text.DocumentException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;

@Helper
@RequiredArgsConstructor
public class SettlementPdfHelper {
private final PdfRender pdfRender;

private final ObjectMapper objectMapper;

private final SpringTemplateEngine templateEngine;

private final S3PrivateFileService s3PrivateFileUploadService;

public void uploadPdfToS3(Event event, EventSettlement eventSettlement, User masterUser)
throws DocumentException, IOException {
SettlementPDFDto settlementPDFDto = getSettlementPDFDto(event, masterUser, eventSettlement);
// 정산 관련 타임리프 파일.
String html = templateEngine.process("settlement", getPdfHtmlContext(settlementPDFDto));
ByteArrayOutputStream outputStream = pdfRender.generatePdfFromHtml(html);
s3PrivateFileUploadService.eventSettlementPdfUpload(event.getId(), outputStream);
}

private Context getPdfHtmlContext(SettlementPDFDto settlementPDFDto) {
Map result = objectMapper.convertValue(settlementPDFDto, Map.class);

Context context = new Context(null, result);
context.setVariable("settlementAt", settlementPDFDto.getSettlementAt());
context.setVariable("now", settlementPDFDto.getNow());
return context;
}

private SettlementPDFDto getSettlementPDFDto(
Event event, User masterUser, EventSettlement eventSettlement) {
return SettlementPDFDto.builder()
.eventTitle(event.getEventBasic().getName())
.hostName(masterUser.getProfile().getName())
.settlementAt(event.getEndAt().plusDays(6L))
.dudoongTicketAmount(eventSettlement.getDudoongAmount().toString())
.pgTicketAmount(eventSettlement.getPaymentAmount().toString())
.totalAmount(eventSettlement.getTotalSalesAmount().toString())
// 초기 두둥 자체 수수료 없음.
.dudoongFee(Money.ZERO.toString())
.pgFee(eventSettlement.getPgFee().toString())
.totalFee(eventSettlement.getPgFee().toString())
.totalFeeVat(eventSettlement.getPgFeeVat().toString())
.totalSettlement(eventSettlement.getTotalAmount().toString())
.now(LocalDateTime.now())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package band.gosrock.excel;
package band.gosrock.helper.excel;


import band.gosrock.domain.common.vo.Money;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package band.gosrock.helper.excel;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Component;

@Component
public class ExcelOrderHelper {

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

public ByteArrayOutputStream execute(List<ExcelOrderDto> excelOrders) {
try (Workbook workbook = new XSSFWorkbook()) {
Field[] declaredFields = ExcelOrderDto.class.getDeclaredFields();

List<String> fieldNames = Arrays.stream(declaredFields).map(Field::getName).toList();
Sheet sheet = workbook.createSheet("orderList");
sheet.setColumnWidth(0, 6000);
sheet.setColumnWidth(1, 6000);
sheet.setColumnWidth(2, 6000);
sheet.setColumnWidth(3, 6000);
sheet.setColumnWidth(4, 6000);
sheet.setColumnWidth(5, 6000);
sheet.setColumnWidth(6, 6000);
sheet.setColumnWidth(7, 6000);
sheet.setColumnWidth(8, 6000);
// create header
Row header = sheet.createRow(0);
CellStyle headerStyle = workbook.createCellStyle();
headerStyle.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
XSSFFont font = ((XSSFWorkbook) workbook).createFont();
font.setFontName("Arial");
font.setFontHeightInPoints((short) 16);
font.setBold(true);
headerStyle.setFont(font);

Cell headerCell0 = header.createCell(0);
headerCell0.setCellValue("주문번호");
headerCell0.setCellStyle(headerStyle);

Cell headerCell1 = header.createCell(1);
headerCell1.setCellValue("주문 방식");
headerCell1.setCellStyle(headerStyle);

Cell headerCell2 = header.createCell(2);
headerCell2.setCellValue("주문 상태");
headerCell2.setCellStyle(headerStyle);

Cell headerCell3 = header.createCell(3);
headerCell3.setCellValue("주문 이름");
headerCell3.setCellStyle(headerStyle);

Cell headerCell4 = header.createCell(4);
headerCell4.setCellValue("주문자 아이디");
headerCell4.setCellStyle(headerStyle);

Cell headerCell5 = header.createCell(5);
headerCell5.setCellValue("총액");
headerCell5.setCellStyle(headerStyle);

Cell headerCell6 = header.createCell(6);
headerCell6.setCellValue("티켓 수량");
headerCell6.setCellStyle(headerStyle);

Cell headerCell7 = header.createCell(7);
headerCell7.setCellValue("생성 일시");
headerCell7.setCellStyle(headerStyle);

Cell headerCell8 = header.createCell(8);
headerCell8.setCellValue("환불 일시");
headerCell8.setCellStyle(headerStyle);

CellStyle style = workbook.createCellStyle();
style.setWrapText(true);
IntStream.range(0, excelOrders.size())
.forEach(
idx -> {
Row row = sheet.createRow(idx + 1);
ExcelOrderDto excelOrderDto = excelOrders.get(idx);
row.createCell(0).setCellValue(excelOrderDto.getOrderNo());

row.createCell(1)
.setCellValue(excelOrderDto.getOrderMethod().getKr());

row.createCell(2)
.setCellValue(excelOrderDto.getOrderStatus().getKr());

row.createCell(3).setCellValue(excelOrderDto.getOrderName());

row.createCell(4).setCellValue(excelOrderDto.getUserId());

row.createCell(5)
.setCellValue(excelOrderDto.getAmount().toString());

row.createCell(6).setCellValue(excelOrderDto.getQuantity());

row.createCell(7)
.setCellValue(
excelOrderDto.getCreatedAt().format(formatter));

LocalDateTime refundAt = excelOrderDto.getRefundAt();
row.createCell(8)
.setCellValue(
refundAt != null
? refundAt.format(formatter)
: null);
});
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
workbook.write(outputStream);
workbook.close();
return outputStream;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Loading

0 comments on commit 7be6653

Please sign in to comment.