package cokr.xit.ens.modules.common.ctgy.intgrnnoti.service.event;

import cokr.xit.ens.modules.common.ctgy.intgrnnoti.domain.repository.IntgrnSendDetailRepository;
import cokr.xit.ens.modules.common.ctgy.intgrnnoti.service.IntgrnNotiService;
import cokr.xit.ens.modules.common.event.SendMastStatUpdateEvent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

@Slf4j
@Component
@RequiredArgsConstructor
public class SendMastStatUpdateEventListener {
    private final IntgrnNotiService intgrnNotiService;
    private final IntgrnSendDetailRepository intgrnSendDetailRepository;


    @Async
    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
    public void IntgrnNotiStatUpdate(SendMastStatUpdateEvent event) {

        Long intSendMastId = intgrnSendDetailRepository.findIntSendMastIdBySendMastId(event.getSendMastId()).orElse(-999L);
        StringBuffer sb = new StringBuffer();
        sb.append("\n=======================================================")
                .append("\n[ENS Intgrn] 통합고지 상태갱신")
                .append("\n요청ID: {}")
                .append("\n상태갱신 결과: {}")
                .append("\n=======================================================");
        log.info(sb.toString(), intSendMastId, intgrnNotiService.statBulk(intSendMastId).toString());
    }
}
