package com.jeeplus.flowable.listener;

import cn.hutool.extra.spring.SpringUtil;
import com.jeeplus.extension.domain.FlowCopy;
import com.jeeplus.extension.service.FlowCopyService;
import com.jeeplus.sys.utils.StringUtils;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

@Component("myExecutionListener")
public class MyExecutionListener implements ExecutionListener {

    public void notify(DelegateExecution delegateExecution) {

        String eventName = delegateExecution.getEventName ( );

        if ( "start".equals ( eventName ) ) {
            System.out.println ( "start=========" );

            if(null != delegateExecution.getVariables() && StringUtils.isNotBlank(delegateExecution.getVariables().get("title").toString())){
                String title = delegateExecution.getVariables().get("title").toString();
                if(title.contains("垃圾处理流程")){
                    String applyUserId = delegateExecution.getVariables().get("applyUserId").toString();

                    FlowCopy flowCopy = new FlowCopy();
                    flowCopy.setProcDefId(delegateExecution.getProcessDefinitionId());
                    flowCopy.setProcInsName(title);
                    flowCopy.setProcInsId(delegateExecution.getProcessInstanceId());
                    flowCopy.setUserId(applyUserId);
                    //将项目信息进行抄送
                    SpringUtil.getBean(FlowCopyService.class).save(flowCopy);

                }
            }
        } else if ( "end".equals ( eventName ) ) {
            System.out.println ( "end=========" );
        } else if ( "take".equals ( eventName ) ) {//连线监听
            System.out.println ( "take=========" );
        }
    }
}
