배치이력 생성 작업
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package app.module.admin.config.application.batch;
|
||||
|
||||
import app.module.admin.usecase.batchLog.ports.BatchLogPortIn;
|
||||
import app.module.admin.usecase.batchLog.ports.application.domain.BatchLogType;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class BatchLogRunner {
|
||||
private final BatchLogPortIn batchLogPortIn;
|
||||
|
||||
public void run(BatchLogType batchType, Runnable action) {
|
||||
// 이미 실행 중인지 확인
|
||||
if (batchLogPortIn.exists(batchType)) {
|
||||
log.warn("[{}] 배치가 이미 실행 중 입니다.", batchType.name());
|
||||
return;
|
||||
}
|
||||
|
||||
// 실행 로그 insert (RUNNING)
|
||||
Long logId = batchLogPortIn.create(batchType);
|
||||
|
||||
try {
|
||||
action.run();
|
||||
// 성공 처리
|
||||
batchLogPortIn.update(logId, "SUCCESS", null);
|
||||
} catch (Exception e) {
|
||||
// 실패 처리
|
||||
batchLogPortIn.update(logId, "FAIL", e.getMessage());
|
||||
log.error("[{}] 배치 실패: {}", batchType.name(), e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package app.module.admin.usecase.batchLog.adapters.persistence;
|
||||
|
||||
import app.module.admin.usecase.batchLog.ports.BatchLogPortOut;
|
||||
import app.module.admin.usecase.batchLog.ports.application.domain.BatchLogCmd;
|
||||
import app.module.admin.usecase.batchLog.ports.application.domain.BatchLogType;
|
||||
import java.time.LocalDateTime;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
@RequiredArgsConstructor
|
||||
class BatchLogDao implements BatchLogPortOut {
|
||||
private final BatchLogMapper mapper;
|
||||
|
||||
@Override
|
||||
public boolean exists(BatchLogType type) {
|
||||
return mapper.exists(type.name());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long create(BatchLogType type) {
|
||||
BatchLogCmd cmd = new BatchLogCmd(null, type.name(), "RUNNING", LocalDateTime.now());
|
||||
mapper.create(cmd);
|
||||
return cmd.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Long id, String status, String errorMessage) {
|
||||
mapper.update(id, status, errorMessage);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package app.module.admin.usecase.batchLog.adapters.persistence;
|
||||
|
||||
import app.module.admin.usecase.batchLog.ports.application.domain.BatchLogCmd;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface BatchLogMapper {
|
||||
boolean exists(String type);
|
||||
Long create(BatchLogCmd cmd);
|
||||
void update(Long id, String status, String errorMessage);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package app.module.admin.usecase.batchLog.ports;
|
||||
|
||||
import app.module.admin.usecase.batchLog.ports.application.domain.BatchLogType;
|
||||
|
||||
public interface BatchLogPortIn {
|
||||
boolean exists(BatchLogType type);
|
||||
Long create(BatchLogType type);
|
||||
void update(Long id, String status, String errorMessage);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package app.module.admin.usecase.batchLog.ports;
|
||||
|
||||
import app.module.admin.usecase.batchLog.ports.application.domain.BatchLogType;
|
||||
|
||||
public interface BatchLogPortOut {
|
||||
boolean exists(BatchLogType type);
|
||||
Long create(BatchLogType type);
|
||||
void update(Long id, String status, String errorMessage);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package app.module.admin.usecase.batchLog.ports.application;
|
||||
|
||||
import app.module.admin.usecase.batchLog.ports.BatchLogPortIn;
|
||||
import app.module.admin.usecase.batchLog.ports.BatchLogPortOut;
|
||||
import app.module.admin.usecase.batchLog.ports.application.domain.BatchLogType;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
class BatchLogService implements BatchLogPortIn {
|
||||
private final BatchLogPortOut portOut;
|
||||
|
||||
@Override
|
||||
public boolean exists(BatchLogType type) {
|
||||
return portOut.exists(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long create(BatchLogType type) {
|
||||
return portOut.create(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Long id, String status, String errorMessage) {
|
||||
portOut.update(id, status, errorMessage);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package app.module.admin.usecase.batchLog.ports.application.domain;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode
|
||||
public class BatchLog {
|
||||
private Long id;
|
||||
private String type;
|
||||
private String status;
|
||||
private LocalDateTime startAt;
|
||||
private LocalDateTime endAt;
|
||||
private String errorMessage;
|
||||
private LocalDateTime createdAt;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package app.module.admin.usecase.batchLog.ports.application.domain;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode
|
||||
public class BatchLogCmd {
|
||||
private Long id;
|
||||
private String type;
|
||||
private String status;
|
||||
private LocalDateTime startAt;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package app.module.admin.usecase.batchLog.ports.application.domain;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public enum BatchLogType {
|
||||
AUTO_SMS,
|
||||
MONTHLY_STATUS,
|
||||
CARRYOVER;
|
||||
}
|
||||
@@ -1,14 +1,12 @@
|
||||
package app.module.admin.usecase.schedule.autoSchedules.adapters.persistence;
|
||||
|
||||
import app.module.admin.usecase.schedule.autoSchedules.ports.AutoSchedulesPortOut;
|
||||
import app.module.admin.usecase.schedule.autoSchedules.ports.application.domain.CreateSmsAutoSendCmd;
|
||||
import java.util.Optional;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Slf4j
|
||||
@Repository
|
||||
@RequiredArgsConstructor
|
||||
@@ -24,24 +22,9 @@ class AutoSchedulesDao implements AutoSchedulesPortOut {
|
||||
|
||||
return Optional.of(true);
|
||||
} catch (Exception e) {
|
||||
// 예외 발생 시 false 반환
|
||||
return Optional.of(false);
|
||||
}
|
||||
log.error("[AutoSchedule][createMonthlyInputStatus] message: {}", e.getMessage(), e);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Optional<Boolean> sendAutoSms(CreateSmsAutoSendCmd cmd) {
|
||||
try {
|
||||
mapper.createSmsAutoSend(cmd);
|
||||
|
||||
return Optional.of(true);
|
||||
} catch (Exception e) {
|
||||
log.info(e.getMessage());
|
||||
|
||||
// 예외 발생 시 false 반환
|
||||
return Optional.of(false);
|
||||
throw new RuntimeException("월별 입력 항목 관리 생성 중 오류: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,8 +38,7 @@ class AutoSchedulesDao implements AutoSchedulesPortOut {
|
||||
} catch (Exception e) {
|
||||
log.error("[AutoSchedule][monthlyCarryOver] message: {}", e.getMessage(), e);
|
||||
|
||||
// 예외 발생 시 false 반환
|
||||
return Optional.of(false);
|
||||
throw new RuntimeException("일계부 월초이월금 생성 중 오류: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
package app.module.admin.usecase.schedule.autoSchedules.adapters.persistence;
|
||||
|
||||
import app.module.admin.usecase.schedule.autoSchedules.ports.application.domain.CreateSmsAutoSendCmd;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface AutoSchedulesMapper {
|
||||
void createMonthlyInputStatus();
|
||||
void createSmsAutoSend(CreateSmsAutoSendCmd cmd);
|
||||
String createMonthlyInputStatus();
|
||||
String createMonthlyCarryOver();
|
||||
}
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
package app.module.admin.usecase.schedule.autoSchedules.adapters.web;
|
||||
|
||||
import app.module.admin.config.application.util.DateUiUtil;
|
||||
import app.module.admin.config.application.util.model.DayRange;
|
||||
import app.module.admin.usecase.schedule.autoSchedules.ports.AutoSchedulesPortIn;
|
||||
import java.time.LocalDate;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -17,13 +14,19 @@ public class AutoSchedulesController {
|
||||
this.portIn = portIn;
|
||||
}
|
||||
|
||||
// 매 월 1일 1초 마다 실행되는 스케줄러
|
||||
/**
|
||||
* 월별입력상태 등록
|
||||
* 매 월 1일 1초 마다 실행되는 스케줄러
|
||||
*/
|
||||
@Scheduled(cron = "1 0 0 1 * *")
|
||||
public void fetchArticlesPeriodically() {
|
||||
portIn.operate();
|
||||
}
|
||||
|
||||
// 매일 오후 5시 마다 실행되는 스케줄러
|
||||
/**
|
||||
* 문자자동발송 등록
|
||||
* 매일 오후 5시 마다 실행되는 스케줄러
|
||||
*/
|
||||
@Scheduled(cron = "0 0 17 * * ?")
|
||||
public void fetchSmsAutoSend() {
|
||||
portIn.sendAutoSms();
|
||||
@@ -37,14 +40,4 @@ public class AutoSchedulesController {
|
||||
public void fetchMonthlyCarryOver() {
|
||||
portIn.monthlyCarryOver();
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO 스케쥴 있다면.. 추가.
|
||||
*/
|
||||
|
||||
// 매일 오후 5시 마다 실행되는 스케줄러
|
||||
@Scheduled(cron = "0 1 * * * ?")
|
||||
public void fetchSmsAutoSenTest() {
|
||||
portIn.sendAutoSmsTest();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,5 +7,4 @@ public interface AutoSchedulesPortIn {
|
||||
Optional<Boolean> operate();
|
||||
Optional<Boolean> sendAutoSms();
|
||||
Optional<Boolean> monthlyCarryOver();
|
||||
Optional<Boolean> sendAutoSmsTest();
|
||||
}
|
||||
@@ -1,11 +1,9 @@
|
||||
package app.module.admin.usecase.schedule.autoSchedules.ports;
|
||||
|
||||
import app.module.admin.usecase.schedule.autoSchedules.ports.application.domain.CreateSmsAutoSendCmd;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface AutoSchedulesPortOut {
|
||||
|
||||
Optional<Boolean> operate();
|
||||
Optional<Boolean> sendAutoSms(CreateSmsAutoSendCmd cmd);
|
||||
Optional<Boolean> monthlyCarryOver();
|
||||
}
|
||||
@@ -1,38 +1,42 @@
|
||||
package app.module.admin.usecase.schedule.autoSchedules.ports.application;
|
||||
|
||||
import app.module.admin.config.application.batch.BatchLogRunner;
|
||||
import app.module.admin.config.application.util.DateUiUtil;
|
||||
import app.module.admin.config.application.util.model.DayRange;
|
||||
import app.module.admin.usecase.batchLog.ports.application.domain.BatchLogType;
|
||||
import app.module.admin.usecase.dashboard.getSmsAutoStatus.ports.GetSmsAutoStatusPortIn;
|
||||
import app.module.admin.usecase.dashboard.getSmsAutoStatus.ports.application.domain.GetSmsAutoStatus;
|
||||
import app.module.admin.usecase.schedule.autoSchedules.ports.AutoSchedulesPortIn;
|
||||
import app.module.admin.usecase.schedule.autoSchedules.ports.AutoSchedulesPortOut;
|
||||
import app.module.admin.usecase.schedule.autoSchedules.ports.application.domain.CreateSmsAutoSendCmd;
|
||||
import app.module.admin.usecase.schedule.getIncompleteHouseholds.ports.GetIncompleteHouseholdsPortIn;
|
||||
import app.module.admin.usecase.schedule.getIncompleteHouseholds.ports.application.domain.GetIncompleteHouseholds;
|
||||
import app.module.admin.usecase.schedule.getIncompleteHouseholds.ports.application.domain.GetIncompleteHouseholdsCmd;
|
||||
import app.module.mms.ports.MMSPortIn;
|
||||
import app.module.mms.ports.application.domain.CreateSmsAutoSendCmd;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
import app.module.mms.ports.MMSPortIn;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Optional;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
class AutoSchedulesService implements AutoSchedulesPortIn {
|
||||
|
||||
private final AutoSchedulesPortOut portOut;
|
||||
private final MMSPortIn mmsPortIn;
|
||||
private final GetSmsAutoStatusPortIn getSmsAutoStatusPortIn;
|
||||
private final GetIncompleteHouseholdsPortIn getIncompleteHouseholdsPortIn;
|
||||
private final BatchLogRunner batchLogRunner;
|
||||
|
||||
@Override
|
||||
public Optional<Boolean> operate() {
|
||||
return portOut.operate();
|
||||
batchLogRunner.run(BatchLogType.MONTHLY_STATUS, portOut::operate);
|
||||
return Optional.of(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -40,63 +44,40 @@ class AutoSchedulesService implements AutoSchedulesPortIn {
|
||||
public Optional<Boolean> sendAutoSms() {
|
||||
GetSmsAutoStatus getSmsAutoStatus = getSmsAutoStatusPortIn.operate().orElse(null);
|
||||
|
||||
// 문자 자동 발송 활성화 상태 체크
|
||||
if (getSmsAutoStatus == null || Boolean.FALSE.equals(getSmsAutoStatus.getActive())) {
|
||||
System.out.println("문자 발송 비활성화 상태");
|
||||
log.info("문자 발송 비활성화 상태");
|
||||
return Optional.of(false);
|
||||
}
|
||||
|
||||
LocalDate today = LocalDate.now();
|
||||
int day = today.getDayOfMonth();
|
||||
|
||||
// 발송일 조건: 8일, 15일, 22일, 1일(다음 달 대상자)
|
||||
if (day == 8 || day == 15 || day == 22 || day == 1) {
|
||||
// 발송 대상 날짜 범위 구하기
|
||||
DayRange dayRange = DateUiUtil.getDayRange(today).orElse(null);
|
||||
if (dayRange == null) return Optional.of(false);
|
||||
if (dayRange == null) {
|
||||
log.error("잘못된 날짜({})에 문자 자동 발송 로직이 실행됨 - 발송 대상일이 아님", today);
|
||||
return Optional.of(false);
|
||||
}
|
||||
|
||||
// 대상 월 계산 (1일 이면 이전 달 / 1일이 아니면 현재 달 yyyyMM 을 가져온다.)
|
||||
LocalDate baseDate = (day == 1) ? today.minusMonths(1) : today;
|
||||
String targetMonth = DateUiUtil.getMonthValue(baseDate);
|
||||
|
||||
// dayRange 날짜 범위의 일계부 미등록한 임가 목록 조회
|
||||
List<GetIncompleteHouseholds> incompleteHouseholds = getIncompleteHouseholdsPortIn.operate(
|
||||
new GetIncompleteHouseholdsCmd(targetMonth, dayRange.getFromDay(), dayRange.getToDay())
|
||||
);
|
||||
|
||||
// 미등록 임가가 있을 경우에만 실행
|
||||
if(!CollectionUtils.isEmpty(incompleteHouseholds)) {
|
||||
portOut.sendAutoSms(new CreateSmsAutoSendCmd(
|
||||
incompleteHouseholds,
|
||||
getSmsAutoStatus.getSendPhone(),
|
||||
getSmsAutoStatus.getMessage()
|
||||
));
|
||||
if (!CollectionUtils.isEmpty(incompleteHouseholds)) {
|
||||
batchLogRunner.run(BatchLogType.AUTO_SMS, () -> {
|
||||
mmsPortIn.sendAutoSms(new CreateSmsAutoSendCmd(
|
||||
incompleteHouseholds,
|
||||
getSmsAutoStatus.getSendPhone(),
|
||||
getSmsAutoStatus.getMessage()
|
||||
));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return Optional.of(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Boolean> sendAutoSmsTest() {
|
||||
String targetMonth = "202507";
|
||||
DayRange dayRange = new DayRange(1, 7);
|
||||
GetSmsAutoStatus getSmsAutoStatus = new GetSmsAutoStatus(true, "테스트", "01012345678", null, null);
|
||||
|
||||
// dayRange 날짜 범위의 일계부 미등록한 임가 목록 조회
|
||||
List<GetIncompleteHouseholds> incompleteHouseholds = getIncompleteHouseholdsPortIn.operate(
|
||||
new GetIncompleteHouseholdsCmd(targetMonth, dayRange.getFromDay(), dayRange.getToDay())
|
||||
);
|
||||
|
||||
System.out.println(incompleteHouseholds);
|
||||
|
||||
// 미등록 임가가 있을 경우에만 실행
|
||||
if(!CollectionUtils.isEmpty(incompleteHouseholds)) {
|
||||
mmsPortIn.sendAutoSms(new app.module.mms.ports.application.domain.CreateSmsAutoSendCmd(
|
||||
incompleteHouseholds,
|
||||
getSmsAutoStatus.getSendPhone(),
|
||||
getSmsAutoStatus.getMessage()
|
||||
));
|
||||
} else {
|
||||
log.info("문자 발송 날짜 아님.");
|
||||
return Optional.of(false);
|
||||
}
|
||||
|
||||
return Optional.of(true);
|
||||
@@ -104,6 +85,7 @@ class AutoSchedulesService implements AutoSchedulesPortIn {
|
||||
|
||||
@Override
|
||||
public Optional<Boolean> monthlyCarryOver() {
|
||||
return portOut.monthlyCarryOver();
|
||||
batchLogRunner.run(BatchLogType.CARRYOVER, portOut::monthlyCarryOver);
|
||||
return Optional.of(true);
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package app.module.admin.usecase.schedule.autoSchedules.ports.application.domain;
|
||||
|
||||
import app.module.admin.usecase.schedule.getIncompleteHouseholds.ports.application.domain.GetIncompleteHouseholds;
|
||||
import java.util.List;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode
|
||||
public class CreateSmsAutoSendCmd {
|
||||
private List<GetIncompleteHouseholds> sendHouseholds;
|
||||
private String sendPhone;
|
||||
private String message;
|
||||
}
|
||||
@@ -19,16 +19,13 @@ public class MMSDao implements MMSPortOut {
|
||||
@Transactional
|
||||
public Optional<Boolean> sendAutoSms(CreateSmsAutoSendCmd cmd) {
|
||||
try {
|
||||
log.debug("@@@ sendAutoSmsTest");
|
||||
|
||||
mapper.createSmsAutoSend(cmd);
|
||||
|
||||
return Optional.of(true);
|
||||
} catch (Exception e) {
|
||||
log.info(e.getMessage());
|
||||
log.error(e.getMessage());
|
||||
|
||||
// 예외 발생 시 false 반환
|
||||
return Optional.of(false);
|
||||
throw new RuntimeException("문자 자동 발송 생성 중 오류: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user