云
2026-10-31 bf5f52656e128f578ed42c7979c31cf4b476ce46
yudao-module-crm/src/main/java/cn/iocoder/yudao/module/crm/service/refund/listener/CrmRefundStatusListener.java
@@ -1,54 +1,46 @@
package cn.iocoder.yudao.module.crm.service.refund.listener;
import cn.iocoder.yudao.module.bpm.api.event.BpmProcessInstanceStatusEvent;
import cn.iocoder.yudao.module.crm.dal.dataobject.refund.CrmRefundDO;
import cn.iocoder.yudao.module.crm.dal.mysql.refund.CrmRefundMapper;
import cn.iocoder.yudao.module.crm.enums.common.CrmAuditStatusEnum;
import cn.iocoder.yudao.module.bpm.api.event.BpmProcessInstanceStatusEventListener;
import cn.iocoder.yudao.module.crm.service.refund.CrmRefundService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
/**
 * 退费审批的结果监听器实现类
 * CRM 退费单审批状态监听器
 *
 * 通过 businessKey(退费单ID)匹配,而不是固定的流程定义 Key,
 * 这样可以支持分类下任意 Key 的流程模型。
 * 监听 BPM 审批结果,回写退费单审核状态,使退费审批统一在「协同办公 - 我的流程」中流转。
 */
@Component
@Slf4j
public class CrmRefundStatusListener implements ApplicationListener<BpmProcessInstanceStatusEvent> {
public class CrmRefundStatusListener extends BpmProcessInstanceStatusEventListener {
    private static final String PROCESS_DEFINITION_KEY = "refund_approve";
    @Resource
    private CrmRefundService refundService;
    @Resource
    private CrmRefundMapper refundMapper;
    @Override
    public void onApplicationEvent(BpmProcessInstanceStatusEvent event) {
        // 通过 businessKey 查询退费单
    protected String getProcessDefinitionKey() {
        return PROCESS_DEFINITION_KEY;
    }
    @Override
    protected void onEvent(BpmProcessInstanceStatusEvent event) {
        Long refundId;
        try {
            refundId = Long.parseLong(event.getBusinessKey());
        } catch (NumberFormatException e) {
            return; // businessKey 不是数字,忽略
        }
        CrmRefundDO refund = refundMapper.selectById(refundId);
        if (refund == null) {
            return; // 不是退费单,忽略
        }
        // 只有审批中状态的退费单才处理
        if (!CrmAuditStatusEnum.PROCESS.getStatus().equals(refund.getAuditStatus())) {
            log.warn("[onApplicationEvent] 退费单({}) 不处于审批中状态,当前状态: {}", refundId, refund.getAuditStatus());
            log.warn("[onEvent] businessKey({}) 不是有效的退费单编号", event.getBusinessKey());
            return;
        }
        log.info("[onApplicationEvent] 退费单({}) 审批状态更新: {}", refundId, event.getStatus());
        // 更新审批状态
        refundService.updateRefundAuditStatus(refundId, event.getStatus());
        log.info("[onEvent] 收到退费单审批事件,refundId={}, status={}", refundId, event.getStatus());
        try {
            refundService.updateRefundAuditStatus(refundId, event.getStatus(), event.getReason());
        } catch (Exception e) {
            log.error("[onEvent] 回写退费单审批状态失败,refundId={}", refundId, e);
        }
    }
}