longz
2026-05-14 11029948d45564975527b2c21e9c2def1903164a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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);
        }
    }
}