package cn.axzo.workflow.core.deletage.approverscope;

import cn.axzo.workflow.common.enums.WorkspaceType;
import cn.axzo.workflow.common.model.dto.CooperationOrgDTO;
import cn.axzo.workflow.common.model.dto.CooperationOrgDTO.OrgScope;
import cn.hutool.core.collection.CollUtil;
import org.flowable.bpmn.model.UserTask;
import org.flowable.engine.delegate.DelegateExecution;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static cn.axzo.workflow.common.constant.BpmnConstants.BIZ_ORG_RELATION;

/**
 * @author syl
 * @date 2023/11/21
 */
@Component
public class ProjectWorkspaceProcesssor implements ApproverScopeProcesssor {

    @Override
    public ApproverScopeDTO select(UserTask userTask, DelegateExecution execution) {
        // 发起项目部
        CooperationOrgDTO orgDTO =(CooperationOrgDTO) execution.getVariables().get(BIZ_ORG_RELATION);
        if (Objects.isNull(orgDTO)) {
            return ApproverScopeDTO.builder().build();
        }
        List<OrgScope> orgScopes = orgDTO.getOrgScopes().stream()
                .filter(o -> WorkspaceType.WORKSPACE == WorkspaceType.getType(o.getWorkspaceType()))
                .collect(Collectors.toList());

        List<OrgScope> workerTeamScopes = CollUtil.defaultIfEmpty(orgDTO.getWorkerTeamScopes(),
                Collections.emptyList())
                .stream()
                .filter(o -> WorkspaceType.WORKSPACE == WorkspaceType.getType(o.getWorkspaceType()))
                .collect(Collectors.toList());
        return ApproverScopeDTO.builder().orgScopes(orgScopes)
                .workerTeamScopes(workerTeamScopes).build();
    }

}
