package com.yanzhu.flowable.listener;

import com.yanzhu.common.core.text.Convert;
import com.yanzhu.common.enums.ApplyStatusEnums;
import com.yanzhu.project.domain.ProProjectApply;
import com.yanzhu.project.service.IProProjectApplyService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Flowable全局监听器
 *
 * @author JiangYuQi
 */
@Slf4j
@Component
public class GlobalEventListener extends AbstractFlowableEngineEventListener {

    @Autowired
    private TaskService taskService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private IProProjectApplyService proProjectApplyService;

    @Override
    protected void processCompleted(FlowableEngineEntityEvent event) {
        log.info("任务流程审批完成...{}",event.getProcessInstanceId());
        super.processCompleted(event);
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(event.getProcessInstanceId()).singleResult();
        try {
            ProProjectApply entity = proProjectApplyService.selectProProjectApplyById(Convert.toLong(processInstance.getBusinessKey()));
            entity.setApplyStatus(ApplyStatusEnums.DYS.getCode());
            proProjectApplyService.updateProProjectApplyEntity(entity);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}


