package com.ho.flow.listener;

import com.ho.flow.constant.WorkOrderConstant;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;

import java.util.List;
import java.util.Map;

/**
 * @author fancl
 * @desc: 第3节点监听, 设置候选人
 * @date 2023/2/4
 */
@Slf4j
public class Node3Listener implements TaskListener {

    @Override
    public void notify(DelegateTask delegateTask) {
        Object variableLocal = delegateTask.getVariableLocal(WorkOrderConstant.CandidateUser.nodeThreeUsers);
        Map<String, Object> variables = delegateTask.getVariables();
        String name = delegateTask.getName();
        String processInstanceId = delegateTask.getProcessInstanceId();
        log.info("当前任务名:{}, 流程实例:{}", name, processInstanceId);
        log.info("Node3Listener 监听得到的 variables:" + variables);
        //
        String variableKey = WorkOrderConstant.CandidateUser.nodeThreeUsers;
        if (variables.containsKey(variableKey)) {
            try {
                List<String> toUsers = (List<String>) variables.get(variableKey);
                delegateTask.addCandidateUsers(toUsers);
                log.info("New add candidate user:" + toUsers);
            } catch (Exception e) {
                log.error("Node3Listener 出错");
                log.error(e.getMessage());
            }

        }
    }
}
