| | |
| | | package cn.iocoder.yudao.module.crm.service.contract.listener; |
| | | |
| | | import cn.iocoder.yudao.module.bpm.api.event.BpmProcessInstanceStatusEvent; |
| | | import cn.iocoder.yudao.module.bpm.api.event.BpmProcessInstanceStatusEventListener; |
| | | import cn.iocoder.yudao.module.crm.dal.dataobject.contract.CrmContractDO; |
| | | import cn.iocoder.yudao.module.crm.dal.mysql.contract.CrmContractMapper; |
| | | import cn.iocoder.yudao.module.crm.enums.common.CrmAuditStatusEnum; |
| | | import cn.iocoder.yudao.module.crm.service.contract.CrmContractService; |
| | | import cn.iocoder.yudao.module.crm.service.contract.CrmContractServiceImpl; |
| | | import jakarta.annotation.Resource; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.context.ApplicationListener; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | /** |
| | | * 合同审批的结果的监听器实现类 |
| | | * 合同审批的结果监听器实现类 |
| | | * |
| | | * 通过 businessKey(合同ID)匹配,而不是固定的流程定义 Key, |
| | | * 这样可以支持分类下任意 Key 的流程模型。 |
| | | * |
| | | * @author HUIHUI |
| | | */ |
| | | @Component |
| | | public class CrmContractStatusListener extends BpmProcessInstanceStatusEventListener { |
| | | @Slf4j |
| | | public class CrmContractStatusListener implements ApplicationListener<BpmProcessInstanceStatusEvent> { |
| | | |
| | | @Resource |
| | | private CrmContractService contractService; |
| | | @Resource |
| | | private CrmContractMapper contractMapper; |
| | | |
| | | @Override |
| | | public String getProcessDefinitionKey() { |
| | | return CrmContractServiceImpl.BPM_PROCESS_DEFINITION_KEY; |
| | | } |
| | | public void onApplicationEvent(BpmProcessInstanceStatusEvent event) { |
| | | // 通过 businessKey 查询合同 |
| | | Long contractId; |
| | | try { |
| | | contractId = Long.parseLong(event.getBusinessKey()); |
| | | } catch (NumberFormatException e) { |
| | | return; // businessKey 不是数字,忽略 |
| | | } |
| | | |
| | | @Override |
| | | protected void onEvent(BpmProcessInstanceStatusEvent event) { |
| | | contractService.updateContractAuditStatus(Long.parseLong(event.getBusinessKey()), event.getStatus()); |
| | | CrmContractDO contract = contractMapper.selectById(contractId); |
| | | if (contract == null) { |
| | | return; // 不是合同,忽略 |
| | | } |
| | | |
| | | // 只有审批中状态的合同才处理 |
| | | if (!CrmAuditStatusEnum.PROCESS.getStatus().equals(contract.getAuditStatus())) { |
| | | log.warn("[onApplicationEvent] 合同({}) 不处于审批中状态,当前状态: {}", contractId, contract.getAuditStatus()); |
| | | return; |
| | | } |
| | | |
| | | log.info("[onApplicationEvent] 合同({}) 审批状态更新: {}", contractId, event.getStatus()); |
| | | // 更新审批状态 |
| | | contractService.updateContractAuditStatus(contractId, event.getStatus()); |
| | | } |
| | | |
| | | } |