261022_02_ENH: 발송 결과·벌크 상세·벌크 목록 응답에 예약 취소 가능 여부(cancelable) 추가

This commit is contained in:
2026-10-22 15:17:57 +09:00
parent f2a9d6bfc6
commit 3162b52a35
4 changed files with 61 additions and 6 deletions

View File

@@ -44,6 +44,9 @@ public class BulkSendInfoDto {
@Schema(description = "예약 여부")
private Boolean resve;
@Schema(description = "조회 시점에 취소 가능한 예약 상세 건이 하나 이상인지 여부. 최종 판단은 취소 API 응답 기준")
private boolean cancelable;
@NotNull
@Schema(description = "발송일시")
private LocalDateTime sendDt;

View File

@@ -51,6 +51,9 @@ public record SendResultInfoDto(
@Schema(description = "예약 여부")
Boolean resve,
@Schema(description = "조회 시점의 상태 기준 예약 취소 가능 여부. 최종 판단은 취소 API 응답 기준")
boolean cancelable,
@NotNull
@Schema(description = "결과 코드")
Short resultCode,
@@ -118,6 +121,11 @@ public record SendResultInfoDto(
public static SendResultInfoDto from(TSendEntity entity,
List<TSendResultCodeEntity> resultCodeEntity) {
return from(entity, resultCodeEntity, false);
}
public static SendResultInfoDto from(TSendEntity entity,
List<TSendResultCodeEntity> resultCodeEntity, boolean cancelable) {
if (entity == null) {
return null;
}
@@ -137,6 +145,7 @@ public record SendResultInfoDto(
.sid(entity.getSid())
.sendTy(entity.getSendTy())
.resve(entity.isResve())
.cancelable(cancelable)
.resultCode(entity.getResultCode())
.resultLabel(canceled ? NotihubConst.CANCELED_LABEL
: resultCode != null ? resultCode.getResultLabel() : null)

View File

@@ -21,6 +21,22 @@ import org.springframework.transaction.annotation.Transactional;
public interface SendRepository extends JpaRepository<TSendEntity, Long> {
// 목록과 벌크 집계에서 동일한 상태 기준을 사용한다. 취소 API의 잠금 후 판단이 최종이다.
String CANCELABLE_CONDITION = """
s.resve = true and s.sendStatus = 0 and s.cancelDt is null
and (
(s.sendTy = :emailSendType and s.resultCode = :emailStandbyCode)
or (s.sendTy in :bizSendTypes and exists (
select 1 from BizMsgEntity m where m.cmid = s.cmid and m.status = 0
))
)
""";
@Query("select s.sendIdx from TSendEntity s where s.sendIdx in :sendIds and ("
+ CANCELABLE_CONDITION + ")")
List<Long> findCancelableSendIds(List<Long> sendIds, short emailSendType,
short emailStandbyCode, List<Short> bizSendTypes);
@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query("select s from TSendEntity s where s.sendIdx = :sendIdx")
Optional<TSendEntity> findForUpdate(Long sendIdx);
@@ -35,14 +51,17 @@ public interface SendRepository extends JpaRepository<TSendEntity, Long> {
String getBulkSendKey();
Long getCanceledCount();
Long getTargetCount();
Long getCancelableCount();
}
@Query("select s.bulkSendKey as bulkSendKey, "
+ "sum(case when s.sendStatus = 8 then 1L else 0L end) as canceledCount, "
+ "count(s) as targetCount "
+ "count(s) as targetCount, "
+ "sum(case when " + CANCELABLE_CONDITION + " then 1L else 0L end) as cancelableCount "
+ "from TSendEntity s where s.bulkSendKey in :bulkSendKeys "
+ "group by s.bulkSendKey")
List<CancellationCount> countCanceledByBulkSendKeys(List<String> bulkSendKeys);
List<CancellationCount> countCanceledByBulkSendKeys(List<String> bulkSendKeys,
short emailSendType, short emailStandbyCode, List<Short> bizSendTypes);
Optional<TSendEntity> findByCmid(String cmid);

View File

@@ -40,6 +40,7 @@ import java.util.Base64;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -69,6 +70,7 @@ public class ReportService {
.toList();
}
@Transactional(readOnly = true)
public List<SendResultInfoDto> getSendResultList(String sid, String prjctCd, String reqstSvc,
ChannelType channelTy) {
List<Short> sendTyList = getSendTypeCodeList(channelTy);
@@ -80,9 +82,11 @@ public class ReportService {
: sendRepository.findBySidAndPrjctCdAndReqstSvcAndSendTyInOrderBySendDtDescSendIdxDesc(
sid, prjctCd, reqstSvc, sendTyList);
Set<Long> cancelableIds = findCancelableSendIds(sendList);
return sendList
.stream()
.map(entity -> SendResultInfoDto.from(entity, resultCodeList))
.map(entity -> SendResultInfoDto.from(entity, resultCodeList,
cancelableIds.contains(entity.getSendIdx())))
.toList();
}
@@ -103,7 +107,8 @@ public class ReportService {
return List.of();
}
Map<String, SendRepository.CancellationCount> counts = sendRepository.countCanceledByBulkSendKeys(
bulks.stream().map(TBulkSendEntity::getBulkSendKey).toList()).stream()
bulks.stream().map(TBulkSendEntity::getBulkSendKey).toList(),
SendType.EMAIL.getCode(), NotihubConst.EMAIL_STANDBY, bizSendTypes()).stream()
.collect(Collectors.toMap(SendRepository.CancellationCount::getBulkSendKey,
Function.identity()));
return bulks.stream().map(bulk -> {
@@ -111,21 +116,40 @@ public class ReportService {
SendRepository.CancellationCount count = counts.get(bulk.getBulkSendKey());
dto.applyCanceledCount(count == null ? 0 : count.getCanceledCount(),
count == null ? 0 : count.getTargetCount());
dto.setCancelable(Boolean.TRUE.equals(bulk.getResve())
&& count != null && count.getCancelableCount() > 0);
return dto;
}).toList();
}
@Transactional(readOnly = true)
public PageResponseDto<SendResultInfoDto> getBulkSendDetail(String bulkSendKey,
PageRequestDto pageReq) {
List<TSendResultCodeEntity> resultCodeList = sendResultCodeRepository.findAll();
Pageable pageable = Pageables.of(pageReq, TSendEntity.class);
Page<SendResultInfoDto> page = sendRepository.findByBulkSendKey(bulkSendKey, pageable)
.map(entity -> SendResultInfoDto.from(entity, resultCodeList));
Page<TSendEntity> sends = sendRepository.findByBulkSendKey(bulkSendKey, pageable);
Set<Long> cancelableIds = findCancelableSendIds(sends.getContent());
Page<SendResultInfoDto> page = sends.map(entity -> SendResultInfoDto.from(
entity, resultCodeList, cancelableIds.contains(entity.getSendIdx())));
return PageResponseDto.from(page);
}
private Set<Long> findCancelableSendIds(List<TSendEntity> sends) {
if (sends.isEmpty()) {
return Set.of();
}
return Set.copyOf(sendRepository.findCancelableSendIds(
sends.stream().map(TSendEntity::getSendIdx).toList(), SendType.EMAIL.getCode(),
NotihubConst.EMAIL_STANDBY, bizSendTypes()));
}
private List<Short> bizSendTypes() {
return Stream.concat(NotihubConst.SMS_TYPE_CODE_LIST.stream(),
NotihubConst.KKO_TYPE_CODE_LIST.stream()).toList();
}
public ReportExcelResponseDto getBulkSendDetailExcel(String bulkSendKey) {
List<TSendResultCodeEntity> resultCodeList = sendResultCodeRepository.findAll();