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
package cn.iocoder.yudao.module.research.service.fund.listener;
 
import cn.iocoder.yudao.module.bpm.api.event.BpmProcessInstanceStatusEvent;
import cn.iocoder.yudao.module.bpm.api.event.BpmProcessInstanceStatusEventListener;
import cn.iocoder.yudao.module.research.service.fund.ResearchFundRecordService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
 
/**
 * 经费拨付台账状态监听器
 *
 * 监听财务报销单(finance_reimbursement_approve)的审批结果,把结果回写到课题侧的经费拨付台账:
 * 通过 → 已拨付(20);驳回 → 已驳回(30) + 驳回原因/时间;撤回或取消 → 退回待拨付(0)。
 * 与 finance 模块自己的 FinanceReimbursementStatusListener 订阅同一流程 key,两者互不干扰。
 *
 * @author 超级管理员
 */
@Component
@Slf4j
public class ResearchFundStatusListener extends BpmProcessInstanceStatusEventListener {
 
    /** 财务报销单审批流程定义 Key */
    private static final String PROCESS_DEFINITION_KEY = "finance_reimbursement_approve";
 
    @Resource
    private ResearchFundRecordService fundRecordService;
 
    @Override
    protected String getProcessDefinitionKey() {
        return PROCESS_DEFINITION_KEY;
    }
 
    @Override
    protected void onEvent(BpmProcessInstanceStatusEvent event) {
        Long reimbursementId;
        try {
            reimbursementId = Long.parseLong(event.getBusinessKey());
        } catch (NumberFormatException e) {
            log.warn("[onEvent] businessKey({}) 不是有效的财务报销单编号", event.getBusinessKey());
            return;
        }
 
        log.info("[onEvent] 收到财务报销审批事件,reimbursementId={}, status={}", reimbursementId, event.getStatus());
 
        try {
            fundRecordService.updateFundStatusByReimbursement(reimbursementId, event.getStatus(), event.getReason());
            log.info("[onEvent] 经费拨付台账(报销单 {}) 状态更新完成", reimbursementId);
        } catch (Exception e) {
            log.error("[onEvent] 更新经费拨付台账状态失败,reimbursementId={}", reimbursementId, e);
        }
    }
 
}