package com.njcn.bpm.listener.business;

import com.njcn.bpm.listener.BpmProcessInstanceStatusEvent;
import com.njcn.supervision.api.BusinessCommonFeignClient;
import com.njcn.supervision.enums.SupervisionKeyEnum;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author hongawen
 */
@Component
public class BpmSupervisionStatusListener  implements ApplicationListener<BpmProcessInstanceStatusEvent> {

    @Resource
    private BusinessCommonFeignClient businessCommonFeignClient;

    @Override
    public final void onApplicationEvent(BpmProcessInstanceStatusEvent event) {
        System.out.println(event);
        //判断当前流程定义的key，是否包含在技术监督的枚举内，没有直接return，否则调用远程接口更新状态
        if(SupervisionKeyEnum.containBusinessKye(event.getProcessDefinitionKey())){
            onEvent(event);
        }
    }



    /**
     * 处理事件
     *
     * @param event 事件
     */
    protected void onEvent(BpmProcessInstanceStatusEvent event) {
        businessCommonFeignClient.updateProcessStatus(event.getProcessDefinitionKey(),event.getBusinessKey(),event.getReason(), event.getStatus());
    }

}
