package cn.iocoder.yudao.module.bpm.service.business.listener;


import cn.iocoder.yudao.module.bpm.dal.dataobject.business.BpmBusinessFormDO;
import cn.iocoder.yudao.module.bpm.enums.BpmBusinessTypeEnum;
import cn.iocoder.yudao.module.bpm.enums.task.BpmProcessInstanceStatusEnum;
import cn.iocoder.yudao.module.bpm.event.BpmProcessInstanceStatusEvent;
import cn.iocoder.yudao.module.bpm.event.BpmProcessInstanceStatusEventListener;
import cn.iocoder.yudao.module.bpm.service.business.BpmBusinessFormService;
import cn.iocoder.yudao.module.system.api.bpm.BpmElderlyChangeApi;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author: jinyd
 * @CreateTime: 2024/9/12  8:56
 * @Description: 转院变更的监听器实现类
 * @Version: 4.0
 */
@Component
public class BpmTransferChangeStatusListener extends BpmProcessInstanceStatusEventListener {

    @Resource
    private BpmBusinessFormService businessFormService;

    @Resource
    private BpmElderlyChangeApi changeApi;

    @Override
    protected String getProcessDefinitionKey() {
        return BpmBusinessTypeEnum.TRANSFER_CHANGE.getKey();
    }

    @Override
    protected void onEvent(BpmProcessInstanceStatusEvent event) {
        // 监听Spring事件修改状态
        BpmBusinessFormDO businessFormDO = businessFormService.getBusinessFormByInstanceId(event.getId());
        businessFormDO.setStatus(event.getStatus());
        businessFormService.updateBusinessForm(businessFormDO);
        if (BpmProcessInstanceStatusEnum.APPROVE.getStatus().equals(event.getStatus())) {
            changeApi.updateTransferChange(businessFormDO.getBusinessId());
        }
    }

}
