package com.sinata.appointment.service.event; import com.sinata.appointment.entity.AppointmentOrder; import com.sinata.appointment.mapper.AppointmentOrderMapper; import com.sinata.appointment.service.IntegrationWechatService; import com.sinata.appointment.service.PostApprovalOrchestrationService; 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 AppointmentLifecycleEventListener { private final AppointmentOrderMapper appointmentOrderMapper; private final PostApprovalOrchestrationService postApprovalOrchestrationService; private final IntegrationWechatService integrationWechatService; @Async("applicationTaskExecutor") @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) public void handleApproved(AppointmentApprovedEvent event) { AppointmentOrder order = appointmentOrderMapper.selectById(event.orderId()); if (order == null) { log.warn("Approved event skipped, order not found orderId={}", event.orderId()); return; } postApprovalOrchestrationService.onOrderFullyApproved(order); } @Async("applicationTaskExecutor") @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) public void handleRejected(AppointmentRejectedEvent event) { AppointmentOrder order = appointmentOrderMapper.selectById(event.orderId()); if (order == null) { log.warn("Rejected event skipped, order not found orderId={}", event.orderId()); return; } postApprovalOrchestrationService.onOrderRejected(order, event.reason()); } @Async("applicationTaskExecutor") @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) public void handleApproversNotify(AppointmentApproversNotifyEvent event) { if (event.instanceId() == null) { return; } AppointmentOrder order = appointmentOrderMapper.selectById(event.orderId()); if (order == null) { log.warn("Approvers notify skipped, order not found orderId={}", event.orderId()); return; } try { integrationWechatService.notifyCurrentApprovers(order, event.instanceId()); } catch (Exception ex) { log.warn( "notify current approvers failed, orderId={}, instanceId={}", event.orderId(), event.instanceId(), ex); } } }