261102_01_ENH: 알림톡 발신 프로필 목록 조회 추가 및 템플릿 API senderKey 필수 검증 적용

This commit is contained in:
2026-11-09 15:19:21 +09:00
parent e6b9760594
commit 67f17e993d
8 changed files with 280 additions and 74 deletions

View File

@@ -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
) {
}

View File

@@ -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;
}
}
}
}

View File

@@ -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();
}
}
}

View File

@@ -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<TSendEntity, Long> {
);
@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<TSendEntity, Long> {
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<SendCostSummaryWithItrntPrjctDto> findSendCostSummaryWithItrntPrjct(
List<SendCostByTypeDto> findSendCostSummaryWithItrntPrjct(
@Param("startDt") LocalDateTime startDt,
@Param("endDt") LocalDateTime endDt
);

View File

@@ -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 {

View File

@@ -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<SendCostSummaryWithItrntPrjctDto> summaries =
List<SendCostByTypeDto> costsByType =
sendRepository.findSendCostSummaryWithItrntPrjct(
startDt,
endDt
);
List<SendCostSummaryWithItrntPrjctDto> summaries = pivotSendCostBySendType(costsByType);
List<String> itrntPrjctCds = summaries.stream()
.map(SendCostSummaryWithItrntPrjctDto::getItrntPrjctCd)
.filter(StringUtils::hasText)
@@ -283,6 +292,95 @@ public class ReportService {
return summaries;
}
/**
* 발송 타입별 row를 (요청 서비스, 인트라넷 프로젝트) 1 row로 합치고 발송 타입을 컬럼으로 펼친다.
* 발송 업체는 구분하지 않으며, 단가는 발송 건이 있으면 비용 ÷ 건수(가중 평균),
* 없으면 사용 중 단가가 하나뿐일 때 그 단가를 사용한다.
*/
private List<SendCostSummaryWithItrntPrjctDto> pivotSendCostBySendType(
List<SendCostByTypeDto> costsByType
) {
Map<SendType, BigDecimal> configuredUnitPriceMap = getConfiguredUnitPriceBySendType();
Map<List<String>, 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<SendType, BigDecimal> getConfiguredUnitPriceBySendType() {
Map<SendType, Set<BigDecimal>> 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<SendType, BigDecimal> 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
) {