package jnpf.workflow.service.impl;

import jnpf.engine.FlowTaskApi;
import jnpf.engine.vo.ApproverByTaskIdAndNodeIdVo;
import jnpf.workflow.service.FlowTaskService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 工作流服务实现
 *
 * @author yanwenfu
 * @create 2024-08-19
 */
@Service
public class FlowTaskServiceImpl implements FlowTaskService {

    @Resource
    private FlowTaskApi flowTaskApi;

    @Override
    public ApproverByTaskIdAndNodeIdVo getApproveInfo(String taskId, String nodeId, String tenantId, Integer type) {

        ApproverByTaskIdAndNodeIdVo vo = null;
        int index = 1;
        while (index <= 5) {
            try {
                index++;
                vo = flowTaskApi.getApproverByTaskIdAndNodeId(taskId, nodeId, tenantId).getData();
                if (null != type && type == 0 ){
                    if (null != vo){
                        break;
                    }else {
                        vo = null;
                        Thread.sleep((long) index * 1000);
                    }
                }else {
                    if (null == vo || null == vo.getUserOperators() || vo.getUserOperators().isEmpty()) {
                        vo = null;
                        Thread.sleep((long) index * 1000);
                    } else {
                        break;
                    }
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        return vo;
    }
}
