package cn.fateverse.workflow.process.listener;

import cn.fateverse.workflow.constant.ProcessConstant;
import cn.fateverse.workflow.mapper.UserInstanceMapper;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

/**
 * 流程结束监听器
 *
 * @author Clay
 * @date 2022/12/29
 */
@Component
public class ProcessListener implements ExecutionListener {
    //用户流程mapper
    private final UserInstanceMapper userInstanceMapper;

    public ProcessListener(UserInstanceMapper userInstanceMapper) {
        this.userInstanceMapper = userInstanceMapper;
    }

    @Override
    public void notify(DelegateExecution execution) {
        //将流程状态设置为已结束
        execution.setVariable(ProcessConstant.PROCESS_STATUS, ProcessConstant.BUSINESS_STATUS_ENDED);
        userInstanceMapper.updateState(execution.getProcessInstanceId(), ProcessConstant.BUSINESS_STATUS_ENDED);
    }
}
