package com.hz.pm.api.todocenter.handle;

import cn.hutool.core.collection.CollUtil;
import com.hz.pm.api.todocenter.model.vo.ProcessProgressDetailVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.runtime.ActivityInstance;
import org.springframework.stereotype.Component;
import java.util.List;

/**
 * @Classname PassHandle
 * @Description
 * @Date 2023/5/5 14:46
 * @Author PoffyZhang
 */
@Component
@Slf4j
@AllArgsConstructor
public class PassHandle {

    private final RuntimeService runtimeService;

    /**
     * 判断节点类型
     * @param instanceId
     * @param employeeCode
     * @return
     */
    public void checkCanPassOrSeal(String instanceId,String taskId,String employeeCode, ProcessProgressDetailVO res) {

        List<ActivityInstance> activities = runtimeService.createActivityInstanceQuery()
                .taskAssignee(employeeCode)
                .processInstanceId(instanceId)
                .unfinished()
                .orderByActivityInstanceStartTime()
                .asc()
                .list();

        if(CollUtil.isEmpty(activities)){
            res.setCanPass(Boolean.FALSE);
            res.setCanReject(Boolean.FALSE);
            res.setCanSeal(Boolean.FALSE);
            return;
        }

        for(ActivityInstance activity : activities){
            if(StringUtils.isNotBlank(taskId)){
                if(taskId.equals(activity.getTaskId())){
                    if(StringUtils.isNotBlank(activity.getActivityId())){
                        res.setCanReject(Boolean.TRUE);
                        if(activity.getActivityId().startsWith("node")){
                            res.setCanPass(Boolean.TRUE);
                        }else if(activity.getActivityId().startsWith("seal")){
                            res.setCanSeal(Boolean.TRUE);
                        }
                        break;
                    }
                }
            }

            if(StringUtils.isNotBlank(activity.getActivityId())){
                res.setCanReject(Boolean.TRUE);
                if(activity.getActivityId().startsWith("node")){
                    res.setCanPass(Boolean.TRUE);
                }else if(activity.getActivityId().startsWith("seal")){
                    res.setCanSeal(Boolean.TRUE);
                }
            }
        }
    }
}
