package com.ho.flow.listener;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
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.ArrayList;
import java.util.List;
import java.util.Map;

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

    @Override
    public void notify(DelegateTask delegateTask) {
        Map<String, Object> variables = delegateTask.getVariables();
        Object nodeUsers = delegateTask.getVariableLocal(WorkOrderConstant.CandidateUser.nodeOneUsers);
        String name = delegateTask.getName();
        String processInstanceId = delegateTask.getProcessInstanceId();
        log.info("当前任务名:{}, 流程实例:{}", name, processInstanceId);
        log.info("Node1Listener 监听得到的 variables:" + variables);
        //
        String variableKey = WorkOrderConstant.CandidateUser.nodeOneUsers;
        List<String> candisUserList = new ArrayList<>();
        if (nodeUsers !=null && nodeUsers instanceof ArrayNode) {
            ArrayNode candiUsers = (ArrayNode) nodeUsers;
            for (JsonNode candiUser : candiUsers) {
                String user = candiUser.asText();
                candisUserList.add(user);
            }
            delegateTask.addCandidateUsers(candisUserList);
            log.info("New add candidate user:" + candisUserList);
        }
    }
}
