package cn.axzo.workflow.server.controller.delegate;

import cn.axzo.workflow.common.enums.ApproverSpecifyEnum;
import cn.axzo.workflow.common.model.request.bpmn.task.BpmnTaskDelegateAssigner;
import cn.axzo.workflow.core.common.utils.BpmnMetaParserHelper;
import com.alibaba.fastjson.JSON;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.bpmn.model.UserTask;
import org.flowable.engine.delegate.DelegateExecution;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 基于"固定人员"查询审批人
 *
 * @author wangli
 * @since 2023/11/18 22:56
 */
@Component
public class BasedFixedPersonTaskAssigneeSelector extends AbstractBpmnTaskAssigneeSelector {
    @Override
    public String getType() {
        return ApproverSpecifyEnum.fixedPerson.getType();
    }

    @Override
    public boolean support(String param) {
        return getType().equals(param);
    }

    @Override
    public List<BpmnTaskDelegateAssigner> select(FlowElement flowElement, DelegateExecution execution,
                                                 Boolean throwException) {
        List<BpmnTaskDelegateAssigner> assigners = new ArrayList<>();
        if (!(flowElement instanceof UserTask)) {
            return super.select(flowElement, execution, throwException);
        }
        BpmnMetaParserHelper.getApproverSpecifyValue((UserTask) flowElement)
                .ifPresent(s -> assigners.addAll(JSON.parseArray(s, BpmnTaskDelegateAssigner.class)));
        return assigners;
    }

}
