diff --git a/src/main/java/com/kri/notihub/server/dto/report/SendCostByTypeDto.java b/src/main/java/com/kri/notihub/server/dto/report/SendCostByTypeDto.java new file mode 100644 index 0000000..b7715a3 --- /dev/null +++ b/src/main/java/com/kri/notihub/server/dto/report/SendCostByTypeDto.java @@ -0,0 +1,15 @@ +package com.kri.notihub.server.dto.report; + +import java.math.BigDecimal; + +/** + * 요청 서비스 · 인트라넷 프로젝트 · 발송 타입별 발송 비용 집계 (조회 전용 projection) + */ +public record SendCostByTypeDto( + String reqstSvc, + String itrntPrjctCd, + Short sendTy, + Long sendCnt, + BigDecimal totalPrice +) { +} diff --git a/src/main/java/com/kri/notihub/server/dto/report/SendCostSummaryWithItrntPrjctDto.java b/src/main/java/com/kri/notihub/server/dto/report/SendCostSummaryWithItrntPrjctDto.java index 4a6e5d7..d7afc00 100644 --- a/src/main/java/com/kri/notihub/server/dto/report/SendCostSummaryWithItrntPrjctDto.java +++ b/src/main/java/com/kri/notihub/server/dto/report/SendCostSummaryWithItrntPrjctDto.java @@ -1,5 +1,6 @@ package com.kri.notihub.server.dto.report; +import com.kri.notihub.server.common.enums.SendType; import com.kri.notihub.server.common.external.intranet.dto.IntranetProjectBaseInfoDto; import io.swagger.v3.oas.annotations.media.Schema; import java.math.BigDecimal; @@ -14,7 +15,7 @@ import lombok.Setter; @Builder @NoArgsConstructor @AllArgsConstructor -@Schema(description = "인트라넷 프로젝트 정보 포함 발송 비용 요약") +@Schema(description = "인트라넷 프로젝트 정보 포함 발송 비용 요약 (발송 타입별 컬럼)") public class SendCostSummaryWithItrntPrjctDto { @Schema(description = "요청 서비스") @@ -23,38 +24,100 @@ public class SendCostSummaryWithItrntPrjctDto { @Schema(description = "인트라넷 프로젝트 코드") private String itrntPrjctCd; - @Schema(description = "발송 타입") - private Short sendTy; + @Schema(description = "EMAIL 발송 건수") + @Builder.Default + private Long emailCnt = 0L; - @Schema(description = "발송 업체") - private Short sendCorp; + @Schema(description = "SMS 발송 건수") + @Builder.Default + private Long smsCnt = 0L; - @Schema(description = "발송 건수") - private Long sendCnt; + @Schema(description = "LMS 발송 건수") + @Builder.Default + private Long lmsCnt = 0L; - @Schema(description = "단가") - private BigDecimal unitPrice; + @Schema(description = "MMS 발송 건수") + @Builder.Default + private Long mmsCnt = 0L; - @Schema(description = "총 비용") - private BigDecimal totalPrice; + @Schema(description = "알림톡 발송 건수") + @Builder.Default + private Long kkoAtCnt = 0L; + + @Schema(description = "EMAIL 단가") + @Builder.Default + private BigDecimal emailUnitPrice = BigDecimal.ZERO; + + @Schema(description = "SMS 단가") + @Builder.Default + private BigDecimal smsUnitPrice = BigDecimal.ZERO; + + @Schema(description = "LMS 단가") + @Builder.Default + private BigDecimal lmsUnitPrice = BigDecimal.ZERO; + + @Schema(description = "MMS 단가") + @Builder.Default + private BigDecimal mmsUnitPrice = BigDecimal.ZERO; + + @Schema(description = "알림톡 단가") + @Builder.Default + private BigDecimal kkoAtUnitPrice = BigDecimal.ZERO; + + @Schema(description = "EMAIL 비용") + @Builder.Default + private BigDecimal emailTotalPrice = BigDecimal.ZERO; + + @Schema(description = "SMS 비용") + @Builder.Default + private BigDecimal smsTotalPrice = BigDecimal.ZERO; + + @Schema(description = "LMS 비용") + @Builder.Default + private BigDecimal lmsTotalPrice = BigDecimal.ZERO; + + @Schema(description = "MMS 비용") + @Builder.Default + private BigDecimal mmsTotalPrice = BigDecimal.ZERO; + + @Schema(description = "알림톡 비용") + @Builder.Default + private BigDecimal kkoAtTotalPrice = BigDecimal.ZERO; @Schema(description = "인트라넷 프로젝트 정보") private IntranetProjectBaseInfoDto itrntPrjctInfo; - public SendCostSummaryWithItrntPrjctDto( - String reqstSvc, - String itrntPrjctCd, - Short sendTy, - Short sendCorp, - Long sendCnt, - BigDecimal unitPrice - ) { - this.reqstSvc = reqstSvc; - this.itrntPrjctCd = itrntPrjctCd; - this.sendTy = sendTy; - this.sendCorp = sendCorp; - this.sendCnt = sendCnt; - this.unitPrice = unitPrice != null ? unitPrice : BigDecimal.ZERO; - this.totalPrice = this.unitPrice.multiply(BigDecimal.valueOf(sendCnt != null ? sendCnt : 0L)); + /** + * 발송 타입에 해당하는 건수 · 단가 · 비용 컬럼을 설정한다. + */ + public void applySendType(SendType sendType, long sendCnt, BigDecimal unitPrice, + BigDecimal totalPrice) { + switch (sendType) { + case EMAIL -> { + this.emailCnt = sendCnt; + this.emailUnitPrice = unitPrice; + this.emailTotalPrice = totalPrice; + } + case SMS -> { + this.smsCnt = sendCnt; + this.smsUnitPrice = unitPrice; + this.smsTotalPrice = totalPrice; + } + case LMS -> { + this.lmsCnt = sendCnt; + this.lmsUnitPrice = unitPrice; + this.lmsTotalPrice = totalPrice; + } + case MMS -> { + this.mmsCnt = sendCnt; + this.mmsUnitPrice = unitPrice; + this.mmsTotalPrice = totalPrice; + } + case KKO_AT -> { + this.kkoAtCnt = sendCnt; + this.kkoAtUnitPrice = unitPrice; + this.kkoAtTotalPrice = totalPrice; + } + } } } diff --git a/src/main/java/com/kri/notihub/server/dto/report/SendCostSummaryWithItrntPrjctExcelDto.java b/src/main/java/com/kri/notihub/server/dto/report/SendCostSummaryWithItrntPrjctExcelDto.java index ae2bc1f..6ea6678 100644 --- a/src/main/java/com/kri/notihub/server/dto/report/SendCostSummaryWithItrntPrjctExcelDto.java +++ b/src/main/java/com/kri/notihub/server/dto/report/SendCostSummaryWithItrntPrjctExcelDto.java @@ -1,8 +1,6 @@ package com.kri.notihub.server.dto.report; import com.kri.common.feature.excel.ExcelColumn; -import com.kri.notihub.server.common.enums.SendCorpType; -import com.kri.notihub.server.common.enums.SendType; import com.kri.notihub.server.common.external.intranet.dto.IntranetProjectBaseInfoDto; import java.math.BigDecimal; import lombok.Builder; @@ -21,20 +19,50 @@ public record SendCostSummaryWithItrntPrjctExcelDto( @ExcelColumn(header = "연구팀 명", order = 3) String researchTeamName, - @ExcelColumn(header = "발송 타입", order = 4) - String sendTy, + @ExcelColumn(header = "EMAIL 건수", order = 4) + Long emailCnt, - @ExcelColumn(header = "발송 업체", order = 5) - String sendCorp, + @ExcelColumn(header = "SMS 건수", order = 5) + Long smsCnt, - @ExcelColumn(header = "발송 건수", order = 6) - Long sendCnt, + @ExcelColumn(header = "LMS 건수", order = 6) + Long lmsCnt, - @ExcelColumn(header = "단가", order = 7) - BigDecimal unitPrice, + @ExcelColumn(header = "MMS 건수", order = 7) + Long mmsCnt, - @ExcelColumn(header = "총 비용", order = 8) - BigDecimal totalPrice + @ExcelColumn(header = "알림톡 건수", order = 8) + Long kkoAtCnt, + + @ExcelColumn(header = "EMAIL 단가", order = 9) + BigDecimal emailUnitPrice, + + @ExcelColumn(header = "SMS 단가", order = 10) + BigDecimal smsUnitPrice, + + @ExcelColumn(header = "LMS 단가", order = 11) + BigDecimal lmsUnitPrice, + + @ExcelColumn(header = "MMS 단가", order = 12) + BigDecimal mmsUnitPrice, + + @ExcelColumn(header = "알림톡 단가", order = 13) + BigDecimal kkoAtUnitPrice, + + @ExcelColumn(header = "EMAIL 비용", order = 14) + BigDecimal emailTotalPrice, + + @ExcelColumn(header = "SMS 비용", order = 15) + BigDecimal smsTotalPrice, + + @ExcelColumn(header = "LMS 비용", order = 16) + BigDecimal lmsTotalPrice, + + @ExcelColumn(header = "MMS 비용", order = 17) + BigDecimal mmsTotalPrice, + + @ExcelColumn(header = "알림톡 비용", order = 18) + BigDecimal kkoAtTotalPrice ) { public static SendCostSummaryWithItrntPrjctExcelDto from( SendCostSummaryWithItrntPrjctDto source) { @@ -49,33 +77,21 @@ public record SendCostSummaryWithItrntPrjctExcelDto( .itrntPrjctCd(source.getItrntPrjctCd()) .title(projectInfo != null ? projectInfo.title() : null) .researchTeamName(projectInfo != null ? projectInfo.researchTeamName() : null) - .sendTy(getSendTypeLabel(source.getSendTy())) - .sendCorp(getSendCorpLabel(source.getSendCorp())) - .sendCnt(source.getSendCnt()) - .unitPrice(source.getUnitPrice()) - .totalPrice(source.getTotalPrice()) + .emailCnt(source.getEmailCnt()) + .smsCnt(source.getSmsCnt()) + .lmsCnt(source.getLmsCnt()) + .mmsCnt(source.getMmsCnt()) + .kkoAtCnt(source.getKkoAtCnt()) + .emailUnitPrice(source.getEmailUnitPrice()) + .smsUnitPrice(source.getSmsUnitPrice()) + .lmsUnitPrice(source.getLmsUnitPrice()) + .mmsUnitPrice(source.getMmsUnitPrice()) + .kkoAtUnitPrice(source.getKkoAtUnitPrice()) + .emailTotalPrice(source.getEmailTotalPrice()) + .smsTotalPrice(source.getSmsTotalPrice()) + .lmsTotalPrice(source.getLmsTotalPrice()) + .mmsTotalPrice(source.getMmsTotalPrice()) + .kkoAtTotalPrice(source.getKkoAtTotalPrice()) .build(); } - - private static String getSendTypeLabel(Short code) { - if (code == null) { - return null; - } - try { - return SendType.fromCode(code).name(); - } catch (IllegalArgumentException e) { - return code.toString(); - } - } - - private static String getSendCorpLabel(Short code) { - if (code == null) { - return null; - } - try { - return SendCorpType.fromCode(code).name(); - } catch (IllegalArgumentException e) { - return code.toString(); - } - } } diff --git a/src/main/java/com/kri/notihub/server/persistence/repository/SendRepository.java b/src/main/java/com/kri/notihub/server/persistence/repository/SendRepository.java index ba63650..cd4ac65 100644 --- a/src/main/java/com/kri/notihub/server/persistence/repository/SendRepository.java +++ b/src/main/java/com/kri/notihub/server/persistence/repository/SendRepository.java @@ -2,7 +2,7 @@ package com.kri.notihub.server.persistence.repository; import com.kri.notihub.server.common.enums.ChannelType; import com.kri.notihub.server.common.enums.SendType; -import com.kri.notihub.server.dto.report.SendCostSummaryWithItrntPrjctDto; +import com.kri.notihub.server.dto.report.SendCostByTypeDto; import com.kri.notihub.server.dto.report.SendResultLastWithSidDto; import com.kri.notihub.server.dto.report.SendResultSummaryDto; import com.kri.notihub.server.persistence.entity.TSendEntity; @@ -270,13 +270,12 @@ public interface SendRepository extends JpaRepository { ); @Query(""" - select new com.kri.notihub.server.dto.report.SendCostSummaryWithItrntPrjctDto( + select new com.kri.notihub.server.dto.report.SendCostByTypeDto( s.reqstSvc, s.itrntPrjctCd, s.sendTy, - s.sendCorp, count(s), - up.price + sum(up.price) ) from TSendEntity s left join TUnitPriceEntity up @@ -286,10 +285,10 @@ public interface SendRepository extends JpaRepository { where s.sendStatus = 1 and s.sendDt >= :startDt and s.sendDt < :endDt - group by s.reqstSvc, s.itrntPrjctCd, s.sendTy, s.sendCorp, up.price - order by s.reqstSvc, s.itrntPrjctCd, s.sendTy, s.sendCorp + group by s.reqstSvc, s.itrntPrjctCd, s.sendTy + order by s.reqstSvc, s.itrntPrjctCd, s.sendTy """) - List findSendCostSummaryWithItrntPrjct( + List findSendCostSummaryWithItrntPrjct( @Param("startDt") LocalDateTime startDt, @Param("endDt") LocalDateTime endDt ); diff --git a/src/main/java/com/kri/notihub/server/service/EmailResultPoller.java b/src/main/java/com/kri/notihub/server/service/EmailResultPoller.java index c1fe038..5d339ab 100644 --- a/src/main/java/com/kri/notihub/server/service/EmailResultPoller.java +++ b/src/main/java/com/kri/notihub/server/service/EmailResultPoller.java @@ -46,6 +46,10 @@ public class EmailResultPoller { private volatile boolean running = true; private Thread worker; + // false면 워커 스레드 · 기동 시 복구 · 큐 등록을 모두 하지 않는다 (local 등에서 Pringo 조회/DB 갱신 방지) + @Value("${kri.email.result-poll.enabled:true}") + private boolean enabled; + @Value("${kri.email.result-poll.initial-delay-ms:10000}") private long initialDelayMs; @@ -62,7 +66,7 @@ public class EmailResultPoller { private int recoverPageSize; public void enqueue(String returnKey) { - if (returnKey == null || returnKey.isBlank()) { + if (!enabled || returnKey == null || returnKey.isBlank()) { return; } enqueueInternal(returnKey.trim(), initialDelayMs, 0); @@ -104,6 +108,9 @@ public class EmailResultPoller { } private void enqueueRecoverableEmailResults(long delayMs) { + if (!enabled) { + return; + } try { LocalDateTime from = LocalDateTime.now().minusDays(Math.max(1, recoverDays)); Pageable pageable = PageRequest.of(0, Math.max(100, recoverPageSize)); @@ -136,6 +143,10 @@ public class EmailResultPoller { @PostConstruct public void start() { + if (!enabled) { + log.info("EmailResultPoller disabled (kri.email.result-poll.enabled=false)"); + return; + } worker = new Thread(() -> { while (running) { try { diff --git a/src/main/java/com/kri/notihub/server/service/ReportService.java b/src/main/java/com/kri/notihub/server/service/ReportService.java index 600b39a..b816dce 100644 --- a/src/main/java/com/kri/notihub/server/service/ReportService.java +++ b/src/main/java/com/kri/notihub/server/service/ReportService.java @@ -12,6 +12,7 @@ import com.kri.notihub.server.common.external.intranet.IntranetApiClient; import com.kri.notihub.server.common.external.intranet.dto.IntranetProjectBaseInfoDto; import com.kri.notihub.server.dto.report.BulkSendInfoDto; import com.kri.notihub.server.dto.report.ReportExcelResponseDto; +import com.kri.notihub.server.dto.report.SendCostByTypeDto; import com.kri.notihub.server.dto.report.SendCostSummaryRequestDto; import com.kri.notihub.server.dto.report.SendCostSummaryWithItrntPrjctDto; import com.kri.notihub.server.dto.report.SendCostSummaryWithItrntPrjctExcelDto; @@ -33,14 +34,20 @@ import com.kri.notihub.server.persistence.repository.UnitPriceRepository; import java.io.ByteArrayOutputStream; import java.io.OutputStream; import java.math.BigDecimal; +import java.math.RoundingMode; import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Base64; import java.util.Comparator; +import java.util.EnumMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.TreeSet; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -256,12 +263,14 @@ public class ReportService { LocalDateTime startDt = condition.startDt().atStartOfDay(); LocalDateTime endDt = condition.endDt().plusDays(1).atStartOfDay(); - List summaries = + List costsByType = sendRepository.findSendCostSummaryWithItrntPrjct( startDt, endDt ); + List summaries = pivotSendCostBySendType(costsByType); + List itrntPrjctCds = summaries.stream() .map(SendCostSummaryWithItrntPrjctDto::getItrntPrjctCd) .filter(StringUtils::hasText) @@ -283,6 +292,95 @@ public class ReportService { return summaries; } + /** + * 발송 타입별 row를 (요청 서비스, 인트라넷 프로젝트) 1 row로 합치고 발송 타입을 컬럼으로 펼친다. + * 발송 업체는 구분하지 않으며, 단가는 발송 건이 있으면 비용 ÷ 건수(가중 평균), + * 없으면 사용 중 단가가 하나뿐일 때 그 단가를 사용한다. + */ + private List pivotSendCostBySendType( + List costsByType + ) { + Map configuredUnitPriceMap = getConfiguredUnitPriceBySendType(); + Map, SendCostSummaryWithItrntPrjctDto> summaryMap = new LinkedHashMap<>(); + + for (SendCostByTypeDto cost : costsByType) { + SendCostSummaryWithItrntPrjctDto summary = summaryMap.computeIfAbsent( + Arrays.asList(cost.reqstSvc(), cost.itrntPrjctCd()), + key -> SendCostSummaryWithItrntPrjctDto.builder() + .reqstSvc(cost.reqstSvc()) + .itrntPrjctCd(cost.itrntPrjctCd()) + .build() + ); + + SendType sendType = toSendTypeOrNull(cost.sendTy()); + if (sendType == null) { + continue; + } + + long sendCnt = cost.sendCnt() != null ? cost.sendCnt() : 0L; + BigDecimal totalPrice = cost.totalPrice() != null ? cost.totalPrice() : BigDecimal.ZERO; + BigDecimal unitPrice = sendCnt > 0 + ? totalPrice.divide(BigDecimal.valueOf(sendCnt), totalPrice.scale(), RoundingMode.HALF_UP) + : BigDecimal.ZERO; + + summary.applySendType(sendType, sendCnt, unitPrice, totalPrice); + } + + // 발송 건이 없는 타입은 사용 중 단가로 단가 컬럼을 채운다. + summaryMap.values().forEach(summary -> configuredUnitPriceMap.forEach((sendType, price) -> { + if (getSendCnt(summary, sendType) == 0L) { + summary.applySendType(sendType, 0L, price, BigDecimal.ZERO); + } + })); + + return new ArrayList<>(summaryMap.values()); + } + + /** + * 발송 타입별 사용 중 단가. 업체별 단가가 서로 다르면 하나로 정할 수 없으므로 제외한다. + */ + private Map getConfiguredUnitPriceBySendType() { + Map> pricesByType = new EnumMap<>(SendType.class); + for (TUnitPriceEntity price : unitPriceRepository.findByUseTrue()) { + SendType sendType = toSendTypeOrNull(price.getSendTy()); + if (sendType == null || price.getPrice() == null) { + continue; + } + pricesByType.computeIfAbsent(sendType, key -> new TreeSet<>()) + .add(price.getPrice()); + } + + Map result = new EnumMap<>(SendType.class); + pricesByType.forEach((sendType, prices) -> { + if (prices.size() == 1) { + result.put(sendType, prices.iterator().next()); + } + }); + return result; + } + + private long getSendCnt(SendCostSummaryWithItrntPrjctDto summary, SendType sendType) { + Long sendCnt = switch (sendType) { + case EMAIL -> summary.getEmailCnt(); + case SMS -> summary.getSmsCnt(); + case LMS -> summary.getLmsCnt(); + case MMS -> summary.getMmsCnt(); + case KKO_AT -> summary.getKkoAtCnt(); + }; + return sendCnt != null ? sendCnt : 0L; + } + + private SendType toSendTypeOrNull(Short code) { + if (code == null) { + return null; + } + try { + return SendType.fromCode(code); + } catch (IllegalArgumentException e) { + return null; + } + } + public ReportExcelResponseDto getSendCostSummaryWithItrntPrjctExcel( SendCostSummaryRequestDto req ) { diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml index 2833f96..0d24507 100644 --- a/src/main/resources/application-local.yml +++ b/src/main/resources/application-local.yml @@ -13,6 +13,9 @@ kri: driver-class-name: com.mysql.cj.jdbc.Driver scheduling: enabled: false + email: + result-poll: + enabled: false spring: datasource: diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index c89de82..336c3b5 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -51,6 +51,7 @@ kri: batch-size: 500 sending-stale-minutes: 30 result-poll: + enabled: true # false면 이메일 결과 조회(워커 · 기동 시 복구 · 큐 등록) 미동작 initial-delay-ms: 10000 max-attempts: 0 max-delay-ms: 30000